#!/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

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see /usr/share/doc/packaging-manual/
#
# quoting from the policy:
#     Any necessary prompting should almost always be confined to the
#     post-installation script, and should be protected with a conditional
#     so that unnecessary prompting doesn't happen if a package's
#     installation fails and the `postinst' is called with `abort-upgrade',
#     `abort-remove' or `abort-deconfigure'.


pkg=lw-pro-installer-43
pkg_name=lispworks
lw_version=4300
lw_dir=/usr/lib/lispworks43
readme_file=readme-${lw_version}.txt
doc_dir=/usr/share/doc/${pkg}
online_dirname=online
lw_proent_exe=lispworks-${lw_version}
lw_console=lw-console
lw_gui=lw-gui
ab_dir=/etc/common-lisp/$pkg_name

lw_usrbin=/usr/bin/lispworks
lw_console_usrbin=/usr/bin/${lw_console}
lw_gui_usrbin=/usr/bin/${lw_gui}

lw_short_version=43
lisp_tgz=lwlproent${lw_short_version}.tar.gz
doc_tgz=lwldoc${lw_short_version}.tar.gz

install_patches()
{
    db_get $pkg/patches
    if [ "$RET" = "Download" ]; then
	echo "Attempting to download and install patches" 1>&2 
	/usr/lib/lispworks43/dl-and-install-patches.sh >/dev/null >&2 || true
    fi
}

make_images()
{
    if [ -x $lw_dir/make-images.sh ]; then
	cd $lw_dir
	cp ${lw_dir}/$lw_proent_exe ${lw_dir}/$lw_console
	./make-images.sh >&2
	ln -sf ${lw_dir}/lw-console $lw_console_usrbin
	ln -sf ${lw_dir}/lw-gui $lw_gui_usrbin
	ln -sf ${lw_dir}/lwlicense /usr/bin
    fi
}

setup_doc()
{
    hs=/usr/lib/$pkg_name/lib/4-3-0-0/manual/online/web/CLHS
    if [ -d $hs ]; then
	rm -r $hs
	ln -s /usr/share/doc/hyperspec $hs
    fi
}




check_agree_license()
{
    db_get $pkg/proent-license
    if [ ${RET} != "Yes" ]; then
	if [ ${RET} != "Yes" ]; then
	    echo "You have not agreed to the license, the installation is aborted." 1>&2 
	    exit 1
	fi
    fi
}



###################################################################
##
## INSTALLATION SECTION
##
###################################################################
 
lw_dir=/usr/lib/lispworks43
doc_dir=/usr/share/doc/$pkg
lw_usrbin=/usr/bin/lispworks
lw_console=lw-console
lw_gui=lw-gui
lw_console_usrbin=/usr/bin/${lw_console}
lw_gui_usrbin=/usr/bin/${lw_gui}
readme_file=readme-${lw_version}.txt
online_dirname=online
clc_bin_dir=/usr/lib/common-lisp/bin
lw_clc=${clc_bin_dir}/lispworks.sh


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

install_lw()
{
    mkdir -p ${lw_dir} || exit 1
    db_get $pkg/copy-dir
    copy_dir="$RET"
    if [ -d ${copy_dir}/lib -a -x ${copy_dir}/$lw_proent_exe ]; then
	install_unpacked
    elif [ -f ${copy_dir}/$doc_tgz -a ${copy_dir}/$lisp_tgz ]; then
	install_archive
    else
	echo "Can't find Lispworks copy in ${copy_dir} to install. Aborting" >&2
	exit 1
    fi
    link_and_chown
}


install_archive()
{
    tar xz -f ${copy_dir}/${lisp_tgz} --directory ${lw_dir}
    tar xz -f ${copy_dir}/${doc_tgz} --directory ${lw_dir}
}

install_unpacked()
{
    cp -a ${copy_dir}/* ${lw_dir}
}

link_and_chown()
{
    # link executable
    ln -sf ${lw_dir}/${lw_proent_exe} ${lw_usrbin}

    # documentation
    mkdir -p ${doc_dir}

    ln -sf ${lw_dir}/lib/*/manual/${online_dirname} ${doc_dir}
    cp ${lw_dir}/${readme_file} ${doc_dir}
    gzip -9 -f ${doc_dir}/${readme_file}

    # chown
    find ${lw_dir} -type d -exec chmod 0755 {} \;
    find ${lw_dir} -type f -exec chmod +r {} \;
    chown -R root.root ${lw_dir}
    chmod 0644 ${doc_dir}/${readme_file}.gz
    chown -R root.root ${doc_dir}/${readme_file}.gz
}

test_file="${lw_dir}/test-${pkg}.$$"
make_test_file()
{
    echo "(quit)" > ${test_file}
}

rm_test_file()
{
    rm -f ${test_file}
}

set_keys()
{
    db_get $pkg/serial-number || true
    serial="${RET}"
    db_get $pkg/license-key || true
    key="${RET}"
    cd ${lw_dir}
    make_test_file
    output="`${lw_dir}/${lw_proent_exe} --lwlicenseserial ${serial} --lwlicensekey ${key} -init ${test_file} 2>&1 || true`"
    rm_test_file
}

test_licensing()
{
    proent_licensing_fail=""
    cd ${lw_dir}
    make_test_file
    output="`${lw_dir}/${lw_proent_exe} -init ${test_file} 2>&1 || true`"
    rm_test_file
    grep_output="`echo ${output} | grep -i 'no license entered' || true`"
    if [ "$grep_output" ]; then
	proent_licensing_fail=1
    fi
}

test_need_license_key()
{
    need_license_key=""
    if [ -f ${lw_dir}/${lw_proent_exe} ]; then
	test_licensing
	if [ "$proent_licensing_fail" ]; then
	    need_license_key=1
	fi
    fi
}

setup_license_key()
{
    test_need_license_key
    while [ "$need_license_key" ]; do
	set_keys
	test_need_license_key
	if [ "$need_license_key" ]; then
	    db_reset $pkg/invalid-key
	    db_input high $pkg/invalid-key || true
	    db_go || true
	    db_get $pkg/invalid-key
	    case "${RET}" in
		abort|Abort)
		    echo "Invalid key for Lispworks, user choose to abort" >& 2
		    exit 1;
		    ;;
		*)
		    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
		    ;;
	    esac
	fi	
    done
}


#######################################################################
##
## Handle config script invocation
##
#######################################################################


case "$1" in
    configure)
        test_already_installed
        if [ "$already_installed" ]; then
	    echo "Lispworks $lw_version already installed, skipping installation" 1>&2
	else
	    check_agree_license
	    install_lw
	    setup_license_key
	fi
	install_patches
	make_images
	setup_doc
	/usr/sbin/register-common-lisp-implementation $pkg_name
	;;
    abort-upgrade)
	rm_lispworks
	;;
    abort-remove|abort-deconfigure)
	;;
    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
	;;
esac

#DEBHELPER#

exit 0


