#!/bin/sh

#
# tools/webCDwriter-installer.sh
#
# This file is part of webCDwriter - Network CD/DVD Writing.
#
# Copyright (C) 2002-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)

MD5="@MD5@"
PRODUCT=webCDwriter
RPM=rpm
RPMBUILD=rpmbuild
VERSION=@VERSION@

echo
echo "$PRODUCT $VERSION installer"
echo

if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
	echo "Usage: $0 [OPTION]"
	echo
	echo "  --tar.bz2  Create $PRODUCT-$VERSION.tar.bz2"
	echo "  --tar.gz   Create $PRODUCT-$VERSION.tar.gz"
	echo
	exit 0
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 [ "$1" = "--tar.bz2" ]; then
	echo -n "Creating $PRODUCT-$VERSION.tar.bz2... "
	tail +$SKIP $0 > $PRODUCT-$VERSION.tar.bz2
	echo "done"
	exit 0
fi

if [ "$1" = "--tar.gz" ]; then
	echo -n "Creating $PRODUCT-$VERSION.tar.gz... "
	tail +$SKIP $0 | bzip2 -cd - | gzip -c - > $PRODUCT-$VERSION.tar.gz
	echo "done"
	exit 0
fi

if [ ! "$1" = "" ]; then
	echo "Invalid option $1"
	exit 1
fi

echo -n "Has rpm... "
hasRPM="no"
if rpm -q rpm > /dev/null 2>&1; then
	hasRPM="yes"
fi
echo "$hasRPM"

echo -n "Get the user ID... "
ID=`id -u`
echo "$ID"

KNOPPIX="`grep cloop /proc/mounts | grep ' /KNOPPIX '`"

if [ "$ID" = "0" ]; then
	if [ ! "$KNOPPIX" ]; then
		echo
		echo "Do not run \"$0\" as root!"
		echo
		exit 1
	fi
elif [ "$KNOPPIX" ]; then
	echo
	echo "To do:"
	echo
	echo "1. Become root (enter \"su\")"
	echo
	echo "2. Enter \"sh $0\""
	echo
	exit 0
fi

echo -n "Trying to run \"g++ -v\"... "
if g++ -v > /dev/null 2>&1; then
	echo "OK"
else
	echo "failed. Install the \"GNU C++ compiler\"!"
	exit 1
fi

echo -n "Trying to run \"make -v\"... "
if make -v > /dev/null 2>&1; then
	echo "OK"
else
	echo "failed. Install \"GNU Make\"!"
	exit 1
fi

if [ "$hasRPM" = "no" ]; then
	DIR=/tmp/$PRODUCT
	rm -rf $DIR/
	mkdir -p $DIR
	tail +$SKIP $0 > $DIR/$PRODUCT-$VERSION.tar.bz2
	cd $DIR
	tar -xjvf $PRODUCT-$VERSION.tar.bz2
	cd $PRODUCT-$VERSION/
	if [ "$KNOPPIX" ]; then
		mv configure configure-orig
		cat configure-orig \
			| sed s*/usr/bin*/var/CDWserver/bin*g \
			| sed s*/usr/sbin*/var/CDWserver/sbin*g \
			| sed s*GROUP=cdwserve*GROUP=backup*g \
			> configure
		chmod +x configure
	fi
	if [ -e /usr/include/security/pam_appl.h ]; then
		PAM="--pam"
	fi
	./configure --doNotCompileWebCDcreator $PAM
	make
	if [ "$KNOPPIX" ]; then
		make install
	else
		echo "0. Enter \"cd $DIR\""
		echo
	fi
	exit 0
fi

echo -n "Has $RPMBUILD... "
if $RPMBUILD --help > /dev/null 2>&1; then
	echo "yes"
else
	RPMBUILD=$RPM
	echo "no -> will use \"$RPMBUILD -tb\""
fi

echo -n "Trying to run \"$RPM --help\"... "
if $RPM --help > /dev/null 2>&1; then
	echo "OK"
else
	echo "failed"
	exit 1
fi

echo -n "Looking for an already installed $PRODUCT... "
upgrade=0
if $RPM -q $PRODUCT > /dev/null 2>&1; then
	upgrade=1
	installed=`$RPM -q $PRODUCT`
	echo $installed
else
	echo "no"
fi

echo
echo "$PRODUCT $VERSION - building RPM..."
echo

DIR=/tmp/$PRODUCT
rm -rf $DIR/ || exit 1
mkdir -p $DIR/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
tail +$SKIP $0 > $DIR/$PRODUCT-$VERSION.tar.bz2
if $RPMBUILD -tb --define "_topdir $DIR/" $DIR/$PRODUCT-$VERSION.tar.bz2; then
	echo
	echo Success
	echo
else
	echo
	echo "I am sorry, but building the RPM failed :-("
	if [ "$RPMBUILD" = "$RPM" ]; then
		echo
		echo "There is no \"rpmbuild\" on your system."
		echo "Ask your package manager (like rpmdrake, yast2, ...) for"
		echo "\"rpm-build\" or a package containing \"/usr/bin/rpmbuild\"."
		echo
	fi
	echo "If you cannot solve the above problem yourself,"
	echo "you may mail the above output to feedback@JoergHaeger.de"
	echo "Perhaps I can refine this installer in a future release ..."
	echo
	exit 1
fi
RESULT=`ls $DIR/RPMS/*/*`
RESULT=`basename $RESULT`
mv -iv $DIR/RPMS/*/* .

echo
echo "To do:"
echo
echo "1. Become root (enter \"su\")"
echo
if [ $upgrade = 0 ]; then
	echo "2. Install $RESULT by"
	echo "        $RPM -ihv $RESULT"
else
	echo "2. Upgrade to $RESULT by"
	echo -n "        $RPM -Uhv"
	if [ "$installed" \> "$RESULT" ]; then
		echo -n " --oldpackage"
	fi
	echo " $RESULT"
fi
echo

exit 0

__ARCHIVE_FOLLOWS__
