#!/bin/sh -e

# A string that denotes the current version of the satellite:
version=0520

# The name of the file that comes from the web site:
archive=AGSatellite0520-glibc21.tar.gz

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

mkdir /var/tmp/agsatellite-unpacked.$$
agstmpdir="${TMPDIR:-/var/tmp}/agsatellite-unpacked.$$"
#echo "DEBUG: \$agstmpdir= $agstmpdir"

abort() {
	# Give up. This should never happen (unless the disk fills up or
	# something).
	cd ..
	rm -rf ${agstmpdir}
	echo "Couldn't extract the tar file, aborting"
	exit 1
}

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

	# Check and make sure that we're supposed to be doing anything
	db_get agsatellite/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.
	# Check anyway, confusing errors may result otherwise.
	db_get agsatellite/download_dir
	if [ ! -e "${RET}/${archive}" ]; then
		echo "Something is horribly wrong, debconf has stale/incorrect file info."
		echo "Please file a bug explaining how you received this error."
		echo "Purging and reinstalling agsatellite may help."
		exit 1
	fi

	DIR="$RET"
	#echo "DEBUG: \$DIR= $DIR"

	cd $agstmpdir
	#echo -n "Aktuelles Verzeichnis:"
	#pwd
	tar xzf $DIR/$archive || abort

	# Actually install the files
	cp ${agstmpdir}/AGSatellite${version}/AGSatellite /usr/bin/
	cp ${agstmpdir}/AGSatellite${version}/*.txt /usr/share/doc/agsatellite
	
	if [ ! -e /usr/lib/agsatellite ]; then 
		mkdir /usr/lib/agsatellite
		#echo DEBUG: /usr/lib/agsatellite installed
	fi
	echo "$version" > /usr/lib/agsatellite/version
	# clean tmp dir
	rm -rf $agstmpdir

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

#DEBHELPER#

