#!/bin/bash

#
# Configures to build webCDwriter
#
# This file is part of webCDwriter - Network CD/DVD Writing.
#
# Copyright (C) 2000-2007 Jrg P. M. Haeger
#
# webCDwriter is free software. See the README for details.
#

# The following directories will be compiled into the CDWserver binary.
# You may later overwrite them by environment variables of the same
# name. See the startup script $INITDIR/CDWserver.
BINDIR=/usr/bin
CONFIGDIR=/etc/CDWserver
EXPORTDIR=/var/CDWserver/export
HTTPDIR=/var/CDWserver/http
INITDIR=/etc/init.d
LOGDIR=/var/log/CDWserver
MNTDIR=/var/CDWserver/mnt
PROJECTSDIR=/var/CDWserver/projects
SBINDIR=/usr/sbin
SPOOLDIR=/var/spool/CDWserver
TOOLSDIR=/var/CDWserver/bin

# the default user and group that run CDWserver
GROUP=cdwserve
USER=cdwserve

# the default HTTP port
PORT=12411

# options
cxx=g++
forPlugin="#"
forPluginRSA="#"
MAKE=make
webCDcreator=""

while [ -n "$1" ]; do
	case $1 in
	--debug)
		DEBUG=yes
		;;
	--destDir=*)
		DESTDIR=`echo $1 | sed "s:--destDir=::g"`
		;;
	--doNotCompileCDWserver)
		doNotCompileCDWserver="on"
		;;
	--doNotCompileWebCDcreator)
		doNotCompileWebCDcreator="on"
		;;
	--group=?*)
		GROUP=`echo $1 | sed "s:--group=::g"`
		;;
	--help)
		HELP=yes
		;;
	--nosCert=?*)
		forNetscape=""
		nosCert=`echo $1 | sed "s/--nosCert=//g"`
		nosCert=`echo $nosCert | sed "s/\#/\\\\\\\\\\\\\#/g"`
		webCDcreator="$webCDcreator 4netscape 4pluginRSA"
		;;
	--pam)
		PAM=yes
		;;
	--prefix=*)
		echo
		echo "Please edit the directories at the beginning of this $0 script!"
		echo
		exit 1
		;;
	--pro)
		PRO=yes
		PROMODULES=SSLSocket.o
		;;
	--port=?*)
		PORT=`echo $1 | sed "s:--port=::g"`
		;;
	--sunCert=?*)
		forPlugin=""
		sunCert=`echo $1 | sed "s/--sunCert=//g"`
		webCDcreator="$webCDcreator 4plugin"
		;;
	--user=?*)
		USER=`echo $1 | sed "s:--user=::g"`
		;;
	*)
		echo $1: unknown argument
		HELP=yes
		;;
	esac
	shift
done

if [ "$HELP" = "yes" ]; then
	cat <<EOF
Usage: $0 [options]

--debug                compile with -g and without -O2

--destDir              use a prefix invisible to the installed files

--doNotCompileCDWserver  if you want to install CDWserver Pro

--doNotCompileWebCDcreator  use the precompiled and signed webCDcreator

--group=cdwserve       default group of CDWserver

--nosCert=certificate  Netscape Object Signing Certificate
                       (required to sign the webCDcreator for Netscape 4
                       and the RSA version for IE)

--pam                  PAM (Pluggable Authentication Modules) support

--port=$PORT           default HTTP port

--sunCert=certificate  certificate for the keytool from Sun
                       (required to sign the webCDcreator for Java Plugin)

--user=cdwserve        default user of CDWserver

EOF
	exit 1
fi


echo
echo "Configuring webCDwriter"
echo

if [ -z "$doNotCompileWebCDcreator" ]; then
echo -n "Checking for a working javac..."
cat > Test0.java << EOF
class Test0 {
	static int foo() {
		return 0;
	}
}
EOF
cat > Test.java << EOF
class Test {
	public static void main(String[] args) {
		Test0.foo();
		new java.awt.Button();
		new javax.swing.JButton();
		javax.jnlp.ServiceManager.getServiceNames();
	}
}
EOF
javac Test.java 2> /dev/null \
	&& webCDcreator="webCDcreator.jar $webCDcreator" \
	&& echo " OK -> will build webCDcreator.jar" \
	|| echo " not found -> will use precompiled webCDcreator.jar"
rm -f Test0.class Test0.java Test.class Test.java
fi

echo -n "$INITDIR"...
if [ -d $INITDIR ]; then
	echo " yes"
else
	echo " no"
	if [ `uname -s` = "FreeBSD" ]; then
		INITDIR2="/etc/rc.d"
	else
		INITDIR2="/etc/rc.d/init.d"
	fi
	echo -n "$INITDIR2"...
	if [ -d $INITDIR2 ]; then
		echo " yes"
		INITDIR=$INITDIR2
	else
		echo " no -> will create $INITDIR"
	fi
fi

echo
echo "Creating CDWserver/config.h"

cat > CDWserver/config.h << EOF

// Default user and group
const char defaultGroup[] = "$GROUP";
const char defaultUser[] = "$USER";

// Default HTTP port
const int defaultPort = $PORT;

// Default directories
const char binDir[] = "$BINDIR";
const char configDir[] = "$CONFIGDIR";
const char exportDir[] = "$EXPORTDIR";
const char httpDir[] = "$HTTPDIR";
const char initDir[] = "$INITDIR";
const char logDir[] = "$LOGDIR";
const char mntDir[] = "$MNTDIR";
const char projectsDir[] = "$PROJECTSDIR";
const char spoolDir[] = "$SPOOLDIR";
const char toolsDir[] = "$TOOLSDIR";

EOF


case `uname -s` in
FreeBSD)
	cxxflags="-O -DFreeBSD -D_REENTRANT"
	lflags="-lcrypt -lc_r"
	MAKE=gmake
	;;
Linux)
	cxxflags="-D_REENTRANT"
	if [ "$DEBUG" = "yes" ]; then
		cxxflags="-g $cxxflags"
	else
		cxxflags="-O2 $cxxflags"
	fi
	lflags="-lcrypt -lpthread"
	if [ "$PRO" = "yes" ]; then
		cxxflags="$cxxflags -DPRO"
		lflags="$lflags -lssl -lcrypto"
	fi
	;;
*)
	cxxflags="-O -D_REENTRANT"
	lflags="-lcrypt -lpthread"
	;;
esac


if [ "$PAM" = "yes" ]; then
	cxxflags="$cxxflags -DPAM"
#	lflags="$lflags -lpam"
	lflags2="$lflags2 -lpam"
fi


if [ -z "$doNotCompileCDWserver" ]; then
	echo 's/CDWserver: /CDWserver: /g' > sed.tmp~
else
	# Prepare for doNotCompileCDWserver
	cat > sed.tmp~ << EOF
s/CDWserver: /CDWserver: CDWverify setScheduler\\
	touch CDWserver\\
\\
CDWserver-orig: /g
EOF

	if cat CDWserver/Makefile.in | sed -f sed.tmp~ > /dev/null 2>&1; then
		echo -n
	else
		echo "Cannot prepare for --doNotCompileCDWserver"
		exit 1
	fi
fi


echo "Creating CDWserver/Makefile"
cat CDWserver/Makefile.in \
	| sed s/Makefile.in/"Makefile (automatically generated by the configure script - do not edit!)"/g \
	| sed -f sed.tmp~ \
> CDWserver/Makefile
rm -f sed.tmp~


cd rcdrecord
./configure --bindir=$BINDIR --configfile=$CONFIGDIR/rcdrecord.conf \
	--destDir=$DESTDIR \
	--dir=rcdrecord/ --product=webCDwriter
cd ..


echo "Creating webCDcreator/Makefile"
if [ -f webCDcreator/4plugin/webCDcreator.jar ]
then
	forPlugin=""
fi

if [ -f webCDcreator/4pluginRSA/webCDcreator.jar ]
then
	forPluginRSA=""
fi

cat webCDcreator/Makefile.in \
	| sed s/@nosCert@/"$nosCert"/g \
	| sed s/@sunCert@/"$sunCert"/g \
	| sed s/@all@/"$webCDcreator"/g \
	| sed s/@4pi@/"$forPlugin"/g \
	| sed s/@4ns@/"$forPluginRSA"/g \
	> webCDcreator/Makefile


echo "Creating config"

INSTALL="install"
install -b README /tmp/README.CDWserver 2> /dev/null \
	&& INSTALL="$INSTALL -b"
rm -f /tmp/README.CDWserver
install -p README /tmp/README.CDWserver 2> /dev/null \
	&& INSTALL="$INSTALL -p"
rm -f /tmp/README.CDWserver

cat > config << EOF

# compiler and linker options
CXX=$cxx
CXXFLAGS=$cxxflags
LFLAGS=$lflags
LFLAGS2=$lflags2

# install command and args
GROUP=$GROUP
INSTALL=$INSTALL
INSTALL_DIR=$INSTALL -d -m 755
INSTALL_FILE=$INSTALL -m 644
INSTALL_644=$INSTALL -m 644
INSTALL_644UG=$INSTALL -g $GROUP -m 644 -o $USER
INSTALL_UG=$INSTALL -g $GROUP -o $USER
PORT=$PORT
USER=$USER

# directories
DESTDIR="$DESTDIR"
BINDIR="\$(DESTDIR)$BINDIR"
CONFIGDIR="\$(DESTDIR)$CONFIGDIR"
CONFIGDIR0="$CONFIGDIR"
EXPORTDIR="\$(DESTDIR)$EXPORTDIR"
HTTPDIR="\$(DESTDIR)$HTTPDIR"
INITDIR="\$(DESTDIR)$INITDIR"
LOGDIR="\$(DESTDIR)$LOGDIR"
MNTDIR="\$(DESTDIR)$MNTDIR"
PAMDIR="\$(DESTDIR)/etc/pam.d/"
PROJECTSDIR="\$(DESTDIR)$PROJECTSDIR"
SBINDIR="\$(DESTDIR)$SBINDIR"
SBINDIR0="$SBINDIR"
SPOOLDIR="\$(DESTDIR)$SPOOLDIR"
TOOLSDIR="\$(DESTDIR)$TOOLSDIR"

ALLDIRS="export BINDIR=$BINDIR; \
export CONFIGDIR=$CONFIGDIR; \
export EXPORTDIR=$EXPORTDIR; \
export HTTPDIR=$HTTPDIR; \
export INITDIR=$INITDIR; \
export LOGDIR=$LOGDIR; \
export PROJECTSDIR=$PROJECTSDIR; \
export SBINDIR=$SBINDIR; \
export SPOOLDIR=$SPOOLDIR; \
export TOOLSDIR=$TOOLSDIR"

PROMODULES=$PROMODULES

EOF

if [ "$PORT" = "80" ]; then
	PORT_EXT=""
else
	PORT_EXT=":$PORT"
fi

cat > Makefile2 << EOF
all:
	$INSTALL -m 755 uninstall.sh $DESTDIR$BINDIR/CDWuninstall.sh 2> /dev/null || :
	@echo
	@echo
	@echo 'To do:'
	@echo
	@echo '1. Enter "$INITDIR/CDWserver start" to start your webCDwriter'
	@echo
	@echo '2. Open your web browser and try'
	@echo
	@echo '   "http://127.0.0.1$PORT_EXT" or "http://`hostname`$PORT_EXT"'
	@echo
	@echo 'to check the status of your webCDwriter'
	@echo
	@echo '3. Run "$DESTDIR$SBINDIR/CDWconfig.sh" to set the rights of CDWserver'
	@echo
	@echo
	@echo 'If you want to remove webCDwriter, enter "$DESTDIR$BINDIR/CDWuninstall.sh".'
	@echo
EOF

echo "Creating uninstall.sh"

cat tools/uninstall.sh \
	| sed s*'$BINDIR'*"$BINDIR"*g \
	| sed s*'$CONFIGDIR'*"$CONFIGDIR"*g \
	| sed s*'$EXPORTDIR'*"$EXPORTDIR"*g \
	| sed s*'$HTTPDIR'*"$HTTPDIR"*g \
	| sed s*'$INITDIR'*"$INITDIR"*g \
	| sed s*'$LOGDIR'*"$LOGDIR"*g \
	| sed s*'$PROJECTSDIR'*"$PROJECTSDIR"*g \
	| sed s*'$SBINDIR'*"$SBINDIR"*g \
	| sed s*'$SPOOLDIR'*"$SPOOLDIR"*g \
	| sed s*'$TOOLSDIR'*"$TOOLSDIR"*g \
	> uninstall.sh
chmod +x uninstall.sh

echo
echo "webCDwriter is now configured for building. Just run $MAKE."
echo
