#!/bin/sh
# ICQJava installer Debian post-install setup script
# by Lalo Martins <lalo@webcom.com>
# this script is in the public domain
set -e


# Find tmp directory
[ "$TMPDIR" ] || TMPDIR=/tmp

# Find run directory
[ "$RUNDIR" ] || RUNDIR=/var/run

cd ${RUNDIR}

# Find and varify the file
icqdist="${TMPDIR}/==DISTNAME=="
owner=`ls -aln ${icqdist} | sed -e 's/[^ ]*[ ]*[0-9]*[ ]*\([0-9]*\).*/\1/'`
if [ "${owner}" = "" -o 0${owner} -ne 0 ] ; then
  echo "Error! ${icqdist} does not exist or is not owned by root!!"
  exit 1
fi

# Unpack it.
if [ -f $icqdist ] ; then
  echo -n "Unpacking file"
  if tar -xzf $icqdist 2>/dev/null ; then
    echo .
  else
    echo " - failed!"
    exit 2
  fi
else
  cat <<-  EOF
	Mirabilis LTD. does not allow redistribution of their software. Therefore,
	this package requires the user to fetch the ICQJava archive seperately and
	place it in the directory pointed to by the TMPDIR environment variable (or
	/tmp if TMPDIR not defined) before attempting to install this package. You
	can get the ICQJava packages via http from "www.mirabilis.com".
EOF
  exit 1
fi


# Display license and allow user to abort installation
echo "Mirabilis LTD. requires you to read their LICENSE file before you"
echo -n "install ICQJava. Do you want to read it now? (y/n) [y] "
read input
if [ "$input" = "n" ] ; then
  echo "The license file is installed as /usr/doc/==PACKAGE==/copyright; you"
  echo "may read it anytime with your favourite text-reading program."
else
  sensible-pager $RUNDIR/ICQJava/LICENSE.TXT
  echo
  echo "If you have not agreed with the license, you may abort installation"
  echo "now and remove the package."
  echo -n "Do you want to abort installation? (y/n) [n] "
  read input
  if [ "$input" = "y" ] ; then
    echo -n "Aborting"
    if rm -R $RUNDIR/ICQJava 2>/dev/null ; then
      echo .
    else
      echo " - failed to delete temporary files!"
    fi
  else
    echo "Great."
  fi
fi

echo


# Install files
mkdir -p /usr/lib/==PACKAGE==
cp -a $RUNDIR/ICQJava/Images $RUNDIR/ICQJava/Sounds $RUNDIR/ICQJava/ICQ.jar \
      /usr/lib/==PACKAGE==
cp $RUNDIR/ICQJava/*.TXT $RUNDIR/ICQJava/ICQJava.faq /usr/doc/==PACKAGE==


# Remove the unpacked material
echo -n "Removing temporary files"
if rm -R $RUNDIR/ICQJava 2>/dev/null ; then
  echo .
else
  echo " - failed!"
fi


# Run update-menus
if [ -x "/usr/bin/update-menus" ] ; then
  /usr/bin/update-menus
fi
