#!/usr/bin/perl
# $Id: postflight 28667 2009-12-10 07:41:53Z sjur $

=head1 NAME

postflight

=head1 AUTHOR

Jeroen Daanen, Polderland
8 feb 2007

Further work by the Divvun project.

=head1 SYNOPSIS

postflight actions

=head1 DESCRIPTION

Will be executed after installation. 
- removes existing files, if already installed
- copies the new files to the existing directory
- creates an Uninstaller AppleScript application


=cut

use strict;
use integer;
no utf8;
use warnings;

#possible installation directories
my @dirs =(
    "Applications/Microsoft Office 2004/Shared applications/Proofing tools",
    "Applications/Microsoft Office 2004/Gedeelde toepassingen/Taalprogramma's",
    "Applications/Microsoft Office 2004/Gemeinsame Anwendungen/Korrekturhilfen",
    "Applications/Microsoft Office 2004/Aplicaciones compartidas/Herramientas de correccio\314\201n",
    "Applications/Microsoft Office 2004/Applications partage\314\201es/Outils de ve\314\201rification",
    "Applications/Microsoft Office 2004/Applicazioni condivise/Strumenti di correzione",
    "Applications/Microsoft Office 2004/Delade program/Spra\314\212kverktyg",
    "Applications/Microsoft Office 10/Shared applications/Proofing tools",
    "Applications/Microsoft Office 10/Gedeelde toepassingen/Taalprogramma's",
    "Applications/Microsoft Office 10/Gemeinsame Anwendungen/Korrekturhilfen",
    "Applications/Microsoft Office 10/Aplicaciones compartidas/Herramientas de correccio\314\201n",
    "Applications/Microsoft Office 10/Applications partage\314\201es/Outils de ve\314\201rification",
    "Applications/Microsoft Office 10/Applicazioni condivise/Strumenti di correzione",
    "Applications/Microsoft Office 10/Delade program/Spra\314\212kverktyg"
);

# This is the library installer dir, from which the tools will be copied into the proper Word dir:
my $sourceDir = $ARGV[2]."Library/DivvunTools/MSOffice2004";
   $sourceDir =~ s/\/\//\//g;
my $uninstallerApp             = "$sourceDir/Remove Divvun for MS Office 2004.app";
my $uninstallerScriptFile      = "$sourceDir/uninstallData/uninstall.pl";
my $uninstallerAppleScriptFile = "$sourceDir/uninstallData/uninstall.scpt";
my $title                      = "Divvun for MS Office 2004";
my $component_ID               = "no.samediggi.divvun.msoffice.2004.proofingtools.pkg";

#new files
my @newfiles = (
# sme:
    "SamiNortAsCatalan-sp",
    "SamiNortLexWithCatalanRez",
    "SamiNortAsCatalan-hy",
    "SamiNorthy-diclx",
    "SamiNorthy-patlx",
# smj:
	"SamiLule_hy-dic.lex",
	"SamiLule_hy-pat.lex",
	"SamiLuleAsBasque-hy",
	"SamiLuleAsBasque-sp",
	"SamiLuleLexWithBasqueRez",
# sma:
    "SamiSoutAsSlovak-sp",
    "SamiSoutLexWithSlovakRez",
    "SamiSoutAsSlovak-hy",
    "SamiSout-hy-dic.lex",
    "SamiSout-hy-pat.lex"
);

my @oldfiles = @newfiles;

sub syscal
{
    my ($cmd, @args) = @_;
    if(scalar(@args)){
#        print "Command $cmd with args: @args \n";
    	system(($cmd, @args)) == 0 or print "$cmd with arguments @args failed!\n";
    } else {
#       print "Command $cmd\n";
	   system($cmd) == 0 or print "$cmd failed!\n";
    }
}

foreach my $installdir (@dirs){
    my $targetdir = $ARGV[2].$installdir;
    $targetdir =~ s/\/\//\//g; # simplify //
#    print STDERR "Divvun MSOffice2008 installer: targetdir is: $targetdir\n";
    if(-e $targetdir){
    
		#always remove the old files before installing the new ones:
		foreach my $oldfile (@oldfiles){
		   syscal("rm", "-rf", "$targetdir/$oldfile");
		}	
	
		#copy the new files from the sourceDir to the targetdir
		foreach my $newfile (@newfiles) {
		    syscal("cp", "-fR", "$sourceDir/$newfile", "$targetdir/$newfile");
		    syscal("chmod", "-R", "g+rw,o+r", "$targetdir/$newfile");
		}
    	
	#UNINSTALLATION:
    open(UAS, ">$uninstallerAppleScriptFile") or die "Cannot open $uninstallerAppleScriptFile. $!";
        print UAS "display dialog (localized string of \"Are you sure you want to uninstall:\") & return & return & \"$title\" with title (localized string of \"Uninstall\") & \": $title\" with icon caution buttons {{localized string of \"Cancel\"}, {localized string of \"Uninstall\"}} cancel button 1 default button 2\n";
        print UAS "try\n";
          print UAS "do shell script \"\'$uninstallerScriptFile\' \'$sourceDir\' \'$targetdir\' \'$component_ID\'\" with administrator privileges\n";
          print UAS "tell application \"Finder\"\n";
            print UAS "activate\n";
            print UAS "display dialog (localized string of \"Finished uninstallation of\") & return & return & \"$title\" with title (localized string of \"Uninstallation done\") buttons {localized string of \"OK\"} default button 1\n";
          print UAS "end tell\n";
        print UAS "on error errText number errNum\n";
          print UAS "if (errNum is equal to -128) then\n";
            print UAS "-- User cancelled, do nothing.\n";
          print UAS "else\n";
            print UAS "tell application \"Finder\"\n";
            print UAS "activate\n";
            print UAS "display dialog (localized string of \"Uninstallation failed.\") & return & return & (localized string of \"Cause:\") & errText with title (localized string of \"Uninstallation failed.\") with icon caution buttons {localized string of \"OK\"} default button 1\n";
            print UAS "end tell\n";
          print UAS "end if\n";
        print UAS "end try\n";
    close(UAS) or die "Cannot close $uninstallerAppleScriptFile. $!";

	#compile the uninstallation AppleScript into a double-clickable application
	syscal("osacompile", "-o", "$uninstallerApp", "$uninstallerAppleScriptFile");

	#remove the applescript text file now that we have a compiled application:
	syscal("rm", "-f", "$uninstallerAppleScriptFile");

	#copy locale files to uninstaller app:
	system("cp -rf $sourceDir/*.lproj \"$uninstallerApp/Contents/Resources/\"");
	system("rm -rf $sourceDir/*.lproj");

	#we've found what we were looking for, exit the loop
	last;
    }
}

exit 0;