#!/bin/sh -e

# A string that denotes the current version of the package:
version=1.2.0-3

# The name of the file that comes from Compaq's web site:
filename=cfalrtl-1.2.0-3.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 cfalrtl/needfile seen false
	db_input critical cfalrtl/needfile || true
	db_go
	exit 1
}

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

db_fset cfalrtl/intro rpmok false

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

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

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

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

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

	db_go || exit 1

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

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