#!/bin/sh
#*******************************************************************************
#                                                                              *
#                                   Viewmol                                    *
#                                                                              *
#                             G E T M A C H I N E                              *
#                                                                              *
#                 Copyright (c) Joerg-R. Hill, October 2003                    *
#                                                                              *
#*******************************************************************************
#
# $Id: getmachine,v 1.6 2003/11/07 12:53:43 jrh Exp $
# $Log: getmachine,v $
# Revision 1.6  2003/11/07 12:53:43  jrh
# Release 2.4
#
# Revision 1.5  2000/12/10 15:07:25  jrh
# Release 2.3
#
# Revision 1.4  1999/05/24 01:25:38  jrh
# Release 2.2.1
#
# Revision 1.3  1999/02/07 22:02:47  jrh
# Release 2.2
#
# Revision 1.2  1998/01/26 00:47:46  jrh
# Release 2.1
#
# Revision 1.1  1996/12/10  18:46:19  jrh
# Initial revision
#
# This shell script adapts the Makefile in dependence
# of the machine and the operating system you use.
# It is called from 'make viewmol' before the compiler
# is invoked.
#

makedir()
{
  if [ ! -d "$dir" ]
  then
    mkdir "$dir"
  fi
  cd $dir
}

givehint()
{
  if [ $hint -eq 1 ]
  then
    echo 'If you want to use environment variables in any of the following'
    echo 'paths, do it like this: $(HOME)'
    hint=0
  fi
}

findProcessor()
{
  # Try -p option first, but since this option is not always
  # available use -m option if -p option fails or return "unknown"
  processor=`uname -p 2> /dev/null`
  if [ "$processor" = "" -o "$processor" = "unknown" ]
  then
    processor=`uname -m`
  fi
}

target=$1
os=`uname -s`

# Check, whether configuration file is up to date
if [ -f .config.$os ]
then
  grep PNGINCLUDE .config.$os > /dev/null 2> /dev/null
  ret=$?
  if [ $ret -eq 1 ]
  then
    rm -f .config.$os
  fi
fi

if [ ! -f .config.$os ]
then
  hint=1

  # TIFF library
  if [ -f /usr/lib/libtiff.a ]
  then
    libtiff="-L/usr/lib"
  elif [ -f /usr/local/lib/libtiff.a ]
  then
    libtiff="-L/usr/local/lib"
  else
    givehint
    echo -n "Please enter the name of the directory where libtiff.a can be found: "
    read libtiff
    if [ "$libtiff" != "" ]
    then
      libtiff="-L${libtiff}"
    fi
  fi
  echo "LIBTIFF = ${libtiff}" > .config.$os

  # TIFF include file
  if [ -f /usr/include/tiff.h ]
  then
    case $os in
      CYGWIN*) tiffinclude="."
               ;;
      *)       tiffinclude="/usr/include"
               ;;
    esac
  elif [ -f /usr/local/include/tiff.h ]
  then
    tiffinclude="/usr/local/include"
  else
    givehint
    echo -n "Please enter the name of the directory where the include file for libtiff can be found: "
    read tiffinclude
  fi
  echo "TIFFINCLUDE = $tiffinclude" >> .config.$os

  # PNG library
  if [ -f /usr/lib/libpng.a -o -f /usr/lib/libpng12.a ]
  then
    libpng="-L/usr/lib"
  elif [ -f /usr/local/lib/libpng.a ]
  then
    libpng="-L/usr/local/lib"
  else
    givehint
    echo -n "Please enter the name of the directory where libpng.a can be found: "
    read libpng
    if [ "$libpng" != "" ]
    then
      libpng="-L${libpng}"
    fi
  fi
  echo "LIBPNG = ${libpng}" >> .config.$os

  # PNG include file
  if [ -f /usr/include/png.h ]
  then
    pnginclude="/usr/include"
  elif [ -f /usr/include/libpng12/png.h ]
  then
    pnginclude="/usr/include/libpng12"
  elif [ -f /usr/local/include/png.h ]
  then
    pnginclude="/usr/local/include"
  else
    givehint
    echo -n "Please enter the name of the directory where the include file for libpng can be found: "
    read pnginclude
  fi
  echo "PNGINCLUDE = $pnginclude" >> .config.$os

  # Python
  list=`find /usr -name Python.h -print 2> /dev/null`
  version=`(for i in $list
            do
              dir=\`dirname $i\`
              echo \`basename $dir\`
            done) | sort | tail -1`
  if [ "$version" != "" ]
  then
    pythoninclude=`dirname \`(for i in $list
                              do
                                echo $i | grep $version
                              done)\``
    pythonversion=$version
  else
    givehint
    echo -n "Please enter the name of the directory where the include files for Python can be found: "
    read pythoninclude
    pythonversion=`basename $pythoninclude`
  fi
  echo "PYTHONVERSION = $pythonversion" >> .config.$os
  echo "PYTHONINCLUDE = $pythoninclude" >> .config.$os
  if [ "$version" != "" ]
  then
    pythonlib=`dirname \`find /usr -name "lib${version}*.a" -print 2> /dev/null | grep "$version/"\``
  else
    givehint
    echo -n "Please enter the name of the directory where the Python library can be found: "
    read pythonlib
  fi
  echo "LIBPYTHON = $pythonlib" >> .config.$os
fi

PYTHON="python2"
if [ $os = "FreeBSD" ]
then
  PYTHON="python2.2"
else
  PYTHON=`which python2 2> /dev/null`
  ret=$?
  if [ $ret -ne 0 ]
  then
    PYTHON=`which python 2> /dev/null`
    ret=$?
    if [ $ret -ne 0 ]
    then
      echo "Cannot find Python."
      exit
    fi
  fi
fi

LINKFORSHARED=`$PYTHON - <<EOF
import distutils.sysconfig
print distutils.sysconfig.get_config_var('LINKFORSHARED')
EOF
`

case $os in
#
# This is an IBM RS/6000.
#
  AIX)     if [ `uname -m | cut -c9-10` = "70" ]
           then
             dir="${os}_POWER2"
           else
             dir="${os}_POWER"
           fi
           makedir
           cat ../.config.$os > makefile
           echo 'COMPILER=cc' >> makefile
           echo 'OPT=-O2' >> makefile
           echo 'CFLAGS=-DAIX' >> makefile
           echo "LDFLAGS=$LINKFORSHARED" >> makefile
           echo 'SCANDIR=' >> makefile
           echo 'INCLUDE=/usr/lpp/OpenGL/include -I$(TIFFINCLUDE) -I$(PNGINCLUDE) -I$(PYTHONINCLUDE)' >> makefile
           echo 'LIBRARY=-L/usr/lpp/OpenGL/lib $(LIBTIFF) $(LIBPNG) -L$(LIBPYTHON)' >> makefile
           echo 'LIBS=-l${PYTHONVERSION} -ltiff -lpng -lz -lGLU -lGL -lXm -lXmu -lXi -lXext -lXt -lX11 -lm' >> makefile
           ;;
#
# This is a Silicon Graphics.
#
  IRIX*)   dir="${os}_`hinv | awk '/CPU:/ {print $3}' | cut -d/ -f2`"
           makedir
           cat ../.config.$os > makefile
           echo 'COMPILER=cc' >> makefile
           echo 'OPT=-O2' >> makefile
           if [ "$dir" = "IRIX_R4400" -o "$dir" = "IRIX_R4600" -o "$dir" = "IRIX_R5000" ]
           then
             echo 'CFLAGS=-DIRIX -n32' >> makefile
             echo "LDFLAGS=-n32 -Wl,-woff,85,-woff,134 $LINKFORSHARED" >> makefile
           elif [ "$dir" = "IRIX64_R8000" -o "$dir" = "IRIX64_R10000" -o "$dir" = "IRIX64_R12000" ]
           then
#
# uses -n32 since libtiff is not available in a 64 bit version on SGIs
#
             echo 'CFLAGS=-DIRIX -n32' >> makefile
             echo "LDFLAGS=-n32 -Wl,-woff,85,-woff,134 $LINKFORSHARED" >> makefile
           else
             echo 'CFLAGS=-DIRIX' >> makefile
             echo "LDFLAGS=-Wl,-woff,85,-woff,134 $LINKFORSHARED" >> makefile
           fi
           echo 'SCANDIR=' >> makefile
           echo 'INCLUDE=$(TIFFINCLUDE) -I$(PNGINCLUDE) -I$(PYTHONINCLUDE)' >> makefile
           echo 'LIBRARY=$(LIBTIFF) $(LIBPNG) -L$(LIBPYTHON)' >> makefile
           echo 'LIBS=-l${PYTHONVERSION} -ltiff -lpng -lz -lGLU -lGL -lXm -lXmu -lXi -lXt -lX11 -lpthread -lm -lCsup' >> makefile
           ;;
#
# This is Linux.
#
  Linux)   dir="$os"
           makedir
           cat ../.config.$os > makefile
           use_icc=`type icc 2> /dev/null`
           if [ "$use_icc" = "" ]
           then
	     findProcessor
	     echo 'COMPILER=gcc' >> makefile
             echo "OPT=-O6 -mcpu=$processor -fomit-frame-pointer -ffast-math -malign-double" >> makefile
             echo 'CFLAGS=-Wall -I/usr/X11R6/include -DLINUX -Wno-strict-aliasing' >> makefile
             echo "LDFLAGS=$LINKFORSHARED" >> makefile
           else
	     echo 'COMPILER=icc' >> makefile
             echo 'OPT=-O2 -ipo' >> makefile
             echo 'CFLAGS=-I/opt/intel/compiler50/ia32/include -I/usr/X11R6/include -DLINUX' >> makefile
             echo "LDFLAGS=$LINKFORSHARED -ipo" >> makefile
           fi
           echo 'SCANDIR=' >> makefile
           echo 'INCLUDE=$(TIFFINCLUDE) -I$(PNGINCLUDE) -I$(PYTHONINCLUDE)' >> makefile
           echo 'LIBRARY=$(LIBTIFF) $(LIBPNG) -L$(LIBPYTHON)' >> makefile
           echo 'LIBS=-L/usr/local/lib -l${PYTHONVERSION} -ltiff -lpng -lz -lGLU -lGL -L/usr/X11R6/lib -lXm -lXmu -lXp -lXi -lXext -lXt -lX11 -lpthread -lutil -ldl -lm' >> makefile
           ;;
#
# This is FreeBSD
#
  FreeBSD) dir="$os"
           makedir
           cat ../.config.$os > makefile
	   findProcessor
	   echo 'COMPILER=cc' >> makefile
           echo "OPT=-O6 -mcpu=$processor -fomit-frame-pointer -ffast-math -malign-double" >> makefile
           echo 'CFLAGS=-Wall -I/usr/X11R6/include -DFREEBSD' >> makefile
           echo "LDFLAGS=$LINKFORSHARED" >> makefile
           echo 'SCANDIR=' >> makefile
           echo 'INCLUDE=$(TIFFINCLUDE) -I$(PNGINCLUDE) -I$(PYTHONINCLUDE)' >> makefile
           echo 'LIBRARY=$(LIBTIFF) $(LIBPNG) -L$(LIBPYTHON)' >> makefile
           echo 'LIBS=-l${PYTHONVERSION} -ltiff -lpng -lz -lGLU -lGL -L/usr/X11R6/lib -lXm -lXmu -lXi -lXext -lXt -lX11 -lutil -lm' >> makefile
           ;;
#
# This is Mac OS X
#
  Darwin)  dir="$os"
           makedir
           cat ../.config.$os > makefile
           echo 'COMPILER=gcc' >> makefile
           echo 'OPT=-O2 -fomit-frame-pointer' >> makefile
           echo 'CFLAGS=-Wall -DDARWIN -I/usr/X11R6/include' >> makefile
           echo "LDFLAGS=-bind_at_load $LINKFORSHARED" >> makefile
           echo 'SCANDIR=' >> makefile
           echo 'INCLUDE=$(TIFFINCLUDE) -I$(PNGINCLUDE) -I$(PYTHONINCLUDE)' >> makefile
           echo 'LIBRARY=$(LIBTIFF) $(LIBPNG) -L$(LIBPYTHON) -L/usr/X11R6/lib' >> makefile
           echo 'LIBS=-l${PYTHONVERSION} -ltiff -lpng -lz -lGLU -lGL -lXm -lXmu -lXi -lXext -lXt -lX11 -lm' >> makefile
           ;;
#
# This is OSF1
#
  OSF1)    dir="$os"
           makedir
           cat ../.config.$os > makefile
           echo 'COMPILER=cc' >> makefile
           echo 'OPT=-O2 -Olimit 615' >> makefile
           echo 'CFLAGS=-ieee_with_no_inexact' >> makefile
           echo "LDFLAGS=$LINKFORSHARED" >> makefile
           echo 'SCANDIR=' >> makefile
           echo 'INCLUDE=$(TIFFINCLUDE) -I$(PNGINCLUDE) -I$(PYTHONINCLUDE)' >> makefile
           echo 'LIBRARY=$(LIBTIFF) $(LIBPNG) -L$(LIBPYTHON)' >> makefile
           echo 'LIBS=-l${PYTHONVERSION} -lXm -lXmu -lXi -lXt -lGLU -lGL -lX11 -ltiff -lpng -lz -lm' >> makefile
           ;;
#
# This is HP-UX
#
  HP-UX)   dir="$os"
           makedir
           cat ../.config.$os > makefile
           echo 'COMPILER=cc' >> makefile
           echo 'OPT=-O2' >> makefile
           echo 'CFLAGS=-Aa -D_HPUX_SOURCE' >> makefile
           echo "LDFLAGS=$LINKFORSHARED" >> makefile
           echo 'SCANDIR=' >> makefile
           echo 'INCLUDE=$(TIFFINCLUDE) -I$(PNGINCLUDE) -I/usr/include/Motif1.2 -I/usr/include/X11R5 -I$(PYTHONINCLUDE)' >> makefile
           echo 'LIBRARY=$(LIBTIFF) $(LIBPNG) -L/usr/lib/Motif1.2 -L/usr/lib/X11R5 -L$(LIBPYTHON)' >> makefile
           echo 'LIBS=-l${PYTHONVERSION} -lXm -lXmu -lXi -lXt -lGLU -lGL -lX11 -ltiff -lpng -lz -lm' >> makefile
           ;;
#
# This is SunOS
#
  SunOS)   dir="$os"
           makedir
           cat ../.config.$os > makefile
           use_gcc=`type gcc 2> /dev/null`
           if [ "$use_gcc" = "" ]
           then
             echo 'COMPILER=cc' >> makefile
             echo 'OPT=-xO4 -native' >> makefile
             echo 'CFLAGS= -DSunOS -xlibmil' >> makefile
             echo "LDFLAGS=-L/usr/dt/lib $LINKFORSHARED" >> makefile
	   else
	     echo 'COMPILER=gcc' >> makefile
             echo "OPT=-O6 -fomit-frame-pointer -ffast-math" >> makefile
             echo 'CFLAGS=-Wall -I/usr/X11R6/include' >> makefile
             echo "LDFLAGS=$LINKFORSHARED" >> makefile
	   fi
           echo 'SCANDIR=scandir.o' >> makefile
           echo 'INCLUDE=$(TIFFINCLUDE) -I$(PNGINCLUDE) -I$(PYTHONINCLUDE)' >> makefile
           echo 'LIBRARY=$(LIBTIFF) $(LIBPNG) -L$(LIBPYTHON)' >> makefile
           echo 'LIBS=-l${PYTHONVERSION} -lXm -lXmu -lXi -lXt -lGLU -lGL -lX11 -lm -Bstatic -ltiff -lpng -lz -Bdynamic ' >> makefile
           ;;
#
# This is Windows with Cygwin
#
   CYGWIN*) dir="$os"
            makedir
            cat ../.config.$os > makefile
	    findProcessor
	    echo 'COMPILER=cc' >> makefile
            echo "OPT=-mcpu=$processor -fomit-frame-pointer -ffast-math -malign-double" >> makefile
            echo 'CFLAGS=-DSYS_NMLN=20 -Wall -I/usr/X11R6/include -DLINUX' >> makefile
            echo "LDFLAGS=-L/usr/local/lib/w32api/ $LINKFORSHARED" >> makefile
            echo 'SCANDIR=' >> makefile
            echo 'INCLUDE=$(TIFFINCLUDE) -I$(PNGINCLUDE) -I$(PYTHONINCLUDE)' >> makefile
            echo 'LIBRARY=$(LIBTIFF) $(LIBPNG) -L$(LIBPYTHON)' >> makefile
            echo 'LIBS=-L/usr/local/BerkeleyDB.4.0/lib -L/usr/local/lib -l${PYTHONVERSION} -ltiff -lpng12 -lz -lGLU -lGL -L/usr/X11R6/lib -lXmu -lXm -lXp -lXi -lXext -lXt -lX11 -lpthread -lm -lSM -lICE' >> makefile
            ;;
#
# This is a (not yet) known operating system.
#
  *)       echo "Sorry, I don't know the operating system '$os'."
           echo "Edit 'getmachine' to include the required options."
           exit 1
           ;;
esac
cat ../Makefile >> makefile
make ${target}_
rm -f makefile
