#! /bin/sh
#
# @description	Simple Configuration script for Maximus/UNIX.
# @note         This script should stay BOURNE SHELL compatible. No bashisms, please.
#
# @author	Wes Garland; wes@ninja250.kingston.net
# @date		May 23 2003
#
# $Id: configure,v 1.6 2003/06/12 02:51:59 wesgarland Exp $
#

OUTPUT='vars_local.mk'

nonlecho()
{
  if [ -f "/usr/bin/printf" ] || [ -f "/bin/printf" ]; then
    if [ "$2" ]; then
      printf '%s' "$*"
    else
      printf "$1"
    fi
  else
    if [ -f "/usr/ucb/echo" ]; then
      /usr/ucb/echo -n "$*"
    else
      echo -n "$*"
    fi
  fi

  return 0 # true
}

beep()
{
  nonlecho "\007"
  return 0 # true
}

if [ "${UID}" = "0" ] || [ "`id -u 2>/dev/null`" = "0" ] || [ "${USER}" = "root" ] || [ "${LOGNAME}" = "root" ]; then
  echo
  echo "WARNING WARNING WARNING WARNING WARNING"
  echo
  echo "This script should not be run as root!"
  nonlecho "Press ^C to abort, or enter to continue:"
  read line
  echo 
fi

locate()
{
  search="$1"
  shift
  prefPath="$*"

  [ ! "${search}" ] &&  return 1

  OLDIFS="${IFS}"
  IFS=":"
  set -- ${prefPath} ${searchPath} ""
  IFS="$OLDIFS"

  while [ "$1" ]
  do
    [ -x "$1/${search}" ] && echo "$1/${search}" && return 0
    shift
  done
}

find()
{
  label="$1"
  progList="$2"
  shift 2

  for prog in `echo ${progList} | sed 's/,/ /g'`
  do
    eval ${label}=\"\`locate ${prog} $*\`\"
    eval [ \"\$${label}\" ] && break
  done

  if eval [ \"\$${label}\" ]; then
    eval echo "Found ${prog} as \$${label}"
    eval echo "${label}=\$${label}" >> ${OUTPUT}
  else
    echo "Warning: Did not find ${prog}" >&2
  fi
}

PREFIX="/var/max"
SRC="`pwd`"

OLDIFS="${IFS}"
IFS="=${OLDIFS}"
set -- $* ""
IFS="${OLDIFS}"

BUILD=DEVEL

while [ "$1" ]
do
  case "$1" in 
    "--help" | "-h")
      echo "Usage:   ./configure [options]"
      echo "Options:"
      echo " --prefix=DIR 	sets the installation directory [${PREFIX}]"
      echo " --help       	generates this help file"
      echo " --source     	sets the location of the source tree [${SRC}]"
      echo " --build=[mode]	Sets build mode to DEVEL|DEBUG|PROFILE|RELEASE [${BUILD}]"
      exit 0
    ;;
    "--prefix")
      PREFIX="$2"
      shift 2
    ;;
    "--source" | "--src")
      SRC="$2"
      shift 2
    ;;
    "--build")
      BUILD="$2"
      [ "${BUILD}" != "DEVEL" ] && \
	[ "${BUILD}" != "RELEASE" ] && \
	[ "${BUILD}" != "PROFILE" ] && \
	[ "${BUILD}" != "DEBUG" ] && \
	echo "Error: --build must be set to DEVEL, DEBUG, PROFILE, or RELEASE" >&2 && exit 1
      shift 2
    ;;
    *)
      set -- "--help"
    ;;
  esac
done

echo "Configuring Maximus.."
echo

searchPath="${PATH}:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/ccs/bin:/usr/ucb"

if [ ! -f "configure" ] || [ ! -f "vars.mk.configure" ]; then
  echo "Error: This script must be run from the directory" >&2
  echo "       where you unpacked Maximus!" >&2
  exit 1
fi

[ ! -f "${OUTPUT}" ] && firstTime=yes
[ -f "${OUTPUT}" ] && mv -f "${OUTPUT}" "${OUTPUT}~"

find CC gcc
find CXX g++,gcc
find MAKE gmake,make
find AR ar
find RANLIB ranlib 
find YACC bison,yacc /usr/ccs/bin
find SED sed
find RM rm
find MV mv
find INSTALL install /usr/ucb
find UNAME uname
find MAKEDEPEND makedepend /usr/openwin/bin /usr/X11R6/bin

[ ! "${SED}" ] && SED=sed
[ ! "${UNAME}" ] && UNAME=uname
[ ! "${MAKE}" ] && MAKE=make
export MAKE

PLATFORM=`${UNAME} -s | ${SED} 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'`

echo
echo "The file 'vars_${PLATFORM}.mk' will be used for various platform-dependant"
echo "rules and build options."
echo

if [ ! -f "${PLATFORM}.mk" ]; then
  echo "Please insure that it makes sense for your platform. If it does not,"
  echo "fix it, and submit your changes back to the Maximus development team."
else
  echo "Your platform has not been tested yet; as such the file does not exist."
  echo "Please take a look at the other files, and create one for your platform."
  echo "Once you get it working, submit it back to the Maximus development team."
fi

echo
echo "If you have any installation-specific changes to make, you can edit"
echo "the file '${OUTPUT}'."
echo
if [ "${firstTime}" ]; then
  nonlecho "Press ^C to abort, or enter to continue:"
  read line
fi

echo
echo "Creating vars.mk; using PREFIX=${PREFIX}"
echo "PREFIX=${PREFIX}" > vars.mk
echo "SRC=${SRC}" >> vars.mk
echo "COMP=unix" >> vars.mk
echo "PLATFORM=${PLATFORM}" >> vars.mk
echo "" >> vars.mk
echo "# Build platform: `uname -a`" >> vars.mk

if [ "${LOGNAME}" ]; then
  BUILDER="${LOGNAME}"
else
  BUILDER="${USER}"
fi

[ ! "${BUILDER}" ] && BUILDER="`id -un`"  # GNU id

echo "# Configured `date` by ${BUILDER}" >> vars.mk
echo "" >> vars.mk

cat vars.mk.configure >> vars.mk

echo 
${MAKE} -f Makefile usage
echo
echo Testing your build environment..
echo

cd configuration-tests
${MAKE} clean >/dev/null 2>&1
./make.sh || exit $?

echo "Testing your C compiler.."
${MAKE} c_compiler >/dev/null || exit $?
./c_compiler || (echo "Your C Compiler does not work!" && exit 1)
echo " - Looks fine to me!"

echo "Determining platform endianness.."
${MAKE} endian >/dev/null || (err=$? && echo "Error $err compiling endian.c" && exit $err)
./endian " - Chose %s" 2>/dev/null
echo
echo "/* Automatically generated by configure */" > ../slib/compiler_details.h
echo "/* Generated: `date` */" >> ../slib/compiler_details.h
echo  >> ../slib/compiler_details.h
echo "#ifndef _COMPILER_DETAILS_H" >> ../slib/compiler_details.h
echo "#define _COMPILER_DETAILS_H" >> ../slib/compiler_details.h
echo "#undef BIG_ENDIAN" >> ../slib/compiler_details.h
echo "#undef LITTLE_ENDIAN" >> ../slib/compiler_details.h
./endian '#define %s' >> ../slib/compiler_details.h || (err=$? && echo " + Error $err running endian test" && exit $err)
echo >> ../slib/compiler_details.h
echo "#endif /* _COMPILER_DETAILS_H */" >> ../slib/compiler_details.h

${MAKE} stamp_combo >/dev/null || (err=$? && echo "Error $err compiling stamp_combo.c" && exit $err)
./stamp_combo || exit $?
cd ..

if [ ! -d "install_tree" ]; then
  beep
  echo "Warning: could not find install_tree. Did you forget to unpack max-3.03-etc.tar.gz?"
  exit 1
fi

echo
echo "Configuration complete."
echo
[ ! -d "${PREFIX}" ] && mkdir -p ${PREFIX} 2>/dev/null
if [ ! -d "${PREFIX}" ]; then
  echo "You can build Maximus and Squish without creating ${PREFIX},"
  echo "however you will probably need to become root to install,"
  echo "or at least create ${PREFIX} with appropriate permissions."
  echo
fi
echo "Post-Install, it would be wise to make a bbs user and"
echo "group, and change directory ownerships appropriately."
echo "'chmod -R bbs:bbs ${PREFIX}' should work nicely."
echo
