#!/bin/sh -e

# (c) Zeus Technology Limited 2000-2002.  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.

PACKAGE=spec
VERSION=0.8

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

echo "Zeus PEPP version $VERSION."
echo "Copyright (C) Zeus Technology Limited 2000-2002.  All rights reserved."
echo

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
# The grep is this way round for /bin/sh variants that don't like !.
if grep HSE_REQ_TRANSMIT_FILEV $INCLUDES/httpext.h >/dev/null
then
	:
else
	echo "Sorry, this code only compiles and runs on Zeus 3.3.8 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" ;;
		*lockf)	LOCKING="-DUSE_LOCKF" ;;
		*semop)	LOCKING= ;;
		# Current default is semop, but we can tweak this based on
		# feedback from customers.
		*)	LOCKING= ;;
	esac
}

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

OUTPROCESS=

# option parsing
while test $# -gt 0; do
	case "$1" in
	--outprocess)
		PACKAGE=${PACKAGE}_eisapi
		OUTPROCESS="-DOUT_PROCESS -D_REENTRANT"
		;;
	--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|*lockf|*semop)
			set_locking $1 ;;
		*)
			echo please specify locking as flock or semop
			exit 1 ;;
		esac
		;;
	--ssl=*)
		case "$1" in
		*no)	SSL=no ;;
		*yes)	SSL=yes ;;
		*)
			echo please specify ssl as yes or no
			exit 1 ;;
		esac
		;;
	--64bit=*)
		case "$1" in
		*no)	SIXTYFOUR=no ;;
		*yes)	SIXTYFOUR=yes ;;
		*)
			echo please specify 64bit as yes or no
			exit 1 ;;
		esac
		;;
	*)
		echo unknown option: $1
		exit 1 ;;
	esac
	shift
done

if [ "$SSL" = yes ]; then
	SENDFILE=
fi

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

# Special things for certain architectures.
case "$ARCH" in
    HP-UX)
	# look for the 64-bit compile platform...
	if [ "$SIXTYFOUR" = yes ] || [ -f "/etc/zeus_PA2.0Wbuild" ]; then
		ARCH=${ARCH}-PA2.0W
	fi
	;;
    SunOS)
	# For SunOS, we need to look at 'uname -p' rather than 'uname -m'.
	MACHINE=`uname -p`
	case "$MACHINE" in
		i?86)	ARCHNAME="SunOS_x86" ;;
		*)	ARCHNAME="SunOS" ;;
	esac
esac

# Set vars
FULLARCH="${ARCH}_${RELEASE}_${MACHINE}"
case "$FULLARCH" in
	HP-UX_B.11*_ia64)
		WANTCC=/opt/ansic/bin/cc
		WANTLD=/opt/aCC/bin/aCC
		CFLAGS="-I $INCLUDES +z +O2 -Ae $DEFINES +DD64"
		LDFLAGS="-s -b +DD64"
		ARCHNAME="HP11-IA64"
		;;
	HP-UX_B.11*_*)
		WANTCC=cc
		WANTLD=ld
		CFLAGS="-I $INCLUDES +z +O2 -Ae +DA1.1 +DS2.0 $DEFINES"
		LDFLAGS="+e GetExtensionVersion +e HttpExtensionProc +e TerminateExtension -b"
		ARCHNAME="HP11-PA1.1N"
		;;
	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,+e,TerminateExtension,+pd,64M,+pi,4M -b"
		ARCHNAME="HP11-PA2.0W"
		;;
	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
		LIBC=`ls -l /lib/libc.so.6 | sed "s/.*libc-//" | cut -f-2 -d.`
		ARCHNAME="Linux-glibc$LIBC"
		;;
	AIX_*_*)
		WANTCC="xlc /usr/ibmcxx/bin/xlc"
		WANTLD="xlC /usr/ibmcxx/bin/xlC"
		CFLAGS="-I $INCLUDES -O2 -qmaxmem=8192 $DEFINES"
		LDFLAGS="-bnoentry -Wl,-G -bexpall -bM:SRE -lc -lbsd"
		ARCHNAME="AIX"
		;;
	OSF1_*_*)
		WANTCC=cc
		WANTLD=cxx
		CFLAGS="-std -I$INCLUDES -O2 -trapuv -tune host -arch generic $DEFINES"
		LDFLAGS="-shared -B symbolic -lc"
		ARCHNAME="Tru64"
		;;
	IRIX64_*_*)
		WANTCC=cc
		WANTLD=CC
		CFLAGS="-I$INCLUDES -mips3 -n32 -O2 -KPIC -DIRIX64 -DHAVE_SEMUN $DEFINES"
		LDFLAGS="-mips3 -n32 -shared"
		ARCHNAME="IRIX64"
		;;
	SunOS_5.[89]_i?86)
		WANTCC=/usr/local/bin/gcc
		WANTLD=/usr/local/bin/gcc
		CFLAGS="-I $INCLUDES -fomit-frame-pointer -O6 -fPIC $DEFINES"
		LDFLAGS="-shared -fPIC -O6 -lc"
		;;
	SunOS_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"
		;;
	SunOS_*_*)
		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"
		;;
	*)
		echo "Sorry, architecture '$FULLARCH' is not yet supported"
		exit 1
		;;
esac

if [ "$SSL" = yes ]; then
	ARCHNAME="${ARCHNAME}_ssl"
fi

# 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!"
	echo "I tried: $WANTCC"
	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!"
	echo "I tried: $WANTLD"
	exit 1
fi

# Create Makefile

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

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