#! /bin/sh
# file: configure             G. Moody         24 May 2000
#                             Last revised:   6 October 2010
# 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.

# Note that any -m options (although parsed below) are currently ignored,
# except on x86_64 Linux, where they are prefixed to CFLAGS.  The help
# text below documents only -m32;  see the gcc manual for other -m options.

# Defaults (overridden by --interactive):
INTERACTIVE=no
LIBTYPE=dynamic
DIR=/usr
MANDIR=unknown

NETLIB=unknown
WAVE=unknown

# Read and process command-line arguments
for i in $*
do
  case $i in
    --dynamic)          LIBTYPE=dynamic ;;
    -i|--interactive)   INTERACTIVE=yes
    			LIBTYPE=
			DIR=
			MANDIR= ;;
    -m*)                if [ "x$MFLAGS" = x ]
                        then
		            MFLAGS=$i
		        else
		            MFLAGS="$MFLAGS $i"
		        fi ;;
    --mandir=*)         MANDIR=`echo $i | sed 's/[-a-zA-Z0-9]*=//'` ;;
    --prefix=*)         DIR=`echo $i | sed 's/[-a-zA-Z0-9]*=//'` ;;
    --shared)           LIBTYPE=dynamic ;;
    --static)           LIBTYPE=static ;;
    --static_only)      LIBTYPE=static
		        SYSLIBS=static ;;
    --without-cygwin)   NOCYGWIN=yes ;;
    --without-netfiles) NETLIB=none ;;
    --with-libcurl)     NETLIB=libcurl ;;
    --with-libwww)      NETLIB=libwww ;;
    *)                  cat <<EOF
usage: $0 [options]
Options:
    --dynamic           make a shared (dynamically linkable) WFDB library
                         if possible [default]
    --help              print this message
    --interactive       ask about library type and installation directories
    -m32                on 64-bit Linux, force compilation in 32-bit mode
    --mandir=MANDIR     install man pages in MANDIR
    --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)
    --without-cygwin    on Cygwin, compile binaries that do not link to the
                         Cygwin DLL. WAVE will not be built.
    --without-netfiles  disable NETFILES even if libcurl or libwww is available
    --with-libcurl      enable NETFILES via libcurl [default]
    --with-libwww       enable NETFILES via libwww
EOF
                        exit ;;
  esac
done

echo "Checking the operating system type ..."
CPUOS=`uname -m`-`uname -s`
ARCH=`echo $CPUOS | tr -d " "`
case $OS in
 mingw*) 
esac
case `uname` in
  Linux*)    case `uname -m` in
	        x86_64) OS=Linux-64 ;;
                *) OS=Linux ;;
             esac ;;
  FreeBSD*)  OS=FreeBSD ;;
  Solaris*)  OS=Solaris ;;
  SunOS*)    OS=Solaris ;;
  HP-UX*)    OS=HPUX ;;
  CYGWIN*)   if [ "x$NOCYGWIN" = "xyes" ]
             then
	         OS=mingw-compat
		 T=`echo $ARCH | sed s/CYGWIN/MINGW/`
		 ARCH=$T
             else
	         OS=cygwin
             fi ;;
  MINGW*)    OS=mingw
             T=`echo $ARCH | sed s/CYGWIN/MINGW/`
             ARCH=$T ;;
  Darwin*)   OS=Darwin ;;
  *)         OS=generic ;;
esac
echo "Configuring the WFDB software package for $OS."

SRCDIR="`pwd`"
LIB=lib
cd conf

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

PACKAGE=`basename "$SRCDIR"`
MAJOR=
MINOR=
RELEASE=
case $PACKAGE in
 wfdb-*)  MAJOR=`echo $PACKAGE | cut -c6- | cut -d . -f 1`
	  MINOR=`echo $PACKAGE | cut -c6- | cut -d . -f 2`	  
	  RELEASE=`echo $PACKAGE | cut -c6- | cut -d . -f 3` ;;
 *)       ./prompt "Major version number: "; read MAJOR
          ./prompt "Minor version number: "; read MINOR
          ./prompt "Release number: "; read RELEASE ;;
esac

if [ "x$MAJOR" = "x" ]; then MAJOR=0; fi
if [ "x$MINOR" = "x" ]; then MINOR=0; fi
if [ "x$RELEASE" = "x" ]; then RELEASE=0; fi

sed s/VMAJ/$MAJOR/ <version.def | sed s/VMIN/$MINOR/ | \
 sed s/VREL/$RELEASE/ >site.def
echo "# Definitions generated by 'configure'" >>site.def
echo >>site.def
echo "PACKAGE = $PACKAGE" >>site.def
echo "SRCDIR = \"$SRCDIR\"" >>site.def
echo LONGDATE = `date '+%e %B %Y'` >>site.def
echo SHORTDATE = `date '+%B %Y' | tr a-z A-Z` >>site.def
echo "ARCH = $ARCH" >>site.def

echo "# _____________________________________________________________________________" >>site.def
echo >>site.def

rm -f site-slib.def
touch site-slib.def

case $OS in
  Linux)
	cat site.def linux-slib.def >site-slib.def
	cat linux.def >>site.def ;;
  Linux-64)
	if [ "x$MFLAGS" = "x-m32" ]
	then
	    OS=Linux
	    cat site.def linux-slib.def | \
		sed "s+MFLAGS =+MFLAGS = $MFLAGS+" >site-slib.def
	    cat linux.def | \
		sed "s+MFLAGS =+MFLAGS = $MFLAGS+" >>site.def
	else
	    LIB=lib64
            WAVE=0
	    cat site.def linux-slib.def | sed s+/lib\$+/lib64+g >site-slib.def
	    cat linux.def | sed s+/lib\$+/lib64+g >>site.def
	fi ;;
  FreeBSD)
	cat site.def freebsd-slib.def >site-slib.def
	cat freebsd.def >>site.def ;;
  Solaris)
	cat site.def solaris-slib.def >site-slib.def
	cat solaris.def >>site.def ;;
  HP-UX)
	cat site.def hpux-slib.def >site-slib.def
	cat hpux.def >>site.def ;;
  cygwin)
	LIB=bin
	cat site.def cygwin-slib.def >site-slib.def
	cat cygwin.def >>site.def ;;
  mingw)
	LIB=bin
	WAVE=0
	sed 's/NOCYGWIN//' site.def mingw-slib.def >site-slib.def
	sed 's/NOCYGWIN//' mingw.def >>site.def ;;
  mingw-compat)
	LIB=bin
	OS=mingw
	WAVE=0
	sed 's/NOCYGWIN/-mno-cygwin/' site.def mingw-slib.def >site-slib.def
	sed 's/NOCYGWIN/-mno-cygwin/' mingw.def >>site.def ;;
  Darwin)
	echo "It is normal for Mac OS X to be recognized as Darwin."
	if [ x$LIBTYPE != xdynamic ]
	then
	    echo "Building static libraries is not supported on Darwin."
	    LIBTYPE=dynamic
	fi
	cat site.def darwin-slib.def >site-slib.def
	cat darwin.def >>site.def ;;
  *)    OS=generic
	cat site.def generic-slib.def >site-slib.def
	cat generic.def >>site.def
	echo "Using the generic configuration, which may need to be customized"
	echo "for your operating system.  'configure' will offer suggestions"
	echo "for customization after it completes the generic configuration."
	;;
esac

if [ -s 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"
    ./prompt "static library, or D for a dynamically loaded library:"
    read LIBTYPE
  fi
else
    LIBTYPE=static
fi

case x$LIBTYPE in
  xS*|xs*) rm -f site-slib.def
           touch 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"
	./prompt "dynamically loaded libraries where available:"
	read SYSLIBS
    fi
    case x$SYSLIBS in
        xS*|xs*) sed "s/LDFLAGS = -L/LDFLAGS = -static -L/" <site.def >site.tmp
		 mv site.tmp site.def
		 echo "Applications will be linked with static libraries only."
		 SYSLIBS=static
		 ;;
	      *) SYSLIBS=dynamic ;;
    esac
else
    SYSLIBS=dynamic
fi

echo
./prompt "Looking for the C compiler ..."
WCC=`which gcc 2>&1`
if [ "x$WCC" = "x" ]
then
    WCC="no gcc"
fi
if ( echo $WCC | egrep "no |not " >wcc.t1 )
then
    WCC=`which cc 2>&1`
    if [ "x$WCC" = "x" ]
    then
        WCC="no cc"
    fi
    if ( echo $WCC | egrep "no |not " >wcc.t2 )
    then
        echo "not found"
	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."
	cd ..
	exit
    else
        sed "s/CC = gcc/CC = cc/" <site.def >site.tmp
	mv site.tmp site.def
        sed "s/CC = gcc/CC = cc/" <site-slib.def >site.tmp
	mv site.tmp site-slib.def
	CC=cc
    fi
else
    CC=gcc
fi
echo "$CC"
rm -f wcc.t1 wcc.t2

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
        ./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

case $DIR in
  /usr) ;;
    /*) sed "s+^WFDBROOT = /usr+WFDBROOT = $DIR+" <site.def >site.tmp
        mv site.tmp site.def
        sed "s+^WFDBROOT = /usr+WFDBROOT = $DIR+" <site-slib.def \
         >site.tmp
        mv site.tmp site-slib.def
esac

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

MANTMP=`grep "MANDIR =" site.def |sed 's+$(WFDBROOT)/++' |cut -d " " -f 3`
MANDEF=$DIR/$MANTMP
if [ x$MANDIR = xunknown ]
then
   MANDIR=$MANDEF
fi

if [ x$MANDIR = x ]
then
    echo
    echo "Where do you wish to install the manual pages?"
    echo "They will be installed in subdirectories of MANDIR (by default,"
    echo "MANDIR is '$MANDEF').  If you install them in a non-standard"
    echo "directory, you will need to add that directory to your MANPATH."
    echo "The value of MANDIR must be a full pathname (beginning with '/',"
    echo "but without a '/' at the end).  If the subdirectories (man1, man3,"
    echo "man5, ...) of MANDIR don't exist, they will be created as necessary."
    echo
    MANDIR=
    while [ x$MANDIR = x ]
    do
       ./prompt "Choose a location, or press <Enter> to install in $MANDEF:"
        read MANDIR
        case x$MANDIR in
            x/usr) ;;
                x) MANDIR=$MANDEF ;;
              x*/) MANDIR=
                   echo "Please type the path again, omitting the '/' at the end." ;;
              x/*) ;;
               x*) MANDIR=
                   echo "Please enter an absolute pathname, beginning with '/'." ;;
        esac
    done
fi

if [ "$MANDIR" != "$MANDEF" ]
then
    sed "s+^MANDIR = $MANDEF+MANDIR = $MANDIR+" <site.def >site.tmp
    mv site.tmp site.def
fi

echo  "The manual pages will be installed in '$MANDIR'."
echo

# Search for NETLIB if not specified on the command line
if [ x$NETLIB = xunknown ]
    then
    ./prompt "Looking for libcurl ..."
    WHICH=`which curl-config 2>&1`
    if [ "x$WHICH" = "x" ]
	then
	WHICH="no curl-config"
    fi
    if ( echo $WHICH | egrep "no |not " >which.t1 )
	then
	echo "not found"
	./prompt "Looking for libwww ..."
	WHICH=`which libwww-config 2>&1`
	if [ "x$WHICH" = "x" ]
	    then
	    WHICH="no libwww-config"
	fi
	if ( echo $WHICH | egrep "no |not " >which.t2 )
	    then
	    echo "not found"
	    echo "The WFDB software will be compiled without NETFILES"
	    echo "access, because neither libcurl nor libwww appears to"
	    echo "be installed on this system.  To add NETFILES access,"
	    echo "install libcurl or libwww and run ./configure again."
	    NETLIB=none
	else
	    echo "found"
	    echo "The WFDB software will be compiled with NETFILES access"
	    echo "using libwww."
	    NETLIB=libwww
	fi
    else
	echo "found"
	echo "The WFDB software will be compiled with NETFILES access"
	echo "using libcurl."
	NETLIB=libcurl
    fi
    rm -f which.t1 which.t2
fi

case $NETLIB in
  libcurl) LC="\`curl-config --cflags\`"
	   LL="\`curl-config --libs\`"
	   NETFILES=1
	   NETFILES_LIBCURL=1
	   VIANF=" via libcurl"
	   WITHNF=with
	   ;;
  libwww)  LC="\`libwww-config --cflags\`"
	   LL="\`libwww-config --libs\`"
	   NETFILES=1
	   NETFILES_LIBCURL=0
	   VIANF=" via libwww"
	   WITHNF=with
	   ;;
  none)    LC=""
	   LL=""
	   NETFILES=0
	   NETFILES_LIBCURL=0
	   VIANF=""
	   WITHNF=without
	   ;;
esac
echo "NETFILES=$NETFILES" >../config.cache
echo "NETFILES_LIBCURL=$NETFILES_LIBCURL" >>../config.cache

sed "s/WFDB_NETFILES 1/WFDB_NETFILES $NETFILES/" < ../lib/wfdb.h0 | \
    sed "s/WFDBMAJOR/$MAJOR/" | \
    sed "s/WFDBMINOR/$MINOR/" | \
    sed "s/WFDBRELEASE/$RELEASE/" | \
    sed "s/WFDB_NETFILES_LIBCURL 1/WFDB_NETFILES_LIBCURL $NETFILES_LIBCURL/" \
      >../lib/wfdb.h
if [ $LIBTYPE = "static" -a $NETLIB != "none" ]
    then
    LL = "$LL $LL $LL"
    sed "s/LC =/LC = $LC/" <site.def | sed "s/LL =/LL = $LL/" >site.tmp
    mv site.tmp site.def
fi
sed "s/LC =/LC = $LC/" <site-slib.def | sed "s/LL =/LL = $LL/" >site.tmp
mv site.tmp site-slib.def

cd ..
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 xml .
  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

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

if [ "x$WAVE" = "x0" ]
    then
    cp -p wave/nomake wave/Makefile
    cp -p wave/nomake waverc/Makefile
    echo "WAVE=0" >>config.cache
    WAVECOMP="WAVE will not be compiled."
else
    echo
    conf/prompt "Looking for the XView libraries ..."
    TEXTEDIT=`which textedit 2>&1`
    if [ "x$TEXTEDIT" = "x" ]
	then
	TEXTEDIT="no textedit"
    fi
    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 ( echo $TEXTEDIT | egrep "no |not " >textedit.t1 )
	then
	WAVE=0;
    else
	WAVE=1;
	TEDIR=`dirname $TEXTEDIT`;
	OWHOME=`dirname $TEDIR`;
	sed "s+/usr/openwin+$OWHOME+" <wave/Makefile >tmp.$$
	mv tmp.$$ wave/Makefile
    fi
    rm -f textedit.t1
    if [ $WAVE = 0 ]
	then
	echo "not found"
	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
	echo "found"
	WAVECOMP="WAVE will be compiled and installed in '$DIR/bin'."
	echo "WAVE=1" >>config.cache
    fi
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
    cat <<EOF

IMPORTANT: You have chosen to build a dynamic (shared) WFDB library in
a directory, $DIR/$LIB, that is not normally searched for such
libraries.  In order to use the applications that will be compiled
with this library, it may be necessary to add this directory to your
LD_LIBRARY_PATH.  If your shell is csh or a variant, do this by
typing:
EOF
    if [ "x$LD_LIBRARY_PATH" = x ]
	then
	echo "  setenv LD_LIBRARY_PATH $DIR/$LIB"
	echo "Otherwise, do this by typing:"
	echo "  LD_LIBRARY_PATH=$DIR/$LIB"
	echo "  export LD_LIBRARY_PATH"
    else
	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"
    fi
fi

if [ $OS = generic ]
    then
    cat <<EOF

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

fi

if [ "x$MFLAGS" != x ]
    then
    CC="$CC $MFLAGS"
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${VIANF}, 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 '$MANDIR'.
Sample data files will be installed in '$DIR/database', which
 will be included in the default WFDB path.

If you need to compile a different WFDB library type, or install into different
directories, rerun this script with its "-i" (interactive) option:
      ./configure -i
Other options can be viewed by rerunning this script with its "-h" option:
      ./configure -h

WFDB software configuration is complete.
Now run 'make' to compile and test, followed by 'make install' to recompile
and install the software, man pages, and example data.  Finally, run
'make check' to verify that the installation is complete and successful.

EOF
