#! /usr/bin/perl

my $cgisite="http://server.com/cgi-bin";

sub installdeb {
  $query=@_[0];
  print "Looking up package: $query..";
  open (INFO,"wget -q --output-document=- $cgisite/apt-info?$query |") or die "apt-info failed:$!\n"; 
  $location=<INFO>;
  close (INFO);
  if ($location eq "") { die "apt-info failed: no valid response !\n"; }
  print "found !\n";
  chomp $location;
  $filename=(split("/",$location))[-1];
  $olddir=`pwd`;
  chdir "/var/cache/apt/archives";
  if (! -f $filename) { 
    print "Downloading from: $location...\n";
    system("wget -q $location && dpkg -i $filename");
  } else {
    print "Installing cached package: $filename...\n";
    system("dpkg -i $filename");
  }
  chdir "$olddir";
}

sub installplusdeps {
  $query=@_[0];
  print "Looking up dependencies for $query...\n";
  open (DEPS,"wget -q --output-document=- $cgisite/apt-deps?$query |") or die "apt-deps failed:$!\n";
  while (<DEPS>) {
    $dep = $_;
    chomp $dep;
    if (! -f "/var/lib/dpkg/info/$dep.list") {
      installdeb ($dep);
    } else {
      print "$dep already available\n";
    }
  }
  close DEPS;
  installdeb ($query);
}

    
sub removedeb {
  $query=@_[0];
  print "Removing $query...\n";
  `dpkg --remove $query`;
}

sub cleandeb {
  `rm -f /var/cache/apt/archives/*.deb`;
}

sub getversions {
  open (STAT,"/var/lib/dpkg/status");
  my ($package,$version,$status);
  while (<STAT>) {
    chomp;
    if (/^Package: |^Version: |Status: /) {
      $line=$_;
      if ($line =~ /^Package: (.+)/) {
        $package=$1;
      } elsif ($line =~ /^Status: (.+)/) {
        $status=$1;
      } elsif ($line =~ /^Version: (.+)/) {
        $version=$1;
        if ($status eq "install ok installed") {
          $pkgversions{$package}=$version;
          $package=""; $status="", $version=""; 
        }
      }
    }
  }
  close STAT;
}

sub upgradepackages {
  my $line;
  my @updatepackages;
  print "Looking up versions of currently installed packages...\n";
  getversions;
  foreach $key (keys %pkgversions) {
    $line.=$key."_";
  }
  print "Checking for new versions...\n";
  open (UPGRADE,"wget -q --output-document=- $cgisite/apt-upgrade?$line |") or die "apt-upgrade failed:$!\n";
  while (<UPGRADE>) {
    ($pack,$ver)=split(" ");
    if (($ver ne "") && ($ver ne $pkgversions{$pack})) {
      print "$pack needs update to $ver\n";
      push (@updatepackages,$pack);
    }
  }
  close UPGRADE;
  foreach (@updatepackages) {
    installdeb ($_);
#    print "faking $_\n";
  }
} 

sub usage {
  print << "EOF";
$0 install <package>        to install a package
$0 remove <package>         to remove a package
$0 clean                    to clean up package cache
$0 upgrade                  to upgrade all installed packages

EOF
}

$!=1;
my %pkgversions;
if (! -d "/var/cache/apt/archives" ) {
  print "Creating archive directory..\n";
  `mkdir -p /var/cache/apt/archives`;
}

if ($ARGV[0] eq "install") {
  installplusdeps ($ARGV[1]);
} elsif ($ARGV[0] eq "remove" ) {
  removedeb ($ARGV[1]);
} elsif ($ARGV[0] eq "clean" ) {
  cleandeb;
} elsif ($ARGV[0] eq "upgrade" ) {
  upgradepackages;
} else {
  usage;
}

