# used to map a drive with authentication upon restart and
#  run todo.pl --go

use strict;

my $mapdrvcmd = 'net use z: \\\\aa1pri\\install installpasswd /USER:install /persistent:no';
my $gocmd = 'start cmd /c c:\\perl\\bin\\perl.exe z:\\bin\\todo.pl --go';
my $golog = "c:\\netinst\\logs\\golog.txt";

open GOLOG, ">>$golog"
    or die "Unable to open $golog for writing: $^E";
my $i;
my $not_mapped=1;

# Try 5 times at 2 second intervals
for ($i=0, $i<6, $i++){
    if (! ($not_mapped = mapdrive() ) ){
        last;
    }
    sleep 2;
}

if ($not_mapped){
    print GOLOG "Could not map drive, giving up.", "\n";
    close GOLOG;
    exit 1;
} else {
    print GOLOG "Map drive successful\n";
    print GOLOG "Running todo.pl\n";
    system $gocmd;
}

print "done.\n";
close GOLOG
    or die "Unable to close $golog: $^E";

sub mapdrive {
    print "Running $mapdrvcmd...";
    if (0 != (system $mapdrvcmd) ){
        print GOLOG "$mapdrvcmd failed: ", ($? ? $? : $^E), "\n";
        print "$mapdrvcmd failed: ", ($? ? $? : $^E);
        return 1;
    }
    return 0;
}