#!/usr/bin/perl

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

#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"
);

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;
