#!/usr/bin/perl -w
#
use strict;
use English;
#
if ( $#ARGV > 1 or $#ARGV < 0 )
  {
  print "Usage: $0 [ path to packages ] list_file \n";
  exit 1;
  }
my($ppath, $spath, $spath1, $spath2, $filename, $record, $rname, $dname);
my($iversion, $version, $package, $available, $status, $check1, $check2);
my($command, $i, $f);
my(@hold, @lst);
if ( $#ARGV < 1 )
  {
  $ppath=`pwd`;
  chomp $ppath;
  $filename=$ARGV[0];
  }
else
  {
  $ppath=$ARGV[0];
  $filename=$ARGV[1];
  }
#
open (INPUTFILE, "<$filename")  or die "Can't open $filename\n";
#
while ($record = <INPUTFILE>)
  {
  chomp $record;
  @lst=split(/ /, $record);
  if ("$lst[0]" ne "#") 
    {
    if ("$lst[0]" eq "-i")
      {
      $rname=$lst[$#lst];
      $spath1=join('',$ppath,$lst[$#lst - 1],$rname,"-");
      $spath2=join('',$ppath,$lst[$#lst - 1],$rname,"_");
      system ("echo 'Preparing to install $rname.'");
#
#  find the available package in the archive
#
      $spath=$spath2;
      @hold=`ls $spath*.deb`;
      if ("$hold[0]" eq "")
         {
         $spath=$spath1;
         @hold=`ls $spath*.deb`;
         }
      $available=$hold[0];
      chomp $available;
#
#  get internal package name (sometimes external and internal names differ)
#
      $command="dpkg -I $spath*.deb 2> /dev/null |gawk 'tolower(";
      $command=$command . '$1' . ") ~ /kage:/ {print " . '$2' . "}'";
      $dname=`$command`;
      chomp $dname;
#
#  get internal package version
#
      $command="dpkg -I $spath*.deb 2> /dev/null |gawk 'tolower(";
      $command=$command . '$1' . ") ~ /rsion:/ {print " . '$2' . "}'";
      $iversion=`$command`;
      chomp $iversion;
#
#  get version of installed package
#
      $command="dpkg -s $dname 2> /dev/null |gawk 'tolower(";
      $command=$command . '$1' . ") ~ /rsion:/ {print " . '$2' . "}'";
      $version=`$command`;
      chomp $version;
      $command="dpkg -s $dname 2> /dev/null |gawk 'tolower(";
      $command=$command . '$1' . ") ~ /atus:/ {print " . '$NF' . "}'";
      $status=`$command`;
      chomp $status;
      $check1=join('',$spath1,$version,".deb");
      $check2=join('',$spath2,$version,".deb");
      if( ($check1 eq $available) and ("$status" eq "installed") ) 
        {
        $package="$rname" . "-" . "$version";
        system ("echo 'Package $package is already installed.'");
        }
      elsif( ($check2 eq $available) and ("$status" eq "installed") ) 
        {
        $package="$rname" . "_" . "$version";
        system ("echo 'Package $package is already installed.'");
        }
      else
        {
        system("dpkg --compare-versions $version gt $iversion 2> /dev/null");
        if ( "$CHILD_ERROR" eq "0" )
           {
           system ("echo 'You are attempting to downgrade this package'");
           system ("echo 'Do you wish to downgrade? [Y/N]'");
           $f=<STDIN>;
           chomp $f;
           }
        else
           {
           $f="Y"
           }
        if ( ("$f" eq "Y") or ("$f" eq "y") )
           {
           $command="dpkg ";
           for ($i = 0; $i < $#lst - 1; $i++) { $command=$command . $lst[$i] . " "; }
           $command=$command . $available;
           system("echo '$command'");
           system($command);
           if ( "$CHILD_ERROR" eq "256" )
              {
              exit 1;
              }
           }
        }
      }
    elsif ( "$lst[0]" eq "--purge" )
      {
      system ("echo 'Preparing to purge $lst[$#lst].'");
      $command="dpkg -s $lst[$#lst] |gawk '/Status:/ {print " . '$NF' . "}'";
      $status=`$command`;
      chomp $status;
      if ("$status" eq "not-installed")
        {
        system ("echo 'Cannot purge $lst[$#lst]. It is not installed.'");
        }
      else
        {
        $command="dpkg ";
        for ($i = 0; $i < $#lst+1; $i++) {$command=$command . $lst[$i] . " ";}
        system("echo '$command'");
        system($command);
        if ( "$CHILD_ERROR" eq "256" )
           {
           exit 1;
           }
        }
      }
    else
      {
      system ("echo 'Executing requested dpkg command.'");
      $command="dpkg ";
      for ($i = 0; $i < $#lst + 1; $i++) { $command=$command . $lst[$i] . " "; }
      system("echo '$command'");
      system($command);
      if ( "$CHILD_ERROR" eq "256" )
         {
         exit 1;
         }
      }
    }
  }
close INPUTFILE;
system ("echo '$0 from file: $filename is complete.'");
exit 0;