#! /bin/sh
#
# This script is used to configure the Xarm library.
#

PROJECT=Xarm
SITECFG=config/site.cf

INSTALL_DIR="/usr/X11"
CXX_COMPILER="c++"
CC_COMPILER="cc"
EXTRA_FLAGS=""
IRULESRC="`pwd`/config"
EXAMPLES=""
BUILD_EXAMPLES="y"
XMKMF="xmkmf"
NOVELL_UNIX="n"

Ask()
{
   if test "${2}" = ""
   then
      echo -n "$1: "
   else 
      echo -n "$1 [$2]: "
   fi

   read ANSWER
   if test "${ANSWER}" = "" 
   then
      ANSWER="$2"
   fi
}

YesNo()
{
   if test "${2}" = ""
   then
      Ask "$1" "y"
   else
      Ask "$1" "$2"
   fi

   if test "${ANSWER}" = "Y"
   then
      ANSWER="y"
   fi
}

GetConfig()
{
   Ask "Install $PROJECT into directory" $INSTALL_DIR
   INSTALL_DIR=$ANSWER

   Ask "C++ compiler to use" $CXX_COMPILER
   CXX_COMPILER=$ANSWER

   if test ${CXX_COMPILER} = "CC"
   then
      # a.k.a.  NCR Unix (I think?)
      NOVELL_UNIX="y"
      YesNo "Is this Novell/NCR Unix" $NOVELL_UNIX
      NOVELL_UNIX=$ANSWER
   fi

   Ask "C compiler to use" $CC_COMPILER
   CC_COMPILER=$ANSWER

   Ask "Specify any extra compiler flags needed" $EXTRA_FLAGS
   EXTRA_FLAGS=$ANSWER

   Ask "Specify the location of xmkmf to use" $XMKMF
   XMKMF=$ANSWER

   YesNo "Build examples" $BUILD_EXAMPLES 
   if test ${ANSWER} = "y"
   then
      EXAMPLES="#define NEED_EXAMPLES"
      BUILD_EXAMPLES="yes"
   else
      BUILD_EXAMPLES="no"
   fi
}

ShowConfig()
{
   echo
   echo "Install into directory.: $INSTALL_DIR"
   echo "C++ compiler...........: $CXX_COMPILER"
   echo "C compiler.............: $CC_COMPILER"
   echo "Extra compiler flags...: $EXTRA_FLAGS"
   echo "xmkmf..................: $XMKMF"
   echo "Build examples.........: $BUILD_EXAMPLES"
   echo 
   YesNo "Is the above information correct"
   if test ${ANSWER} != "y"
   then
      echo "You need to run Configure again."
      exit 0
   fi
}

MakeSiteConfig()
{
   rm -f $SITECFG
   echo "EXTRA_IRULESRC=$IRULESRC" >> $SITECFG
   echo "BASEDIR=$INSTALL_DIR" >> $SITECFG
   echo "CXX=$CXX_COMPILER" >> $SITECFG
   echo "CC=$CC_COMPILER" >> $SITECFG
   echo "EXTRA_FLAGS=$EXTRA_FLAGS" >> $SITECFG
   echo "$EXAMPLES" >> $SITECFG
   if test ${NOVELL_UNIX} = "y"
   then
      echo "#define _NOVELL_UNIX_NATIVE_CXX_HACK_" >> $SITECFG 
   fi
   echo "#include \"C++.rules\"" >> $SITECFG
}

echo
echo "Did you read the INSTALL notes?"
echo

GetConfig

ShowConfig

MakeSiteConfig

$XMKMF
make Makefiles

echo
echo "Please email bug fixes or comments to the maintainer of this project."
echo "See the README file for details."
echo
echo "Configuration complete."
echo 

echo "To compile type 'make'" 
echo "To install type 'make install'"
echo 
