#!/bin/sh -e

# A string that denotes the current version of the package:
version=5.1.0-4

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

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

# It seems these need to be reset on upgrade
db_fset cpml-ev5/intro rpmok false
db_fset cpml-ev5/intro seen false
db_fset cpml-ev5/download_dir seen false

# Loopdee loop
db_fget cpml-ev5/intro rpmok
while [ "$RET" != "true" ]
do
	# Check for valid CPU type using Thomas Weyergraf's trick
	# (Actually, this doesn't use his trick because implver has not been
	# installed, but postinst uses it properly.)
	cputype=`grep "cpu model" /proc/cpuinfo | cut -b 14-`
	if [[ ! "$cputype" < "EV6" ]]
	then
		db_input critical cpml-ev5/wrongcpu || abort
		db_go

		db_get cpml-ev5/wrongcpu
		if [ "$RET" != "true" ]
		then
			# Tell them to remove this and quit
			db_input low cpml-ev5/remove || true
			db_go
			exit 0
		fi
	fi

	# reset all these.. lala
	db_fset cpml-ev5/intro seen false
	db_fset cpml-ev5/download_dir seen false
	db_input critical cpml-ev5/intro || abort
	db_go

	# Get response (continue y/n)
	db_get cpml-ev5/intro

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

	# Otherwise continue
	db_subst cpml-ev5/download_dir filename "$filename"

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

	db_go || exit 1

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

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