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

# directories
BINDIR=/usr/bin
HTTPDIR=/var/CDWserver/http

# files
CONFIGFILE=/etc/rcdrecord.conf

# options
cxx=g++
dir=""
host=localhost
port=12411
product=rcdrecord

while [ -n "$1" ]; do
	case $1 in
	--bindir=*)
		BINDIR=`echo $1 | sed "s*--bindir=**g"`
		;;
	--configfile=*)
		CONFIGFILE=`echo $1 | sed "s*--configfile=**g"`
		;;
	--destDir=*)
		RPM_BUILD_ROOT=`echo $1 | sed "s:--destDir=::g"`
		;;
	--dir=*)
		dir=`echo $1 | sed "s*--dir=**g"`
		;;
	--host=?*)
		host=`echo $1 | sed "s/--host=//g"`
		;;
	--httpdir=*)
		HTTPDIR=`echo $1 | sed "s*--httpdir=**g"`
		;;
	--port=?*)
		port=`echo $1 | sed "s/--port=//g"`
		;;
	--product=?*)
		product=`echo $1 | sed "s/--product=//g"`
		;;
	--server=?*)
		host=`echo $1 | sed "s/--server=//g"`
		;;
	--help)
		HELP=yes
		;;
	*)
		echo $1: unknown argument
		HELP=yes
		;;
	esac
	shift
done


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

--bindir=DIR          directory for the binaries ($BINDIR)

--configfile=DIR      global config file ($CONFIGFILE)

--server=localhost    replace localhost by your host running CDWserver

--port=12411          by this option you may change the standard port

EOF
	exit 1
fi


if [ "$dir" = "" ]; then
echo
echo "Configuring rcdrecord"
echo
fi

echo -n "RPM_BUILD_ROOT..."
if [ "$RPM_BUILD_ROOT" = "" ]; then
	ROOT=""
	echo " none"
else
	ROOT=$RPM_BUILD_ROOT
	echo " $RPM_BUILD_ROOT"
fi

echo
echo "Creating ${dir}config.h"

cat > config.h << EOF

// defaults for the connection to CDWserver

const char
	defaultHost[] = "$host";

const int
	defaultPort = $port;


// filenames

const char
	configFileName[] = "$CONFIGFILE",
	userFileName[] = ".rcdrecord";

EOF


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

# install command and args
INSTALL=$INSTALL
INSTALL_FILE=$INSTALL -m 644
INSTALL_644=$INSTALL -m 644

# directories
BINDIR=$ROOT$BINDIR
HTTPDIR=$ROOT$HTTPDIR

# rcdrecord or webCDwriter (for use in the Makefile)
PRODUCT=$product

EOF


if [ "$dir" = "" ]; then
echo
echo "rcdrecord is now configured for building. Just run make."
echo
fi
