#! /bin/sh
#*******************************************************************************
#                                                                              *
#                                   Viewmol                                    *
#                                                                              *
#                             G E T M A C H I N E                              *
#                                                                              *
#                 Copyright (c) Joerg-R. Hill, December 1997                   *
#                                                                              *
#*******************************************************************************
#
# $Id: getmachine,v 1.2 1998/01/26 00:47:46 jrh Exp jrh $
# $Log: getmachine,v $
# 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
}

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

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

if [ ! -f .config.$os ]
then
  echo 'If you want to use environment variables in any of the following'
  echo 'paths, do it like this: $(HOME)'
  echo 'Please enter the name of the directory where libtiff.a can be found: \c'
  read libtiff
  echo "LIBTIFF = -L$libtiff" > .config.$os
  echo 'Please enter the name of the directory where the include file for libtiff can be found: \c'
  read tiffinclude
  echo "TIFFINCLUDE = $tiffinclude" >> .config.$os
  if [ "$use_mesa" = "yes" ]
  then
    echo 'Pleae enter the name of the directory where the Mesa libraries can be found: \c'
    read mesalib
    echo "MESALIB = -L$mesalib" >> .config.$os
    echo 'Pleae enter the name of the directory where the include files for Mesa can be found: \c'
    read mesainclude
    echo "MESAINCLUDE = $mesainclude" >> .config.$os
  fi
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=' >> makefile
	     echo 'LDFLAGS=' >> makefile
	     if [ "$use_mesa" = "yes" ]
	     then
             echo 'INCLUDE=$(MESAINCLUDE) -I$(TIFFINCLUDE)' >> makefile
             echo 'LIBRARY=$(MESALIB) $(LIBTIFF)' >> makefile
             echo 'LIBS=-ltiff -lGLw -lMesaGLU -lMesaGL -lXm -lXmu -lXext -lXt -lX11 -lm' >> makefile
	     else
             echo 'INCLUDE=/usr/lpp/OpenGL/include -I$(TIFFINCLUDE)' >> makefile
             echo 'LIBRARY=-L/usr/lpp/OpenGL/lib $(LIBTIFF)' >> makefile
             echo 'LIBS=-ltiff -lXGLW -lGLU -lGL -lXm -lXmu -lXext -lXt -lX11 -lm' >> makefile
	     fi
	     echo 'STAT=' >> makefile
           ;;
#
# 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_R5000" ]
           then
             echo 'CFLAGS=-DIRIX -n32' >> makefile
             echo 'LDFLAGS=-n32' >> makefile
	     elif [ "$dir" = "IRIX64_R8000" -o "$dir" = "IRIX64_R10000" ]
           then
             echo 'CFLAGS=-DIRIX -n64' >> makefile
             echo 'LDFLAGS=-n64' >> makefile
	     else
             echo 'CFLAGS=-DIRIX' >> makefile
             echo 'LDFLAGS=' >> makefile
           fi
	     if [ "$use_mesa" = "yes" ]
	     then
             echo 'INCLUDE=$(MESAINCLUDE) -I$(TIFFINCLUDE)' >> makefile
             echo 'LIBRARY=$(MESALIB) $(LIBTIFF)' >> makefile
             echo 'LIBS=-ltiff -lMesaGLw -lMesaGLU -lMesaGL -lXm -lXext -lXt -lX11 -lm' >> makefile
	     else
             echo 'INCLUDE=$(TIFFINCLUDE)' >> makefile
             echo 'LIBRARY=$(LIBTIFF)' >> makefile
             echo 'LIBS=-lGLw -lGLU -lGL -lXm -lXt -lX11 -ltiff -lm' >> makefile
           fi
           echo 'STAT=' >> makefile
           ;;
#
# This is Linux.
#
  Linux)   dir="$os"
           makedir
           cat ../.config.$os > makefile
           processor=`cat /proc/cpuinfo | grep cpu | grep -v cpuid | awk '{printf("%s\n", $3);}'`
           if [ "$processor" = "586" -o "$processor" = "686" ]
           then
             echo 'OPT=-O6 -mpentium -fomit-frame-pointer -ffast-math -malign-double' >> makefile
           else
             echo 'OPT=-O6 -m486 -fomit-frame-pointer' >> makefile
           fi
           echo 'CFLAGS=-Wall -DLINUX' >> makefile
           echo 'LDFLAGS=' >> makefile
	     if [ "$use_mesa" = "yes" ]
	     then
             echo 'INCLUDE=$(MESAINCLUDE) -I$(TIFFINCLUDE)' >> makefile
             echo 'LIBRARY=$(MESALIB)' >> makefile
             echo 'LIBS=-L/usr/X11R6/lib -ltiff -lGLw -lMesaGLU -lMesaGL -lXm -lXmu -lXext -lXt -lX11 -lm' >> makefile
           else
             echo 'INCLUDE=$(TIFFINCLUDE)' >> makefile
             echo 'LIBRARY=' >> makefile
             echo 'LIBS=-ltiff -lGLw -lGLU -lGL -lXm -lXext -lXt -lX11 -lm' >> makefile
           fi
           echo 'STAT=stat.o' >> makefile
           ;;
#
# 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
	     if [ "$use_mesa" = "yes" ]
	     then
             echo 'INCLUDE=$(MESAINCLUDE) -I$(TIFFINCLUDE)' >> makefile
             echo 'LIBRARY=$(MESALIB) $(LIBTIFF)' >> makefile
             echo 'LIBS=-ltiff -lMesaGLw -lMesaGLU -lMesaGL -lXm -lXmu -lXext -lXt -lX11 -lm' >> makefile
           else
             echo 'INCLUDE=$(TIFFINCLUDE)' >> makefile
             echo 'LIBRARY=$(LIBTIFF)' >> makefile
             echo 'LIBS=-lGLw -lXm -lXt -lGLU -lGL -lX11 -ltiff -lm' >> makefile
	     fi
           echo 'STAT=' >> makefile
	     ;;
#
# 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
	     if [ "$use_mesa" = "yes" ]
	     then
             echo 'INCLUDE=$(MESAINCLUDE) -I$(TIFFINCLUDE) -I/usr/include/Motif1.2 -I/usr/include/X11R5' >> makefile
             echo 'LIBRARY=$(MESALIB) $(LIBTIFF) -L/usr/lib/Motif1.2 -L/usr/lib/X11R5' >> makefile
             echo 'LIBS=-ltiff -lGLw -lMesaGLU -lMesaGL -lXm -lXext -lXt -lX11 -lm' >> makefile
           else
             echo 'INCLUDE=$(TIFFINCLUDE) -I/usr/include/Motif1.2 -I/usr/include/X11R5' >> makefile
             echo 'LIBRARY=$(LIBTIFF) -L/usr/lib/Motif1.2 -L/usr/lib/X11R5' >> makefile
             echo 'LIBS=-lGLw -lXm -lXt -lGLU -lGL -lX11 -ltiff -lm' >> makefile
	     fi
           echo 'STAT=' >> 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
