#!/bin/sh
###
# $Revision: 2.3 $
# $Date: 1996/05/03 02:20:22 $
#
# Configure script for Count 1.2
# muquit@semcor.com
# November 23, 1995
#
# This script generates the following files from user input
#   config.h
#   Makefile
#   combine/Makefile
#
# wrote this script with bash functions, then realized it will  not be very
# portable.. rewrote: Nov 25, 1995
#
###

trap 'echo '';echo "Interrupt Detected..exiting";rm -f "./Config.tmpl" 2>/dev/null;rm -f "./.alfcf" ;rm -f "./strict" 2>/dev/null;echo '';exit 1' 1 2 3 15

Tmpl="./Config.tmpl"
CgiBinDir="/usr/local/etc/httpd/cgi-bin"

if [ ! -f "./VERSION" ] 
then
cat << EOV

        The file ./VERSION does not exist!
        aborting!

EOV
        exit 1
fi

Version=`cat ./VERSION`
Basedir="wwwcount$Version"
ConfigH="config.h"
BaseDir="/usr/local/etc/Counter"
ConfigDir="conf"
ConfFile="count.cfg"
DigitDir="digits"
DataDir="data"
LogDir="Log"
LogFile="Count$Version.log"
RgbFile="./wcount/rgb.txt"
#allow_f_cr=0
#strict_mode=0

(echo "hi there\c" ; echo " ") >.echotest
if grep c .echotest >/dev/null 2>&1 ; then
n='-n'
c=''
else
n=''
c='\c'
fi
rm -f .echotest >/dev/null 2>&1

Pwd=`pwd`
checkbd=`basename $Pwd`

###
# check if we'r inside valid directory
    if [ $checkbd != $Basedir ]
    then
cat << EOR2

************************************************************************
    Your current working directory must be $Basedir 
    in order configure the counter!
    Wrong current working directory: $checkbd
************************************************************************

EOR2
    exit 2
    fi


##
# cleanup
###
if [ -f "./config.h" ]
then
    rm -f "./config.h"
fi

##
# initial screeen
###
cat << EOSI
    Welcome to the configuration procedure of Count $Version
    ---------------------------------------------------

    o You must know where your system keeps CGI programs (cgi-bin directory)
      It is necessary to generate the install program.
      This directory must exist. If this directory does not exist, the
      configuration procedure will Abort!

    o You have to decide a directory, where you will keep all counter related
      stuff. This directory will have other directories inside. Default
      values will be supplied, press Return key to accept the default value.
      Accept the default value, it will make your life much easier.

    During installation, the directories will be created for you
    if they do not exist and if you have the permission to do so.
    ++
EOSI


###
echo $n "Continue [y|n]$c? "
read answer

if [ .$answer = . ]
then
    answer=x
fi
    until [ $answer = "y" -o $answer = "Y" -o $answer = "n" -o $answer = "N" ]
    do
        echo $n "Continue [y|n]$c? "
        read answer
            if [ .$answer = . ]
            then
                answer=x
            fi
    done

    case $answer in
        y|Y)
####################
# Ask for cgi-bin     --starts
###
cat <<EOB

    You need to enter the full path of the directory where you system
    keeps the CGI programs. This directory must exist!

EOB
echo $n "*cgi-bin dierctory [$CgiBinDir]:$c "
read newpath
if [ .$newpath != . ]
then
    CgiBinDir=$newpath
fi
# check if the directory exist
if [ ! -d $CgiBinDir ]
then
cat <<EOCG  

    Directory "$CgiBinDir" does not exist! If you do not know where your
    system keeps CGI programs, find it out first. If you did not configure
    your http server yet, please do so and come back!

    Aborting configuration procedure!

EOCG
    exit 2
fi

####################
# Ask for cgi-bin     --ends
###
####################
# Ask for basedir     --starts
###
cat <<EOB

    You need to enter the base directory of the counter related stuff.

EOB
echo $n "*Base directory [$BaseDir]:$c "
read newpath
if [ .$newpath != . ]
then
    BaseDir=$newpath
fi

####################
# Ask for basedir     --ends
###
####################
# Ask for configdir     --starts
###
cat <<EOB

    You need to enter the directory of the configuration file.

EOB
echo $n "* Config directory [$BaseDir/$ConfigDir]:$c "
read newpath
if [ .$newpath != . ]
then
    ConfigDir=$newpath
fi
####################
# Ask for configdir     --ends
###
####################
# Ask for configfile     --starts
###
cat <<EOB

    You need to enter the name of the configuration file.
    This file contains the information about

        o if you want to ignore access from certain hosts
        o host acccess authentication

    You will create this file later by running the program "Gen_conf".

EOB
echo $n "* Name of the configuration file [$ConfFile]:$c "
read newpath
if [ .$newpath != . ]
then
    ConfFile=$newpath
fi
####################
# Ask for configfile     --end
###
####################
# Ask for datadir     --starts
###
cat <<EOB
              
    You need to enter the directory of the counter data file.
    
EOB
echo $n "*Data directory [$BaseDir/$DataDir]:$c "
read newpath
if [ .$newpath != . ]
then
    DataDir=$newpath
fi  
####################
# Ask for datadir     --ends
###

####################
# Ask for logdir     --starts
###
cat <<EOB   

    You need to enter the directory of the Log file.

EOB
echo $n "*Log directory [$BaseDir/$LogDir]:$c "
read newpath
if [ .$newpath != . ]
then
    LogDir=$newpath
fi
####################
# Ask for logdir     --ends
###
####################
# Ask for logfile     --starts
###
cat <<EOB

    You need to enter the name of the Log file.
    This file hold the error messages of the counter. It also
    logs if someone tried to access your counter remotely.

EOB
echo $n "* Name of the log file [$LogFile]:$c "
read newpath
if [ .$newpath != . ]
then
    LogFile=$newpath
fi
####################
# Ask for logfile     --ends
###
        ;;
        n|N)
            echo "Exiting..Better luck next time!"
            exit 1
        ;;

        *)
            echo ""
            echo "Illegal choice"
     esac

echo ""
echo "You entered:"
echo "++++++++++++++++++++++++++++++"
    echo "CgiBinDir=$CgiBinDir"
    echo "BaseDir= $BaseDir"
    echo "DigitDir= $BaseDir/$DigitDir"
    echo "ConfDir = $BaseDir/$ConfigDir"
    echo "ConfFile= $ConfFile"
    echo "DataDir= $BaseDir/$DataDir"
    echo "LogDir= $BaseDir/$LogDir"
    echo "LogFile= $LogFile"
echo "++++++++++++++++++++++++++++++"

echo $n "Everything looks ok [y|n]$c? "
read answer

if [ .$answer = . ]
then
    answer=x
fi
    until [ $answer = "y" -o $answer = "Y" -o $answer = "n" -o $answer = "N" ]
    do
        echo $n "Everything looks ok [y|n]$c? "
        read answer
            if [ .$answer = . ]
            then
                answer=x
            fi
    done

    case $answer in
        y|Y)
        echo "Great! creating header file $ConfigH"
###
# create header file --starts
###
d=`date`
cat <<EOH>$ConfigH
#ifndef _COUNT_CONFIG_H
#define _COUNT_CONFIG_H 1

/*
** This file is automatically generated by the Configure script 
** Count-config written by muquit@semcor.com
** Counter Version: $Version
** created on: $d
**
**  If you edit this file, you better make sure you know what are 
**  you doing.
*/
EOH
echo "#define ConfigDir \"$BaseDir/$ConfigDir\"" >> $ConfigH
echo "#define ConfigFile \"$ConfFile\"" >> $ConfigH
echo "#define DataDir \"$BaseDir/$DataDir\"" >> $ConfigH
echo "#define DigitDir \"$BaseDir/$DigitDir\"" >> $ConfigH
echo "#define LogDir \"$BaseDir/$LogDir\"" >> $ConfigH
echo "#define LogFile \"$LogFile\"" >> $ConfigH

cat<<EOX>>$ConfigH

/*****************************************************************************
* Defaults parameters not supplied in calling.
* Change 'em here if you insist...
*****************************************************************************/
#define DefaultMaxDigits       6      /* Max digits in output        */
#define DefaultLeftpad         True   /* 0 indicates no padding      */

#define DefaultThickness       6       /* 0 indicates no frame        */
#define Default_FrameRed       69
#define DefaultFrameGreen      139     /* RGB=0;0;0 for black         */
#define DefaultFrameBlue       50
#define FRGB_ImpliedFt         5       /* Use this FT if we get FRGB only */

#define DefaultTransparency       False    /* 0 indicates no transparent  */
#define DefaultTransparentRed     0
#define DefaultTransparentGreen   0 /* RGB=255;255;255 for white   */
#define DefaultTransparentBlue    0
#define TRGBImpliedTr              1   /* Use this TR if we get TRRBG only */

#define DefaultStartCount          1      /* Initial counter value       */
#define DefaultShowCount           1      /* 1 indicates count is shown  */
#define DfForRandom            "RANDOM"    /* This DF name means use rand */
#define DefaultDatafile        "RANDOM"    /* Make rand the default, too  */

#define DefaultDigitsStyle       "A" /* Use GIFs in digits/A/files  */
#define ParamDelimiters           "|&"    /* These separate params       */

/*****************************************************************************
* This specifies a file listing RGB values mapped to names.
* If not defined, no attempt to use RGB names is made.  If you're not sure,   
* just leave this be--it's okay if the file does not exist.
*
* When RGB_MAPPING_ISERROR is set to 1, an error is returned when RGB name
* value is used, but it shows the corresponding RGB triplet.  This is done
* so RGB names can be looked up, but yet not allow users to make constant
* use of this very ineffiecient lookup.
*****************************************************************************/
#define RgbMappingDict        "$BaseDir/rgb.txt"
EOX

#if [ $allow_f_cr -eq 1 ]
#then
#echo "#define ALLOW_FILE_CREATION   1" >>$ConfigH
#else
#echo "#define ALLOW_FILE_CREATION   0" >>$ConfigH
#fi

#if [ $strict_mode -eq 1 ]
#then
#echo "#define STRICT_MODE   1" >>$ConfigH
#else
#echo "#define STRICT_MODE   0" >>$ConfigH
#fi

cat<<EOXXX>>$ConfigH
/*****************************************************************************
* End of parameter defaults
*****************************************************************************/
#endif /* _COUNT_CONFIG_H*/
EOXXX

###
# create header file --ends
###
##
# create template for install program --starts
##
echo "creating variables template file $Tmpl for the install program.."
echo "BASE_DIR=\"$BaseDir\"" > $Tmpl
echo "CGIBIN_DIR=\"$CgiBinDir\"" >> $Tmpl
echo "CONFIG_FILE=\"$ConfFile\"" >> $Tmpl
echo "CONFIG_DIR=\"$BaseDir/$ConfigDir\"" >> $Tmpl
echo "DIGIT_DIR=\"$BaseDir/$DigitDir\"" >> $Tmpl
echo "DATA_DIR=\"$BaseDir/$DataDir\"" >> $Tmpl
echo "LOG_DIR=\"$BaseDir/$LogDir\"" >> $Tmpl
echo "LOG_FILE=\"$LogFile\"" >> $Tmpl
echo "RGB_FILE=\"$RgbFile\"" >> $Tmpl
echo "RGB_DIR=\"$BaseDir\"" >> $Tmpl
##
# create template for install program --ends
##
echo "now run ./configure"
        ;;
        n|N)
            echo "Exiting..Better luck next time!"
        ;;

        *)
            echo ""
            echo "Illegal choice"
     esac
# End of configuration script
