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


lw_version=4300
pkg_name=lispworks-personal
lw_dir=/usr/lib/$pkg_name
lw_exe=${pkg_name}-$lw_version
lw_usrbin=/usr/bin/$pkg_name

doc_dir=/usr/share/doc/$pkg
lw_usrbin=/usr/bin/lispworks-personal
readme_file=readme-${lw_version}.txt
online_dirname=online

pkg=lw-per-installer
lw_short_version=43
lisp_tgz=lwlper${lw_short_version}.tar.gz
doc_tgz=lwlperdoc${lw_short_version}.tar.gz


rm_lispworks()
{
    rm -f ${doc_dir}/${readme_file}.gz
    rm -f ${doc_dir}/${online_dirname}
    rm -fr ${lw_dir}
    rm -f ${lw_usrbin}
    rmdir ${doc_dir} || true
}

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
}




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


BASE_URL=ftp://ftp.lispworks.com/pub/software_tools/downloads/lwlper43r2
FILE_ARCHIVE=${lisp_tgz}
FILE_DOC=${doc_tgz}
FILES_ALL="${FILE_ARCHIVE} ${FILE_DOC}"
DL_DIR=/root/tmp

local_dir=""   # Local directory that user entered
downloaded=""  # Set to 1 if downloaded ok.

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
}

try_download()
{
    mkdir -p ${DL_DIR} || exit 1

    for file in ${FILES_ALL}; do
	ok ${file}
	if [ ! "$file_ok" ]; then
	    echo "Downloading $file..." 1>&2 
	    wget --quiet --directory-prefix=${DL_DIR} --passive ${BASE_URL}/${file} > /dev/null || true
	fi
    done
    all_ok
}

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


download()
{
    all_ok
    if [ ! "$downloaded" ]; then
	db_get ${pkg}/need-download
	if [ "${RET}" = "Download" ]; then
	    until try_download
	      do
	      db_reset ${pkg}/tryagain || true
	      db_input medium ${pkg}/tryagain || true
	      db_go || true
	      db_get ${pkg}/tryagain
	      if [ "${RET}" != "Yes" ]; then
		  echo "Unable to download and you don't want to try again" 1>&2
		  exit 1;
	      fi
	    done
	else
	    echo "Aborting without download" 1>&2
	    exit 1;
	fi
    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
}

check_valid_copy()
{
    valid_copy=""
    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
}

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

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

get_local_copy()
{
    db_reset ${pkg}/copy-dir || true
    db_get ${pkg}/select-source
    if [ "${RET}" = "Download Personal Edition" ]; then
	download
	db_set ${pkg}/copy-dir ${DL_DIR}
    elif [ "${RET}" = "Use Local Copy" ]; then
	local_copy;
	db_set ${pkg}/copy-dir $local_dir || true
    else
	echo "Unknown selection '${RET}'" 1>&2
	exit 1
    fi
}


###################################################################
##
## INSTALLATION SECTION
##
###################################################################
 


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_exe} ]; then
	install_unpacked
    elif [ -f ${copy_dir}/${doc_tgz} -a -f ${copy_dir}/${lisp_tgz} ]; then
	install_archive
    else
	echo "Can't find Lispworks copy in ${copy_dir} to install. Aborting" 1>&2
	exit 1
    fi
    link_and_chown
}

lw_usrbin=/usr/bin/lispworks-personal
doc_dir=/usr/share/doc/${pkg}
readme_file=readme-${lw_version}.txt
online_dirname=online


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

    # documentation
    mkdir -p ${doc_dir}
    ln -sf ${lw_dir}/lib/*/manual/${online_dirname} ${doc_dir}/${online_dirname}
    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
}

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}
}



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


case "$1" in
    configure)
	check_agree_license
	get_local_copy
	install_lw
	setup_doc
	;;
    abort-upgrade)
	rm_lispworks
	;;
    abort-remove|abort-deconfigure)
	rm_lispworks
	;;
    *)
        echo "postinst called with unknown argument \`$1'" 1>&2
        exit 1
	;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0


