#!/bin/sh

#
# @(#) Configure 1.28@(#)
# pmg@alliance.centerclick.org|Configure|20011110165853|10121
#

echo
echo -n "Determining OS... "
uname
ostype=`uname`
platform=`uname -m`
short_ostype=`uname | cut -d "_" -f 1`
libs=""

rm -f config.error.* config.temp*

case "$ostype" in
 Linux | FreeBSD | OpenBSD | NetBSD | IRIX | IRIX64 | \
 OSF1 | Rhapsody | Darwin | AIX )
  # nothing fancy
  ;;
 SunOS )
  libs="$libs -lsocket -lnsl"
  ;;
 BSD/OS )
  ostype=BSD_OS
  ;;
 HP-UX )
  ostype=HPUX
  ;;
 CYGWIN* )
  ostype=$short_ostype
  libs="$libs -lregex"
  
  # only 1.1.7 or later is supported
  echo -n "Checking for cygwin >= 1.1.7... "
  ver=`uname -r | cut -d '(' -f 1`
  ver_A=`echo $ver | cut -d '.' -f 1`
  ver_B=`echo $ver | cut -d '.' -f 2`
  ver_C=`echo $ver | cut -d '.' -f 3`
  
  # 2.x.x - 9.x.x, 1.2.x - 1.9.x, and 1.1.7 - 1.1.9 are ok
  if [ \( "$ver_A" -ge "2" \)                                         \
    -o \( "$ver_A" -eq "1" -a "$ver_B" -ge "2" \)                     \
    -o \( "$ver_A" -eq "1" -a "$ver_B" -eq "1" -a "$ver_C" -ge "7" \) \
     ]; then
   echo "OK, $ver"
  else
   echo "***ERROR***: $ver < 1.1.7"
   exit # fatal error
   waserror=yes
  fi
  ;;
 *)
  echo "***ERROR***: This OS is not supported"
  exit
  ;;
esac

if [ "$1" = "debug" -o "$2" = "debug" -o "$3" = "debug" ]; then
echo "Configuring for debugging."
debug="-g"
fi

if [ "$1" = "warn" -o "$2" = "warn" -o "$3" = "warn" ]; then
echo "Configuring for extra warnings."
WARNS="-Wshadow
-Wpointer-arith
-Wcast-qual
-Wcast-align
-Wstrict-prototypes
-Wmissing-prototypes
-Wmissing-declarations
-Winline
-Wwrite-strings"
fi

if [ "$1" = "prof" -o "$2" = "prof" -o "$3" = "prof" ]; then
echo "Configuring for profiling."
PROF="-pg"
fi


echo -n "Checking for make... "
if [ -f `type make | awk '{print $NF}'` ]; then
 maketype="make"
 if [ -x `type $maketype | awk '{print $NF}'` ]; then
  echo "found $maketype"
 else
  echo "found $maketype. ***ERROR***: You are not allowed to execute $maketype"
  waserror=yes
 fi
elif [ -f `type gmake | awk '{print $NF}'` ]; then
 maketype="gmake"
 if [ -x `type $maketype | awk '{print $NF}'` ]; then
  echo "found $maketype"
 else
  echo "found $maketype. ***ERROR***: You are not allowed to execute $maketype"
  waserror=yes
 fi
else
 echo "not found. ***ERROR***: Couldn't find make or gmake"
 maketype="make"
 waserror=yes
fi

echo -n "Checking for gcc/cc... "
if [ -f `type gcc | awk '{print $NF}'` ]; then
 cctype="gcc"
 if [ -x `type $cctype | awk '{print $NF}'` ]; then
  echo "found $cctype"
 else
  echo "found $cctype. ***ERROR***: You are not allowed to execute $cctype"
  waserror=yes
 fi
elif [ -f `type cc | awk '{print $NF}'` ]; then
 cctype="cc"
 if [ -x `type $cctype | awk '{print $NF}'` ]; then
  echo "found $cctype"
 else
  echo "found $cctype. ***ERROR***: You are not allowed to execute $cctype"
  waserror=yes
 fi
else
 echo "not found. ***ERROR***: Couldn't find gcc or cc"
 maketype="gcc"
 waserror=yes
fi

echo -n "Checking standard #include's... "
echo "
#define GEX 
#include \"src/defines.h\"
#include \"src/headers.h\"
#include \"src/globals.h\"
int main (int argc, char **argv) {exit(0);}
" > config.temp.c
if [ -z "`$cctype -D_OS_$ostype -DSIGNEDSOCK=signed $libs -o config.temp config.temp.c -Wall 2>&1`" ]; then
echo "looks good"
else
echo "`$cctype -D_OS_$ostype -DSIGNEDSOCK=signed $libs -o config.temp config.temp.c -Wall 2>&1`" > config.error.1
echo " ***ERROR***: couldn't build with standard #include's, errors have been placed in the file config.error.1"
rm -f config.temp*
waserror=yes
fi

echo -n "Checking for signedness of addrlen... "
echo "
#define GEX 
#include \"src/defines.h\"
#include \"src/headers.h\"
#include \"src/globals.h\"
int main (int argc, char **argv) {
 int a; struct sockaddr_in b; SIGNEDSOCK int c;
 getsockname(a,(struct sockaddr *)&b,&c);
 exit(0);
 }
" > config.temp.c
if [ -z "`$cctype -D_OS_$ostype -DSIGNEDSOCK=signed $libs -o config.temp config.temp.c -Wall 2>&1`" ]; then
echo "signed"
type=signed
elif [ -z "`$cctype -D_OS_$ostype -DSIGNEDSOCK=unsigned $libs -o config.temp config.temp.c -Wall 2>&1`" ]; then
echo "unsigned"
type=unsigned
else
echo "signed:" > config.error.2
echo "`$cctype -D_OS_$ostype -DSIGNEDSOCK=signed $libs -o config.temp config.temp.c -Wall 2>&1`" >> config.error.2
echo "unsigned:" >> config.error.2
echo "`$cctype -D_OS_$ostype -DSIGNEDSOCK=unsigned $libs -o config.temp config.temp.c -Wall 2>&1`" >> config.error.2
echo "Unknown. ***ERROR***: neither signed or unsigned worked, errors have been placed in the file config.error.2"
rm -f config.temp*
type=signed
waserror=yes
fi

echo -n "Checking for snprintf()... "
echo "
#define GEX 
#include \"src/defines.h\"
#include \"src/headers.h\"
#include \"src/globals.h\"
int main (int argc, char **argv) {snprintf(\"%s\",10,\"blah\"); exit(0);}
" > config.temp.c
if [ -z "`$cctype -D_OS_$ostype -DSIGNEDSOCK=$type $libs -o config.temp config.temp.c -Wall 2>&1`" ]; then
SNPRINTF=
echo "found"
else
SNPRINTF="-DNO_SNPRINTF"
echo "missing, will emulate"
fi

echo -n "Checking for regcomp()... "
echo "
#define GEX 
#include \"src/defines.h\"
#include \"src/headers.h\"
#include \"src/globals.h\"
int main (int argc, char **argv) {regex_t *t; regcomp(t,\"blah\",REG_ICASE|REG_NOSUB); exit(0);}
" > config.temp.c
if [ -z "`$cctype -D_OS_$ostype -DSIGNEDSOCK=$type $libs -o config.temp config.temp.c -Wall 2>&1`" ]; then
echo "found"
else
echo "`$cctype -D_OS_$ostype -DSIGNEDSOCK=$type $libs -o config.temp config.temp.c -Wall 2>&1`" > config.error.3
echo "Missing. ***ERROR***: couldn't find regcomp(), errors have been placed in the file config.error.3"
rm -f config.temp*
waserror=yes
fi

echo -n "Seeing if crypt.h is needed... "
echo "
#define GEX 
#include \"src/defines.h\"
#include \"src/headers.h\"
#include \"src/globals.h\"
int main (int argc, char **argv) {crypt(\"aaaaaaaa\",\"aa\"); exit(0);}
" > config.temp.c
if [ -z "`$cctype -c -D_OS_$ostype -DSIGNEDSOCK=$type -DCRYPT_H -o config.temp.o config.temp.c -Wall 2>&1`" ]; then
CRYPTH="-DCRYPT_H"
echo "needed"
elif [ -z "`$cctype -c -D_OS_$ostype -DSIGNEDSOCK=$type -o config.temp.o config.temp.c -Wall 2>&1`" ]; then
CRYPTH=
echo "not needed"
else
echo "Missing. ***ERROR***: couldn't find crypt() with or without crypt.h"
rm -f config.temp*
CRYPTH="-DCRYPT_H"
waserror=yes
fi

echo -n "Seeing if -lcrypt is needed... "
if [ -z "`$cctype -o config.temp config.temp.o -lcrypt -Wall 2>&1`" ]; then
libs="$libs -lcrypt"
echo "needed"
elif [ -z "`$cctype -o config.temp config.temp.o -Wall 2>&1`" ]; then
libs="$libs"
echo "not needed"
else
echo "Neither!"
echo -n "Seeing if -lcrypto works instead... "
if [ -z "`$cctype -o config.temp config.temp.o -lcrypto -Wall 2>&1`" ]; then
libs="$libs -lcrypto"
echo "yes"
else
echo "Missing. ***ERROR***: couldn't link with or without -lcrypt or -lcrypto"
rm -f config.temp*
libs="$libs -lcrypt"
waserror=yes
fi
fi

rm -f config.temp*

echo -n "Creating Makefile... "

cp /dev/null Makefile
(
echo "SOCK=$type"
echo "OS=$ostype"
echo LIBS=$PROF $libs
echo DEFNS=$CRYPTH $SNPRINTF $PROF $WARNS
echo "DEBUG=$debug"
echo "OBJLINE=$OBJLINE"
echo "CC=$cctype"
cat Makefile.config 
) > Makefile

echo Done
echo

if [ -n "$waserror" ]; then
echo "!!!!!WARNING!!!!!  one or more errors occured, guesses for those values that failed were included."
echo "You may need to manually edit the first 6 lines of the makefile to remove defines or libraries"
echo
fi

echo "Type \"$maketype\" to compile"
echo "No errors or warnings should appear when compiling, if they do, something is wrong"
echo

# end
