#!/bin/sh
# This is part of the lw-installer program: 
#    http://b9.com/lw-installer.html
#
# Copyright (c) 2002 Kevin M. Rosenberg <kmr@debian.org>
#
# lw-installer is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License (version 2) as
# published by the Free Software Foundation.
#
# lw-installer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have a copy of the GNU General Public License on your
# Debian system in the file /usr/share/common-licenses/GPL-2

set -e

. /usr/share/debconf/confmodule

pkg=lw-per-installer

DL_DIR=/root/tmp
lw_short_version=43
lisp_tgz=lwlper${lw_short_version}.tar.gz
doc_tgz=lwlperdoc${lw_short_version}.tar.gz
FILE_ARCHIVE=${lisp_tgz}
FILE_DOC=${doc_tgz}

ok()
{
    file_ok=""
    if [ -f ${DL_DIR}/$1 ]; then
	output="`echo $1 | grep z\$`"
	if [ "$output" ]; then
	    output="`gzip --test ${DL_DIR}/${1} 2>&1 || true`"
	    if [ ! "$output" ]; then
		file_ok=1
	    fi
	else
	    file_ok=1
	fi
	if [ ! "$file_ok" ]; then
	    rm ${DL_DIR}/$1
	fi
    fi
}

all_ok()
{
    ok ${FILE_ARCHIVE}
    if [ "$file_ok" ]; then
	ok ${FILE_DOC} 
	if [ "$file_ok" ]; then
	    downloaded=1
	fi
    fi
}


if [ "$1" = "reconfigure" ]; then
    db_reset ${pkg}/select-source
    db_reset ${pkg}/personal-license
    db_reset ${pkg}/local-directory
fi

case "$1" in
    configure|reconfigure) 
	db_get ${pkg}/personal-license || true
	if [ ${RET} != "Yes" ]; then
	    db_reset ${pkg}/personal-license
	fi
	db_input high ${pkg}/personal-license || true
	db_go || true
	db_get ${pkg}/personal-license
	if [ ${RET} = "Yes" ]; then
	    all_ok
	    if [ ! "$downloaded" ]; then
		db_reset ${pkg}/select-source
		db_input critical ${pkg}/select-source || true
		db_go || true
		if [ "${RET}" = "Use Local Copy" ]; then
		    db_input critical ${pkg}/local-directory
		    db_go || true
		elif [ "${RET}" = "Download Personal Edition" ]; then
		    db_reset ${pkg}/need-download || true
		    db_input medium ${pkg}/need-download || true
		    db_go || true
		fi
	    fi
	fi
	;;
    *) 
	echo "config called with invalid option" >&2
	exit 0
	;;
esac

