#!/bin/sh

#
# tools/@PRODUCT@-installer.sh
#
# This file is part of webCDwriter - Network CD/DVD Writing.
#
# Copyright (C) 2004 Jörg P. M. Haeger
#
# webCDwriter is free software. See CDWserver.cpp for details.
#

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

GROUP=cdwserve
MD5="@MD5@"
PRODUCT="@PRODUCT@"
PRODUCT2="for webCDwriter"
TOOLSDIR=/var/CDWserver/bin
VERSION="@VERSION@"

echo
echo "$PRODUCT $VERSION $PRODUCT2 Installer"
echo

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

if [ ! -e "$TOOLSDIR" ]; then
	old=/home/CDWserver/bin
	if [ -e "$old" ]; then
		GROUP=cdwriter
		TOOLSDIR="$old"
	else
		if [ ! "`id -u`" = "0" ]; then
			echo "This installer will replace $TOOLSDIR/$PRODUCT."
			echo "To do so run it as root!"
			echo
			exit 1
		fi
		echo "The default TOOLSDIR \"$TOOLSDIR\" does not exist."
		echo "Retry"
		echo "   sh $0 TOOLSDIR"
		echo "with the TOOLSDIR you used in the configure script!"
		echo
		exit 1
	fi
fi

DEST="$TOOLSDIR"

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

echo -n "Installing $PRODUCT to $DEST/..."

rm -f "$DEST/$PRODUCT"
tail +$SKIP $0 | bzip2 -cd - > "$DEST/$PRODUCT"
chown "root.$GROUP" "$DEST/$PRODUCT"
chmod 4750 "$DEST/$PRODUCT"

echo " done"
echo

exit 0

__ARCHIVE_FOLLOWS__
