#! /bin/sh
# postinst script for hyperspec
#
# 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/HyperSpec-6-0.tar.gz
OLD_INDEX=/usr/share/doc/hyperspec/FrontMatter/index.html
NEW_INDEX=/usr/share/doc/hyperspec/Front/index.htm

case "$1" in
    configure) 
      if [ -f $OLD_INDEX -o -f $NEW_INDEX ] ; then
        VERSION=$(grep '<!-- Common Lisp HyperSpec (TM), version '  /usr/share/doc/hyperspec/FrontMatter/index.html /usr/share/doc/hyperspec/Front/index.htm \
	           2> /dev/null | sed 's/.*version //;s/ -->//' | head -n 1)
	if [ "X$VERSION" = "X" ] ; then
	  echo Error in version-checking, please report this as a bug.
	  exit 1
	fi
	if [ "$VERSION" != "6.0" ] ; then
	  cat <<EOF
An older version of the HyperSpec has been found. Upgrading it now...
EOF
	  rm -rf  /usr/share/doc/hyperspec/Body /usr/share/doc/hyperspec/Data /usr/share/doc/hyperspec/FrontMatter \
	     /usr/share/doc/hyperspec/Graphics /usr/share/doc/hyperspec/HyperSpec /usr/share/doc/hyperspec/HyperSpec-Legalese.text \
	     /usr/share/doc/hyperspec/Issues /usr/share/doc/hyperspec/Front || true
         else
           cat <<EOF
The hyperspec seems installed already, not doing anything
EOF
	 exit 0
         fi
      fi
            if [ ! -f ${FILE} ] ; then
	        cat <<EOF
	You don't have the file ${FILE}.
	
	If you want I'll download the file now so I can install it.
	
	Should I download the file now? 
EOF
		read -p '[Yes,No] ' a
		case "$a" in
		  Y*|y*|"")
		  	(cd /root ; mkdir tmp || true 2> /dev/null ; cd tmp ; umask 077 ;  
			    wget --directory-prefix=. --passive ftp://ftp.xanalys.com/pub/software_tools/reference/HyperSpec-6-0.tar.gz )
			echo Downloaded!
			;;
		*)
			echo Please download the file from ftp://ftp.xanalys.com/pub/software_tools/reference/HyperSpec-6-0.tar.gz to $FILE
			echo and run \"dpkg-reconfigure hyperspec\" as root.
			exit 0
			;;
		esac
            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/share/doc/hyperspec &&
		 tar zxf ${FILE} &&
		 mv HyperSpec/* . )
	# fix permissions.
	(cd /usr/share/doc/hyperspec &&
	 chown -R root.root . &&
	 find . -type f -print0 | xargs -0 chmod 644 && 
	 find . -type d -print0 | xargs -0 chmod 755 )
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
	rm -rf /usr/share/doc/hyperspec/*
    ;;

    *)
        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


