#!/bin/ksh

# It is assumed you will be running this on solaris, or at least
# a professional UNIX that has /bin/ksh. if not, but you have  
# a fully POSIX-compliant sh, replace /bin/ksh with /bin/sh


######################################################################
# expects a default value set, in $default
# sets variable $ans
ask(){
	print ""
	print "$@"
	print -n "[$default]? "
	read ans
	if [ "$ans" = "" ] ; then
		ans="$default"
	fi
}



############################################################
perlloc=`whence perl`
if [ $? -ne 0 ] ; then
	print ERROR: you do not seem to have perl installed
fi

default=$perlloc
ask "Where is your perl binary?"
perlloc="$ans"

if [ ! -x "$perlloc" ] ; then
	print ERROR: invalid binary
	exit 1
fi




if [ -d /usr/local/apache ] ; then
	default=/usr/local/apache
elif [ -d /var/apache ] ; then
	default=/var/apache
else
	default=unknown
fi
ask "Where is your webserver 'root' directory (where cgi-bin and htdocs live)"
serverroot="$ans"

if [ ! -d "$serverroot" ] ; then
	print ERROR: invalid directory
	exit 1
fi

if [ -d $serverroot/htdocs ] ; then
	default=$serverroot/htdocs/wcal
else
	default=unknown
fi
ask "Where do you want the image files and static data to go?"
imagedir="$ans"

default=${imagedir##$serverroot/htdocs}
print ""
print "What is the relative URL to the image dir?"
ask "(if full URL is http://localhost/images, answer '/images')"
imageurl="$ans"

if [ -d $serverroot/cgi-bin ] ; then
	default=$serverroot/cgi-bin
else
	default=$imagedir
fi
ask "Where do you want the actual cgi frontend to go?"
cgidir="$ans"



print ""
print Setting up wget.cgi to use $perlloc
sed -e "s?SED_PERLBIN?$perlloc?" \
    -e "s?SED_LIBDIR?$imagedir?" < wcal.tpl > wcal.cgi
chmod 755 wcal.cgi

sed -e "s?SED_IMGURL?$imageurl?" < constants.tpl > constants.pl

if [ ! -d "$imagedir" ] ; then
	default=y
	ask "'$imagedir' does not exist. Create it?"
	if [ "$default"  != "y" ] ; then
		print quitting
		exit 1
	fi
	mkdir $imagedir
fi
if [ ! -d "$cgidir" ] ; then
	default=y
	ask "$cgidir does not exist. Create it?"
	if [ "$default"  != "y" ] ; then
		print quitting
		exit 1
	fi
	mkdir $cgidir
fi




print copying files...

cp *.cgi $cgidir
cp *.gif *.pl README $imagedir
print done

