#!/bin/sh
#
# Configures to build webCDwriter
#

httpRoot="/var/www/html"
MAKE=make
prefix=""
webCDcreator=""

while [ -n "$1" ]; do
	case $1 in
	--help)
		HELP=yes
		;;
	--httpRoot=?*)
		httpRoot=`echo $1 | sed "s/--httpRoot=//g"`
		;;
	--nosCert=?*)
		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=?*)
		sunCert=`echo $1 | sed "s/--sunCert=//g"`
		webCDcreator="$webCDcreator 4plugin"
		;;
	*)
		echo $1: unknown argument
		HELP=yes
		;;
	esac
	shift
done

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

--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 form Sun
                       (required to sign the webCDcreator for Java Plugin)
EOF
	exit 1
fi


case `uname -s` in
FreeBSD)
	echo "Creating CDWserver/Makefile"
	cat CDWserver/Makefile.in \
		| sed s/CXXFLAGS=/"CXXFLAGS=-O -DFreeBSD -D_REENTRANT"/g \
		| sed s/LFLAGS=/"LFLAGS=-lcrypt -lc_r"/g \
		> CDWserver/Makefile
	MAKE=gmake
	;;
Linux)
	echo "Creating CDWserver/Makefile"
	cat CDWserver/Makefile.in \
		| sed s/CXXFLAGS=/"CXXFLAGS=-O -D_REENTRANT"/g \
		| sed s/LFLAGS=/"LFLAGS=-lcrypt -lpthread"/g \
		> CDWserver/Makefile
	;;
*)
	echo "Creating CDWserver/Makefile"
	cat CDWserver/Makefile.in \
		| sed s/CXXFLAGS=/"CXXFLAGS=-O -D_REENTRANT"/g \
		| sed s/LFLAGS=/"LFLAGS=-lcrypt -lpthread"/g \
		> CDWserver/Makefile
	;;
esac


echo "Creating webCDcreator/Makefile"
cat webCDcreator/Makefile.in \
	| sed s/nosCert=unknown/"nosCert=$nosCert"/g \
	| sed s/sunCert=unknown/"sunCert=$sunCert"/g \
	| sed s/all:/"all: $webCDcreator"/g \
	> webCDcreator/Makefile

if [ -f webCDcreator/4netscape/webCDcreator.jar ]
then
	:
else
	:
fi

cat > config << EOF

# compiler and linker options
CXX=g++

# 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 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
