#!/bin/sh

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

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

echo
echo "webCDwriter $VERSION rpmbuilder"
echo

echo -n "Get the user ID... "
ID=`id -u`
if [ $ID = "0" ]; then
	echo "$ID -> Do not run $0 as root!"
	exit 1
fi
echo $ID

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

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 webCDwriter > /dev/null 2>&1; then
	upgrade=1
	$RPM -q webCDwriter
else
	echo "no"
fi

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

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

DIR=/tmp/webCDwriter
rm -rf $DIR/ || exit 1
mkdir -p $DIR/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
tail +$SKIP $0 > $DIR/webCDwriter-$VERSION.tar.bz2
$RPMBUILD -tb --define "_topdir $DIR/" $DIR/webCDwriter-$VERSION.tar.bz2 \
	|| exit 1

echo
echo "To do:"
echo
echo "1. Become root (enter \"su\")"
echo
if [ $upgrade = 0 ]; then
	echo "2. Install `basename /tmp/webCDwriter/RPMS/*/*` by"
	echo "        $RPM -ihv `ls $DIR/RPMS/*/*`"
else
	echo "2. Upgrade to `basename /tmp/webCDwriter/RPMS/*/*` by"
	echo "        $RPM -Uhv `ls $DIR/RPMS/*/*`"
fi
echo

exit 0

__ARCHIVE_FOLLOWS__
