#!perl -w
# (C) 2003-2007 Willem Jan Hengeveld <itsme@xs4all.nl>
# Web: http://www.xs4all.nl/~itsme/
#      http://wiki.xda-developers.com/
#
# $Id$
# 
# this script creates windows batch file wrappers for several perl scripts from xda-devtools
# in the '$bindir' directory.
#
# in this script '$basedir' should specify the root of the xda-devtools cvs directory
#  and '$bindir' specifies the target directory.
# 
#
use strict;
use IO::File;

my $bindir= "c:/local/bin";

my $basedir;
for my $dir (
	"c:/local/projects/nah6",
	"z:/cvsprj",
	"c:/local/cvsprj")
{
	if (-e $dir) {
	    $basedir= $dir;
	    last;
	} 
}
if (!$basedir) {
    die "please specify in the pl2bat script where the root of the cvs tree is\n";
}


my $tplprefix= <<'__EOF__';
@rem = '--*-Perl-*--  vim:filetype=perl
@echo off
if "%OS%" == "Windows_NT" goto WinNT
perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9
goto endofperl
:WinNT
perl -e "exit(-e '/usr/bin/cygwin1.dll' ? 1:0)"
if errorlevel 1 (
    perl -x -S %0.bat %*
) else (
    perl -x -S %0 %*
)
if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl
if %errorlevel% == 9009 echo You do not have Perl in your PATH.
if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
goto endofperl
@rem ';
#!/usr/bin/perl -w
#line 20

__EOF__

my $tplsuffix= "\n__END__\n:endofperl\n";

makebatfile("$basedir/xda-devtools/perlutils/winresdumper.pl",      "$bindir/winresdumper.bat");
makebatfile("$basedir/xda-devtools/perlutils/hexedit",      "$bindir/hexedit.bat");
makebatfile("$basedir/xda-devtools/perlutils/imagedmp.pl",  "$bindir/imagedmp.bat");
makebatfile("$basedir/xda-devtools/perlutils/decodesms.pl",  "$bindir/decodesms.bat");
makebatfile("$basedir/xda-devtools/perlutils/symboldecode.pl",  "$bindir/symboldecode.bat");
makebatfile("$basedir/xda-devtools/perlutils/add",          "$bindir/add.bat");
makebatfile("$basedir/xda-devtools/perlutils/pl2bat",       "$bindir/pl2bat.bat");
makebatfile("$basedir/xda-devtools/perlutils/base64",       "$bindir/base64.bat");
makebatfile("$basedir/xda-devtools/perlutils/crc32",        "$bindir/crc32.bat");
makebatfile("$basedir/xda-devtools/romtools/cabdump.pl",    "$bindir/cabdump.bat");
makebatfile("$basedir/xda-devtools/romtools/sdtool.pl",     "$bindir/sdtool.bat");
makebatfile("$basedir/xda-devtools/romtools/regsort.pl",    "$bindir/regsort.bat");
makebatfile("$basedir/xda-devtools/romtools/dmphv.pl",      "$bindir/dmphv.bat");
makebatfile("$basedir/xda-devtools/perlutils/dirsummary",   "$bindir/dirsummary.bat");
makebatfile("$basedir/xda-devtools/dumprom/dumpxip.pl",     "$bindir/dumpxip.bat");
makebatfile("$basedir/xda-devtools/dumprom/rdmsflsh.pl",    "$bindir/rdmsflsh.bat");
makebatfile("$basedir/xda-devtools/dumprom/editimgfs.pl",   "$bindir/editimgfs.bat");
makebatfile("$basedir/xda-devtools/romtools/fatinfo.pl",    "$bindir/fatinfo.bat");
makebatfile("$basedir/xda-devtools/perlutils/findstr",      "$bindir/findstr.bat");
makebatfile("$basedir/xda-devtools/perlutils/google.pl",    "$bindir/google.bat");
makebatfile("$basedir/xda-devtools/perlutils/uncomp.pl",    "$bindir/uncomp.bat");
makebatfile("$basedir/xda-devtools/perlutils/lstm",         "$bindir/lstm.bat");
makebatfile("$basedir/xda-devtools/perlutils/pfind",        "$bindir/pfind.bat");
makebatfile("$basedir/xda-devtools/perlutils/pgrep",        "$bindir/pgrep.bat");
makebatfile("$basedir/xda-devtools/perlutils/rfscopy",      "$bindir/rfscopy.bat");
makebatfile("$basedir/xda-devtools/perlutils/rfsdir",       "$bindir/rfsdir.bat");
makebatfile("$basedir/xda-devtools/romtools/splitrom.pl",   "$bindir/splitrom.bat");
makebatfile("$basedir/xda-devtools/perlutils/sumdiff",      "$bindir/sumdiff.bat");
makebatfile("$basedir/xda-devtools/perlutils/tdiff",        "$bindir/tdiff.bat");
makebatfile("$basedir/xda-devtools/perlutils/transpose",    "$bindir/transpose.bat");
makebatfile("$basedir/xda-devtools/perlutils/updatedb",     "$bindir/updatedb.bat");
makebatfile("$basedir/xda-devtools/perlutils/xdu",          "$bindir/xdu.bat");
makebatfile("$basedir/xda-devtools/perlutils/lsq",          "$bindir/lsq.bat");
makebatfile("$basedir/xda-devtools/perlutils/urldecode",    "$bindir/urldecode.bat");
makebatfile("$basedir/xda-devtools/perlutils/which",        "$bindir/which.bat");
makebatfile("$basedir/xda-devtools/perlutils/xcalc",        "$bindir/xcalc.bat");
makebatfile("$basedir/xda-devtools/perlutils/clocks",       "$bindir/clocks.bat");
makebatfile("$basedir/xda-devtools/perlutils/mp3tag",       "$bindir/mp3tag.bat");
makebatfile("$basedir/xda-devtools/perlutils/mimedecode",   "$bindir/mimedecode.bat");
makebatfile("$basedir/xda-devtools/perlutils/htmldecode",   "$bindir/htmldecode.bat");
makebatfile("$basedir/xda-devtools/perlutils/dnstrace",     "$bindir/dnstrace.bat");
makebatfile("$basedir/xda-devtools/perlutils/vcpdiff",      "$bindir/vcpdiff.bat");
makebatfile("$basedir/xda-devtools/perlutils/cvsls",        "$bindir/cvsls.bat");
makebatfile("$basedir/xda-devtools/perlutils/svnls",        "$bindir/svnls.bat");
makebatfile("$basedir/xda-devtools/perlutils/cvsstat",      "$bindir/cvsstat.bat");
makebatfile("$basedir/xda-devtools/perlutils/cvslog",       "$bindir/cvslog.bat");
makebatfile("$basedir/xda-devtools/perlutils/cvsupd",       "$bindir/cvsupd.bat");
makebatfile("$basedir/xda-devtools/perlutils/zipdbg.pl",    "$bindir/zipdbg.bat");
makebatfile("$basedir/xda-devtools/perlutils/peinfo.pl",    "$bindir/peinfo.bat");
makebatfile("$basedir/xda-devtools/perlutils/dumpstack.pl", "$bindir/dumpstack.bat");
makebatfile("$basedir/xda-devtools/romtools/dumpptable.pl", "$bindir/dumpptable.bat");
makebatfile("$basedir/xda-devtools/romtools/dumpbootsec.pl", "$bindir/dumpbootsec.bat");
makebatfile("$basedir/xda-devtools/xda2nbftool/typhoonnbfdecode.pl",    "$bindir/typhoonnbfdecode.bat");
makebatfile("$basedir/xda-devtools/xda2nbftool/alpinenbfdecode.pl",    "$bindir/alpinenbfdecode.bat");
makebatfile("$basedir/xda-devtools/xda2nbftool/decoderadio.pl",    "$bindir/decoderadio.bat");
makebatfile("$basedir/xda-devtools/xda2nbftool/dbhdecode.pl",    "$bindir/dbhdecode.bat");
makebatfile("$basedir/xda-devtools/xda2nbftool/nbh2dbh.pl",    "$bindir/nbh2dbh.bat");
makebatfile("$basedir/xda-devtools/romtools/splsplit.pl",    "$bindir/splsplit.bat");
makebatfile("$basedir/xda-devtools/perlutils/xml2pl",    "$bindir/xml2pl.bat");
makebatfile("$basedir/xda-devtools/perlutils/asn1dump.pl",    "$bindir/xasn1dump.bat");
makebatfile("$basedir/xda-devtools/perlutils/eventtime.pl",    "$bindir/eventtime.bat");
makebatfile("$basedir/xda-devtools/itsutils/comm/pppdump.pl",    "$bindir/pppdump.bat");

sub makebatfile {
    my ($src, $dst)= @_;

    my $fin= IO::File->new($src, "r") or die "$src: $!\n";
    my @lines= <$fin>;
    $fin->close();

    shift @lines if ($lines[0] =~ /^#!\S*perl/);

    my $fout= IO::File->new($dst, "w+") or die "$dst: $!\n";

    $fout->print($tplprefix);
    $fout->print(@lines);
    $fout->print($tplsuffix);

    $fout->close();

    chmod 0755, $dst;
}
