#!/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
STATIC=false
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..."
if [ -x /sbin/md5 ]; then
	# FreeBSD
	MD5TOOL="/sbin/md5 -q"
else
	MD5TOOL=md5sum
fi
MD5test="`tail +$SKIP $0 | $MD5TOOL`"
if [ ! "$MD5test" = "$MD5" ]; then
	MD5test="$MD5test  -"
	if [ ! "$MD5test" = "$MD5" ]; then
		echo " does not match -> retry to download this installer!"
		echo
		exit 1
	fi
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

if [ "$STATIC" = "false" ]; then
	if [ ! -e /usr/lib/libcrypto.so.0 ] && [ ! -e /lib/libcrypto.so.0 ]; then
		if [ -e /lib/libcrypto.so.4 ]; then
			# Fedora Core release 1.91 (FC2)
			ln -s libcrypto.so.4 /lib/libcrypto.so.0
		elif [ -e /usr/lib/libcrypto.so.0.9.7 ]; then
			# Mandrake Linux release 10.0
			ln -s libcrypto.so.0.9.7 /usr/lib/libcrypto.so.0
		fi
	fi
	if [ ! -e /usr/lib/libssl.so.0 ] && [ ! -e /lib/libssl.so.0 ]; then
		if [ -e /lib/libssl.so.4 ]; then
			# Fedora Core release 1.91 (FC2)
			ln -s libssl.so.4 /lib/libssl.so.0
		elif [ -e /usr/lib/libssl.so.0.9.7 ]; then
			# Mandrake Linux release 10.0
			ln -s libssl.so.0.9.7 /usr/lib/libssl.so.0
		fi
	fi
fi

if [ -x /etc/rc.d/CDWserver ]; then
	# FreeBSD
	INITDIR=/etc/rc.d
fi

$INITDIR/CDWserver stop
if [ ! -e $DEST/$PRODUCT-GPL ]; then
	echo -n "Moving $DEST/$PRODUCT to $DEST/$PRODUCT-GPL..."
	mv $DEST/$PRODUCT $DEST/$PRODUCT-GPL
	echo " done"
fi
echo -n "Installing $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__
