#!/bin/sh
#*******************************************************************************
#                                                                              *
#                                   Viewmol                                    *
#                                                                              *
#                             G E T M A C H I N E                              *
#                                                                              *
#                 Copyright (c) Joerg-R. Hill, December 2000                   *
#                                                                              *
#*******************************************************************************
#
# $Id: getmachine,v 1.5 2000/12/10 15:07:25 jrh Exp $
# $Log: getmachine,v $
# 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
}

use_mesa=no
if [ "$1" = "mesa" ]
then
  use_mesa=yes
  shift
fi

target=$1
os=`uname -s`
#if [ "$os" = "Linux" -o "$os" = "FreeBSD" ]
if [ "$os" = "FreeBSD" ]
then
  use_mesa=yes
fi

if [ ! -f .config.$os ]
then
  hint=1
  if [ -f /usr/lib/libtiff.a ]
  then
    libtiff="-L/usr/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
  if [ -f /usr/include/tiff.h ]
  then
    tiffinclude="/usr/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
  if [ "$use_mesa" = "yes" ]
  then
    if [ -f /usr/lib/libMesaGL.a -a -f /usr/lib/libMesaGLU.a ]
    then
      mesalib="-L/usr/lib"
    else
      givehint
      echo -n "Please enter the name of the directory where the Mesa libraries can be found: "
      read mesalib
	if [ "$mesalib" != "" ]
	then
	  mesalib="-L${mesalib}"
      fi
    fi
    echo "MESALIB = $mesalib" >> .config.$os
    if [ -f /usr/include/GL/gl.h ]
    then
      mesainclude="/usr/include"
    else
      givehint
      echo -n "Pleae enter the name of the directory where the include files for Mesa can be found: " 
      read mesainclude
    fi
    echo "MESAINCLUDE = $mesainclude" >> .config.$os
  fi
  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 = -L$pythonlib" >> .config.$os
fi

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 'OPT=-O2' >> makefile
	     echo 'CFLAGS=-DAIX' >> makefile
	     echo 'LDFLAGS=' >> makefile
	     echo 'SCANDIR=' >> makefile
	     if [ "$use_mesa" = "yes" ]
	     then
             echo 'INCLUDE=$(MESAINCLUDE) -I$(TIFFINCLUDE) -I$(PYTHONINCLUDE)' >> makefile
             echo 'LIBRARY=$(MESALIB) $(LIBTIFF) $(LIBPYTHON)' >> makefile
             echo 'LIBS=-l${PYTHONVERSION} -ltiff -lMesaGLU -lMesaGL -lXm -lXmu -lXi -lXext -lXt -lX11 -lm' >> makefile
	     else
             echo 'INCLUDE=/usr/lpp/OpenGL/include -I$(TIFFINCLUDE) -I$(PYTHONINCLUDE)' >> makefile
             echo 'LIBRARY=-L/usr/lpp/OpenGL/lib $(LIBTIFF) $(LIBPYTHON)' >> makefile
             echo 'LIBS=-l${PYTHONVERSION} -ltiff -lGLU -lGL -lXm -lXmu -lXi -lXext -lXt -lX11 -lm' >> makefile
	     fi
           ;;
#
# This is a Silicon Graphics.
#
  IRIX*)   dir="${os}_`hinv | awk '/CPU:/ {print $3}' | cut -d/ -f2`"
           makedir
           cat ../.config.$os > 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' >> 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' >> makefile
	     else
             echo 'CFLAGS=-DIRIX' >> makefile
             echo 'LDFLAGS=-Wl,-woff,85,-woff,134' >> makefile
           fi
	     echo 'SCANDIR=' >> makefile
	     if [ "$use_mesa" = "yes" ]
	     then
             echo 'INCLUDE=$(MESAINCLUDE) -I$(TIFFINCLUDE) -I$(PYTHONINCLUDE)' >> makefile
             echo 'LIBRARY=$(MESALIB) $(LIBTIFF) $(LIBPYTHON)' >> makefile
             echo 'LIBS=-l${PYTHONVERSION} -ltiff -lMesaGLU -lMesaGL -lXm -lXmu -lXi -lXext -lXt -lX11 -lpthread -lm' >> makefile
	     else
             echo 'INCLUDE=$(TIFFINCLUDE) -I$(PYTHONINCLUDE)' >> makefile
             echo 'LIBRARY=$(LIBTIFF) $(LIBPYTHON)' >> makefile
             echo 'LIBS=-l${PYTHONVERSION} -ltiff -lGLU -lGL -lXm -lXmu -lXi -lXt -lX11 -lpthread -lm -lCsup' >> makefile
           fi
           ;;
#
# This is Linux.
#
  Linux)   dir="$os"
           makedir
           cat ../.config.$os > makefile
	     gcc -v 2>&1 | grep egcs > /dev/null
	     egcs=$?
	     if [ $egcs -eq 0 ]
	     then
#            processor=`cat /proc/cpuinfo | grep cpu | grep -v cpuid | awk '{printf("%s\n", $3);}'`
		 processor=`uname --machine`
             if [ "$processor" = "i686" ]
             then
               echo 'OPT=-O6 -mpentiumpro -fomit-frame-pointer -ffast-math -malign-double' >> makefile
             else [ "$processor" = "i586" ]
               echo 'OPT=-O6 -mpentium -fomit-frame-pointer -ffast-math -malign-double' >> makefile
	       fi
           else
             echo 'OPT=-O6 -m486 -fomit-frame-pointer -ffast-math -malign-double' >> makefile
           fi
           echo 'CFLAGS=-Wall -I/usr/X11R6/include -DLINUX' >> makefile
           echo 'LDFLAGS=' >> makefile
	   echo 'SCANDIR=' >> makefile
	   if [ "$use_mesa" = "yes" ]
	   then
             echo 'INCLUDE=$(MESAINCLUDE) -I$(TIFFINCLUDE) -I$(PYTHONINCLUDE)' >> makefile
             echo 'LIBRARY=$(MESALIB) $(LIBPYTHON)' >> makefile
             echo 'LIBS=-L/usr/X11R6/lib -l${PYTHONVERSION} -ltiff -lMesaGLU -lMesaGL -lXm -lXmu -lXp -lXi -lXext -lXt -lX11 -lpthread -ldb -lutil -ldl -lm' >> makefile
           else
             echo 'INCLUDE=$(TIFFINCLUDE) -I$(PYTHONINCLUDE)' >> makefile
             echo 'LIBRARY=$(LIBPYTHON)' >> makefile
             echo 'LIBS=-L/usr/local/lib -l${PYTHONVERSION} -ltiff -lGLU -lGL -L/usr/X11R6/lib -lXm -lXp -lXi -lXext -lXt -lX11 -lpthread -ldb -lutil -ldl -lm' >> makefile
           fi
           ;;
#
# This is FreeBSD
#
  FreeBSD) dir="$os"
           makedir
           cat ../.config.$os > makefile
           echo 'OPT=-O6 -m486 -fomit-frame-pointer' >> makefile
           echo 'CFLAGS=-Wall -DLINUX' >> makefile
           echo 'LDFLAGS=' >> makefile
	     echo 'SCANDIR=' >> makefile
           if [ "$use_mesa" = "yes" ]
           then
             echo 'INCLUDE=$(MESAINCLUDE) -I$(TIFFINCLUDE) -I$(PYTHONINCLUDE)' >> makefile
             echo 'LIBRARY=$(MESALIB) $(LIBTIFF) $(LIBPYTHON)' >> makefile
             echo 'LIBS=-L/usr/X11R6/lib -l${PYTHONVERSION} -ltiff -lMesaGLU -lMesaGL -lXm -lXmu -lXi -lXext -lXt -lX11 -lm' >> makefile
           else
             echo 'INCLUDE=$(TIFFINCLUDE) -I$(PYTHONINCLUDE)' >> makefile
             echo 'LIBRARY=$(LIBTIFF) $(LIBPYTHON)' >> makefile
             echo 'LIBS=-l${PYTHONVERSION} -ltiff -lGLU -lGL -lXm -lXmu -lXi -lXext -lXt -lX11 -lm' >> makefile
           fi
           ;;
#
# This is OSF1
#
  OSF1)    dir="$os"
           makedir
           cat ../.config.$os > makefile
           echo 'OPT=-O2 -Olimit 615' >> makefile
           echo 'CFLAGS=-ieee_with_no_inexact' >> makefile
           echo 'LDFLAGS=' >> makefile
	     echo 'SCANDIR=' >> makefile
	     if [ "$use_mesa" = "yes" ]
	     then
             echo 'INCLUDE=$(MESAINCLUDE) -I$(TIFFINCLUDE) -I$(PYTHONINCLUDE)' >> makefile
             echo 'LIBRARY=$(MESALIB) $(LIBTIFF) $(LIBPYTHON)' >> makefile
             echo 'LIBS=-l${PYTHONVERSION} -ltiff -lMesaGLU -lMesaGL -lXm -lXmu -lXi -lXext -lXt -lX11 -lm' >> makefile
           else
             echo 'INCLUDE=$(TIFFINCLUDE) -I$(PYTHONINCLUDE)' >> makefile
             echo 'LIBRARY=$(LIBTIFF) $(LIBPYTHON)' >> makefile
             echo 'LIBS=-l${PYTHONVERSION} -lXm -lXmu -lXi -lXt -lGLU -lGL -lX11 -ltiff -lm' >> makefile
	     fi
	     ;;
#
# This is HP-UX
#
  HP-UX)   dir="$os"
           makedir
           cat ../.config.$os > makefile
           echo 'OPT=-O2' >> makefile
           echo 'CFLAGS=-Aa -D_HPUX_SOURCE' >> makefile
           echo 'LDFLAGS=' >> makefile
	     echo 'SCANDIR=' >> makefile
	     if [ "$use_mesa" = "yes" ]
	     then
             echo 'INCLUDE=$(MESAINCLUDE) -I$(TIFFINCLUDE) -I/usr/include/Motif1.2 -I/usr/include/X11R5 -I$(PYTHONINCLUDE)' >> makefile
             echo 'LIBRARY=$(MESALIB) $(LIBTIFF) -L/usr/lib/Motif1.2 -L/usr/lib/X11R5 $(LIBPYTHON)' >> makefile
             echo 'LIBS=-l${PYTHONVERSION} -ltiff -lMesaGLU -lMesaGL -lXm -lXi -lXext -lXt -lX11 -lm' >> makefile
           else
             echo 'INCLUDE=$(TIFFINCLUDE) -I/usr/include/Motif1.2 -I/usr/include/X11R5 -I$(PYTHONINCLUDE)' >> makefile
             echo 'LIBRARY=$(LIBTIFF) -L/usr/lib/Motif1.2 -L/usr/lib/X11R5 $(LIBPYTHON)' >> makefile
             echo 'LIBS=-l${PYTHONVERSION} -lXm -lXmu -lXi -lXt -lGLU -lGL -lX11 -ltiff -lm' >> makefile
	     fi
	     ;;
#
# This is SunOS
#
  SunOS)   dir="$os"
           makedir
           cat ../.config.$os > makefile
           echo 'OPT=-xO4 -native' >> makefile
           echo 'CFLAGS= -DSunOS -xlibmil' >> makefile
           echo 'LDFLAGS=-L/usr/dt/lib ' >> makefile
	     echo 'SCANDIR=scandir.o' >> makefile
           if [ "$use_mesa" = "yes" ]
           then
             echo 'INCLUDE=$(MESAINCLUDE) -I$(TIFFINCLUDE) -I$(PYTHONINCLUDE)' >> makefile
             echo 'LIBRARY=$(MESALIB) $(LIBTIFF) $(LIBPYTHON)' >> makefile
             echo 'LIBS=-l${PYTHONVERSION} -lMesaGLU -lMesaGL -lXm -lXmu -lXi -lXt -lX11 -lm -Bstatic -ltiff -Bdynamic' >> makefile
           else
             echo 'INCLUDE=$(TIFFINCLUDE) -I$(PYTHONINCLUDE)' >> makefile
             echo 'LIBRARY=$(LIBTIFF) $(LIBPYTHON)' >> makefile
             echo 'LIBS=-l${PYTHONVERSION} -lXm -lXmu -lXi -lXt -lGLU -lGL -lX11 -lm -Bstatic -ltiff -Bdynamic ' >> makefile
           fi
           ;;
#
# 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
