#!/bin/sh
#
#  This is unclassified Government software.
#
#  This source was developed under a prototype development contract by
#  Science Applications International Corporation (SAIC) in McLean,
#  Virginia for the Jet Propulsion Laboratories (JPL) of the National
#  Aeronautics and Space Administration (NASA) for the Space
#  Communications Protocol Standards (SCPS) project.
#
#  SAIC assumes no legal responsibility for the source code and its
#  subsequent use.  No warranty is expressed or implied.
#
#
#**************************************************************
#  Created by    :
#                      Steven R. Sides
#                      steven.r.sides@cpmx.saic.com
#                      Wednesday, June 26, 1996 12:00 pm
#
#  Modified by   :
#
#**************************************************************
#**************************************************************
#  Shell script:       configure
#
#  Description:
#     Creates the Makefile that builds the SCPS-FP reference
#     implementation.  Takes input from the command line, and
#     from Makefile.in.
#
#  Usage example:
#     configure --size medium --system linux --use yes --debug no
#
#     argument names and values may be abbreviated to their
#     shortest unambiguous length.
#
#  Change History:
# $Id: configure,v 1.8 2002/09/23 19:52:15 scps Exp $
#
# $Log: configure,v $
# Revision 1.8  2002/09/23 19:52:15  scps
# Added the following pieces of code for this rev
#
# 1)  Rewrote the readme tun based on user feedback
#
# 2)  Added ability to disable the rule generation for gateway operating
#
# 3)  Added support for OpenBSD based on user feedback.
#
#         PDF
#
# Revision 1.7  1999/03/23 20:24:34  scps
# Merged reference implementation with gateway-1-1-6-k branch.
#
# Revision 1.6.2.1  1998/12/29 14:27:29  scps
# Monolithic update to include gateway code.
#
# Revision 1.6  1998/12/01 16:44:36  scps
# Update to version 1.1.6 --ks
#
# Revision 1.2.2.1  1997/11/25 01:35:05  steven
# Changed it around so it will be less difficult to
# modify in the future.  Call config.guess to get the
# system.
#
# Revision 1.2  1997/09/18 17:57:16  steven
# Red-3 except files of CCSDS packets
#
# Revision 1.1  1997/02/28 21:38:51  steven
# Initial revision
#
#
#**************************************************************

size=small
timing=off
debug=no
syst=sunos
usetp=yes
OPTDEF=
COMMONDEF=
MYFLAGSDEF=
DEBUGDEF=
BLDSIZEDEF=
LIBSDEF=
OBJCDEF=
OBJSDEF=

for arg in $*;
do
  # Handle --size<sp> or
  #        --size=<sp>
  case $next_arg in
  --size)
    # Handle --size<sp>=<sp>arg,
    #        --size<sp>=arg, and
    #        --size<sp>arg
    if [ x$arg != x= ]; then
      size=`echo $arg | sed 's/=//'`
      next_arg=
    fi
    ;;
  --timing)
    # Make spaces and = optional
    if [ x$arg != x= ]; then
      timing=`echo $arg | sed 's/=//'`
      next_arg=
    fi
    ;;
  --debug)
    # Make spaces and = optional
    if [ x$arg != x= ]; then
      debug=`echo $arg | sed 's/=//'`
      next_arg=
    fi
    ;;
  --tp)
    # Make spaces and = optional
    if [ x$arg != x= ]; then
      usetp=`echo $arg | sed 's/=//'`
      next_arg=
    fi
    ;;
  *)
    case $arg in
    -size | --size | --siz | --si)
      next_arg=--size
      ;;
    -size=* | --size=* | --siz=* | --si=*)
      size=`echo $arg | sed 's/^[-a-z_]*=//'`
      if [ x$size = x ]; then
        next_arg=--size
      fi
      ;;
    -timing | --timing | --timin | --timi | --tim | --ti )
      next_arg=--timing
      ;;
    -timing=* | --timing=* | --timin=* | --timi=* | --tim=* | --ti=* )
      timing=`echo $arg | sed 's/^[-a-z_]*=//'`
      if [ x$timing = x ]; then
        next_arg=--timing
      fi
      ;;
    -debug | --debug | --debu | --deb | --de | --d)
      next_arg=--debug
      ;;
    -debug=* | --debug=* | --debu=* | --deb=* | --de=* | --d=*)
      debug=`echo $arg | sed 's/^[-a-z_]*=//'`
      if [ x$debug = x ]; then
        next_arg=--debug
      fi
      ;;
    -tp | --tp | --t)
      next_arg=--tp
      ;;
    -tp=* | --tp=* | --t=*)
      usetp=`echo $arg | sed 's/^[-a-z_]*=//'`
      if [ x$usetp = x ]; then
        next_arg=--usetp
      fi
      ;;
    *)
      echo Unrecognized parameter: $arg
      ;;
    esac
  esac
done

if [ x$next_arg != x ]; then
  echo No parameter for $next_arg
  exit 1
fi

tmp=$size
case $tmp in
xlarge | xlarg | xlar | xla | xl | x)
  size=xlarge
  ;;
large | larg | lar | la | l)
  size=large
  ;;
medium | mediu | medi | med | me | m)
  size=medium
  ;;
small | smal | sma | sm | s)
  size=small
  ;;
*)
  echo $size\?
  echo Pick  { small  medium  large  xlarge }
  exit 1
esac

host=`../source/config.guess`
echo
echo Configuring make file for $host
echo

host_cpu=`echo $host | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\1/'`
host_vendor=`echo $host | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\2/'`
host_os=`echo $host | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\3/'`
OS=`echo $host_os | sed 's/^\([^\.]*\).\(.*\)$/\1/'`
OSNAME=`echo $OS | sed 's/^\([^-0-9]*\)\([-0-9]\)\(.*\)$/\1/'`
OSVER=`echo $OS | sed 's/^\([^-0-9]*\)\([-0-9]\)\(.*\)$/\2/'`

syst=$OSNAME
tmp=$syst
case $tmp in
# For SunOS 4.1.3
sunos)
  syst=sunos
  ;;
#
freebsd | netbsd )
  syst=bsd
  ;;
# For OpenBSD
openbsd )
  ;;
# For Linux
linux)
  syst=linux
  ;;
*)
  echo $syst\?
  echo warning: expecting { sunos \| freebsd \| netbsd |\ openbsd |\ linux }
  syst=sunos
esac

tmp=$usetp
case $tmp in
yes | ye | y)
  usetp=yes
  ;;
no | n)
  usetp=no
  ;;
*)
  echo $usetp \?
  echo usetp must be \'yes\' or  \'no\'
  exit 1
esac

tmp=$timing
if [ x$timing = x ]; then
  echo timing must be \'on\' or \'off\'
  exit 1
else
  case $tmp in
  on)
    ;;
  off | of)
    timing=off
    ;;
  *)
    echo $timing \?
    echo Timing must be \'on\' or \'off\'
    exit 1
  esac
fi

tmp=$debug
if [ x$debug = x ]; then
  echo Debug must be \'on\', \'off\', \'yes\' or \'no\'
  exit 1
else
  case $tmp in
  on | yes | ye | y)
    debug=on
    ;;
  off | of | no | n)
    debug=off
    ;;
  *)
    echo $debug\?
    echo Debug must be \'on\', \'off\', \'yes\' or \'no\'
    exit 1
  esac
fi

# Create Makefile from Makefile.in.
if [ ! -r Makefile.in ]; then
  echo Couldn\'t open Makefile.in in the current directory.
  exit 1
fi

# OK I have syst, size, debug and usetp now.

# Set the Build Size
case $size in
small)
  BLDSIZEDEF=-DSMALL
  OBJCDEF=\${OBJSC}
  OBJSDEF=\${OBJSS}
  ;;
medium)
  BLDSIZEDEF=-DMEDIUM
  OBJCDEF=\${OBJMC}
  OBJSDEF=\${OBJMS}
  ;;
large)
  BLDSIZEDEF=-DLARGE
  OBJCDEF=\${OBJLC}
  OBJSDEF=\${OBJLS}
  ;;
xlarge)
  BLDSIZEDEF=-DXLARGE
  OBJCDEF=\${OBJXC}
  OBJSDEF=\${OBJXS}
  ;;
*)
  echo Invalid size.
  exit 1
esac

# x$timing should never equal x here, but if it does, don't die of it.

if [ x$timing = xon ]; then
  OBJCDEF="$OBJCDEF \${OBJTIME}"
  OBJSDEF="$OBJSDEF \${OBJTIME}"
  MYFLAGSDEF=-DDO_TIMING
fi

case $syst in
sunos)
  MYFLAGSDEF="$MYFLAGSDEF -DSUNOS"
  LIBSDEF="-L./ -lm"
  ;;
openbsd)
  MYFLAGSDEF="$MYFLAGSDEF -D__BSD__"
  LIBSDEF="-L./ -lm"
  ;;
bsd)
  MYFLAGSDEF="$MYFLAGSDEF -D__BSD__"
  LIBSDEF="-L./ -lm -lcrypt"
  ;;
linux)
  MYFLAGSDEF="$MYFLAGSDEF -DLINUX"
  LIBSDEF=-L./
  ;;
esac


if [ x$usetp = xyes ]; then
  LIBSDEF="$LIBSDEF -L../lib/ -lscps"
  MYFLAGSDEF="$MYFLAGSDEF -I../include -I../source"
else
  COMMONDEF=-DNOTTP
fi


if [ x$debug = xon ]; then
  OPTDEF="-g -DDEBUG"
else
  OPTDEF=-O2
fi

# Remove all the .o files so the make recompiles everything.
# Many source files test for BLDSIZE with #ifdef, so it's not
# enough to just relink after configuring for a different size.
rm -f *.o sfp server scpsdiff crcchk

rm -f makefile.in1
cat Makefile.in  | sed "s+^OPT=+OPT= $OPTDEF+" > makefile.in1
cat makefile.in1 | sed "s+^COMMON=+COMMON= $COMMONDEF+" > makefile.in2
cat makefile.in2 | sed "s+^MYFLAGS=+MYFLAGS= $MYFLAGSDEF+" > makefile.in1
cat makefile.in1 | sed "s+^DEBUG=+DEBUG= $DEBUGDEF+" > makefile.in2
cat makefile.in2 | sed "s+^BLDSIZE=+BLDSIZE= $BLDSIZEDEF+" > makefile.in1
cat makefile.in1 | sed "s+^LIBS=+LIBS= $LIBSDEF+" > makefile.in2
cat makefile.in2 | sed "s+^OBJC=+OBJC= $OBJCDEF+" > makefile.in1
cat makefile.in1 | sed "s+^OBJS=+OBJS= $OBJSDEF+" > makefile.in2
mv makefile.in2 Makefile

# Remove temporary files
rm -f makefile.in1 makefile.in2

echo Created \"Makefile\"
if [ x$usetp = xyes ]; then
echo "system=$syst  size=$size  debug=$debug  Using TP sockets"
else
echo "system=$syst  size=$size  debug=$debug  Using Operating System sockets"
fi
exit 0
