#!/usr/bin/perl

=head1 NAME

uninstall.pl

=head1 AUTHOR

Original work by Jeroen Daanen, Polderland Language & Speech Technology bv
9 feb 2007

Further developed by the Divvun project.

=head1 SYNOPSIS

Remove the installed Sami Proofing Tools

=head1 DESCRIPTION

Tries to remove the installed Divvun Tools.
Looks up installation location from file, and
removes installed files.

usage: perl uninstall.pl pathToThisScript pathWhereFilesWereCopiedTo
e.g. perl uninstall.pl /Library/DivvunTools/MSOffice2008 /Applications/Microsoft Office 2008/Shared applications/Proofing tools

=cut

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

#old files
my @oldfiles;

#new files
my @newfiles = (
	"SamiLuleAsBasqueHyphenation.proofingtool",
	"SamiNortAsCatalanHyphenation.proofingtool",
	"SamiLuleAsBasqueSpeller.proofingtool",
	"SamiNortAsCatalanSpeller.proofingtool",
	"SamiSoutAsSlovakHyphenation.proofingtool",
	"SamiSoutAsSlovakSpeller.proofingtool"
);

sub syscal
{
    my ($cmd, @args) = @_;
    if(scalar(@args)){
    	system(($cmd, @args));
    } else {
	system($cmd);
    }
}

my $budir = $ARGV[0];
my $InstallLocation = $ARGV[1];
my $component_ID = $ARGV[2];
my $DivvunDir = `echo $budir | rev | cut -d'/' -f2- | rev`;
chop $DivvunDir;

#remove the new files from location
foreach my $newfile (@newfiles){
    syscal("rm", "-rf", "$InstallLocation/$newfile");
}

#delete the DivvunTools dir for the specific Office package:
syscal("rm", "-Rf", "$budir");

#remove the system's knowledge about the installed software:
if ( $component_ID ) {
    system("pkgutil --forget $component_ID");
}

# Check if the DivvunTools dir is now emtpy, delete it as well if it is
my $installedpackages = `ls -1 $DivvunDir` ;
if (not $installedpackages ){
    syscal("rm", "-Rf", "$DivvunDir");
}

#succes message
print STDERR "Finished automatic uninstallation from $InstallLocation\n";

exit 0;
