#!/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-pro-installer-43

if [ "$1" = "reconfigure" ]; then
    db_reset ${pkg}/proent-license
    db_reset ${pkg}/serial-number
    db_reset ${pkg}/license-key
    db_reset ${pkg}/patches
fi

ask_patches()
{
    db_input medium $pkg/patches || true
    db_go || true
}

######################################################################
##
## GET LOCAL COPY SECTION
##
######################################################################

lw_short_version=43
lw_version=4300
lw_exe=lispworks-${lw_version}
lisp_tgz=lwlproent${lw_short_version}.tar.gz
doc_tgz=lwldoc${lw_short_version}.tar.gz
lw_dir=/usr/lib/lispworks43

local_dir=""   # Local directory that user entered

test_already_installed()
{
    already_installed=""
    if [ -x ${lw_dir}/$lw_exe -a -d ${lw_dir}/lib ]; then
        already_installed=1
    fi
}

read_local_dir()
{
    if [ ! "${RET}" ]; then
	echo "Aborting" >&2
	exit 1
    else
	local_dir="${RET}"
    fi
    db_set $pkg/local-directory $local_dir
}

check_valid_copy()
{
    valid_copy=""
    if [ "$DEBCONF_DEBUG" =  "developer" ]; then
	echo "Checking for $local_dir/$doc_tgz && $local_dir/$lisp_tgz" >& 2
    fi
    if [ -d $local_dir/lib -a -x $local_dir/$lw_exe ]; then
	valid_copy=1
    elif [ -f $local_dir/$doc_tgz -a -f $local_dir/$lisp_tgz ]; then
	valid_copy=1
    fi
}


local_copy()
{
    db_input high $pkg/local-directory || true
    db_go || true
    db_get $pkg/local-directory
    read_local_dir
    valid_copy=""
    until [ "$valid_copy" ]; do
	if [ ! -d $local_dir ]; then
	    db_set $pkg/dir-not-exist $local_dir
	    db_fset $pkg/dir-not-exist seen false
	    db_input high $pkg/dir-not-exist || true
	    db_go || true
	    db_get $pkg/dir-not-exist
	    read_local_dir
	else
	    check_valid_copy
	    if [ ! "$valid_copy" ]; then
		db_set $pkg/copy-not-found $local_dir
		db_fset $pkg/copy-not-found seen false || true
		db_input high $pkg/copy-not-found || true
		db_go || true
		db_get $pkg/copy-not-found
		read_local_dir
	    fi
	fi
    done
}


get_local_copy()
{
    db_reset $pkg/copy-dir || true
    local_copy;
    db_set $pkg/copy-dir $local_dir || true
}

case "$1" in
    configure|reconfigure)
        test_already_installed
        if [ ! "$already_installed" ]; then
	    db_get $pkg/proent-license
	    if [ ${RET} != "Yes" ]; then
		db_reset ${pkg}/proent-license
	    fi
	    db_input high ${pkg}/proent-license || true
	    db_go || true
	    db_get ${pkg}/proent-license
	    if [ ${RET} = "Yes" ]; then
		db_fset $pkg/serial-number seen false
		db_fset $pkg/license-key seen false
		db_input critical $pkg/serial-number || true
		db_input critical $pkg/license-key || true
		db_go || true
		db_reset $pkg/local-directory
		get_local_copy
		ask_patches
	    fi
	fi
	;;
    *) 
	echo "config called with invalid option" >&2
	exit 0
	;;
esac

