#! /bin/sh
# file: configure             G. Moody               24 May 2000
#                             Last revised:         18 July 2002
# Configuration script for the WFDB Software Package

# This script was not generated using 'autoconf'.  If you can implement
# autoconf input files for a 'configure' that does what this one does,
# please send them to me (george@mit.edu) for inclusion in a future
# version of this package.

if [ $# -gt 0 ]
then
    DIR=/usr
    LIBTYPE=dynamic
    SYSLIBS=dynamic
fi
 
for i in $*
do
  case $i in
    --dynamic)     LIBTYPE=dynamic ;;
    --prefix=*)    DIR=`echo $i | sed 's/[-a-zA-Z0-9]*=//'` ;;
    --shared)      LIBTYPE=dynamic ;;
    --static)      LIBTYPE=static ;;
    --static_only) LIBTYPE=static
		   SYSLIBS=static ;;
    *)             cat <<EOF
usage: $0 [options]
Options:
    --dynamic         make a shared (dynamically linkable) WFDB library
                       if possible [default]
    --help            print this message
    --prefix=PREFIX   install files in PREFIX [default: $DIR]
    --shared          (same as --dynamic)
    --static          make a static WFDB library (do not make a shared
                       library even if possible)
    --static_only     link WFDB applications to static libraries only
                       (assumes --static;  compilation may fail if any
	               necessary libraries are available as shared libraries
		       only)
EOF
                   exit ;;
  esac
done

echo "Checking the operating system type ..."
OS=`uname`
echo "Configuring the WFDB software package for $OS."

echo "Checking the echo command ..."
echo -n >echo.out
if [ -s echo.out ]
then
ln -sf prompt-c conf/prompt
else
ln -sf prompt-n conf/prompt
fi
rm echo.out

cp conf/version.def conf/site.def

echo "# Definitions generated by 'configure'" >>conf/site.def
echo >>conf/site.def
PWD=`pwd`
PACKAGE=`basename $PWD`
echo "PACKAGE = $PACKAGE" >>conf/site.def
echo LONGDATE = `date '+%e %B %Y'` >>conf/site.def
echo SHORTDATE = `date '+%B %Y' | tr a-z A-Z` >>conf/site.def
echo "# _____________________________________________________________________________" >>conf/site.def
echo >>conf/site.def

cp /dev/null conf/site-slib.def
case `uname` in
  Linux*) OS=Linux
    cat conf/linux.def >>conf/site.def
    cat conf/version.def conf/linux-slib.def >conf/site-slib.def ;;
  FreeBSD*) OS=FreeBSD
    cat conf/freebsd.def >>conf/site.def
    cat conf/version.def conf/freebsd-slib.def >conf/site-slib.def ;;
  SunOS*) OS=Solaris
    cat conf/solaris.def >>conf/site.def
    cat conf/version.def conf/solaris-slib.def >conf/site-slib.def
    echo "It is normal, and harmless, for Solaris to be recognized as"
    echo "SunOS." ;;
  HP-UX*) OS=HPUX
    cat conf/hpux.def >>conf/site.def
    cat conf/version.def conf/hpux-slib.def >conf/site-slib.def ;;
  CYGWIN*) OS=cygwin
    cat conf/cygwin.def >>conf/site.def ;;
  *)      OS=generic
    cat conf/generic.def >>conf/site.def
    cat conf/version.def conf/generic-slib.def >conf/site-slib.def
    echo "Using the generic configuration, which may need to be customized"
    echo "for your operating system.  'configure' will offer suggestions for"
    echo "customization after it completes the generic configuration." ;;
esac

if [ -s conf/site-slib.def ]
then
  if [ x$LIBTYPE = x ]
  then
    echo
    echo "The WFDB library may be compiled as a static library or as a"
    echo "dynamically loaded (shared) library (the default).  Type S for a"
    conf/prompt "static library, or D for a dynamically loaded library: "
    read LIBTYPE
  fi
else
    LIBTYPE=static
fi

case x$LIBTYPE in
  xS*|xs*) cp /dev/null conf/site-slib.def
           LIBTYPE=static ;;
        *) LIBTYPE=dynamic ;;
esac

if [ $LIBTYPE = static ]
then
    if [ x$SYSLIBS = x ]
    then
	echo
	echo "You may link the WFDB applications to dynamically loaded"
	echo "system libraries (such as the standard C library) to"
	echo "save disk space and for faster loading even while using"
	echo "the static WFDB library, or you may link them to static"
	echo "libraries only, which will make them easier to copy to"
	echo "other systems.  Note that choosing to link to static"
	echo "libraries only may cause compilation to fail if your"
	echo "system does not have a complete set of static libraries."
	echo "Type S to link to static libraries only, or D to link to"
	conf/prompt "dynamically loaded libraries where available: "
	read SYSLIBS
    fi
    case x$SYSLIBS in
        xS*|xs*) sed "s/LDFLAGS = -L/LDFLAGS = -static -L/" \
		     <conf/site.def >conf/site.tmp
		 mv conf/site.tmp conf/site.def
		 echo "Applications will be linked with static libraries only."
		 ;;
	      *) ;;
    esac
fi

echo
conf/prompt "Looking for the C compiler ..."
if ( which gcc 2>&1 | grep "no \|not " >/dev/null )
then
    if (which cc 2>&1 | grep "no \|not " >/dev/null )
    then
        echo
        echo "The WFDB software cannot be compiled, because there does not"
        echo "appear to be a C compiler installed on this system.  Please"
        echo "install one, be sure that it is in your PATH, and that it is"
	echo "accessible as either 'cc' or 'gcc', then run ./configure again."
	exit
    else
        sed "s/CC = gcc/CC = cc/" <conf/site.def >conf/site.tmp
	mv conf/site.tmp conf/site.def
        sed "s/CC = gcc/CC = cc/" <conf/site-slib.def >conf/site.tmp
	mv conf/site.tmp conf/site-slib.def
	CC=cc
    fi
else
    CC=gcc
fi

echo "$CC"

if [ x$DIR = x ]
then
    echo
    echo "Where do you wish to install the WFDB Software Package?"
    echo "It will be installed in subdirectories of WFDBROOT (by default,"
    echo "WFDBROOT is /usr, so that files are installed in /usr/bin,"
    echo "/usr/include, /usr/lib, etc.).  Other reasonable choices for"
    echo "WFDBROOT are /usr/local, /opt, or your home directory ($HOME)."
    echo "The value of WFDBROOT must be a full pathname (beginning with '/',"
    echo "but without a '/' at the end).  If the subdirectories (bin, include,"
    echo "lib, ...) of WFDBROOT don't exist, they will be created as necessary."
    echo
    DIR=
    while [ x$DIR = x ]
    do
        conf/prompt "Choose a location, or press <Enter> to install in /usr: "
        read DIR
        case x$DIR in
            x/usr) ;;
                x) DIR=/usr ;;
              x*/) DIR=
                   echo "Please type the path again, omitting the '/' at the end." ;;
              x/*) ;;
               x*) DIR=
                   echo "Please enter an absolute pathname, beginning with '/'." ;;
        esac
    done
fi

echo  "The WFDB software will be installed in '$DIR'."

case $DIR in
  /usr) ;;
    /*) sed "s+^WFDBROOT = /usr+WFDBROOT = $DIR+" <conf/site.def >conf/site.tmp
        mv conf/site.tmp conf/site.def
        sed "s+^WFDBROOT = /usr+WFDBROOT = $DIR+" <conf/site-slib.def \
         >conf/site.tmp
        mv conf/site.tmp conf/site-slib.def
esac
 
echo
echo "Looking for libwww ..."
if ( which libwww-config 2>&1 | grep "no \|not " >/dev/null )
then
    echo "The WFDB software will be compiled without NETFILES access, because"
    echo "libwww does not appear to be installed on this system."
    echo "To add NETFILES access, install libwww and run ./configure again."
    echo "NETFILES=0" >config.cache
    sed "s/LWC = libwww-config/LWC = :/" <conf/site.def >conf/site.tmp
    mv conf/site.tmp conf/site.def
    sed "s/LWC = libwww-config/LWC = :/" <conf/site-slib.def >conf/site.tmp
    mv conf/site.tmp conf/site-slib.def
    sed "s/WFDB_NETFILES 1/WFDB_NETFILES 0/" <lib/wfdb.h >lib/wfdb.tmp
    mv lib/wfdb.tmp lib/wfdb.h
    WITHNF=without
else
    echo "The WFDB software will be compiled with NETFILES access."
    echo "NETFILES=1" >config.cache
    sed "s/LWC = :/LWC = libwww-config/" <conf/site.def >conf/site.tmp
    mv conf/site.tmp conf/site.def
    if [ $LIBTYPE = "static" ]
    then
        sed "s/LDLIBWWW = #/LDLIBWWW =/" <conf/site.def >conf/site.tmp
	mv conf/site.tmp conf/site.def
    fi
    sed "s/LWC = :/LWC = libwww-config/" <conf/site-slib.def >conf/site.tmp
    mv conf/site.tmp conf/site-slib.def
    sed "s/WFDB_NETFILES 0/WFDB_NETFILES 1/" <lib/wfdb.h >lib/wfdb.temp
    mv lib/wfdb.temp lib/wfdb.h
    WITHNF=with
fi

echo
echo "Creating Makefile in lib ..."
if [ -s conf/site-slib.def ]
then
    cat lib/Makefile.top conf/site-slib.def lib/Makefile.tpl >lib/Makefile
else
    cat lib/Makefile.top conf/site.def lib/Makefile.tpl >lib/Makefile
fi
for D in app checkpkg convert data doc doc/wag-src doc/wpg-src doc/wug-src \
         examples fortran psd wave waverc .
do
   if [ -s $D/Makefile.top ]
   then
       echo "Creating Makefile in $D ..."
       cat $D/Makefile.top conf/site.def $D/Makefile.tpl >$D/Makefile
   fi
done

rm conf/site.def conf/site-slib.def

echo
echo "Adding $DIR/database to the default WFDB path ..."
sed "s+DBDIR+$DIR/database+" <lib/wfdblib.h0 >lib/wfdblib.h

echo
echo "Looking for the XView libraries ..."
if [ -d /usr/openwin ]
then
    WAVE=1;
elif [ -d /usr/local/openwin ]
then
    WAVE=1;
    sed "s+/usr/openwin+/usr/local/openwin+" <wave/Makefile >tmp.$$
    mv tmp.$$ wave/Makefile
elif [ -d /opt/openwin ]
then
    WAVE=1;
    sed "s+/usr/openwin+/opt/openwin+" <wave/Makefile >tmp.$$
    mv tmp.$$ wave/Makefile
elif ( which textedit 2>&1 | grep "no \|not " >/dev/null )
then
    WAVE=0;
else
    WAVE=1;
    TEXTEDIT=`which textedit`;
    TEDIR=`dirname $TEXTEDIT`;
    OWHOME=`dirname $TEDIR`;
    sed "s+/usr/openwin+$OWHOME+" <wave/Makefile >tmp.$$
    mv tmp.$$ wave/Makefile
fi
if [ $WAVE = 0 ]
then
    echo "WAVE will not be compiled, because the XView libraries do not"
    echo "appear to be installed on this system."
    echo "To compile WAVE, install XView, add the directory containing the"
    echo "XView textedit application to your PATH, and run ./configure again."
    cp -p wave/nomake wave/Makefile
    cp -p wave/nomake waverc/Makefile
    echo "WAVE=0" >>config.cache
    WAVECOMP="WAVE will not be compiled."
else
    WAVECOMP="WAVE will be compiled and installed in '$DIR/bin'."
    echo "WAVE=1" >>config.cache
fi

# Clean up old *.o files in lib, to avoid possibly using binaries intended
# for a shared library in a static library or vice versa.
rm -f lib/*.o

if [ $DIR != "/usr" -a $LIBTYPE = dynamic ]
then
    echo
    echo "IMPORTANT:  You have chosen to build a dynamic (shared) WFDB"
    echo "library in a directory, $DIR/lib, that is not normally"
    echo "searched for such libraries.  In order to use the applications"
    echo "that will be compiled with this library, it may be necessary to"
    echo "add this directory to your LD_LIBRARY_PATH.  If your shell is csh"
    echo "or a variant, do this by typing:"
    case "x$LD_LIBRARY_PATH" in
      x) echo "  setenv LD_LIBRARY_PATH $DIR/lib"
         echo "Otherwise, do this by typing:"
         echo "  LD_LIBRARY_PATH=$DIR/lib"
         echo "  export LD_LIBRARY_PATH" ;;
      *) echo "  setenv LD_LIBRARY_PATH \${LD_LIBRARY_PATH}:$DIR/lib"
         echo "Otherwise, do this by typing:"
         echo "  LD_LIBRARY_PATH=\${LD_LIBRARY_PATH}:$DIR/lib"
         echo "  export LD_LIBRARY_PATH" ;;
    esac
fi

if [ $OS = generic ]
then
    echo
    echo "No specific configuration information is available for your"
    echo "operating system.  Please try compiling using the generic Makefiles"
    echo "that have been generated by this configuration utility.  If there"
    echo "are problems, it may be possible to solve them by customizing"
    echo "conf/generic.def (and conf/generic-slib.def if you are attempting"
    echo "to compile a shared WFDB library).  Run ./configure again after"
    echo "making any necessary changes to these files.  Please send reports"
    echo "of success or failure, together with the output of the command"
    echo "'uname -a', and copies of any files you have changed, to:"
    echo "        george@mit.edu"
fi


cat <<EOF

Summary:

$PACKAGE-$OS is now ready to be compiled using '$CC'.
The WFDB library will be compiled as a $LIBTYPE library $WITHNF NETFILES
 access, and it will be installed in '$DIR/lib'.
The WFDB library .h files will be installed in '$DIR/include/wfdb'.
The WFDB applications will be linked to $SYSLIBS system libraries, and
 they will be installed in '$DIR/bin'.
$WAVECOMP
Manual pages will be installed in subdirectories of '/usr/local/man'
 (change MANDIR in 'doc/Makefile' if you prefer another location).
Sample data files will be installed in '$DIR/database', which
 will be included in the default WFDB path.

WFDB software configuration is complete.
Now run 'make', followed by 'make install'.

EOF
