#! /usr/bin/perl
# $Id: update,v 1.4.2.2 1998/05/18 20:51:28 asm21 Exp $

#use diagnostics;
#use strict;

# extract the directory containing the binary files, and read a global set of
# routines from a file in that directory.
BEGIN {
    my @path=split("/",$0);
    $#path--;
    my $bindir=join("/",@path);
    do "$bindir/generic" or die "Failed to execute generic routine library $bindir/generic: $!\n";
}

boldecho "Install from a mountable filesystem\n";

my $vardir=$ARGV[0];
my $method=$ARGV[1];
my $option=$ARGV[2];

chdir("$vardir/methods/mountable");

do options or die "Couldn't read configuration file (re-choose under Access)";

my $updatetype='update';
my $used;

if ($option eq "debug") {
    local $^W = 0;
    eval 'sub syscmd { my $cmd=shift; echo($cmd); }';
}

sub dpkgupdate {
    my $rv;
    my $f=shift;

    syscmd("dpkg --$updatetype-avail $f") or die "dpkg error, stopped";
    $updatetype="merge";
    $used=1;
    return $rv;
}

sub dpkgupdategz {
    my $rv;
    my $f=shift;

    echo "Uncompressing $f ...";
    syscmd("gzip -dc $f > Packages.tmp") or die "cannot decompress packages file, stopped";
    dpkgupdate("Packages.tmp");
    unlink("Packages.tmp") or warn "failed to delete temporary file. Continuing anyway, ";
}

sub checkfile {
    my $file=shift;

#    print("Checking for $file ...");
    if ( -e "$file" ) {
#	echo(" found.");
	return 1;
    } else {
#	echo(" not found.");
	return 0;
    }
}

domount();

my $d;
for $d (@::dirlist) {
    $used=0;
    
    # if there's a packages file available in the normal distribution for this directory, use that.
    if (checkfile("$::rootpath/$d/Packages")) {
	dpkgupdate("$::rootpath/$d/Packages");
    } elsif (checkfile("$::rootpath/$d/Packages.gz")) {
	dpkgupdategz("$::rootpath/$d/Packages.gz");
    }

    # if there's one in the local packages, use that
    if (checkfile("$::localpath/$d/Packages")) {
	dpkgupdate("$::localpath/$d/Packages");
    } elsif (checkfile("$::localpath/$d/Packages.gz")) {
	dpkgupdategz("$::localpath/$d/Packages.gz");
    # if the directory is there, and dpkg-dev is installed, we can make our own Packages file
    } elsif (checkfile("$::localpath/$d") && -x "/usr/bin/dpkg-scanpackages" ) {
	if ( ! checkfile("$::localpath/$d/Packages.auto.gz")
	    || ( -C $::localpath . "/" . $d < -C $::localpath . "/" . $d . "/Packages.auto.gz" )) {
	    chdir($::localpath);
	    echo "Constructing Packages file for local directory $d...";
	    syscmd("dpkg-scanpackages $d /dev/null | gzip > $d/Packages.auto.gz")
		or die "Error constructing new packages file for $::localpath, stopped";
	    chdir("$vardir/methods/mountable");
	}
	dpkgupdategz "$::localpath/$d/Packages.auto.gz";
    }
    print STDERR "Warning: Directory $d not found in local or root trees.\n" if (!$used);
}

# what does this actually do?!
syscmd("dpkg --forget-old-unavail");

# keep a backup of the available file
echo "Copying available packages file.";
syscmd("rm -f $vardir/methods/mountable/available"); # ignoring errors
syscmd("cp $vardir/available $vardir/methods/mountable/available") or warn "Error backing up available file: $!\n";

doumount();
    
waitkey();

exit 0;
