#!/usr/bin/perl
#
# inst-get -- inspired by all the requests for Debian "apt-get" functionality
#             in IRIX's inst. This is a crude implementation, as it is
#             just a wrapper around "wget" and "inst". Since inst has
#             slow startup and shutdown, this script can be very slow,
#             in addition to how slow downloading the packages is over
#             your internet connection. 
#
# Usage:
#	inst-get [-v] [-d <dir>] package [package ...]
#		"-v" sets wget to be verbose.
#		"-d <dir>" uses a non-default directory
#		"package" is a (usually) unique name part to get
#
# Author:
#	Scott Henry <scotth@sgi.com>, 2001-05-08
#
# License:	What the heck:
#	(C) Copyright 2001-2002 Scott Henry and Silicon Graphics, Inc
#	This program is distributed under the GNU Public License,
#	version 2. There is no warranty on this software, use at
#	your own risk.
#
# History:
#	0.1	(2001-05-08) first version, does downloads only.
#		It doesn't know about "or" dependencies, and
#		downloads all of them...
#	0.14	(2003-04-28) Denis.Pugnere@igh.cnrs.fr
#		- bug resolved, for users without SOCKS
#		- correction in regex "Also install" (for fw_gtk+)
#		- added "delay_conflicts off" to parameter file
#		- un-URLIZE the packages (example for fw_gtk+ : %2b is the '+' character)

# autoflush output after every write.
$|=1;

use Getopt::Std;
use URI::Escape;
#
# Some sites (like here) may need to use SOCKS to get out to the Internet
# Uncomment and modify as needed
#
# $ENV{"SOCKS_SERVER"} = "";
# $ENV{"SOCKS_NS"} = "";
$SOCKS = "";			# pmsocks or whatever

getopts("vd:") or die "Usage: $0 [-v] [-d dir] package [package ...]\n";

$FW_URL = "http://freeware.sgi.com/Dist";
$WGET = "/usr/freeware/bin/wget";
$WG_OPTS = "-q";
if ($opt_v) {
	$WG_OPTS = "";
}
$INDEX_FILE = "index.html";
$DIST_DIR = $opt_d ? $opt_d : "/var/tmp/inst-get";
-d $DIST_DIR or mkdir $DIST_DIR,0775 or die "Can't mkdir $DIST_DIR? $?\n";

chdir $DIST_DIR or die "Can't cd to $DIST_DIR? $?\n";

@pkgs = @ARGV;		# initial packages to download
unless (@pkgs) {
	die "Nothing to fetch? no packages on command line\n";
}

# grab the directory listing of the freeware site.
# also makes sure that we can access it.
unlink $INDEX_FILE if (-e $INDEX_FILE);
if ($SOCKS) {
	system($SOCKS, $WGET, $WG_OPTS, $FW_URL."/.");
} else {
	system($WGET, $WG_OPTS, $FW_URL."/.");
}

# parse the package tardist names from the downloaded index file.
@packages = ();
open(R, "<$INDEX_FILE") or die "Failed to download $INDEX_FILE from $FW_URL?\n";
while(<R>) {
	next unless /HREF=\"([^\/\"]+)\"/;
	push @packages, uri_unescape($1);
}
close(R);

# setup the inst stuff
$CMDFILE = "cmd.file";
open(C, ">$CMDFILE") or die "Can't create inst command file? $?\n";
print C "delay_conflicts off\ninstall prereqs\nconflicts\nkeep *\nquit\n";
close(C);
$INSTCMD = "/usr/sbin/inst -E -M -V delay_conflicts:off -f . -c $CMDFILE -I default";

$downloaded = 0;
# now loop while we have packages to fetch.
while ($#pkgs >= 0) {
	@fetch = ();		# clear it out
	# convert package name to tardist file
	foreach $p (@pkgs) {
		push(@fetch, grep(/$p/, @packages));
	}
	if ($#fetch < 0) {
		die "no packages found to download\nMisspelled package names or broken dependencies?\n";
	}
	# download each tardist file in turn
	foreach $p (@fetch) {
		if (-f $p) {
			print "$p already downloaded\n";
		} else {
			print "downloading $p ...";
			if ($SOCKS) {
				system($SOCKS, $WGET, $WG_OPTS, $FW_URL."/".$p);
			} else {
				system($WGET, $WG_OPTS, $FW_URL."/".$p);
			}
			print "done\ntar -xf $p ...";
			system("/usr/bin/tar", "-xf", "$p");
			print "done\n";
			$downloaded++;
		}
	}
	# now run inst on the files
	@pkgs = ();
	print "running : $INSTCMD\n";
	open(I, "$INSTCMD |");
	while(<I>) {
		last if /^No conflicts/;
		next unless /Also install\s+([-_\+\w]+)\.[-_\w]+\.[-_\w]+\s/;
		push @pkgs, $1;
		print "Dependance : $1\n";
		while(<I>) {
			last unless /\s([-_\+\w]+)\.[-_\w]+\.[-_\w]+\s/;
			push @pkgs, $1;
		}
	}
	close(I);
}
print "$downloaded packages fetched\n";
unlink $INDEX_FILE;
