#!/bin/csh -f
#			Empire configure
#                       by Ken Stevens
#                       5 Jun 1995
#
#	Figure out what sort of unix this is
#	(NeXT machines don't have uname!)

# Check to see that we are where we think we are

if (! -e ./lib/commands/zdon.c) then
  echo "ERROR: configure should be run in the root Empire directory.  Aborting."
  exit 1
endif

setenv SRCDIR `pwd`
setenv SMALL_S no

if (-r ./config.status) then
  setenv JUST_GET_VARIABLES
  source ./config.status
  unsetenv JUST_GET_VARIABLES
endif

if (! $?HOSTNAME) then
  setenv HOSTNAME `hostname | nslookup | grep 'Name:' | awk '{print $2}'`
endif

if ( $?S_P_ETU_S ) goto questionaire

set UNAME=""
if (-e /usr/bin/uname)		set UNAME=`/usr/bin/uname`
if (-e /bin/uname)		set UNAME=`/bin/uname`
if (-e /usr/apollo/bin)		set UNAME=`ver sys5.3 /bin/uname`
if (-e /usr/bin/ncrm)		set UNAME=ncr
if (-e /usr/bin/swconfig)	set UNAME=sco
if ( $UNAME == "" ) then
    if (-r /NextApps ) then
        hostinfo | grep I386
        if  ( $status == 0 ) then
            set UNAME=next-386
        else
            set UNAME=next
        endif
    endif
endif
set HOSTNAME=`hostname`
if ( $UNAME == $HOSTNAME)       set UNAME=`/bin/uname -v`

setenv UNAME $UNAME

if ($UNAME == "next")		setenv SYSTYPE	NeXT
if ($UNAME == "HP-UX")		setenv SYSTYPE	hpux
if ($UNAME == "IRIX")		setenv SYSTYPE	irix5
if ($UNAME == "SunOS") then
	setenv SYSTYPE `arch`
	set VERS=`/bin/uname -r`
	if ( $VERS:r == 5 ) then
		setenv SYSTYPE	solaris
		set COMPILER=gcc
		if ( $VERS:e != 5 ) then
			echo "Warning: I used Solaris 5.5 and you have $VERS"
		endif
	endif
endif
if ($UNAME == "ULTRIX")		setenv SYSTYPE	ultrix-4.2
if ($UNAME == "AIX")    	setenv SYSTYPE	aix
if ($UNAME == "OSF1")   	setenv SYSTYPE	alpha
if ($UNAME == "Linux")		setenv SYSTYPE	linux
if ($UNAME == "UMIPS")          setenv SYSTYPE  mips

if (! $?SYSTYPE) then
  cat <<EOF

STRANGENESS ALERT:
  I do not know how to test for your operating system.  Please send me
  <children@empire.net> the output of "uname" and "uname -v" on your
  system so that I can add your operating system to this configure script.

EOF
endif

set GMAKE="`which gnumake`"
if ("$GMAKE" == "") then
  set GMAKE="`which gmake`"
  if ("$GMAKE" == "") then
    set GMAKE="`which make`"
    if ("$GMAKE" == "") set GMAKE="/usr/local/bin/make"
  endif
endif

set COMPILER="`which cc`"
if ("$COMPILER" == "") then
  set COMPILER="`which gcc`"
  if ("$COMPILER" == "") set COMPILER="/bin/cc"
endif

setenv FULLNAME "`finger $USER | grep 'In real life:' | uniq | sed 's/^.*In real life:[ ]*//'`"
setenv IPADDRESS `hostname | nslookup | grep 'Address:' | tail -1 | awk '{print $2}'`
set domain=`echo $HOSTNAME | sed 's/^[^\.]*\.//'`
setenv EMAIL "$USER@$domain"
setenv GNUMAKE "$GMAKE"
setenv CC "$COMPILER"
setenv GAMEDIR `pwd`
setenv PORT 7777
setenv MAXNOC 52
setenv WORLD_X 64
setenv WORLD_Y 32
setenv ETUS 60
setenv S_P_ETU_S 10m
setenv BLITZ_S yes
setenv SMALL_S no

questionaire:

cat <<EOF
Please answer the following questions.  If you hit return, then the
default answer in [brackets] will be chosen.  Your answers will be
saved to the file ./config.status which will be run the next time you
type "compile".  If, at some point, you wish to change your answers,
then you should run the program called "configure".  You may quit this
program at any time by typing ^C <RETURN>.
EOF

ask_for_SYSTYPE:
echo ""
echo "Which operating system will you be running on?"
if (! $?SYSTYPE) then
  setenv SYSTYPE "linux"
  echo "Your choices are:"
  ls ./make/Systems
endif
echo -n "[$SYSTYPE]: "
set ans=$<
if ( "$ans" != "" ) setenv SYSTYPE $ans
if (! -r ./make/Systems/$SYSTYPE) then
  echo "'$SYSTYPE' is not a valid choice."
  unsetenv SYSTYPE
  echo "Please choose again or hit ^C <RETURN> to quit."
  goto ask_for_SYSTYPE
endif
echo "SYSTYPE='$SYSTYPE'"
echo ""
echo "What is your full name?"
if (! $?FULLNAME) setenv FULLNAME "Joe Deity"
echo -n "[$FULLNAME]: "
set ans=$<
if ( "$ans" != "" ) setenv FULLNAME "$ans"
echo "FULLNAME='$FULLNAME'"
echo ""
echo "What is your email address?"
if (! $?EMAIL) setenv EMAIL 'joe@empire.net'
echo -n "[$EMAIL]: "
set ans=$<
if ( "$ans" != "" ) setenv EMAIL $ans
echo "EMAIL='$EMAIL'"
echo ""
ask_for_GNUMAKE:
echo "Where is gnumake (AKA make) located on your system?  (If you don't have it,"
echo "you can get it from ftp.empire.net:/pub/empire/deity/tools/emptools.tar.gz)"
if (! $?GNUMAKE) setenv GNUMAKE '/usr/local/bin/gnumake'
echo -n "[$GNUMAKE]: "
set ans=$<
if ( "$ans" != "" ) setenv GNUMAKE $ans
if (! -x $GNUMAKE) then
  echo "$GNUMAKE is not an executable file"
  unsetenv GNUMAKE
  echo "Please choose again or hit ^C <RETURN> to quit."
  goto ask_for_GNUMAKE
endif
echo "GNUMAKE='$GNUMAKE'"
echo ""
ask_for_CC:
echo "Which compiler would you like to use?"
if (! $?CC) setenv CC '/bin/cc'
echo -n "[$CC]: "
set ans=$<
if ( "$ans" != "" ) setenv CC $ans
if (! -x $CC) then
  echo "$CC is not an executable file"
  unsetenv CC
  echo "Please choose again or hit ^C <RETURN> to quit."
  goto ask_for_CC
endif
echo "CC='$CC'"

set CPP="`which cpp`";
if ("$CPP" == "") then
  setenv CPPM "$CC -M";
else if ( "$CPP" =~ no* ) then
  setenv CPPM "$CC -M";
else if ( "$CPP" =~ "Command not found" ) then
  setenv CPPM "$CC -M";
else
  setenv CPPM "$CPP -M";
endif


echo ""
echo "In which directory would you like to put the files for this game?"
echo -n "[$GAMEDIR]: "
set ans=$<
if ( "$ans" != "" ) setenv GAMEDIR $ans
if ( ! -d $GAMEDIR ) then
  echo "The directory '$GAMEDIR' does not exist.  Would you like to create it?"
  echo -n "[y]: "
  set ans=$<
  if ( "$ans" != "" && "$ans" != "y" ) then
    echo "Aborting configure."
    exit 1
  endif
  echo "Creating directory '$GAMEDIR'"
  mkdir -p $GAMEDIR
endif
if ( ! -d $GAMEDIR/bin ) then
  echo "Creating directory '$GAMEDIR/bin'"
  mkdir $GAMEDIR/bin
endif
if ( ! -d $GAMEDIR/data ) then
  echo "Creating directory '$GAMEDIR/data'"
  mkdir $GAMEDIR/data
endif
echo "GAMEDIR='$GAMEDIR'"
echo ""
echo "What is the IP address of this machine?"
if (! $?IPADDRESS) setenv IPADDRESS '205.164.80.15'
echo -n "[$IPADDRESS]: "
set ans=$<
if ( "$ans" != "" ) setenv IPADDRESS $ans
echo "IPADDRESS='$IPADDRESS'"
echo ""
echo "Which port would you like to run the game on?"
echo "Note this number should be between 1601 and 9999"
echo -n "[$PORT]: "
set ans=$<
if ( "$ans" != "" ) setenv PORT $ans
echo "PORT='$PORT'"
echo ""
echo "What is the maximum number of countries that will be in this game?"
echo "This will affect the size of your server and your data files."
echo -n "[$MAXNOC]: "
set ans=$<
if ( "$ans" != "" ) setenv MAXNOC $ans
echo "MAXNOC='$MAXNOC'"
echo ""
echo "How wide (east-west) do you want your planet to be?"
echo "This number must be divisible by 2."
echo -n "[$WORLD_X]: "
set ans=$<
if ( "$ans" != "" ) setenv WORLD_X $ans
echo "WORLD_X='$WORLD_X'"
echo ""
echo "How high (north-south) do you want your planet to be?"
echo -n "[$WORLD_Y]: "
set ans=$<
if ( "$ans" != "" ) setenv WORLD_Y $ans
echo "WORLD_Y='$WORLD_Y'"
echo ""
echo "How many ETU's (Empire Time Units) do you want each update to have?"
echo "This number must be divisible by 4."
echo -n "[$ETUS]: "
set ans=$<
if ( "$ans" != "" ) setenv ETUS $ans
echo "ETUS='$ETUS'"
echo ""

ask_for_S_P_ETU_S:
echo "How often would you like your updates to occur?"
echo "For once every ten minutes, type '10m',"
echo "for once every two hours, thpe '2h',"
echo "for once every day, type '1d', etc..."
echo -n "[$S_P_ETU_S]: "
set ans=$<
if ( "$ans" != "" ) setenv S_P_ETU_S $ans
if ( $S_P_ETU_S =~ ?*m ) then
  setenv S_P_ETU "`echo $S_P_ETU_S | sed 's/m//'`"
  @ spetu = ( $S_P_ETU * 60 ) / $ETUS
  setenv S_P_ETU $spetu
else if ( $S_P_ETU_S =~ ?*h ) then
  setenv S_P_ETU "`echo $S_P_ETU_S | sed 's/h//'`"
  @ spetu = ( $S_P_ETU * 60 * 60 ) / $ETUS
  setenv S_P_ETU $spetu
else if ( $S_P_ETU_S =~ ?*d ) then
  setenv S_P_ETU "`echo $S_P_ETU_S | sed 's/d//'`"
  @ spetu = ( $S_P_ETU * 60 * 60 * 24 ) / $ETUS
  setenv S_P_ETU $spetu
else
  echo "'$S_P_ETU_S' is not a valid choice."
  setenv S_P_ETU_S 10m
  echo "Please choose again or hit ^C <RETURN> to quit."
  goto ask_for_S_P_ETU_S
endif
echo "S_P_ETU_S='$S_P_ETU_S'"
echo "S_P_ETU='$S_P_ETU'"
echo ""
echo "Is this game a Blitz?  Please answer 'yes' or 'no'."
echo -n "[$BLITZ_S]: "
set ans=$<
if ( "$ans" != "" ) setenv BLITZ_S $ans
if ( "$BLITZ_S" == "yes" ) then
  setenv BLITZ "#define BLITZ"
else
  setenv BLITZ "/* #define BLITZ */"
endif
echo "BLITZ_S='$BLITZ_S'"
echo "BLITZ='$BLITZ'"
echo ""
echo "Do you require your server to be unusually small?"
echo "Please answer 'yes' or 'no'."
echo -n "[$SMALL_S]: "
set ans=$<
if ( "$ans" != "" ) setenv SMALL_S $ans
if ( "$SMALL_S" == "yes" ) then
  setenv SMALL "#define SMALL_SERVER"
else
  setenv SMALL "/* #define SMALL_SERVER */"
endif
echo "SMALL_S='$SMALL_S'"
echo "SMALL='$SMALL'"
echo ""

rm -f config.status
cat >! config.status <<EOF
#!/bin/csh -f
# Generated automatically by configure
# Run this file to recreate the current configuration.
# Check to see that we are where we think we are

if (! -e ./lib/commands/zdon.c) then
  echo "ERROR: configure should be run in the root Empire directory.  Aborting."
  exit 1
endif
EOF

cat >> config.status <<EOF
setenv SYSTYPE '$SYSTYPE'
setenv FULLNAME '$FULLNAME'
setenv EMAIL '$EMAIL'
setenv GNUMAKE '$GNUMAKE'
setenv CC '$CC'
setenv CPPM '$CPPM'
setenv GAMEDIR '$GAMEDIR'
setenv SRCDIR '$SRCDIR'
setenv HOSTNAME '$HOSTNAME'
setenv IPADDRESS '$IPADDRESS'
setenv PORT '$PORT'
setenv MAXNOC '$MAXNOC'
setenv WORLD_X '$WORLD_X'
setenv WORLD_Y '$WORLD_Y'
setenv ETUS '$ETUS'
setenv S_P_ETU_S '$S_P_ETU_S'
setenv S_P_ETU '$S_P_ETU'
setenv BLITZ_S '$BLITZ_S'
setenv BLITZ '$BLITZ'
setenv SMALL_S '$SMALL_S'
setenv SMALL '$SMALL'
setenv USER '$USER'
EOF

cat >> config.status <<\EOF
if ( $?JUST_GET_VARIABLES ) exit 0
echo "Running config.status.  To change the values it uses, run the"
echo "program 'configure'"
echo ""
echo "Configuring with the following parameters:"
echo "SYSTYPE = '$SYSTYPE'"
echo "FULLNAME = '$FULLNAME'"
echo "EMAIL = '$EMAIL'"
echo "GNUMAKE = '$GNUMAKE'"
echo "CC = '$CC'"
echo "CPPM = '$CPPM'"
echo "GAMEDIR = '$GAMEDIR'"
echo "SRCDIR = '$SRCDIR'"
echo "HOSTNAME = '$HOSTNAME'"
echo "IPADDRESS = '$IPADDRESS'"
echo "PORT = '$PORT'"
echo "MAXNOC = '$MAXNOC'"
echo "WORLD_X = '$WORLD_X'"
echo "WORLD_Y = '$WORLD_Y'"
echo "ETUS = '$ETUS'"
echo "S_P_ETU_S = '$S_P_ETU_S'"
echo "S_P_ETU = '$S_P_ETU'"
echo "BLITZ_S = '$BLITZ_S'"
echo "BLITZ = '$BLITZ'"
echo "SMALL_S = '$SMALL_S'"
echo "SMALL = '$SMALL'"
echo "USER = '$USER'"

foreach file (`find . -name '*.in' -print`)
echo "creating $file:r"
rm -f $file:r
if ( $file:t == compile.in) then
echo "#\!/bin/csh -f" > $file:r
endif
if ( $file:t =~ mkdep.in ) then
else if ( $file:t =~ *Makefile.in || $file:t == auth.in || $file:t == econfig.in || $file:t == compile.in || $file:t =~ hours.*.in) then
echo "# Generated automatically from `echo $file | sed 's|.*/||'` by configure." >> $file:r
else
echo "/* Generated automatically from `echo $file | sed 's|.*/||'` by configure. */" > $file:r
endif
  sed -e "\
s%@SYSTYPE@%$SYSTYPE%g\
s%@FULLNAME@%$FULLNAME%g\
s%@EMAIL@%$EMAIL%g\
s%@GNUMAKE@%$GNUMAKE%g\
s%@CC@%$CC%g\
s%@CPPM@%$CPPM%g\
s%@GAMEDIR@%$GAMEDIR%g\
s%@SRCDIR@%$SRCDIR%g\
s%@HOSTNAME@%$HOSTNAME%g\
s%@IPADDRESS@%$IPADDRESS%g\
s%@PORT@%$PORT%g\
s%@MAXNOC@%$MAXNOC%g\
s%@WORLD_X@%$WORLD_X%g\
s%@WORLD_Y@%$WORLD_Y%g\
s%@ETUS@%$ETUS%g\
s%@S_P_ETU@%$S_P_ETU%g\
s%@BLITZ@%$BLITZ%g\
s%@SMALL@%$SMALL%g\
s%@USER@%$USER%g\
" $file >> $file:r
end
if (-e $GAMEDIR/data/auth) then
echo "will not overwrite $GAMEDIR/data/auth"
else
echo "creating $GAMEDIR/data/auth"
cp ./templates/auth $GAMEDIR/data
endif
if (-e $GAMEDIR/data/hours) then
echo "will not overwrite $GAMEDIR/data/hours"
else
echo "creating $GAMEDIR/data/hours"
cp ./templates/hours.long $GAMEDIR/data/hours
endif
chmod +x compile
chmod +x ./make/mkdep
echo ""
echo "Your code has been successfully configured.  To compile it, type 'compile &'."
echo "If you would like to change any game parameters without recompiling, then"
echo "just edit the file $GAMEDIR/data/econfig before you start the server."
exit 0
\EOF
chmod +x config.status
./config.status
