#!/bin/sh
#
#   @(#)$Id: config.sh,v 1.3 1996/12/22 01:48:54 steve Exp steve $
#
#   cddbd - CD Database Protocol Server
#
#   Copyright (C) 1996  Steve Scherf
#   Email: steve@moonsoft.com
#   Moondog Software Productions - makers of fine public domain software.
#
#   Based on the original source by Ti Kan.
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

PATH=/bin:/usr/bin:/sbin:/usr/sbin:/etc:/usr/local/bin
export PATH

CDDBD_VER=1.4
UMASK=022
ERRFILE=/tmp/cddbd.err
TMPFILE=/tmp/cddbd.$$

#
# Utility functions
#

doexit()
{
	if [ $1 -eq 0 ]
	then
		$ECHO "Configuration of cddbd is now complete."
		$ECHO "You may now run \"make\"."
	else
		$ECHO "\nErrors have occurred during configuration."
		if [ $ERRFILE != /dev/null ]
		then
			$ECHO "See $ERRFILE for an error log."
		fi
	fi
	exit $1
}

logerr()
{
	if [ "$1" = "-p" ]
	then
		$ECHO "Error: $2"
	fi
	$ECHO "$2" >>$ERRFILE
	ERROR=1
}

getstr()
{
	$ECHO "$* \c"
	read ANS
	if [ -n "$ANS" ]
	then
		return 0
	else
		return 1
	fi
}



#
# Main execution starts here.
#

# Catch some signals
trap "rm -f $TMPFILE; exit 1" 1 2 3 5 15

# Use Sysv echo if possible
if [ -x /usr/5bin/echo ]
then
	ECHO=/usr/5bin/echo				# SunOS SysV echo
elif [ -z "`(echo -e a) 2>/dev/null | fgrep e`" ]
then
	ECHO="echo -e"					# GNU bash, etc.
else
	ECHO=echo					# generic SysV
fi

# Remove old error log file.
ERROR=0
rm -f $ERRFILE
if [ -f $ERRFILE ]
then
	$ECHO "Cannot remove old $ERRFILE: output logging not enabled."
	ERRFILE=/dev/null
fi

$ECHO "\nConfiguring \"cddbd\" CDDB Protocol Server $CDDBD_VER by Steve Scherf"

# Determine BASEDIR.

if [ -z "$BASEDIR" ]
then
	if [ -d /usr/lib/X11 ]
	then
		BASEDIR=/usr/lib/X11
	elif [ -d /usr/X/lib ]
	then
		BASEDIR=/usr/X/lib
	elif [ -d /usr/X11/lib/X11 ]
	then
		BASEDIR=/usr/X11/lib
	elif [ -d /usr/X386/lib/X11 ]
	then
		BASEDIR=/usr/X386/lib
	elif [ -d /usr/X11R5/lib/X11 ]
	then
		BASEDIR=/usr/X11R5/lib/X11
	elif [ -d /usr/X11R6/lib/X11 ]
	then
		BASEDIR=/usr/X11R6/lib/X11
	elif [ -d /usr/openwin/lib ]
	then
		BASEDIR=/usr/openwin/lib/X11
	elif [ -d /usr/local/lib/X11 ]
	then
		BASEDIR=/usr/local/lib/X11
	else
		BASEDIR=/usr/lib/X11
	fi
else
	BASEDIR=`echo $BASEDIR | sed 's/\/\//\//g'`
fi

ACCESS=${BASEDIR}/xmcd/cddbd

while :
do
	if getstr "\nEnter the path to the cddbd access file dir\n[${ACCESS}]"
	then
		if [ -d `dirname "$ANS"` ]
		then
			ACCESS=$ANS
			break
		else
			$ECHO "Error: $ANS does not exist."
		fi
	else
		break
	fi
done

echo


# Create the target files.

umask $UMASK
rm -f cddbd
echo "ACCESSFILE $ACCESS" > .accessfile

cat > access.h << __EOF__
/* This file is generated by config.sh. Do not edit. */

#ifdef _AIX
#include <sys/select.h>
#include <net/nh.h>
typedef int len_t;
#else
typedef size_t len_t;
#endif

#define ACCESSFILE "${ACCESS}/access"

__EOF__

cat > secure.c << __EOF__
/* This file is generated by config.sh. Do not edit. */

char *secure_users[] = {
__EOF__

$ECHO "Enter the list of trusted users, one per line."
$ECHO "Press return alone when finished.\n"

secusers=0

while :
do
	if getstr "Secure user:"
	then
		if [ $ANS = "" ]
		then
			break
		else
			$ECHO "	\"$ANS\"," >> secure.c
			secusers=`expr $secusers + 1`
		fi
	else
		break
	fi
done

cat >> secure.c << __EOF__
	0
};
__EOF__

echo ""

echo "SECUSERS ${secusers}" >> .accessfile

doexit $ERROR
