#!/bin/sh
# $Id: configure,v 1.19 1996/06/25 21:30:57 deaven Exp $
# Copyright (c)1995 David Deaven

ARGS="$*"
ARG1="$1"
ECHO="echo"
use_prompts="1"
target=""
arch="__unknown_arch__"

for uc in `uname` `uname -m`
do
  case $uc in
    NetBSD)
	  define="__NetBSD__"
	  case `uname -m` in 
	    i386)
	      target="NetBSD/i386"
	      arch="__i386__"
	      ;;
	    *)
	      target="NetBSD"
	      ;;
	    esac
	  ;;

    AIX)
	  target="AIX"
	  define="__AIX__"
	  ;;

    Paragon)
	  target="i860"
	  define="__I860__"
	  ECHO="/usr/ucb/echo"
	  ;;

    IRIX)
	  target="SGI"
	  define="__sgi__"
	  ;;
     
    SunOS)
	  target="Sun"
	  define="__SUN__"
	  ;;
     
    HP-UX)
	  target="HP-UX"
	  define="__HP_UX__"
	  if [ "$ARG1" != "NO" ]
	  then
	     exec /bin/ksh configure NO $ARGS
	  fi
	  ;;
     
    ULTRIX)
	  target="Ultrix"
	  define="__ULTRIX__"
	  if [ "$ARG1" != "NO" ]
	  then
	     exec /usr/bin/ksh configure NO $ARGS
	  fi
	  ;;

    CRAY | C90)
	  target="CRAY"
	  define="__CRAY__"
	  ;;

  esac
done

if [ "X$target" = "X" ]
then
  target="GENERIC"
  define="__unix__"
fi

getresp() {
	if [ $use_prompts = "1" ]
	then
	  read resp
	  if [ "X$resp" = "X" ]; then
		  resp=$1
	  fi
	else
	  $ECHO "<default>"
	  resp=$1
	fi
}

checkdir() {
  if [ -d $1 ]
  then
    $ECHO "Using existing $1 directory"
  else
    $ECHO -n "Directory $1 does not exist.  Create? [y] "
    getresp "y"
    if [ "$resp" = "y" ]
    then
      $ECHO "Creating $1 directory"
      mkdir $1
      if [ "X$?" != "X0" ]
      then
	$ECHO "Cannot create $1"
	exit 1
      fi
    else
      $ECHO "Stopping."
      exit 1
    fi
  fi
}

# Begin the configuration.

installdir=""
for arg in $ARGS
do
  case $arg in
  -n)
    use_prompts="0"
    ;;

  --installdir=*)
    installdir=`echo $arg | sed 's/--installdir=//'`
    ;;
  
  ncube)
    target="nCube (cross-compile)"
    define="__nCube__"
    ;;

  NO)
    ;;

  *)
    echo "Unrecognized argument $arg"
    exit 1
    ;;
  esac
done

$ECHO "Configuring for $target"

if [ "X$installdir" = "X" ]
then
  if [ -r install-path ]
    then
      installdir=`cat install-path`
      if [ ! -d $installdir ]
	then
	  installdir=$HOME
      fi
  else
    installdir=$HOME
  fi
  $ECHO "This library requires a fixed installation directory, under which"
  $ECHO "several subdirectories with standard names (bin, lib, man, include)"
  $ECHO "will be created (unless they already exist).  Your home directory"
  $ECHO "is the recommended installation point."
  which
  $ECHO -n "Installation directory? [$installdir] "
  getresp "$installdir"
  installdir="$resp"
fi

# We want an absolute pathname for the installation directory.
curdir=`pwd`
cd $installdir
installdir=`pwd`
cd $curdir

checkdir $installdir
checkdir $installdir/include
checkdir $installdir/include/dmd
checkdir $installdir/lib
checkdir $installdir/bin
checkdir $installdir/man
checkdir $installdir/man/cat1
checkdir $installdir/man/man1
checkdir $installdir/man/cat3
checkdir $installdir/man/man3
checkdir $installdir/man/cat5
checkdir $installdir/man/man5
checkdir $installdir/classes
checkdir $installdir/classes/deaven

$ECHO $installdir > install-path

# The `dmdpp' program is a very simple, portable implementation of
# cpp-like preprocessing.
# If we are on my development machine, an extra make variable is
# defined --dmd.
$ECHO "Making dmdpp"
if [ `hostname` = "ishmael.ameslab.gov" ]
then
  development="1"
else
  development="0"
fi
cc -o dmdpp dmdpp.c             \
  -DDEVELOPMENT=$development    \
  -DSYSTEM=\"$define\"          \
  -DARCH=\"$arch\"              \
  -DDMDDIR=\"$installdir\"

if [ "X$?" != "X0" ]
then
  $ECHO "error -- default C compiler cannot compile dmdpp.c"
  exit 1
fi
$ECHO "Creating dmdconfig script"
./dmdpp dmdconfig.src > dmdconfig
chmod 755 dmdconfig

$ECHO "Making the Makefile"
if [ -r Makefile ]
then
  rm -f Makefile
fi
./dmdpp Makefile.mk > Makefile

$ECHO "Making clean depend"
make clean
make depend
rm -f headstamp

$ECHO "Configuring in ./doc"
cd doc
../dmdpp Makefile.mk > Makefile
cd ..

$ECHO "Done.  Type \`make' to make the library, \`make install' to install."
$ECHO "To install just the library, type \`make libinstall', or to install"
$ECHO "just the manual pages, \`make maninstall'."
$ECHO "To configure other pacakages that require libdmd, use"
$ECHO "the script $installdir/bin/dmdconfig"

