#!/bin/sh
#    This file is part of the FElt finite element analysis package.
#    Copyright (C) 1993 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.

elements="truss beam beam3d CSTPlaneStrain CSTPlaneStress iso2d_PlaneStrain\
	  iso2d_PlaneStress quad_PlaneStrain quad_PlaneStress"

prompt() {
    answer=
    default=
    read default
    if [ $# -lt 3 -o "$3" != "" ]; then
	if [ $echostyle -eq 0 ]; then
	    echo -n $1 [$default] "? "
	else
	    echo $1 [$default] "? "\\c		# for stupid HP machines
	fi
	read answer < /dev/tty
	: ${answer:=$default}
    fi
    if [ "$answer" = none ]; then
	answer=
    fi
    eval "$2=\"$answer\""
    echo $answer >> .config.in
}


# add a flag to each of the arguments

addflags() {
    flagged=
    for arg in $3
    do
	case $arg in
	$2*)
	    flagged="$flagged $arg";;
	*)
	    flagged="$flagged $2$arg";;
	esac
    done
    eval "$1=\"$flagged\""
}


# find a program in your path

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


# 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
}


# if a config.in file exists then use it, otherwise create one

if [ ! -f config.in ]; then
   which CC gcc
   which FC f77
   findfile DIR1 "/usr/lib/X11/app-defaults /usr/X11/lib/app-defaults\
		  /usr/X386/lib/X11/app-defaults /usr/lib/X11R5/app-defaults"
   findfile DIR2 "/usr/include/X11R5/X11 /usr/X11/include/X11\
		  /usr/X386/include/X11"
   findfile DIR3 "/usr/lib/X11R5 /usr/lib/X11 /usr/X11/lib /usr/X386/lib/X11"
   findfile DIR4 "/usr/remote/bin /usr/local/bin /usr/bin"
   findfile DIR5 "/usr/remote/man /usr/local/man /usr/man"

   if [ "$DIR2" != "" ]; then
	DIR2=`dirname $DIR2`
   fi

   CC=`basename $CC`
   FC=`basename $FC`
   : ${CC:=cc}

   cat > config.in << --EOF--
$CC
-O

$FC
-O

$DIR1
$DIR2
$DIR3

$DIR4
$DIR5
--EOF--
fi

exec < config.in

rm -f .config.in
echostyle=`echo -n | wc -w`

prompt "What is your C compiler" CC
prompt "What are the compile options for $CC" CCOPTS
prompt "What are the link options for $CC" LDOPTS
echo ""
prompt "What is your Fortran compiler" FC
prompt "What are the compile options for $FC" FCOPTS "$FC"
prompt "What libraries are required by $FC" FLIBS "$FC"
echo ""
prompt "Where are your X11 application defaults" XDEFDIR
prompt "Where are your X11 include files" XINCDIR
prompt "Where are your X11 libraries" XLIBDIR
prompt "What other libraries are needed by X applications" XLIBS
echo ""
prompt "Destination directory for binaries" BINDIR
prompt "Destination directory for man pages" MANDIR

mv -f .config.in config.in


# finish setting up the options

if [ -x /bin/ranlib -o -x /usr/bin/ranlib ]; then
    RANLIB=ranlib
else
    RANLIB=true
fi

addflags FLIBS    -l "$FLIBS"
addflags XCFLAGS  -I "$XINCDIR"
addflags XLDFLAGS -L "$XLIBDIR"
addflags XLIBS    -l "$XLIBS"


# place the f2c'd files in the appropriate directory

if [ "$FC" = "" ]; then
    ln -f ../lib/Geompack/F2C/*.c ../lib/Geompack 2> /dev/null
else
    rm -f ../lib/Geompack/*.c
fi


# create the Makefile.conf file

cat > ../Makefile.conf << --EOF--
AR       = ar clq
BINDIR   = $BINDIR
CC       = $CC
CCOPTS   = $CCOPTS
FC       = $FC
FCOPTS   = $FCOPTS
FLIBS    = $FLIBS
LD       = $CC
LDOPTS   = $LDOPTS
LEX      = flex
LFLAGS   =
MANDIR   = $MANDIR
RANLIB   = $RANLIB
SHELL    = /bin/sh
SYSLIBS  = $SYSLIBS
XCFLAGS  = $XCFLAGS
XDEFDIR  = $XDEFDIR
XLDFLAGS = $XLDFLAGS
XLIBS    = -lXaw -lXmu -lXt -lX11 -lXext $XLIBS
YACC     = yacc
YFLAGS   = -d
--EOF--


# create the element.h file

exec > ../inc/element.h

echo "# ifndef _ELEMENT_H"
echo "# define _ELEMENT_H"
echo "# include \"fe.h\""
echo "# include \"error.h\""
echo ""

num=0
for elt in $elements
do
    echo "# define ${elt}Type $num"
    echo "extern struct definition ${elt}Definition;"
    echo "extern int ${elt}EltStiffness ( );"
    echo "extern int ${elt}EltStress ( );"
    echo ""
    num=`expr $num + 1`
done

echo "# ifdef ELEMENTS"
echo "static struct {"
echo "    char      *name;"
echo "    int      (*stiffness) ( );"
echo "    int      (*stress) ( );"
echo "    Definition definition;"
echo "} ElementArray [ ] = {"
for elt in $elements
do
  echo "    {\"${elt}\", ${elt}EltStiffness, ${elt}EltStress, &${elt}Definition},"
done
echo "    {0, 0, 0, 0}"
echo "};"
echo "# endif"
echo ""

echo "# endif /* _ELEMENT_H */"


# make a simple stdlib.h if one does not exist

if [ ! -f /usr/include/stdlib.h ]; then
    exec > ../inc/stdlib.h
    echo "double atof ( );"
    echo "double strtod ( );"
    echo "long   strtol ( );"
fi

exit 0
