#! /bin/sh
# postinst script for acl-installer
#
# see: dh_installdeb(1)

set -e

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

FILE=/root/tmp/acl61_trial.bz2

function backout {
	rm -rf /usr/lib/acl6/acl61_trial 2> /dev/null
	find /usr/lib/acl6 -type f -not -name "devel.lic" -print0 | xargs -0 rm 2> /dev/null || true
	find /usr/share/doc/acl6 -type f -not -name "devel.lic" -print0 | xargs -0 rm 2> /dev/null || true
	find /usr/lib/acl6 -type d -print0 | xargs -0 -n 1 rmdir 2> /dev/null 2> /dev/null || true
	find /usr/share/doc/acl6 -type d -print0 | xargs -0 -n 1 rmdir 2> /dev/null 2> /dev/null || true
}

case "$1" in
    configure) 
      if [ -f /usr/lib/acl6/alisp ] ; then
        echo ACL6 already installed, just rebuilding image...
	echo if you have an earlier version and want to upgrade, please
	echo deinstall acl-installer first.
      fi

      if [ ! -f /usr/lib/acl6/alisp ] ; then
	      if [ ! -f ${FILE} ] ; then
	        cat <<EOF
	You don't have the file ${FILE}.
	
	Go to www.franz.com and register and download the trial
	version for Linux.
EOF
		exit 1
	      fi
		if [ -L ${FILE} ] ; then
		  echo ${FILE} is a symbolic link! This is foul, exiting...
		  exit 2
		fi
		if [ ! -O ${FILE} ] ; then
		  echo How comes root doesn\'t own ${FILE}? This is foul, exiting...
		  exit 3
		fi
		if [ ! -G ${FILE} ] ; then
		  echo How comes roots group doesn\'t own ${FILE}? This is foul, exiting...
		  exit 4
		fi
	
		(cd /usr/lib/acl6 &&
		 echo Untarring archive &&
		 bzip2 -dc ${FILE} | tar --extract --file -  &&
		 echo Moving files... &&
		 mv acl61_trial/doc/* /usr/share/doc/acl6/ && 
		 mv acl61_trial/* . &&
		 rmdir acl61_trial/ ) || ( echo unpacking failed, cleaning up... ; backout ; exit 5 )
     fi
     (  cd /usr/lib/acl6 &&
		 echo Configuring ANSI Lisp mode
		 if [ ! -f /usr/lib/acl6/devel.lic ] ; then
		 	cat <<EOF
	Please copy the devel.lic file from Franz to
	/usr/lib/acl6 and press any key...
EOF
			read a 
	        fi
	rm /usr/lib/acl6/lisp /usr/lib/acl6/acl6.dxl 2> /dev/null || true
	./alisp -qq -batch -e '(progn (build-lisp-image "acl6.dxl" :case-mode :case-insensitive-upper :include-ide nil :restart-app-function nil) (sys:copy-file "sys:alisp" "sys:lisp"))' -kill || \
	(echo I could not run mlisp, maybe the licence file is bad... ; exit 1 ) 
	ln -sf /usr/lib/acl6/lisp /usr/bin/acl6) || ( backout ; exit 6 ) 
	# /usr/sbin/register-common-lisp-implementation acl
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
	# /usr/sbin/unregister-common-lisp-implementation acl
	backout 
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 0
    ;;
esac

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

#DEBHELPER#

exit 0


