#!/usr/bin/perl
use strict;
use warnings;

#  Name:    rmvpkg
#  Author:  Terry Neve
#  Created: Jan 2017
#
#  Program to remove developement packages installed during package build
#  process.  It takes the *.remove file created by addpkg and builds/executes
#  an "apt-get remove" to remove the packages that were newly installed.
#  It is designed to be called via Right-Click service menu in pkgutils.
#
#  Revisions:
#       2017012.01  Initial working version

my $instlog = shift or die "Usage:\n$0 install-log-file\n";
my @packages;
my $instflag = undef;

open(LOG, $instlog) || die "Can't open log file: $!\n";

while (my $line = <LOG>) {
    if ($instflag) {
        if ($line =~ m#^\s+(\S+)\s+\((\S+)\)#) {
            push @packages, $1;
        }
    }
    else {
        if ($line =~ m/The following NEW packages will be installed:/i) {
            $instflag = 1;
        }
        else {
            $instflag = undef;
        }
    }
}

print "Getting root priviledge\n";
system(qq(su -c "apt-get remove @packages"));

exit 0;
