#!/bin/ksh

# Tool to build multiple Unix platforms' libdb
#
# Author: jsalter@netscape.com, 99/02/02
#
PATH=/bin:/usr/bin:/tools/ns/bin:/usr/ucb:${PATH}
export PATH

# libtool build the target to .libs directory, we have to check from there
LIBTOOLOBJDIR=.libs
export LIBTOOLOBJDIR
LIBDBNAME=libdb31

# Note: "SunOS5.8" MUST be the last entry in the list
#
platforms="AIX HP-UX HP-UX64 IRIX IRIX64 Linux OSF1 UnixWare SunOS5.5.1 SunOSx865.6 SunOS5.6 SunOS5.7 SunOS5.8"
libdb_exp=""
DLL_SUFFIX=.so
log=1
USE_64=0
PLATFORMCCFLAGS=""
PLATFORMLDFLAGS=""

# If the first argument is -n, output to stdout, not to the log file
#
if [[ "$1" = "-n" ]] ; then
	log=0
	force=1
	shift
fi

if [[ "$1" = "-64" ]] ; then
	USE_64=1
	shift
fi

if [[ -n "$*" ]] ; then
	bldplatforms="$1"
else
	bldplatforms=$(uname -s)
	if [[ ${bldplatforms} = "SunOS" ]] ; then
		bldversion=$(uname -r)
		bldplatforms="SunOS${bldversion}"
	fi
	if [[ ${bldplatforms} = "HP-UX" ]] ; then
		if [[ ${USE_64} -eq 1 ]] ; then
			bldversion=64
			bldplatforms="HP-UX${bldversion}"
		fi
	fi
fi

for platform in ${platforms} ; do

	# If the current machine is a supported platform, break out
	# of the machine validity check
	#
	if [[ ${platform} = ${bldplatforms} ]] ; then
		break
	fi
	[[ "${platform}" = "SunOS5.8" ]] || continue

	# If this is the last platform
	#
	echo "\nERROR: Platform '${bldplatforms}' is not one of the supported."
	echo "       Valid platforms include:"
	echo "\t$(echo ${platforms} | tr ' ' '\t')"
	exit 1
done

for platform in $bldplatforms ; do
	case ${platform} in
		AIX* | aix* ) version=`uname -v`.`uname -r`
			      CC=xlC_r; export CC
			      DLL_SUFFIX=.a
			      platform=AIX ; libdb_exp=libdb31.exp ;;
		HP-UX* | HPUX* ) version=`uname -r` ; platform=HPUX ; DLL_SUFFIX=.sl 
				 PLATFORMCCFLAGS="+DAportable"
				 PLATFORMLDFLAGS="+DAportable"
				 if [ ${USE_64} -ne 0 ] ; then
					 PLATFORMCCFLAGS="+DA2.0w +DChpux -D_LARGEFILE64_SOURCE"
					 PLATFORMLDFLAGS="+DA2.0w +DChpux"
				 fi
				 PATH=/opt/ansic/bin:${PATH}; export PATH
				 export PATH;;
		IRIX* | Irix* ) version=`uname -r` ; platform=IRIX ;;
		LINUX* | Linux* ) version=`uname -r` ; platform=Linux ;;
		OSF1* | osf1* ) version=`uname -r` ; platform=OSF1 ;;
		SOLARIS* | Solaris* | SunOS* | SUNOS* )
			PATH=/tools/ns/workshop/bin:${PATH}:/usr/ccs/bin
			export PATH
			case `uname -p` in
				x86 ) platform=SOLARISx86 ; version=`uname -r`;;
				sparc ) platform=SOLARIS ; version=`uname -r` ;;
			esac ;;
		UnixWare* | UNIXWARE* | SCO* ) version=`uname -v` ; platform=SCOOS ;;
		* ) echo "\nERROR: Platform not known.  Please edit this $0 script."
		    exit 1
		    ;;
	esac

	[[ ${USE_64} -eq 0 ]] || platform=${platform}_64

	# Loop through the debug/optimize build levels
	#
	for debug in full optimize ; do

		builddir="build.${platform}_${version}-${debug}"
		if [[ -d ${builddir} && ${force} -eq 0 ]] ; then
			echo "\nWARNING: Build directory already exists: ${builddir}"
			echo "         Do you want to rebuild this platform [y]/n? \c"
			read answer
			case "${answer}" in
				y* | Y* | "" ) ;;
				* ) echo "\nNOTICE: Skipping build of ${platform}_${version}"
				    continue
				    ;;
			esac
		else
			echo "\nNOTICE: Creating subdirectory: ${builddir}"
		fi

		# Create the platform-specific subdirectory
		#
		[[ -d ${builddir} ]] || mkdir -p ${builddir}

		cd ${builddir} || exit 1

		if [[ ! -x ../dist/configure ]] ; then
			echo "\nERROR: Unable to run ../dist/configure script."
			exit 1
		fi

		if [[ ${log} -eq 1 ]] ; then
		    logfile=buildlog.$(date +%y%m%d.%H%M)
		    echo "\nNOTICE: Output to log: ${builddir}/${logfile}"
		    touch ${logfile}
		fi

		echo "NOTICE: Cleaning up previous DEBUG=${debug} build"
		if [[ ${log} -eq 1 ]] ; then
		    [[ ! -r Makefile ]] || gmake USE_64=${USE_64} DEBUG=${debug} realclean >> ${logfile} 2>&1
		else
		    [[ ! -r Makefile ]] || gmake USE_64=${USE_64} DEBUG=${debug} realclean
		fi

		echo "NOTICE: Running ../dist/configure to generate DEBUG=${debug} files"
		if [[ ${debug} = full ]] ; then
			if [[ ${log} -eq 1 ]] ; then
			    ../dist/configure --enable-debug --enable-dynamic --enable-diagnostic >> ${logfile} 2>&1
			else
			    ../dist/configure --enable-debug --enable-dynamic --enable-diagnostic
			fi
		else
			if [[ ${log} -eq 1 ]] ; then
			    ../dist/configure --disable-debug --enable-dynamic >> ${logfile} 2>&1
			else
			    ../dist/configure --disable-debug --enable-dynamic
			fi
		fi

		for file in db_int.h db.h ; do
			if [[ ! -f ${file} ]] ; then
				echo "\nERROR: Configure failed to generate DEBUG=${debug} file: ${file}"
				[[ ${log} -eq 0 ]] || echo "       Log file is ${logfile}"
				exit 1
			fi
		done

		echo "NOTICE: Running gmake to build the DEBUG=${debug} product: ${platform}_${version}"
		if [[ ${log} -eq 1 ]] ; then
		    echo gmake PLATFORMCCFLAGS="${PLATFORMCCFLAGS}" \
			  PLATFORMLDFLAGS="${PLATFORMLDFLAGS}" \
			  USE_64=${USE_64} DEBUG=${debug} libdb.a >> ${logfile} 2>&1
		    gmake PLATFORMCCFLAGS="${PLATFORMCCFLAGS}" \
			  PLATFORMLDFLAGS="${PLATFORMLDFLAGS}" \
			  USE_64=${USE_64} DEBUG=${debug} libdb.a >> ${logfile} 2>&1
		    echo gmake PLATFORMCCFLAGS="${PLATFORMCCFLAGS}" \
			  PLATFORMLDFLAGS="${PLATFORMLDFLAGS}" \
			  USE_64=${USE_64} DEBUG=${debug} all >> ${logfile} 2>&1
		    gmake PLATFORMCCFLAGS="${PLATFORMCCFLAGS}" \
			  PLATFORMLDFLAGS="${PLATFORMLDFLAGS}" \
			  LIBDB_ARGS="libdb.a" \
			  USE_64=${USE_64} DEBUG=${debug} all >> ${logfile} 2>&1
		    mv libdb.a libdbs.a
		else
		    echo gmake PLATFORMCCFLAGS="${PLATFORMCCFLAGS}" \
			  PLATFORMLDFLAGS="${PLATFORMLDFLAGS}" \
			  USE_64=${USE_64} DEBUG=${debug} libdb.a
		    gmake PLATFORMCCFLAGS="${PLATFORMCCFLAGS}" \
			  PLATFORMLDFLAGS="${PLATFORMLDFLAGS}" \
			  USE_64=${USE_64} DEBUG=${debug} libdb.a
		    echo gmake PLATFORMCCFLAGS="${PLATFORMCCFLAGS}" \
			  PLATFORMLDFLAGS="${PLATFORMLDFLAGS}" \
			  USE_64=${USE_64} DEBUG=${debug} all
		    gmake PLATFORMCCFLAGS="${PLATFORMCCFLAGS}" \
			  PLATFORMLDFLAGS="${PLATFORMLDFLAGS}" \
			  LIBDB_ARGS="libdb.a" \
			  USE_64=${USE_64} DEBUG=${debug} all
		    mv libdb.a libdbs.a
		fi

		for file in ${LIBDBNAME}${DLL_SUFFIX} ${libdb_exp}; do
			if [[ ! -f ${LIBTOOLOBJDIR}/${file} ]] ; then
				echo "\nERROR: gmake failed to create DEBUG=${debug} file: ${file}"
				[[ ${log} -eq 0 ]] || echo "       Log file is ${logfile}"
				exit 1
			fi
		done

		for file in db_archive db_checkpoint db_deadlock db_dump db_load db_printlog db_recover db_stat ; do
			if [[ ! -f ${file} ]] ; then
				echo "\nERROR: gmake failed to create DEBUG=${debug} file: ${file}"
				[[ ${log} -eq 0 ]] || echo "       Log file is ${logfile}"
				exit 1
			fi
		done

		# change back to the top-level subdirectory
		#
		cd ..

		# Create the platform-specific built directory
		#
		# If 64-bit option is set, then push out with _64 name tag
		#
		builtdir=built/${platform}
		if [[ ${platform} = SOLARIS || ${platform} = SOLARISx86 ]] ; then
			builtdir=built/${platform}${version}
		fi
		builtdir=${builtdir}-${debug}

		echo "NOTICE: Creating platform specific target directory: ${builtdir}"

		[[ -d ${builtdir} ]] || mkdir -p ${builtdir}
		builtdir_lib=${builtdir}/lib
		[[ -d ${builtdir_lib} ]] || mkdir -p ${builtdir_lib}
		builtdir_bin=${builtdir}/bin
		[[ -d ${builtdir_bin} ]] || mkdir -p ${builtdir_bin}
		builtdir_incl=${builtdir}/include
		[[ -d ${builtdir_incl} ]] || mkdir -p ${builtdir_incl}

		for file in ${LIBDBNAME}${DLL_SUFFIX} ${libdb_exp} ; do
			targetfile=${file}
			cp ${builddir}/${LIBTOOLOBJDIR}/${file} ${builtdir_lib}/${targetfile}
			if [[ ! -f ${builtdir_lib}/${targetfile} ]] ; then
				echo "\nERROR: Copy from ${builddir} failed for ${builtdir_lib}/${targetfile}"
				[[ ${log} -eq 0 ]] || echo "       Log file is ${logfile}"
				exit 1
			fi
		done
		for file in libdbs.a ; do
			targetfile=${file}
			cp ${builddir}/${file} ${builtdir_lib}/${targetfile}
			if [[ ! -f ${builtdir_lib}/${targetfile} ]] ; then
				echo "\nERROR: Copy from ${builddir} failed for ${builtdir_lib}/${targetfile}"
				[[ ${log} -eq 0 ]] || echo "       Log file is ${logfile}"
				exit 1
			fi
		done
		for file in db_archive db_checkpoint db_deadlock db_dump db_load db_printlog db_recover db_stat ; do
			targetfile=${file}
			cp ${builddir}/${file} ${builtdir_bin}/${targetfile}
			if [[ ! -f ${builtdir_bin}/${targetfile} ]] ; then
				echo "\nERROR: Copy from ${builddir} failed for ${builtdir_bin}/${targetfile}"
				[[ ${log} -eq 0 ]] || echo "       Log file is ${logfile}"
				exit 1
			fi
		done
		for file in db.h ; do
			targetfile=${file}
			cp ${builddir}/${file} ${builtdir_incl}/${targetfile}
			if [[ ! -f ${builtdir_incl}/${targetfile} ]] ; then
				echo "\nERROR: Copy from ${builddir} failed for ${builtdir_incl}/${targetfile}"
				[[ ${log} -eq 0 ]] || echo "       Log file is ${logfile}"
				exit 1
			fi
		done
		if [ $DLL_SUFFIX = ".a" ]; then
			mv -f ${builtdir_lib}/${LIBDBNAME}${DLL_SUFFIX} ${builtdir_lib}/${LIBDBNAME}.so
		fi
	done
done

