#!/bin/sh -e

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

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

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

# Use debconf.
. /usr/share/debconf/confmodule
db_version 2.0

# 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
}

abort() {
	# Give up. This should never happen (unless the disk fills up or
	# something).
	cd ..                                                                           
	rm -r ${ccctmpdir}                                                                 
	echo "Couldn't extract the rpm, aborting"
	echo "You can set \$TMPDIR if /tmp is not an option"
	exit 1  
}

ccctmpdir="${TMPDIR:-/tmp}/ccc-unpacked.$$"

# Check if external files are already installed.
if [ "$1" = "configure" -a \( ! -e "/usr/lib/compaq/ccc-version" -o \
     "`cat /usr/lib/compaq/ccc-version 2>/dev/null`" != "$version" \) ]; then

	# Check and make sure that we're supposed to be doing anything
		db_get ccc/intro
		if [ "$RET" != "true" ]
			then
			# We haven't been configured so exit
			exit 0
		fi

	# The config script should have handled prompting for a file
	# and ensuring it exists and can be extracted with cpio.
	db_get ccc/download_dir
	DIR="$RET"
	
	mkdir $ccctmpdir
	cd $ccctmpdir
	rpm2cpio $DIR/$archive | cpio --extract --make-directories >/dev/null 2>&1 || abort

	# Note this only allows one version of ccc to be installed.  A future
	# installer package might not have this limitation.
	# Remove all of the old files (should be the same as in prerm)
	rm -rf /usr/lib/compaq/ccc-version /usr/share/doc/ccc/README* \
	  /usr/lib/compaq/ccc-6.2.9.506-1/alpha-linux/man \
	  /usr/lib/compaq/ccc-6.2.9.506-1/alpha-linux/include \
	  /usr/lib/compaq/ccc-6.2.9.506-1/alpha-linux/bin/.version \
	  /usr/lib/compaq/ccc-6.2.9.506-1/alpha-linux/bin/comp.config \
	  /usr/lib/compaq/ccc-6.2.9.506-1/alpha-linux/bin/create-comp-config.sh \
	  /usr/lib/compaq/ccc-6.2.9.506-1/alpha-linux/bin/lnxcc \
	  /usr/lib/compaq/ccc-6.2.9.506-1/alpha-linux/bin/lnxcc_driver \
	  /usr/lib/compaq/ccc-6.2.9.506-1/alpha-linux/bin/probe_linux.sh \
	  /usr/lib/compaq/ccc-6.2.9.506-1/alpha-linux/bin/protect_headers_setup.sh \
	  /usr/share/doc/ccc/*.htm /usr/share/doc/ccc/*.gif \
	  /usr/share/doc/ccc/*.txt /usr/share/doc/ccc/Programmers_Guide \
	  /usr/share/doc/ccc/Language_Reference /usr/share/doc/ccc/COPYING

	# Fix permissions.
	cd $ccctmpdir
	chown -R root.root *
	chmod -R go=rX *
	chmod -R u+rw *

	# Install the docs
	cd usr/doc/ccc-6.2.9.506
	cp -a * /usr/share/doc/ccc
	cd $ccctmpdir

	# Install the meat of the package, stripping binaries
	# (just to make sure).
	install -d /usr/lib/compaq/ccc-6.2.9.506-1/alpha-linux/bin
	cd usr/lib/compaq/ccc-6.2.9.506-1/alpha-linux
	gzip man/*sml man/*/*
	cp -a include man /usr/lib/compaq/ccc-6.2.9.506-1/alpha-linux
	cd bin
	strip lnxcc
	strip lnxcc_driver
	mv -f create-comp-config.sh lnxcc lnxcc_driver probe_linux.sh \
	  protect_headers_setup.sh \
	  /usr/lib/compaq/ccc-6.2.9.506-1/alpha-linux/bin

	# Run create-comp-config.sh, specifying the gcc dir so we can depend on
	# a particular version (currently the gcc-2.95 package).
	/usr/lib/compaq/ccc-6.2.9.506-1/alpha-linux/bin/create-comp-config.sh \
	  ccc-6.2.9.506-1 /usr/lib/gcc-lib/alpha-linux/2.95.4

	# Finally, create the -version file for comparison in future upgrades.
	echo "$version" > /usr/lib/compaq/ccc-version

	cd /
	rm -r $ccctmpdir

	db_get ccc/deletefile
	if [ "$RET" = true ]; then
		rm -f $DIR/$archive
	fi
fi

# Install cc alternatives symlink, since we provide c-compiler
if [ "$1" = "configure" ]; then
    update-alternatives --install /usr/bin/cc cc /usr/bin/ccc 10 \
    --slave /usr/share/man/man1/cc.1.gz cc.1.gz /usr/share/man/man1/ccc.1.gz
fi

#DEBHELPER#
