print "Perl2Exe Binary Distribution setup\n";
# this script is only used if you are using your own perl instalation

die "Perl version 5.006 required, this is version $]\n" if ($] != 5.006);

if ($ENV{WINDIR}) {
	$platform = "AP";
}
else {
    $uname = `uname`; chop $uname; $uname =~ s%/%%g; # BSD/OS -> BSDOS
    $platform = $uname;
}
print "Platform = $platform\n";

$perl2exeinifile = "perl2exe-$platform.ini";
$ini = SimpleIniHash::load($perl2exeinifile);

# pick up potential library directories from @INC
@perLibList = @INC;

$key = "$platform" . "PERL5LIB";
$oldliblist = $ini->{$key};
print "oldliblist = $oldliblist\n";

#push ( @perLibList, split(";", $oldliblist) );

@newlist = ();
foreach $path (@perLibList) {
	next if $list{$path};
	$list{$path} = 1;
	push (@newlist, $path) if (-d $path);
}

$newliblist = join(";", @newlist);
print "newliblist = $newliblist\n";

print "No change\n" if ($newliblist eq $oldliblist);
print "Changed\n" if ($newliblist ne $oldliblist);

$ini->{$key} = $newliblist;
$ini->save("$perl2exeinifile");




#----------------------------------------------------------------------------

package SimpleIniHash;

sub load
{
    #print "load: ", join(", ", @_), "\n";
    if (@_ < 1) {
        print "SimpleIniHash::load Invalid arguments\n";
        return undef;
    }
    my ($filename) = @_;

    my $self = {};
    bless $self;

    open (INI, $filename) || return $self;  # return empty list
    my ($name, $value);
    while (<INI>) {
        chop;
        #s/\s*;.*//;		# remove trailing comment
        s/^\s*;.*//;		# remove start of line comment
        next if ($_ eq "");
        next if m/^\[.*\]$/;	# ignore section name
        ($name, $value) = split("=", $_);
        $name =~ s/\s*$//;
        $value =~ s/^\s*//;
        next if (($name eq "") or ($value eq ""));
        #print "set '$name' = '$value'\n";
        $self->{$name} = $value;
    }
    close INI;

    return $self;
}


sub save
{
    #print "save: ", join(", ", @_), "\n";
    my $self = shift @_;

    if (@_ < 1) {
        print "SimpleIniHash::save Invalid arguments\n";
        return undef;
    }
    my ($filename) = @_;

    my $rc = open (INI, ">$filename");
    if (!$rc) {
        print "SimpleIniHash::saveCan't create $filename\n";
        return undef;
    }

    my $key;
    foreach $key (sort keys %$self) {
        #print "key = $key\n";
        print INI "$key = $self->{$key}\n";
    }
    close INI;

    return $self;
}

