#!/bin/sh
# Hand-rolled by John Viega (viega@list.org)
# For the few things I had to check, this was much easier
# for me than was figuring out autoconf.  However, I did look
# at an autoconf-generated configure script for a few things.
# Most notably, I adapted its test for a fast install program.
#
# I hear ${foo-bar} may not be portable, so I won't use it.
# Looks like it probably is, though, since the install-sh program uses it.
###### Initialization
FLAGS=
RM_CMD='rm -f tmp.c a.out a.exe 2>&1 >/dev/null'
trap "${RM_CMD};exit 1" 1 2 15

#defaults
PREFIX=/usr/local
BINDIR=
DATADIR=
MANDIR=
PROGNAME=its4
OPTIMIZATION=-O3
QUIET=
CC=
INSTALLPROG=

# Parse command-line arguments.
for OPTION
do
    case ${OPTION} in
    -*=*) 
       ARGUMENT=`echo ${OPTION} | sed 's/[-_a-zA-Z0-9]*=//'`
       OPTION=`echo ${OPTION} | sed 's/=.*//'`
       ;;
    -*)
       ARGUMENT=
       ;;
    esac
    OPTION=`echo ${OPTION}|\
            sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'` 
    case `echo ${OPTION}|sed 's/^-*//'` in
    with-cpp)
      if test -n "${ARGUMENT}"; then
         CC=${ARGUMENT}
      else
         echo "Option ${OPTION} requires an argument."
         exit 1
      fi
      ;;
    prefix)
      if test -n "${ARGUMENT}"; then
         #Remove trailing slashes.
         PREFIX=`echo ${ARGUMENT} | sed 's/\/*\$//'`
      else
         echo "Option ${OPTION} requires an argument."
         exit 1
      fi
      ;;
    bindir)
      BINDIR=`echo ${ARGUMENT} | sed 's/\/*\$//'`
      ;;
    datadir)
      DATADIR=`echo ${ARGUMENT} | sed 's/\/*\$//'`
      ;;
    mandir)
      MANDIR=`echo ${ARGUMENT} | sed 's/\/*\$//'`
      ;;
    program-name | program | prog)
      if test -n "${ARGUMENT}"; then
         PROGNAME=${ARGUMENT}
      else
         echo "Option ${OPTION} requires an argument."
         exit 1
      fi
      ;;
    optimization-level | optimization)
      if test -n "${ARGUMENT}"; then
         OPTIMIZATION=-O${ARGUMENT}
      else
         echo "Option ${OPTION} requires an argument."
         exit 1
      fi
      ;;
    quiet | silent)
      QUIET=yes
      ;;
    help | help | h | ?)
      cat <<EOF
Usage: configure [options]
Options:
  --prefix=PATH           Set base directory for installation to directory
                          specified by PATH.  Default is '/usr/local'.
  --bindir=PATH           Install binaries to the directory specified by PATH.
                          Default is '\${prefix}/bin'.
  --datadir=PATH          Install the ITS4 database in the specified directory.
                          Default is '\${prefix}/its4'.
  --mandir=PATH           Install man pages to the specified directory.
  --with-cpp=PATH         Use the C++ compiler located at PATH.
                          Default is to try g++ then CC.
  --program-name=NAME     Name of the scanner executable.  Default is 'its4'.
  --optimization=LEVEL    Set the level of optimization to pass to the compiler.
                          Default is '3'.
  --quiet                 Do not print 'Checking...' messages.
  --help                  This message.

  Note: There must be no spaces surrounding an '='.
EOF
    exit 0
    ;;
    *)
      echo configure: invalid option \'${OPTION}\'
      echo Use \'configure --help\' to see list of options.
      exit 1
    esac
done

if test -z "${QUIET}"; then exec 5>&1; else exec 5>/dev/null; fi

###### Can we invoke the compiler?
cat >tmp.c <<EOF 
#include <iostream.h>
int main(){return 0;}
EOF
if test -n "${CC}"; then
   printf %s "Checking for user-supplied C++... " 1>&5
   if `${CC} tmp.c 2>/dev/null` 2>/dev/null; then
     echo "yes" 1>&5
   else
     echo "no" 1>&5
     CC=
   fi
fi
if test -z "${CC}"; then
    printf %s "Looking for a C++ compiler... " 1>&5
    if `g++ tmp.c 2>/dev/null` 2>/dev/null; then
      echo "g++" 1>&5
      CC=g++
      FLAGS="${FLAGS} -Wall"
    else
      if `CC tmp.c 2&>1 >/dev/null` 2>/dev/null; then
        echo "CC" 1>&5
	CC=CC
	printf %s "Testing for -Wall... " 1>&5
	if `${CC} -Wall tmp.c 2>/dev/null` 2</dev/null; then
          echo "yes" 1>&5
	  FLAGS="${FLAGS} -Wall"
        else 
          echo "no" 1>&5
        fi
      else
        echo "no" 1>&5
        echo 1>&5
        echo "Can't find a C++ compiler in your path."
        echo "Looked for g++ and CC."
        echo "Please use the option '--with-cpp' to set a valid compiler."
        ${RM_CMD}
        exit 0	
      fi
    fi
fi
${RM_CMD}

# Check for a good install program.  
# Adapted somewhat from autoconf generated stuff.
printf %s "Checking for a BSD compatible install... " 1>&5
IFS="${IFS=	}"; save_IFS="$IFS"; IFS=":"
for DIRNAME in $PATH; do
  DIRNAME=`echo ${DIRNAME} | sed 's/\/*\$//'`
    # OSF1 and SCO ODT 3.0 have their own names for install.
    # Don't use installbsd from OSF since it installs stuff as root
    # by default.
  for PNAME in ginstall scoinst install; do
      if test -f "${DIRNAME}/${PNAME}"; then
        if test "${PNAME}" = install &&
          grep dspmsg ${DIRNAME}/${PNAME} >/dev/null 2>&1; then
	  # AIX install.  It has an incompatible calling convention.
	  :
	else
	  INSTALLPROG="${DIRNAME}/${PNAME} -c"
	  break 2
	fi
      fi
  done
done
IFS="${save_IFS}"
if test -n "${INSTALLPROG}"; then
   echo "yes" 1>&5
else
   echo "no" 1>&5
   INSTALLPROG=./install-sh
fi

###### Check for snprintf()
printf %s "Checking for snprintf()... " 1>&5
cat >tmp.c <<EOF
#include <stdio.h>
#include <string.h>
int main()
{
    char buf[12];
    snprintf(buf, 2, "%s", "foof");
}
EOF
if `${CC} tmp.c 2>/dev/null`; then 
    echo "yes" 1>&5
    FLAGS="${FLAGS} -DHAVE_SNPRINTF"
else
    echo "no" 1>&5
fi
${RM_CMD}

###### Check for getopt_long()
printf %s "Checking for getopt_long()... " 1>&5
cat > tmp.c <<EOF
#include <getopt.h>
int main()
{
    getopt_long(0,0,0,0,0);
}
EOF
if `${CC} tmp.c 2>/dev/null`; then 
    echo "yes" 1>&5
    FLAGS="${FLAGS} -DHAVE_GETOPT_LONG"
else
    echo "no" 1>&5
    EXTRA_OBJS="getopt.o getopt1.o"
fi
${RM_CMD}

###### Check for unistd.h
printf %s "Checking for unistd.h... " 1>&5
cat > tmp.c <<EOF
#include <unistd.h>
int main(){}
EOF

if `${CC} tmp.c 2>/dev/null`; then
    echo "yes" 1>&5
    FLAGS="${FLAGS} -DHAVE_UNISTD"
else
    echo "no" 1>&5
fi
${RM_CMD}

# Finalize variables before writing the makefile.
if test -z "${BINDIR}";  then BINDIR=${PREFIX}/bin; fi
if test -z "${DATADIR}"; then DATADIR=${PREFIX}/its4; fi
if test -z "${MANDIR}";  then MANDIR=${PREFIX}/man; fi

# Write the makefile.
cat > Makefile <<EOF
# Warning! DO NOT EDIT THIS FILE!
#
# This file was auto-generated by ./configure.
# If you need to make changes, try looking at './configure --help'
# If that doesn't work, modify Makefile.in

CC=${CC}
EXTRA_FLAGS=${FLAGS}
INSTALL=${INSTALLPROG}
INSTALL_BINDIR=${BINDIR}
INSTALL_DATADIR=${DATADIR}
INSTALL_MANDIR=${MANDIR}
PROGNAME=${PROGNAME}
OPTIMIZATION=${OPTIMIZATION}
EXTRA_OBJS=${EXTRA_OBJS}
##### End of user-defined options.

EOF
cat  Makefile.in >> Makefile

echo 
echo "******************* Configuration completed *******************"
echo "To build, type 'make'.  Then, to install, type 'make install'."
