#!/usr/bin/perl -w
# Copyright 2000 by Tomasz Wegrzanowski <maniek@beer.com>
# Licensed under GPL2 or newer

# global variables :
# $l       - current line content
# $ll      - current line length
# $chapter - current nationset's chapter (ex: [foo])
# $state   - what is next line about (ex: cities)
# %data    - all data from source nationsets
# $misc_percent - percent of cities that will go to misc
$misc_percent = 15;

unless (@ARGV) {
  print "Usage : $0 file1 [file2 file3 ...]\n";
  exit;
}

foreach my $file (@ARGV) {
  open IN, $file;
  while (<IN>) {
      $l = $_;
      $l =~ s/;.*$//;
      $l =~ s/\s+/ /g;
      $l =~ s/\s?=\s?/=/g;
      $l =~ s/\s?,\s?/,/g;
      $l =~ s/^\s//g;
      $l =~ s/\s$//g;
      analize () if ( $l );
  }
  close IN;
}

make_new_nationset ();

sub analize {
  $l =~ /\[(.*)\]/;
  if ($1) {
      $chapter = $1;
      push @{$data{chapter}},$1 unless ( $1 eq 'nation_barbarian' or $1 eq 'misc' or $1 eq 'datafile');
      return;
  }
  return unless ( $chapter );
  if ( $chapter eq 'nation_barbarian' ) { $barbarian .= $l."\n"; return }
  if ( $chapter eq 'datafile' ) {
      if ( $l =~ /^description=/ ) { $datafile .= "description=\"File generated by nationset randomizer\"\n" }
      else { $datafile .= $l."\n" }
      return;
  }

  unless ( $state ) {
      $ll = length $l;
      (   analize_simple_equation ( 'name' )
      or  analize_simple_equation ( 'plural' )
      or  analize_simple_equation ( 'flag' )
      or  analize_simple_equation ( 'flag_alt' )
      or  analize_simple_equation ( 'city_style' )
      or  analize_simple_equation ( 'attack' )
      or  analize_simple_equation ( 'expand' )
      or  analize_simple_equation ( 'civilized' )
      or  analize_simple_equation ( 'wonder' )
      or  analize_simple_equation ( 'government' ))
      and return
  }

  analize_state_entry ( 'cities' );
  analize_state_entry ( 'leader' );
  analize_state_entry ( 'leader_sex' );
  analize_state_entry ( 'advisors' );
  analize_state_entry ( 'tech_goals' );
  analize_state_entry ( 'ruler_titles' );
  return unless ( $l );

  if ( $state ) {
      my $state_tmp = $state;
      if ( $state eq 'ruler_titles' ) {
            $value .= $l."\n";
	    if ( $l eq '}' ) {
		    push @{$data{$state_tmp}},$value;
		    undef $value;
		    undef $state
	     }
      } elsif ( $state eq 'tech_goals' ) {
            if ( $l =~ /,$/ ) { $l =~ s/,$//; } else { $ende = 1; }
            my @values = split ',',$l;
            push @value, @values;
	    if ( $ende ) {
		    $#{$data{$state_tmp}}++;
		    push @{${$data{$state_tmp}}[-1]},@value;
		    undef @value;
		    undef $state;
		    undef $ende;
	    }
      } else {
            if ( $l =~ /,$/ ) { $l =~ s/,$//; } else { undef $state }
            my @values = split ',',$l;
            push @{$data{$state_tmp}},@values;
      }
      undef $state_tmp;
      return;
  }

  print $chapter,": ",$l,"\n";
}

sub analize_state_entry {
  my $eq = shift;
  my $eqlen = (length $eq)+1;
  return 0 unless ( $ll >= $eqlen );
  my $l0 = substr $l,0,$eqlen;
  if ( $l0 eq "$eq=" ) {
      $state = $eq;
      $l1 = substr $l,$eqlen;
      $l = $l1;
      return 1;
  }
  return 0;
}

sub analize_simple_equation {
  my $eq = shift;
  my $eqlen = (length $eq)+1;
  my $llen = length $l;
  return 0 unless ( $llen >= $eqlen );
  my $l0 = substr $l,0,$eqlen;
  if ( $l0 eq "$eq=" ) {
      my $l1 = substr $l, $eqlen;
      push @{$data{$eq}}, $l1;
      return 1;
  }
  return 0;
}

sub make_new_nationset {
  my ($n,$i,$j,$c);
  my $misccities = $#{$data{cities}} * ($misc_percent/100);
  $#{$data{ruler_titles}}=$#{$data{chapter}};
  print "[datafile]\n$datafile" if ($datafile);
  $c = $#{$data{chapter}};
  foreach $n (0..$c) {
  print "\n[", ${$data{chapter}}[$n],"]\n";
  $i = rand_int ($#{$data{name}});
  print "name=",${$data{name}}[$i],"\n";
  splice @{$data{name}},$i,1;
  print "plural=",${$data{plural}}[$i],"\n";
  splice @{$data{plural}},$i,1;
  $leaders = int($#{$data{leader}}/($#{$data{flag}}+1));
  foreach $j(0..$leaders) {
      $i = rand_int ($#{$data{leader}});
      push @lead,${$data{leader}}[$i];
      splice @{$data{leader}},$i,1;
      push @leadsex,${$data{leader_sex}}[$i];  
      splice @{$data{leader_sex}},$i,1;
  }
  print "leader=",(join ",",@lead),"\n";
  undef @lead;
  print "leader_sex=",(join ",",@leadsex),"\n";
  undef @leadsex;
  $i = rand_int ($#{$data{flag}});
  print "flag=",${$data{flag}}[$i],"\n";
  splice @{$data{flag}},$i,1;
  print "flag_alt=",${$data{flag_alt}}[$i],"\n";
  splice @{$data{flag_alt}},$i,1;
  $i = rand_int ($#{$data{city_style}});
  print "city_style=",${$data{city_style}}[$i],"\n";
  splice @{$data{city_style}},$i,1;
  $i = rand_int ($#{$data{ruler_titles}});
  if (${$data{ruler_titles}}[$i]) {
      print "ruler_titles=",${$data{ruler_titles}}[$i],"\n";
  }
  splice @{$data{ruler_titles}},$i,1;
  $i = rand_int ($#{$data{attack}});
  print "attack=",${$data{attack}}[$i],"\n";
  splice @{$data{attack}},$i,1;
  $i = rand_int ($#{$data{expand}});
  print "expand=",${$data{expand}}[$i],"\n";
  splice @{$data{expand}},$i,1;
  $i = rand_int ($#{$data{civilized}});
  print "civilized=",${$data{civilized}}[$i],"\n";
  splice @{$data{civilized}},$i,1;
  $i = rand_int (int($#{$data{advisors}}/7));
  print "advisors=",(join ",",@{$data{advisors}}[$i*7..$i*7+6]),"\n";
  splice @{$data{advisors}},$i*7,7;
  $i = rand_int ($#{$data{tech_goals}});
  print "tech_goals=",join (',',@{${$data{tech_goals}}[$i]}),"\n";
  splice @{$data{tech_goals}},$i,1;
  $i = rand_int ($#{$data{wonder}});
  print "wonder=",${$data{wonder}}[$i],"\n";
  splice @{$data{wonder}},$i,1;
  $i = rand_int ($#{$data{government}});
  print "government=",${$data{government}}[$i],"\n";
  splice @{$data{government}},$i,1;
  $cities = int ( ($#{$data{cities}}-$misccities) / ($#{$data{flag}}+2) );
  foreach $j(0..$cities) {
      $i = rand_int ($#{$data{cities}});
      push @cit,${$data{cities}}[$i];
      splice @{$data{cities}},$i,1;
  }
  print "cities=",(join ",",@cit),"\n";
  undef @cit;
  }
  print "\n[nation_barbarian]\n$barbarian" if ($barbarian);
  print "\n[misc]\ncities=",join (",",@{$data{cities}}),"\n";
}

sub rand_int {
my $i = shift;
return 0 if ( $i == 0 );
return rand() * $i % $i;
}
