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

forPlugin="#"
forPluginRSA="#"
group=cdwriter
httpRoot="/var/www/html"
MAKE=make
prefix=""
user=cdwriter
webCDcreator=""

while [ -n "$1" ]; do
	case $1 in
	--group=?*)
		group=`echo $1 | sed "s:--group=::g"`
		;;
	--help)
		HELP=yes
		;;
	--httpRoot=?*)
		httpRoot=`echo $1 | sed "s/--httpRoot=//g"`
		;;
	--nosCert=?*)
		forNetscape=""
		nosCert=`echo $1 | sed "s/--nosCert=//g"`
		nosCert=`echo $nosCert | sed "s/\#/\\\\\\\\\\\\\#/g"`
		webCDcreator="$webCDcreator 4netscape 4pluginRSA"
		;;
	--prefix=*)
		prefix=`echo $1 | sed "s:--prefix=::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]

--group=cdwriter       default group of CDWserver

--httpRoot=DIR         root directory of the httpd document tree
                       ($httpRoot is the default)

--prefix=DIR           base directory of the installation

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

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

--user=cdwriter        default user of CDWserver

EOF
	exit 1
fi


echo
echo "Configuring webCDwriter"
echo

echo -n "Checking for a working javac..."
cat > Test.java << EOF
import java.awt.*;
import javax.swing.*;
class Test {}
EOF
javac Test.java \
	&& webCDcreator="webCDcreator.jar $webCDcreator" \
	&& echo "OK -> will build webCDcreator.jar"
rm -f Test.class Test.java

echo
echo "Creating CDWserver/Config.h"
cat CDWserver/Config.h.in \
	| sed s/@defaultGroup@/$group/g \
	| sed s/@defaultUser@/$user/g \
	| sed s*@prefix@*$prefix*g \
	> CDWserver/Config.h


case `uname -s` in
FreeBSD)
	cxxflags="-O -DFreeBSD -D_REENTRANT"
	lflags="-lcrypt -lc_r"
	MAKE=gmake
	;;
Linux)
	cxxflags="-O -D_REENTRANT"
	lflags="-lcrypt -lpthread"
	;;
*)
	cxxflags="-O -D_REENTRANT"
	lflags="-lcrypt -lpthread"
	;;
esac


echo "Creating CDWserver/Makefile"
cat CDWserver/Makefile.in \
	| sed s/@CXXFLAGS@/"$cxxflags"/g \
	| sed s/@GROUP@/$group/g \
	| sed s/@LFLAGS@/"$lflags"/g \
	| sed s/Makefile.in/"Makefile (automatically generated by the configure script - dont edit!)"/g \
	| sed s/@USER@/$user/g \
> CDWserver/Makefile


cd rcdrecord
./configure --dir=rcdrecord/ --prefix=$prefix/usr
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=g++

# install command and args
INSTALL=$INSTALL

# directories
APPLET=$prefix/$httpRoot/webCDcreator
PREFIX=$prefix

EOF

cat > Makefile2 << EOF
all:
	@echo
	@echo Now you can start CDWserver by
	@echo '   $prefix/etc/rc.d/init.d/CDWserver start'
	@echo Try to get the status of CDWserver by
	@echo '   $prefix/etc/rc.d/init.d/CDWserver status'
	@echo or
	@echo '   http://`hostname`:12412'
	@echo If this is OK, try to start webCDcreator by visiting
	@echo "   http://`hostname`/webCDcreator/"
	@echo or try rcdrecord or files2cd on the command line.
	@echo If there is a problem, edit $prefix/etc/CDWserver/config.
	@echo
EOF

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