#! /bin/sh

# $Id: configure,v 1.2 2001/02/23 16:43:16 pjx Exp $

# This builds a configuration for installation of MAPSLICER
# based on the value of an argument which is
# meant to describe a supported system.

# Ripped off from the old CCP4 main configure script...

set -e 2>/dev/null    # not sure whether this still gives an
                                # error message with old sh... 

usage='echo "Bad argument for MAPSLICER configure";
       exit 1'

###################################################################
#
# START
#
###################################################################

echo "****************************************************"
echo "*            CCP4MAPWISH CONFIGURE                 *"
echo "****************************************************"
echo
echo "This script will attempt to configure and make the \"ccp4mapwish\""
echo "interpreter to run the MAPSLICER program"
echo
echo "The configuration and installation of ccp4mapwish has not been"
echo "widely tested so there may be problems. Please let us know of"
echo "any successes or problems that you have"
echo
echo "See $CCP4/.../INSTALL for more"
echo "information if configure or make fail"

# Deal with the arguments
# Don't allow any specific arguments at the moment
for arg
do
  case $arg in
  -include-dir=* | --include-dir=*)
    tryincs=`echo $arg | sed 's/[-a-z_]*=//'`
    echo
    echo "User specified include directory for tcl.h: $tryincs"
    ;;
  -lib-dir=* | --lib-dir=*)
    trydirs=`echo $arg | sed 's/[-a-z_]*=//'`
    echo
    echo "User specified directory for TCL and Tk libraries: $trydirs"
    ;;
  -install-dir=* | --install-dir=*)
    INSTALLDIR=`echo $arg | sed 's/[-a-z_]*=//'`
    echo
    echo "User specified install directory: $INSTALLDIR"
    ;;
  *)
    if test -z "$system" ; then system=$arg
    else eval $usage ; fi ;; # bad flag
  esac
done

test -z "$system" && eval $usage || true # system must be defined

###################################################################
#
# BASIC SYSTEM CONFIGUATION
#
###################################################################

# So far I have only tested installation on OSF1, IRIX (o32) and
# Linux with g77/gcc

# These are the standard compiler options for CCP4 build
# Only the libraries are a real problem...
case $system in
    osf1)
    echo
    echo "*** Configuring to build CCP4mapwish on OSF1 system ***"
    FC=${FC-f77}
    CC=${CC-cc}
    FFLAGS=${FFLAGS-"-fpe0 -assume dummy_aliases -nowarn -O1"}
    CFLAGS=${CFLAGS-"-w -DPROTOTYPE"}
    SYSLIBS="-lfor -lUfor -lm -lX11"
    ;;
    irix)
    echo
    echo "*** Configuring to build CCP4mapwish on IRIX system ***"
    echo
    echo "Warning: the ccp4mapwish build assumes (o)32 architecture"
    echo "         so you may have problems if libccp4 and/or the"
    echo "         TCL and Tk libraries have been built as n32"
    FC=${FC-f77}
    CC=${CC-cc}
    FFLAGS=${FFLAGS-"-32 -nocpp"}
    CFLAGS=${CFLAGS-"-32 -DPROTOTYPE"}
    SYSLIBS="-lftn -lm -lX11"
    ;;
    linux)
    echo
    echo "*** Configuring to build CCP4mapwish on Linux system ***"
    echo "*** using GNU compilers                              ***"
    FC=${FC-g77}
    CC=${CC-gcc}
    FFLAGS=${FFLAGS-"-fno-second-underscore -fno-globals"}
    CFLAGS=${CFLAGS-"-DG77 -DPROTOTYPE -DHAVE_G2C_H"}
    SYSLIBS="-lg2c -lm -ldl -L/usr/X11R6/lib -lX11"
    ;;
    *)
    echo
    echo "*** CCP4mapwish installation is completely untested on this system ***"
    echo
    echo "If you want to try the MAPSLICER program then you will need"
    echo "to build ccp4mapwish manually - see $CCP4/.../INSTALL for"
    echo "more information."
    echo
    echo "Aborting build - sorry!"
    exit 1
    ;;
esac

# Installation directory defaults to $CBIN for all systems
if test ! "$INSTALLDIR" ; then
    INSTALLDIR=${CBIN}
    echo
    echo "Installation directory for ccp4mapwish: $INSTALLDIR"
fi

# Install program defaults to cp
INSTALL_PROGRAM=${INSTALL_PROGRAM-"cp"}

###################################################################
#
# TCL HEADER FILE CONFIGURATION
#
###################################################################

# Try to find tcl.h
if test ! "$tryincs" ; then
  tryincs="/usr/include /usr/local/include $CCP4I_TCLTK/../include"
fi

echo
echo "-> Step 1: look for tcl.h header file..."
for testinc in $tryincs
do
if test ! "$INCDIR" && test -f "$testinc/tcl.h" ; then
  echo "...found tcl.h in $testinc"
  # Need to check version - must be 8.0 or better
  versionline=`eval grep TCL_MAJOR_VERSION $testinc/tcl.h`
  version=`echo $versionline | sed 's/#define TCL_MAJOR_VERSION //'`
  if test $version -ge 8 ; then
    echo "   TCL major version is $version - OK"
    INCDIR="$testinc"
  else
    echo "   TCL major version is only $version ... too old"
  fi
fi
done
if test ! "$INCDIR" ; then
  echo
  echo "*** Can't find a version of tcl.h which is at least 8.0"
  echo "*** Build failed - sorry!"
  exit 1
fi

###################################################################
#
# TCL/TK LIBRARY CONFIGUATION
#
###################################################################

# Try to find TCL and Tk libraries
#
# There are lots of options and possible dirs
#
# Test for: extension .a or .so, in different directories:
# /usr/lib
# /usr/local/lib
# $(CCP4I_TCLTK)/../lib
#
# and with different names:
# tcl8.0
#
# NB: can't use tclx8.0.4!
#
# This is hardly comprehensive but it will do for a start

echo
echo "-> Step 2: look for TCL and Tk libraries..."

# Possible directories
# A directory may already have been specified by the user
if test ! "$trydirs" ; then
  trydirs="/usr/lib /usr/local/lib $CCP4I_TCLTK/../lib"
fi
# Possible extensions
tryexts="a so"
# Possible version numbers
tryvers="8.3 8.2 8.1 8.0"
# Possible tcl names

if test -n "$trydirs" ; then
  # Cycle through directories...
  for testdir in $trydirs
  do
    # ... and possible versions for tcl ...
    for testvers in $tryvers
    do
      # ... and possible extensions (.a or .so)
      for testext in $tryexts
      do
	if test ! "$TCLDIR" ; then
	    if test -s "$testdir/libtcl$testvers.$testext" ; then
	    echo "...found TCL library: $testdir/libtcl$testvers.$testext"
	    TCLLIB="tcl$testvers"
	    TCLDIR="-L$testdir"
	    fi
	fi
      done
      # ... and possible versions for tk
      for testvers in $tryvers
      do
	if test ! "$TKDIR" ; then
	    if test -s "$testdir/libtk$testvers.$testext" ; then
	    echo "...found TK library:  $testdir/libtk$testvers.$testext"
	    TKLIB="tk$testvers"
	    TKDIR="-L$testdir"
	    fi
	fi
      done
    done
  done
fi

# Error if no libraries found
if test ! "$TCLDIR" || test ! "$TKDIR" ; then
  echo
  echo "*** Can't find appropriate TCL and/or Tk libraries"
  echo "*** Build failed - sorry!"
  exit 1
fi

###################################################################
#
# CREATE MAKEFILE
#
###################################################################

echo
echo "-> Step 3: create Makefile..."
cat >Makefile <<EOF
# Makefile for the CCP4mapwish interpreter
# This file was generated by configure

FC      = $FC
CC      = $CC
FFLAGS = $FFLAGS
CFLAGS = $CFLAGS
SYSLIBS = $SYSLIBS

# Needs to be set to $CCP4_LIB to pick up libccp4.a
libdir = $CCP4_LIB
# Where to find library.h
incdir = $CLIBS
# Where to find tcl.h
tclinc = $INCDIR

# TCL and Tk libraries
TCLLIBDIR = $TCLDIR
TCLLIB    = $TCLLIB
TKLIBDIR  = $TKDIR
TKLIB     = $TKLIB

# Installation
bindir = $INSTALLDIR
INSTALL_PROGRAM = $INSTALL_PROGRAM

EOF

cat Makefile.in >>Makefile
echo "... Makefile created"

###################################################################
#
# PERFORM COMPILATION
#
###################################################################
echo
echo "-> Step 4: perform make..."
echo "*** Check make output for errors ***"
make clean
make
echo "...make completed"
echo
echo "-> Step 5: install ccp4mapwish..."
make install
echo "...installed"
echo
echo "Finished okay - bye!"
exit 0