@rem = '--*-Perl-*--';
@rem = '
@echo off
if not exist ntt\perl.exe goto perlnotthere
echo ntt\perl.exe exists, copying into current directory
del  perl.exe
copy ntt\perl.exe .
if not exist ntt\perl100.dll goto perlnotthere
del  perl100.dll
copy ntt\perl100.dll .
:perldllnotthere
:perlnotthere
if exist perl.exe goto perlhere
echo Could not find Perl interpreter!!
echo *gasp* *wheez* *choke*
pause
goto endofperl 
:perlhere
if exist perl100.dll goto perldllhere
echo Could not find Perl100.dll
echo *gasp* *wheez* *choke*
pause
goto endofperl
:perldllhere
perl.exe install.bat %1 %2 %3 %4 %5 %6 %7 %8 %9
goto endofperl
@rem ';

BEGIN{
@INC = qw( Lib 
	Ext );

};

require "NT.ph";


#(c) 1995 Microsoft Corporation. All rights reserved. 
#        Developed by hip communications inc., http://info.hip.com/info/

print <<'--end--';
This install script assumes that you have unpacked 
Perl for Win32 into the final intended destination 
directory. It will copy the executable and batch files
into a separate directory, but all documentation and 
libraries remain where they currently are. 
Do you wish to proceed? [Y/n]
--end--
$in = <STDIN>;
until ( $in eq "\n" || $in =~ /^y/i ) {
	exit if ( $in =~ /^no?\n$/i );
	print "Do you wish to proceed? [Y/n]";
	$in = <STDIN>;
}


chop( $perldir = `cd` );

$libdir = $perldir . '\lib';
$docdir = $perldir . '\docs';

$perldir =~ s/\\5\.001//;
$bindir = $perldir . '\bin';

require 'ctime.pl' || warn "$!\nDid you preserve the directory structure when you unzipped?\n";

$date = ctime( time );

if( Win32::IsWinNT() ){
	( $myversion ) = grep( /perl[^\d]*(\d\.\d\.?\d\.?\d)/, `perl -v` );
	( $myversion ) = $myversion =~ /perl[^\d]*(\d\.(\d\.?)+)/;
}
else{
	$myversion=95;
	}
	

open ( LOG, '>>install.log' ) || warn "Couldn't open log file!\n";

&log( $date );

sub log {
    ( $message ) = @_;
    print LOG $message;
    print $message;
}

sub gripe {
    ( $message ) = @_;
    print LOG $message;
    warn $message;
}

if ( -d $bindir ) {
    &log( "$bindir exists\n" );
} else {
    &log( "creating $bindir\n" );
    mkdir( $bindir, 0 ) || die $!;
}

&log( "Installing Perl for Win32 into $perldir\n" );



if ( -f "$bindir\\perl.exe" ) {
	if( Win32::IsWinNT() ){
	    ( $version ) = grep( /perl[^\d]*(\d\.\d\.?\d\.?\d)/, `$bindir\\perl -v` );	
	    ( $version ) = $version =~ /perl[^\d]*(\d\.(\d\.?)+)/;
	}
	else{
		$version = 95;
	}

    &log( "Previous version $version exists in bin directory,\n" );
    &log( "Renaming it to perl.$version.exe\n" );
    rename "$bindir\\perl.exe", "$bindir\\perl.$version.exe";
}

if ( -f "$bindir\\perl100.dll"){
	$version=$version?$version:"old";                
	&log("Renaming perl100.dll to perl100.$version.dll\n");
	rename "$bindir\\perl100.dll","$bindir\\perl100.$version.dll";
}


&log("Copying over perl100.dll to $bindir \n");
`copy ntt\\perl100.dll "$bindir\\perl100.dll"`;


&log( "Copying over perl and utility scripts to $bindir\n" );

`copy ntt\\perl.exe "$bindir"`;
`copy ntt\\perlglob.exe "$bindir"`;
`copy ntt\\cmd32.exe "$bindir"`;
`copy pl2bat.bat "$bindir"`;


NTRegCreateKeyEx( &HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\Resource Kit\PERL5',
    &NULL, 'NT Perl 5', &REG_OPTION_NON_VOLATILE, &KEY_ALL_ACCESS, &NULL,
    $hkey, $disposition ) ?
    &log( "Added key for Perl 5 to NT Registry Database..\n" ):
    &gripe( "Couldn't add key for Perl 5 to NT Registry Database!!\n" );

if ( $disposition  == &REG_OPENED_EXISTING_KEY ) {
    &gripe( "Key exists already, modifying existing key...\n" );
}

NTRegSetValueEx( $hkey, 'BIN', &NULL, &REG_SZ, "$bindir" ) ?
    &log( "Adding $bindir to script path information\n" ):
    &gripe( "Couldn't add script path to registry!!\n" );

NTRegSetValueEx( $hkey, 'PRIVLIB', &NULL, &REG_SZ, "$libdir" ) ?
    &log( "Adding $libdir to library include path information\n" ):
    &gripe( "Couldn't add library path to registry!!\n" );

NTRegSetValueEx( $hkey, 'HTML-DOCS', &NULL, &REG_SZ, "$docdir" ) ?
    &log( "Adding $docdir to documentation directory\n" ) :
    &gripe( "Couldn't add documentation directory to registry!!\n" );

NTRegCloseKey( $hkey );

$fred = 'foo bar baz';

until ( $fred =~ /^(y|n)/i || $fred eq '' ) {
    print "Modify search path? [Y/n]";
    chop( $fred = <STDIN> );
}

unless ( $fred =~ /^n/i ) {
        if( Win32::IsWinNT() ){
            NTRegOpenKeyEx ( &HKEY_LOCAL_MACHINE,
                'SYSTEM\CurrentcontrolSet\control\Session Manager\Environment',
                &NULL, &KEY_ALL_ACCESS, $hkey ) ?
                &log( "Retrieving Path information from session manager\n" ):
                &gripe( "Couldn't retrieve path information from session manager!!\n" );
        
            NTRegQueryValueEx( $hkey, 'Path', &NULL, $type, $pathstring );
        
            $pathstring =
                ".;$bindir;" . join (';', grep(!/(perl|\.)/i, split(/;/, $pathstring))) . ";";
        
            NTRegSetValueEx( $hkey, 'Path', &NULL, $type, $pathstring ) ?
                &log( "Updated path information in session manager\n" ):
                &gripe( "Couldn't update path information in session manager!!\n" );
            NTRegCloseKey( $hkey );
        }
       else{ # must be win95, so update path in autoexec.bat
        
        &log("Attempting to change path in autoexec.bat");
        ( $bootdrive = $ENV{'windir'} ) =~ s|\\.*||g;
        &log( "bootdrive is $bootdrive\n");

        open( ABAT,">>$bootdrive\\autoexec.bat")||
                gripe( "Couldn't open $bootdrive\\autoexec.bat");

        print ABAT "path %path%;$bindir;\n";
        close ABAT;

       }

}

#Check for IIS.
if(!NTRegOpenKeyEx (&HKEY_LOCAL_MACHINE,
				'SYSTEM\CurrentControlSet\Services\W3SVC\Parameters\Script Map',
				&NULL,&KEY_ALL_ACCESS,$Servkey))
{
	print "No Microsoft Internet Information Server Installed\n";
	print "Continue to install PerlIS.dll? [Y/n]";
	$in = <STDIN>;
	until( $in eq "\n" || $in =~ /^y/i )
	{
		if ( $in =~ /^no?\n$/i )
		{
			goto finishUp;
		}
		print "Continue? [Y/n]";
		$in = <STDIN>;
	}
}

#copy PerlIS.dll into the bin directory.
if ( -f "$bindir\\PerlIS.dll"){
	$version=$version?$version:"old";                
	&log("Renaming PerlIS.dll to PerlIS.$version.dll\n");
	rename "$bindir\\PerlIS.dll","$bindir\\PerlIS.$version.dll";
}


&log("Copying over PerlIS.dll to $bindir \n");
`copy ntt\\PerlIS.dll "$bindir\\PerlIS.dll"`;

#Ask for the extension to associate with perlIS.dll.
print "\nPlease choose a file extension to associate with PerlIS.dll.";
print "\nExtension:<pl is the default>";

$extension = <STDIN>;
chop($extension);
$extension = "pl" if ( $extension eq "" );	
$extension = '.'.$extension unless ($extension =~ /\..*/ );

#make association.

NTRegSetValueEx( $Servkey,"$extension", &NULL, &REG_SZ, "$bindir\\PerlIS.dll" ) ?
    &log( "-->Association made\n" ) :
    &gripe( "**Association of $extension to $bindir\\PerlIS.dll failed!!\n" );

NTRegCloseKey( $Servkey );

finishUp:
&log( "Perl $myversion installation finished.\n" );
print "NOTE: The updated path information will not take effect\n";
if( Win32::IsWinNT() ){
        print "      until you log off and back on again. enjoy ;-)\n";
}
else{
        print "      until you reboot your machine. enjoy ;-)\n";
}


__END__
:endofperl
