#!/bin/sh -e

# (c) Zeus Technology Limited 2000-2001.  All rights reserved.
# 
# Copyright in the source code ("the Source Code") listed below in whatever
# form, whether printed electronic compiled or otherwise, belongs to Zeus
# Technology Limited ("we"/"us").
# 
# If you have entered into a written agreement with us under which the Source
# Code is licensed to you ("Licence"),  you may not use, sell, license,
# transfer, copy or reproduce the Source Code in whole or in part or in any
# manner or form other than in accordance with your Licence.  To do so is
# strictly prohibited and may be unlawful and a serious criminal offence.
# 
# If you have not entered into a Licence, disclosure of the Source Code is
# made "as is".   You may use the Source Code for non-commercial purposes
# only.  You may distribute the Source Code to individual third parties for
# their non-commercial purposes only, but only if (1) you acknowledge our web
# site as the source of the Source Code and include such acknowledgement and
# our web address (www.zeus.com) in any copy of the Source Code; (2) the
# Source Code you distribute is complete and not modified in any way and
# includes this notice; and (3) you inform such third parties that these
# conditions apply to them and that they must comply with them.
# 
# If you have not entered into a Licence, all express and implied warranties,
# conditions, terms, undertakings and representations, including without
# limitation as to quality, performance, fitness for purpose, or
# non-infringement, are excluded to the fullest extent permitted by law.
# Neither we nor any other person involved in disclosing the Source Code shall
# have any liabilities whatsoever to you, howsoever arising, in connection
# with the Source Code, or its use by you.
# 
# If you have not entered into a Licence but would like to do so,  please
# contact us at pepp@zeus.com.

VERSION=0.6

ARCH=`uname -s`
RELEASE=`uname -r`

ZEUSHOME=${ZEUSHOME:=/usr/local/zeus}
if [ ! -f $ZEUSHOME/web/include/httpext.h ]
then
	echo "Please set ZEUSHOME variable to where Zeus installation lives"
	exit 1
fi
INCLUDES=$ZEUSHOME/web/include
grep HSE_REQ_TRANSMIT_FILEV $INCLUDES/httpext.h > /dev/null
if [ $? -ne 0 ]
then
	echo "Sorry, this code only compiles and runs on Zeus 3.3.7 or higher"
	exit 1
fi


# work out default sendfile config...
enable_sendfile() {
	case "$ARCH" in
		HP-UX)
			case "$RELEASE" in
				*11*)	SENDFILE="-DUSE_SENDFILE_HP" ;;
				*)	SENDFILE= ;;
			esac
			;;
		AIX)	SENDFILE="-DUSE_SENDFILE_AIX" ;;
		*)	SENDFILE= ;;
	esac
}

# Set post.log locking strategy
set_locking() {
	case "$1" in
		*flock)	LOCKING="-DUSE_FLOCK" ;;
		*semop)	LOCKING= ;;
		# Current default is semop, but we can tweak this based on
		# feedback from customers.
		*)	LOCKING= ;;
	esac
}

CHECKCONFIG="-DCHECK_CONFIG"
SPECDIR="/space/specweb"
case "$ARCH" in
	HP-UX|AIX)	enable_sendfile ;;
	*)		SENDFILE= ;;
esac
set_locking

OUTPROCESS=

# option parsing
while test $# -gt 0; do
	case "$1" in
	--outprocess)
		OUTPROCESS="-DOUT_PROCESS"
		;;
	--sendfile=*)
		case "$1" in
		*no)	SENDFILE= ;;
		*yes)	enable_sendfile ;;
		*)
			echo please specify sendfile as yes or no
			exit 1
		esac
		;;
	--checkconfig=*)
		case "$1" in
		*no)	CHECKCONFIG= ;;
		*yes)	CHECKCONFIG="-DCHECK_CONFIG" ;;
		*)
			echo please specify checkconfig as yes or no
			exit 1
		esac
		;;
	--locking=*)
		case "$1" in
		*flock|*semop)
			set_locking $1
			;;
		*)
			echo please specify locking as flock or semop
			exit 1
		esac
		;;
	*)
		echo unknown option: $1
		exit 1
	esac
	shift
done

# Set any compiler defines here, as determined by the above options
DEFINES="$SENDFILE $CHECKCONFIG $OUTPROCESS $LOCKING"

# look for the 64-bit compile platform...
case "$ARCH" in
    HP-UX)
	if [ -f "/etc/zeus_PA2.0Wbuild" ]; then
	    ARCH=${ARCH}-PA2.0W
	fi
    ;;
esac

# Set vars
case "$ARCH" in
	HP-UX)
		WANTCC=cc
		WANTLD=ld
		CFLAGS="-I $INCLUDES +z +O2 -Ae +DA1.1 +DS2.0 $DEFINES"
		LDFLAGS="+e GetExtensionVersion +e HttpExtensionProc -b"
		;;
	HP-UX-PA2.0W)
		WANTCC=cc
		WANTLD="/opt/aCC/bin/aCC aCC"
		CFLAGS="-I $INCLUDES +z +O2 -Ae +DA2.0W +DD64 +DS2.0 -Wl,+pd,64M,+pi,4M $DEFINES"
		LDFLAGS="+DA2.0W -Wl,+e,GetExtensionVersion,+e,HttpExtensionProc,+pd,64M,+pi,4M -b"
		;;
	Linux)
		WANTCC=gcc
		WANTLD=gcc
		CFLAGS="-Wall -I $INCLUDES -fomit-frame-pointer -O6 -fPIC -pipe $DEFINES"
		LDFLAGS="-shared -fPIC -O6"
		if echo /lib/libc-*.so | grep '2\.0' >/dev/null 2>&1; then
			CFLAGS="$CFLAGS -DHAVE_SEMUN"
		fi
		;;
	AIX)
		WANTCC="/usr/ibmcxx/bin/xlc xlc"
		WANTLD="/usr/ibmcxx/bin/xlC xlC"
		CFLAGS="-I $INCLUDES -O2 -qmaxmem=8192 $DEFINES"
		LDFLAGS="-bnoentry -Wl,-G -bexpall -bM:SRE -lc -lbsd"
		;;
	OSF1)
		WANTCC=cc
		WANTLD=cxx
		CFLAGS="-std -I$INCLUDES -O2 -trapuv -tune host -arch generic $DEFINES"
		LDFLAGS="-shared -B symbolic -lc"
		;;
	IRIX64)
		WANTCC=cc
		WANTLD=CC
		CFLAGS="-I$INCLUDES -mips3 -n32 -O2 -KPIC -DIRIX64 -DHAVE_SEMUN $DEFINES"
		LDFLAGS="-mips3 -n32 -shared"
		;;
	SunOS)
		case "$RELEASE" in
		    5.[89])
			WANTCC=/opt/SUNWspro/bin/cc
			WANTLD=/opt/SUNWspro/bin/CC
			CFLAGS="-I$INCLUDES -xtarget=ultra2 -Kpic -dalign -fns -fsimple=2 -ftrap=%none -xlibmil -xO5 $DEFINES"
			LDFLAGS="-z muldefs -G"
			;;
		    *)
			WANTCC="/usr/local/egcs/bin/gcc gcc"
			WANTLD="/usr/local/egcs/bin/gcc gcc"
			CFLAGS="-I $INCLUDES -fomit-frame-pointer -O6 -fPIC $DEFINES"
			LDFLAGS="-shared -fPIC -O6 -lc"
			;;
		esac
		;;
	*)
		echo "Sorry, architecture '$ARCH' is not yet supported"
		exit 1
esac

# Check for a compiler and linker on the path

for TRYCC in $WANTCC; do
	if type $TRYCC | grep -v 'not found' >/dev/null 2>&1; then
		CC=$TRYCC
		break
	fi
done
if [ -z "$CC" ]; then
	echo "Error, unable to find a C compiler!"
	exit 1
fi

for TRYLD in $WANTLD; do
	if type $TRYLD | grep -v 'not found' >/dev/null 2>&1; then
		LD=$TRYLD
		break
	fi
done
if [ -z "$LD" ]; then
	echo "Error, unable to find a linker!"
	exit 1
fi

# Create Makefile

sed -e "s|@VERSION@|$VERSION|" \
    -e "s|@CC@|$CC|" \
    -e "s|@LD@|$LD|" \
    -e "s|@CFLAGS@|$CFLAGS|" \
    -e "s|@LDFLAGS@|$LDFLAGS|" \
    < Makefile.in > Makefile

echo
echo "Makefile built, now type 'make'!"
