#!/bin/sh

#
# tools/CDWserverPro-installer.sh
#
# Copyright (C) 2002-2004 Jörg P. M. Haeger
#

# Thanks to Ramon Casha for the "Self-extracting Archive HOWTO"
# (see http://linux.org.mt/article/selfextract)

INITDIR=/etc/init.d
MD5="@MD5@"
PRODUCT=CDWserver
PRO=Pro
SBINDIR=/usr/sbin
VERSION=@VERSION@

echo
echo "$PRODUCT $PRO $VERSION"
echo

if [ ! "$1" = "" ]; then
	SBINDIR="$1"
fi

DEST="$SBINDIR"

if [ ! -e "$DEST/$PRODUCT" ]; then
	echo "The server binary $DEST/$PRODUCT does not exist."
	echo "Make sure webCDwriter is installed!"
	echo
	echo "The default SBINDIR is \"$SBINDIR\"."
	echo "Retry"
	echo "   sh $0 SBINDIR"
	echo "with the SBINDIR you used in the configure script!"
	echo
	exit 1
fi

SKIP=`awk '/^__ARCHIVE_FOLLOWS__/ { print NR + 1; exit 0; }' $0`

echo -n "Verifying the checksum..."
MD5test="`tail +$SKIP $0 | md5sum -`"
if [ ! "$MD5test" = "$MD5" ]; then
	echo " does not match -> retry to download this installer!"
	echo
	exit 1
fi
echo " OK"

if [ ! "`id -u`" = "0" ]; then
	echo "This installer will replace $DEST/$PRODUCT."
	echo "To do so run it as root!"
	echo
	exit 1
fi

more << EOF

This version of CDWserver 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.

EOF

echo "Do you accept? (yes or no)"
read input
if [ ! "$input" = "yes" ]; then
	echo "Installation cancelled!"
	exit 1
fi

echo
echo "$PRODUCT $PRO $VERSION - starting installation... please wait"
echo

$INITDIR/CDWserver stop
echo -n "replacing $DEST/$PRODUCT..."
sleep 3
tail +$SKIP $0 | bzip2 -cd - > $DEST/$PRODUCT
chmod +x $DEST/$PRODUCT
echo " done"
$INITDIR/CDWserver start

echo
echo "Now you may visit http://`hostname`:12411"
echo

exit 0

__ARCHIVE_FOLLOWS__
