#!/usr/bin/perl -w

#
# A quick hack - hopefully will all go away shortly.
#

my $NSisPath = '/cygdrive/c/Program\\ Files/NSIS/makensis.exe';

sub write_nsis_script($$$$)
{
    my ($fname, $pack_name, $lang, $file_list_name) = @_;
    my $Script;

    open $Script, ">$fname" || die "Can't open '$fname': $!";

    print $Script <<"EOS";

    !include "MUI.nsh"
    Name "OpenOffice.org lang-pack $lang"
    OutFile "$pack_name"

    !insertmacro MUI_PAGE_DIRECTORY
    !insertmacro MUI_UNPAGE_CONFIRM
    !insertmacro MUI_UNPAGE_INSTFILES
    !define MUI_ABORTWARNING
    !insertmacro MUI_LANGUAGE "English"

    Page instfiles
    Section "InstallFiles" SecDummy
	SetOutPath "\$INSTDIR"
        File /r program
        File /nonfatal /r share
        File /nonfatal /r user 
	ExecWait "\$INSTDIR\\program\\setofficelang.exe -f -a $lang"
        WriteRegStr HKCU "Software\\OpenOffice Lang-Pack $lang" "" \$INSTDIR
	CreateDirectory "\$INSTDIR\\$lang"
        WriteUninstaller "\$INSTDIR\\$lang\\Uninstall.exe"
EOS
    print $Script <<'EOS';
    SectionEnd
    Function .onInit
	ReadRegStr $INSTDIR HKCU "Software\OpenOffice.org\UNO\InstallPath" ""
	Push $INSTDIR
	Call GetParent
	Pop $INSTDIR
    FunctionEnd
EOS
    print $Script <<"EOS";
    Section "Uninstall"
	Delete "\$INSTDIR\\$lang\\Uninstall.exe"
	RMDir "\$INSTDIR\\$lang"
	DeleteRegKey /ifempty HKCU "Software\\OpenOffice Lang-Pack $lang"
    SectionEnd
EOS
    print $Script <<'EOS';
    ; input, top of stack  (e.g. C:\Program Files\Poop)
    ; output, top of stack (replaces, with e.g. C:\Program Files)
    ; modifies no other variables.
    ;
    ; Usage:
    ;   Push "C:\Program Files\Directory\Whatever"
    ;   Call GetParent
    ;   Pop $R0
    ;   ; at this point $R0 will equal "C:\Program Files\Directory"

    Function GetParent
 
	Exch $R0
	Push $R1
	Push $R2
	Push $R3
   
	StrCpy $R1 0
	StrLen $R2 $R0
	
      loop:
	    IntOp $R1 $R1 + 1
	    IntCmp $R1 $R2 get 0 get
	    StrCpy $R3 $R0 1 -$R1
EOS
    print $Script <<"EOS";
	    StrCmp \$R3 "\\" get
EOS
    print $Script <<'EOS';
	    Goto loop
   
     get:
	StrCpy $R0 $R0 -$R1
     
	Pop $R3
	Pop $R2
	Pop $R1
	Exch $R0
     
    FunctionEnd
EOS
    close ($Script);
}

sub package_one_lang($)
{
    my $lang = shift;
    my $lang_iso = `$Xlate -i $lang`;
    chomp ($lang_iso);
    $lang_iso =~ s/\r//;

    print "Packaging lang $lang_iso ($lang)\n";

    $lang_install = $setup_vars{BUILDDIR} . "/lang-install";
    print " removing $lang_install\n";
    `rm -Rf $lang_install`;
    `mkdir -p $lang_install`;

    print " unpacking files\n";
    `package-lang --lang=$lang --OOINSTDIR=$lang_install`;

    my $lang_pack_name = "LangPack-$lang_iso.exe";

    print " writing nsis script\n";
    write_nsis_script("$lang_install/script", $lang_pack_name, $lang_iso,
		      $setup_vars{'BUILDDIR'} . '/lang_' . $lang_iso . '_list.txt');

    if (!-d "$lang_install/program" ||
	!-d "$lang_install/share" ||
	!-d "$lang_install/user") {
	print "Install failed; lang $lang_iso not supported\n";
	return;
    }

    print " invoking nsis\n";
    `cd $lang_install; $NSisPath script`;
    -f "$lang_install/$lang_pack_name" || die "Failed to make $lang_install/$lang_pack_name: $!";
	
    print " storing output\n";
    `mv $lang_install/$lang_pack_name $lang_output`;
}

print "Sucking env from setup\n";
open ($Vars, "bash -c '. ./setup ; set'|") || die "Can't find setup: $!";
while (<$Vars>) {
    /([^=]*)=(.*)/ || next;
    $setup_vars{$1} = $2;
}
close ($Vars);

$Xlate = $setup_vars{'TOOLSDIR'}. "/bin/openoffice-xlate-lang";
$lang_output = $setup_vars{BUILDDIR} . "/lang-packs";
`mkdir -p $lang_output`;

my @Langs = split (' ', `$Xlate -p all`);

for $a (@ARGV) {
    if ($a =~ m/--lang=(\S+)/) {
	@Langs = ( $1 );
    }
}

for $lang (@Langs) {
    package_one_lang ($lang);
}
