#!/bin/sh -e

version=0520

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

trap ":" INT QUIT TSTP

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

# Establish the preliminaries.
db_version 2.0

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

# Loopdee loop
RUN_LOOP=1
while [ "$RUN_LOOP" = "1" ]
do

	db_reset agsatellite/intro
	db_input critical agsatellite/intro || abort
	db_go

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

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

	# Otherwise continue
	db_fset agsatellite/download_dir seen false
	db_subst agsatellite/download_dir filename "$filename"

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

	db_go || exit 1

	# check if file is ok, and .. deal with it
	db_get agsatellite/download_dir
	if [ -e "$RET/$filename" ] && \
			(tar tzf $RET/$filename >/dev/null 2>&1 ); then
		# happy days.
		RUN_LOOP=0
	else
		# shit happens
		# display a message and then loop.
		db_subst agsatellite/badfile filename "$RET/$filename"
		db_fset agsatellite/badfile seen false
		db_input critical agsatellite/badfile || exit 1
		RUN_LOOP=1
	fi

done

