#!/bin/sh -e

# A string that denotes the current version of the package:
version=6.2.9.506-1

# The name of the file that comes from Compaq's web site:
filename=ccc-6.2.9.506-1.alpha.rpm

##############################################################################

# This package doesn't pre-depend on cpio, so if it is being preconfigured
# and cpio is not present, exit. This script will be re-ran when the postinst
# runs.
if [ ! -x /bin/cpio ]; then
	exit 0
fi

# This script will check to see if a new file needs to be downloaded, and if
# so will prompt the user to do so.

# Use debconf.
. /usr/share/debconf/confmodule || exit

# Establish the preliminaries.
db_version 2.0
#db_capb 'backup'

# Minimalist rpm2cpio in perl.
# Code taken from http://www.eleves.ens.fr:8080/home/espel/rpm2cpio
rpm2cpio() {
	perl -e '
		undef $/;
		$|=1;
		$rpm = <>;

		($magic, $major, $minor, undef) = unpack("NCC C90", $rpm);
		exit "Not an RPM\n" if $magic != 0xedabeedb;
		exit "Not a version 3 RPM\n" if $major != 3;
		
		$rpm = substr($rpm, 96);
		
		while ($rpm ne "") {
			$rpm =~ s/^\c@*//s;
			($magic, undef, $sections, $bytes) = unpack("N4", $rpm);
			$smagic = unpack("n", $rpm);
			last if $smagic eq 0x1f8b;
			die "Error: header not recognized\n" if $magic != 0x8eade801;
			$rpm = substr($rpm, 16*(1+$sections) + $bytes);
		}
		
		exit "bogus RPM\n" if $rpm eq "";
		
		open(ZCAT, "|gzip -cd") || die "cannot pipe to gzip\n";
		print ZCAT $rpm;
		close ZCAT;
	' $1
}

# Called if we cannot communicate with the user.
abort() {
	# They can't see it..
	# Try to leave them a message anyway.
	db_fset ccc/needfile seen false
	db_input critical ccc/needfile || true
	db_go
	exit 1
}

# Go ahead and check if we're a new upstream or not
if [ ! -e "/usr/lib/compaq/ccc-version" -o \
     "`cat /usr/lib/compaq/ccc-version 2>/dev/null`" != "$version" ]
then

# This seems to be needed for upgrade
db_fset ccc/intro rpmok false

# Loopdee loop
db_fget ccc/intro rpmok
while [ "$RET" != "true" ]
do
	# reset all these.. lala
	db_fset ccc/intro seen false
	db_fset ccc/download_dir seen false
	db_input critical ccc/intro || abort
	db_go

	# Get response (continue y/n)
	db_get ccc/intro

	if [ "$RET" != "true" ]
		then
		# If No, tell them how to do it later and quit.
		db_input low ccc/later || true
		db_go
		exit 0
	fi

	# Otherwise continue
	db_subst ccc/download_dir filename "$filename"

	db_beginblock
	# ask them where to find the rpm, important
	db_input critical ccc/download_dir || abort
	# ask them if we can delete the rpm, lower priority
	db_fset ccc/deletefile seen false
	db_input medium ccc/deletefile || false
	db_endblock

	db_go || exit 1

	# Actually check if rpm is ok, and .. deal with it
	db_get ccc/download_dir
		if [ -e "$RET/$filename" ] && (rpm2cpio $RET/$filename | cpio --list >/dev/null 2>&1)
		then
			# happy days.
			db_fset	ccc/intro rpmok true
		else	
			# shit happens
			# Display a message and then loop.
			db_subst ccc/badfile filename "$RET/$filename"
			db_fset ccc/badfile seen false
			db_input critical ccc/badfile || exit 1
			db_fset ccc/intro rpmok false
		fi

done
else
	echo "Not reinstalling ccc since our upstream version matches"
	echo "the one currently installed."
	echo "My upstream version is ${version}, I'm looking at "
	echo "/usr/lib/compaq/ccc-version"
	exit 0
fi
