@rem = '--*-Perl-*--
@echo off

if not "%1" == "" goto Execute
cmd /K setup.bat TkEditor.pl TkEditor
goto EndOfPerl

:Install
@echo on
call ppm install Jcode
call ppm install Math-Matrix
call ppm install Math-MatrixReal
rem call ppm install http://www.bribes.org/perl/ppm/OpenGL.ppd 

:Execute
rem echo perl -x -S %0 %*
perl -x -S %0 %*

goto EndOfPerl

#!/usr/bin/perl -w
#line 23

use strict;
use Cwd;

my $BaseDir = cwd();
$BaseDir =~ s/\/[^\/]*$//;
print "Base directory: $BaseDir\n";
my $FileName = $ARGV[0];
unless($FileName) {
	print "Error: Perl Script Name is not specified.\n";
	exit 1;
}

my $AppDir   = $ARGV[1];
$AppDir = 'TkPlot' unless($AppDir);
my $outfile = "$BaseDir/$AppDir/$FileName";
if(-e $outfile) {
	print "Script path: $outfile\n";
}
else {
	print "$outfile not found.\n";
	exit 1;
}
my $infile = $outfile . ".prev";
unlink($infile);
unless(rename($outfile, $infile)) {
	print "Error: Could not rename [$outfile] to [$infile].\n";
	exit 1;
}

open(IN,  "<$infile") or die "Error: $!\n";
open(OUT, ">$outfile") or die "Error: $!\n";
while(<IN>) {
#print "line: $_";
	if(/^#Start:\s*use\s/i) {
#print "Start in\n";
		print OUT;
		print OUT "use lib \"$BaseDir/lib\";\n";
		print OUT "use lib \"$BaseDir/$AppDir\";\n";
		while(<IN>) {
#print "line2: $_";
			if(/^#End:\s*use\s/i) {
#print "End out\n";
				print OUT;
				last;
			}
		}
	}
	else {
		print OUT;
	}
}
close(OUT);
close(IN);

print "$outfile updated.\n";
__END__

:EndOfPerl
