#!/bin/sh

echo
echo -n "Determining OS... "
uname
ostype=`uname`
platform=`uname -m`

if [ "$ostype" != "Linux" -a "$ostype" != "FreeBSD" -a "$ostype" != "OpenBSD" -a "$ostype" != "BSD/OS" -a "$ostype" != "SunOS" ]; then
echo "***ERROR***: This OS is not supported"
exit
fi

if [ "$ostype" = "BSD/OS" ]; then
ostype="BSD_OS"
fi

if [ "$ostype" = "Linux" ]; then
 echo -n "Determining Linux Platform... "
 if [ "$platform" = "i386" -o "$platform" = "i486" -o "$platform" = "i586" -o "$platform" = "i686" ]; then
  ostype="Linux_x86"
  echo "Linux_x86"
 else if [ "$platform" = "sparc" ]; then
  ostype="Linux_sparc"
  echo "Linux_sparc"
 else if [ "$platform" = "sparc64" ]; then
  ostype="Linux_sparc64"
  echo "Linux_sparc64"
 else if [ "$platform" = "ppc" ]; then
  ostype="Linux_PPC"
  echo "Linux_PPC"
 else if [ "$platform" = "alpha" ]; then
  ostype="Linux_alpha"
  echo "Linux_alpha"
 else
  ostype="Linux_unknown"
  echo "***ERROR***: This Linux platform is not supported"
  exit
  fi
 fi
 fi
 fi
fi

echo -n "Checking for signedness of paddrlen/__addr_len... "

line=`grep -A 1 "int accept" /usr/include/sys/socket.h | tail -1`

if [ "$line" = "		int *__paddrlen));" ]; then
type=signed
echo $type
else if [ "$line" = "			socklen_t *__addr_len));" ]; then
type=unsigned
echo $type
else if [ "$platform" = "ppc" ]; then
type=unsigned
echo $type
else 
echo "unsure, trying signed"
type=signed
fi
fi
fi
else
type=signed
fi

if [ "$ostype" = "SunOS" ]; then
libs="-lsocket -lnsl"
else
libs=
fi

if [ "$1" = "debug" ]; then
debug="-g"
fi

echo -n "Checking for make... "
if [ -f `which make | awk '{print $1}'` ]; then
echo "found make"
maketype="make"
elif [ -f `which gmake | awk '{print $1}'` ]; then
echo "found gmake"
maketype="gmake"
else
echo "not found. ***ERROR***: Couldn't find make or gmake"
exit
fi

echo -n "Determining if $maketype is executable by you... "
if [ -x `which $maketype` ]; then
echo "Yes"
else
echo "No. ***ERROR***: You are not allowed to execute $maketype"
exit
fi

#echo -n "Determining make type... "
#if [ `make -version 2>&1 | grep GNU | wc -l` -eq 1 ]; then
#echo "GNU";
#OBJLINE="\${objects}: %.o: %.cpp \${headers}"
#else
#echo "normal"
#OBJLINE=".cpp.o: \${headers}"
#fi

echo -n "Checking for gcc... "
if [ -f `which gcc | awk '{print $1}'` ]; then
echo "found"
else
echo "not found. ***ERROR***: Couldn't find gcc"
exit
fi

echo -n "Determining if gcc is executable by you... "
if [ -x `which gcc` ]; then
echo "Yes"
else
echo "No. ***ERROR***: You are not allowed to execute gcc"
exit
fi

if [ "`uname`" = "Linux" ]; then
 echo -n "Determining if /proc/net/tcp is readable by you... "
 if [ -r /proc/net/tcp ]; then
 echo "Yes"
 else
 echo "No. ***WARNING***: You are not allowed to read /proc/net/tcp, Transfers may not be completed."
 fi
fi

echo -n "Checking for snprintf()... "
if [ -n "`grep "snprintf" /usr/include/stdio.h`" ]; then
echo "found"
else
echo "Missing. ***ERROR***: snprintf() missing, contact your sysadmin and have them update the box"
exit
fi

echo -n "Checking for regcomp()... "
if [ -n "`grep "regcomp" /usr/include/r*x*`" ]; then
echo "found"
else
echo "Missing. ***ERROR***: regcomp() missing, contact your sysadmin and have them update the box"
exit
fi



echo -n "Creating Makefile... "

cp /dev/null Makefile
(
echo "SOCK=$type"
echo "OS=$ostype"
echo "LIBS=$libs"
echo "DEBUG=$debug"
echo "OBJLINE=$OBJLINE"
cat Makefile.config 
) > Makefile

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