#!/usr/pd/tk/bin/tclsh
# CHANGE: The row above must be set to find your tclsh!

# This script installs the mailreceiver's dirs for galaxy reports
#    creates/updates the resourcefile
#    creates data files
#    changes ~/.forward to take care of incoming mail


# CHANGE: This should be set to the included file mailreceiver

set MAILREC /users/dtek/d90/d0malaga/games/galaxy/mailreceiver 

if [expr $argc < 2] {
    puts "Usage: install_mailrec <complete path to gamedir> <report 0>";
    puts "\012Will create a .forward, .mailreciver and .galaxyrsc in your root";
    puts "and create a directory reports under given gamedir";
    exit 1;
}

# Create new .forward and copy .mailreceiver from ~d0malaga/

if [file exists $env(HOME)/.forward] {
    puts "\012You already have a .forward";
    puts "\012To install the mailreceiver add \"\|$env(HOME)/.mailreceiver\"";
    puts "or remove the file and install again\012";
} else {
    exec echo \\$env(USER), \"\|$env(HOME)/.mailreceiver\" > $env(HOME)/.forward
    exec cp $MAILREC $env(HOME)/.mailreceiver
    puts "\012Mailreceiver installed in $env(HOME)/.forward\012";
}

# This file in your root will be created or appended to

set resourcefile $env(HOME)/.galaxyrsc

#
# Procedures to be used by main script
#

# Splitline: Splits a string into substrings, tabs & spaces are delimiters
#
# In:  String to be splitted into substrings
# Out: List of substrings
#

proc splitline instring {
	set inlist [split $instring];
	foreach s1 $inlist {
		if [string compare "$s1" ""] {
			lappend outlist $s1;
		}
	}
	return $outlist;
}


# Buildline: builds a string by inserting \tab as delimiter between substrings
#
# In:  List of substrings
# Out: New string with substrings separated with tabs
#

proc buildline inlist {
	set outstring "";
	foreach s1 $inlist {
		lappend outstring $s1;
		lappend outstring " ";
#		lappend outstring "  \t";
	}
	return [join $outstring];
}

#
# Main script
#

set dir [lindex $argv 0]

if [file exists $dir/reports] {
    puts "$dir/reports already exists, installation aborted";
    exit 1;
}

set infile [lindex $argv 1]
set maildata [split [exec cat $infile] \012]
set status noreport

foreach row $maildata {
    switch $status {
    noreport {
	if [regexp {^Subject:[^0-9]*([0-9]*)[^0-9]*} $row t1 t2] {
	    if [string length $t2] {
	        set t3 "[string range $t1 0 [expr [string first $t2 $t1]-1]]";
	        set t4 "(.*)[string range $t1 [expr [string first $t2 $t1]+1] end]";
	        set matchexp "^$t3$t4";
	    } else {
		set matchexp "^$row";
	    }	
	    if [expr ![file exists $dir/reports/0]] {
		set status findturn;
	    } else {
		puts "Report 0 exists, installation aborted";
		exit 1;
	    }
	}
    }

    findturn {
	if [regexp ^Size:(.*).centered.* $row t1 t2] {
	    set galaxsize $t2;
	} elseif [regexp {[^0-z]*([0-z]+).Report.for.Galaxy.*} $row t1 player] {
	    puts " ";
	    puts "        Welcome $player!";
	    puts " ";
	    puts "Size of galaxy: $t2";
	    puts "Match:$matchexp";
	    set planetdata {"# X Y Size R Owner \[known_observers\]"}
	    set playerdata {"Player D W S C Turn"}
	    set status identify;
	}
    }

    identify {
	if [regexp .*N.*D.*W.*S.*C.*M.*P.* $row] {
	    puts "Status for players";
	    set status playlist;
	} elseif [regexp ^#\040+X\040+Y.* $row] {
	    puts "Unknown systems";
	    set status planetlist;
	} elseif [regexp .*N\040+#\040+X\040+Y\040+S\040+P.* $row] {
	    puts "Inhabited systems ($player)";
	    set status ownerlist;
	} elseif [regexp ^(.*).Systems.* $row t1 t2] {
	    set name [string trim $t2];
	}
    }

    playlist {
	if [string length $row] {
	    set t1 [lreplace [splitline $row] 5 end 0];
	    lappend playerdata $t1;
	} else {
	    set status identify;
	}
    }

    planetlist {
	if [string length $row] {
	    set t1 [splitline $row];
            lappend t1 ? ? ? ?;
	    lappend planetdata [buildline $t1];
	} else {
	    break;
	}
    }

    ownerlist {
	if [string length $row] {
	    set t1 [splitline $row];
	    set homeplanet [lindex $t1 1];
	} else {
	    set status identify;
	}
    }
    }
}

if [string compare "$status" "planetlist"] {
    puts "Couldn't extract unknown planets from report, installation aborted";
    exit 1;
} else {

    exec mkdir $dir/reports
    puts "Writing $dir/reports/0";
    exec cp $infile $dir/reports/0;

    puts "Writing $dir/reports/players";
    set filep [open $dir/reports/players w];
    foreach s1 $playerdata {
	puts $filep $s1;
    }
    close $filep;

    puts "Writing $dir/reports/planets";
    set planetdata [linsert $planetdata [expr $homeplanet + 1] [buildline [splitline "$homeplanet 0.00 0.00 1000 10 $player ?"]]];
    set filep [open $dir/reports/planets w];
    foreach s1 $planetdata {
	puts $filep $s1;
    }
    close $filep;

    puts "Writing $dir/reports/ships";
    set filep [open $dir/reports/ships w];
    puts $filep [buildline [splitline "Player Name D A W S C M    Speed  sh  Turn"]];
    close $filep;

    puts "Writing $dir/reports/battles";
    set filep [open $dir/reports/battles w];
    puts $filep [buildline [splitline "Turn Planet Name Mass Left"]];
    close $filep;
}

    puts "Writing $resourcefile"
    exec echo $dir $galaxsize $matchexp >>$resourcefile;

exit 0;



