#!/bin/sh
CBASE=""
ASFLAGS=""
LFLAGS=""
ASPREP=""
if [ `uname` = Linux -o `uname` = FreeBSD -o `uname` = NetBSD -o `uname` = OpenBSD -o `uname` = SunOS ]
then
  if [ `uname` = SunOS ]
  then
    if [ `uname -r` != 5.8 ]
    then
      echo "*** WARNING ***"
      echo "Only Solaris 8 Sparc is supported. If you can get vpnd working on"
      echo "any other Solaris version please contact ast@domdv.de."
      echo "Please note that I don't have any other Solaris version"
      echo "available so I can't give any support for any other version."
    fi
    if [ ! -r /var/run/egd-pool ]
    then
      echo "Please install the package prngd 0.9.22 or later."
      echo "You can find it at:"
      echo "ftp://ftp.aet.tu-cottbus.de/pub/postfix_tls/related/prngd/"
      echo "or in the 'extra' directory"
    fi
    if [ `grep -c clone:ppp /etc/minor_perm 2> /dev/null` != 1 ]
    then
      echo "Please install the ppp kernel and streams modules of the"
      echo "Australian National University pppd package."
      echo "You can find these on the open source cdrom of the"
      echo "Solaris 8 Media Kit or in the 'solaris8' directory."
      echo "Please see the README of this directory for details."
    fi
    VERSION=""
    CFLAGS="-DSunOS"
    hw=""
    mf=Makefile.Solaris.in
  elif [ `uname` = Linux ]
  then
    VERSION=`uname -r | cut -b1`
    CFLAGS="-DLINUX -DLINUX$VERSION"
    hw=`uname -m`
    mf=Makefile.in
  elif [ `uname` = NetBSD ]
  then
    VERSION=""
    CFLAGS="-DOSNetBSD"
    if [ -x /sbin/sysctl ]
    then
      SYSCTL=/sbin/sysctl
    else
      SYSCTL=/usr/sbin/sysctl
    fi
    hw=`$SYSCTL hw.model | sed -e 's/.*486.*/i486/' -e 't done' \
	-e 's/.*586.*/i586/' -e 't done' -e 's/.*686.*/i686/' -e 't done' \
	-e 's/.*/i386/' -e ':done'`
    if [ `uname -m` != i386 ]
    then
      hw=`uname -m`
    fi
    if [ `uname -r | cut -d'.' -f1-2 | sed 's/\.//g` -ge 15 ]
    then
      ASPREP="cat"
    else
      ASPREP="sed -f netbsdasm.sed"
    fi
    mf=Makefile.NetBSD.in
  elif [ `uname` = OpenBSD ]
  then
    VERSION=""
    CFLAGS="-DOSOpenBSD"
    hw=`/usr/sbin/sysctl hw.model | sed -e 's/.*486.*/i486/' -e 't done' \
	-e 's/.*586.*/i586/' -e 't done' -e 's/.*686.*/i686/' -e 't done' \
	-e 's/.*/i386/' -e ':done'`
    if [ `uname -m` != i386 ]
    then
      hw=`uname -m`
    fi
    mf=Makefile.OpenBSD.in
  else
    VERSION=""
    CFLAGS="-DFreeBSD"
    if [ -x /sbin/sysctl ]
    then
      SYSCTL=/sbin/sysctl
    else
      SYSCTL=/usr/sbin/sysctl
    fi
    hw=`$SYSCTL hw.model | sed -e 's/.*80286.*/i286/' -e 't done' \
	-e 's/.*i386.*/i386/' -e 't done' -e 's/.*i486.*/i486/' -e 't done' \
	-e 's/.*/i586/' -e ':done'`
    if [ `uname -m` != i386 ]
    then
      hw=`uname -m`
    fi
    mf=Makefile.FreeBSD.in
  fi
  comp=0
  if [ $hw = x86_64 ]
  then
    spath="/usr/local/lib64 /usr/lib64"
  else
    spath="/usr/local/lib /usr/lib"
  fi
  for dir in $spath
  do
    for lib in gz z
    do
      if [ -f $dir/lib$lib.a -o -f $dir/lib$lib.so ]
      then
	if [ "$LFLAGS" = "" ]
	then
	  for hdr in /usr/local/include /usr/include
	  do
	    if [ -f $hdr/zlib.h ]
	    then
	      comp=1
	      if [ "`grep -c '.*int.*compress2' $hdr/zlib.h`" != "0" ]
	      then
		if [ "$hdr" = "/usr/include" ]
		then
		  CFLAGS="-DCOMPRESS $CFLAGS"
		else
		  CFLAGS="-I$hdr -DCOMPRESS $CFLAGS"
		fi
		if [ "$dir" = "/usr/lib" -o "$dir" = "/usr/lib64" ]
		then
		  LFLAGS="-l$lib"
		else
		  LFLAGS="-L$dir -l$lib"
		fi
		  ASFLAGS="--defsym COMPRESS=1"
	      else
		echo "Please upgrade your zlib package to at least 1.1.4"
		echo "You can find it at:"
		echo "http://www.gzip.org/zlib/"
		echo "or in the 'extra' directory"
		echo "Building vpnd with runtime compression disabled."
	      fi
	    fi
	  done
	fi
      fi
    done
  done
  if [ $comp = 0 ]
  then
    echo "Please install the package zlib 1.1.4 or later."
    echo "You can find it at:"
    echo "http://www.gzip.org/zlib/"
    echo "or in the 'extra' directory."
    echo "If your system has libz.so installed please"
    echo "make sure that the zlib-devel rpm is installed,"
    echo "too."
    echo "Building vpnd with runtime compression disabled."
  fi
  if [ "$VERSION" = "2" ]
  then
    if [ -f /usr/include/linux/if_slip.h ]
    then
      if [ ! -f /usr/include/net/if_slip.h ]
      then
	CFLAGS="$CFLAGS -DLINSLIP"
      fi
    fi
  fi
  inl=`gcc $CFLAGS -DMD5_HMAC_FAST -DSHA1_HMAC_FAST -DRMD160_HMAC_FAST -O3 -fno-inline -S route.c -o - | grep -c __fswab`
  if [ "$inl" = "0" ]
  then
    INLINE=" -fno-inline"
  else
    INLINE=""
  fi
  case $hw in
   i486)cobj="blowfishx86.o cryptox86.o md5x86.o sha1x86.o rmd160x86.o"
	robj="blowfishx86.o"
	cflg="-DCRYPTOX86"
	aflg="--defsym MD5_HMAC_FAST=1 --defsym SHA1_HMAC_FAST=1 --defsym RMD160_HMAC_FAST=1"
	;;
   i586)cobj="blowfishx86.o cryptox86.o md5x86.o sha1x86.o rmd160x86.o"
	robj="blowfishx86.o"
	cflg="-DCRYPTOX86"
	aflg="--defsym MD5_HMAC_FAST=1 --defsym SHA1_HMAC_FAST=1 --defsym RMD160_HMAC_FAST=1"
	;;
   i686)cobj="blowfishx86.o cryptox86.o md5x86.o sha1x86.o rmd160x86.o"
	robj="blowfishx86.o"
	cflg="-DCRYPTOX86"
	aflg="--defsym MD5_HMAC_FAST=1 --defsym SHA1_HMAC_FAST=1 --defsym RMD160_HMAC_FAST=1"
	;;
   x86_64)cobj="blowfishx86_64.o cryptox86_64.o md5x86_64.o sha1x86_64.o rmd160x86_64.o"
	robj="blowfishx86_64.o"
	cflg="-DCRYPTOX86"
	aflg="--defsym MD5_HMAC_FAST=1 --defsym SHA1_HMAC_FAST=1 --defsym RMD160_HMAC_FAST=1"
	;;
   *)   cobj="blowfish.o md5.o sha1.o rmd160.o"
	robj="blowfish.o"
	cflg=""
	aflg=""
	;;
  esac
  (
  echo '# uncomment the following line for debug code'
  echo '#DEBUG=-DDEBUG'
  if [ "$VERSION" = "1" ]
  then
    echo "ADDON=randomd"
  fi
  if [ `uname` = SunOS ]
  then
    echo "CC=gcc"
    echo "LINK=gcc"
  else
    echo "CRYPTOOBJ1=$cobj"
    echo "CRYPTOOBJ2=$robj"
    echo "CRYPTOFLG=$cflg"
    echo "CC=gcc"
    echo "AS=as"
    echo "LINK=gcc"
    if [ "$ASPREP" != "" ]
    then
      echo "ASPREP=$ASPREP"
    fi
  fi
  echo "CBASE=$CFLAGS -Wall -O3$INLINE"
  echo "LFLAGS=-s $LFLAGS"
  if [ `uname` = OpenBSD ]
  then
    (
    for i in $ASFLAGS $aflg END
    do
      case $i in
	--defsym)
		;;
	END)
		;;
	*)	echo "		$i"
		;;
      esac
    done
    ) > flags.s
  elif [ `uname` != SunOS ]
  then
    if [ "$ASFLAGS" != "" ]
    then
      echo "ASFLAGS=$ASFLAGS $aflg"
    else
      echo "ASFLAGS=$aflg"
    fi
  fi
  cat $mf
  ) > Makefile
else
  echo 'Operating system not (yet) supported. Please port!!!'
  exit 1
fi
