#!/bin/sh
#    This file is part of the FElt finite element analysis package.
#    Copyright (C) 1993-1995 Jason I. Gobat and Darren C. Atkinson
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


# find a program in your path

which() {
    IFS=:
    for dir in $PATH; do
	if [ -x $dir/$2 -a ! -d $dir/$2 ]; then
	    eval "$1=$dir/$2"
	    break
	fi
    done
    IFS=" "
}


# find a file in a list

findfile() {
    for file in $2; do
	if [ -f $file -o -d $file ]; then
	    eval $1=$file
	    break
	fi
    done
}


# parse a command line option

parse() {
    eval $1="`echo \"$2\" | sed -e 's/^[^=]*=//'`"
}


# print help information and exit

help() {
    echo "usage: configure [options]"
    echo "  --contrib           build contributed elements [no]"
    echo "  --prefix=dir        destination directory prefix [$DEST]"
    echo "  --cc=prog           C compiler [$CC]"
    echo "  --cpp=prog          C preprocessor [$CPP]"
    echo "  --ccopts=options    C compiler options [$CCOPTS]"
    echo "  --syslibs=libs      system dependent libraries [$SYSLIBS]"
    echo "  --x-defaults=dir    X11 defaults directory [$X11DEF]"
    echo "  --x-includes=dir    X11 include directory [$X11INC]"
    echo "  --x-libraries=dir   X11 library directory [$X11LIB]"
    exit 0
}


# try to find the defaults

echo Configuring ...

which CC gcc
which RANLIB ranlib
which CAT cat
which TRUE true

findfile DEST "/usr/felt \
	       /usr/local \
	       /usr/contrib \
	       /usr"

findfile READDIR "/lib/libreadline.a \
		  /usr/lib/libreadline.a \
		  /usr/local/lib/libreadline.a \
		  /usr/gnu/lib/libreadline.a \
		  /opt/gnu/lib/libreadline.a"

findfile CPP "/lib/cpp \
	      /usr/lib/cpp \
	      /usr/ccs/lib/cpp \
	      /usr/lang/cpp \
	      /usr/bin/cpp \
	      /bin/cpp"

findfile X11DEF "/usr/X11R6/lib/X11/app-defaults \
		 /usr/X11R5/lib/X11/app-defaults \
		 /usr/X11R4/lib/X11/app-defaults \
		 /usr/X386/lib/X11/app-defaults \
		 /usr/X11/lib/X11/app-defaults \
		 /usr/X11R6/lib/app-defaults \
		 /usr/X11R5/lib/app-defaults \
		 /usr/X11R4/lib/app-defaults \
		 /usr/X386/lib/app-defaults \
		 /usr/X11/lib/app-defaults \
		 /usr/lib/X11R6/app-defaults \
		 /usr/lib/X11R5/app-defaults \
		 /usr/lib/X11R4/app-defaults \
		 /usr/lib/X386/app-defaults \
		 /usr/lib/X11/app-defaults \
		 /usr/openwin/lib/app-defaults"

findfile X11INC "/usr/X11R6/include \
		 /usr/X11R5/include \
		 /usr/X11R4/include \
		 /usr/X386/include \
		 /usr/X11/include \
		 /usr/include/X11R6 \
		 /usr/include/X11R5 \
		 /usr/include/X11R4 \
		 /usr/include/X11 \
		 /usr/openwin/include"

findfile X11LIB "/usr/X11R6/lib \
		 /usr/X11R5/lib \
		 /usr/X11R4/lib \
		 /usr/X386/lib \
		 /usr/X11/lib \
		 /usr/lib/X11R6 \
		 /usr/lib/X11R5 \
		 /usr/lib/X11R4 \
		 /usr/lib/X11 \
		 /usr/openwin/lib"


# provide worst-case defaults

: ${CC:=cc}
: ${RANLIB:=$TRUE}
: ${READDIR:=/lib}
: ${DEST}:=.}
: ${CPP:=$CAT}
: ${X11DEF:=.}
: ${X11INC:=.}
: ${X11LIB:=.}

CC=`basename $CC`
RANLIB=`basename $RANLIB`
READDIR=`dirname $READDIR`
CONTRIBDIR=none

if [ $CC = gcc ]; then CCOPTS=-O3; else CCOPTS=-O; fi


# defaults for known architectures that fail the above

case `uname -rs` in
    HP-UX*)	RANLIB=true;;
    SunOS?5.*)	CCOPTS="$CCOPTS -D_XOPEN_SOURCE";;
    Irix*)	if [ $CC = cc ]; then CCOPTS="$CCOPTS -Wf,-XNl16384"; fi;;
esac


# parse command line arguments

for arg in $*; do
    case $arg in
	-h | --help)	 help;;

	--cc=*)		 parse CC $arg;;
	--cpp=*)	 parse CPP $arg;;
	--prefix=*)	 parse DEST $arg;;
	--ccopts=*)	 parse CCOPTS $arg;;
	--syslibs=*)	 parse SYSLIBS $arg;;
	--x-defaults=*)	 parse X11DEF $arg;;
	--x-includes=*)	 parse X11INC $arg;;
	--x-libraries=*) parse X11LIB $arg;;
	--readline=*)	 parse READDIR $arg;;

	CFLAGS=*)	 parse CCOPTS $arg;;

	--contrib)	 CONTRIBFLAGS=-DCONTRIB; CONTRIBDIR=contrib;;
	--contrib=y)	 CONTRIBFLAGS=-DCONTRIB; CONTRIBDIR=contrib;;
	--contrib=yes)	 CONTRIBFLAGS=-DCONTRIB; CONTRIBDIR=contrib;;
    esac
done


# determine the readline library options

if [ -f $READDIR/libreadline.a ]; then
    READLINE=-DREADLINE
    READLIBS="-L$READDIR -lreadline -ltermcap"
else
    READDIR=none
    READLINE=
    READLIBS=
fi


# print defaults

echo "  C compiler                 =" $CC
echo "  C compiler options         =" $CCOPTS
echo "  C preprocessor             =" $CPP
echo "  ranlib program             =" $RANLIB
echo "  destination prefix         =" $DEST
echo "  readline library directory =" $READDIR
echo "  X11 application defaults   =" $X11DEF
echo "  X11 include directory      =" $X11INC
echo "  X11 library directory      =" $X11LIB


# create Makefile.conf

cat > ./etc/Makefile.conf << EOF
AR		= ar
CC		= $CC
CCOPTS		= $CCOPTS
CONTRIBDIR	= $CONTRIBDIR
CONTRIBFLAGS	= $CONTRIBFLAGS
CPP		= $CPP
DESTBIN		= $DEST/bin
DESTLIB		= $DEST/lib/felt
DESTMAN		= $DEST/man
INSTALL		= sh \$(TOPDIR)/etc/install.sh -c
LDOPTS		=
LIBELT		= \$(TOPDIR)/lib/Elements/libelt.a
LIBFELT		= \$(TOPDIR)/lib/Felt/libfelt.a
LIBGEN		= \$(TOPDIR)/lib/Generate/libgen.a
LIBGEOMPACK	= \$(TOPDIR)/lib/Geompack/libgeompack.a
LIBMTX		= \$(TOPDIR)/lib/Matrix/libmtx.a
LIBWIDGETS	= \$(TOPDIR)/lib/Widgets/libwidgets.a
MKDIR		= sh \$(TOPDIR)/etc/mkdirhier.sh
RANLIB		= $RANLIB
READLINE	= $READLINE
READLIBS	= $READLIBS
RM		= rm -f
SYSLIBS		=
X11DEF		= $X11DEF
X11INC		= $X11INC
X11LIB		= $X11LIB
EOF

exit 0
