#!/bin/sh
#+##############################################################################
#                                                                              #
# File: configure                                                              #
#                                                                              #
# Description: simplistic configure script to detect and check the system      #
#              and to assemble the proper Makefile.sys and config.h            #
#                                                                              #
#-##############################################################################

# @(#)configure	1.14	01/04/00	Written by Lionel Cons

#
# argument parsing
#
usage="Usage: $0 [-debug] [-noicc] [-gcc [-wall]] [-optim flag]"
debug=""
noicc=0
cc=""
optim="-O "
strip="-s "
xsock="/tmp/.X11-unix/X%d"
while [ $# -gt 0 ]; do
  case $1 in
    -debug|--debug)    debug="-DDEBUG "; shift;;
    -noicc|--noicc)    noicc=1; shift;;
    -gcc|--gcc)        cc="gcc"; shift;;
    -optim|--optim)    optim="$2 "; strip=""; shift; shift;;
    -wall|--wall)      cc="gcc"; optim="-Wall -W -Wshadow -Wpointer-arith \
-Wcast-align -Wmissing-prototypes -Wmissing-declarations -Wnested-externs \
-Waggregate-return $optim"; shift;;
    *)                 echo $usage; exit 1;;
  esac
done

#
# system checks
#
uname=`uname -s`
rev=`uname -r`
case $uname in
  AIX)          os="AIX";;
  HP-UX)        os="HP-UX";;
  IRIX|IRIX64)  os="IRIX";;
  Linux)        os="Linux";;
  OSF1)         os="OSF1";;
  SunOS)        if [ $rev -ge 5 ]; then os="Solaris"; else os="SunOS"; fi;;
  ULTRIX)       os="ULTRIX";;
  *) echo "Sorry, $uname is not yet supported"; exit 1;;
esac
echo "This seems to be a $os machine..."

#
# start the Makefile.sys
#
rm -f Makefile.sys
cat <<EOT > Makefile.sys
#
# system dependent Makefile generated by $0
# (ran by $USER@`hostname` on `date`)
#

EOT

#
# AIX 3.* and 4.*
#
if [ "$os" = "AIX" ]; then
  if [ ! -f /usr/lib/libXau.a ]; then
    # oops, Xau not in the standard place
    if [ -f /usr/lpp/X11/lib/libXau.a ]; then
      echo "XAU_LIB = /usr/lpp/X11/lib/libXau.a" >> Makefile.sys
    else
      echo "*** can't find the Xau library"
      exit 1
    fi
  fi
  echo "SYS_LDFLAGS = $strip" >> Makefile.sys
fi

#
# HP-UX 9.* and 10.*
#
if [ "$os" = "HP-UX" ]; then
  if [ "x$cc" = "x" ]; then
    flags="-Aa -D_HPUX_SOURCE -DANSI_CPP"
  else
    flags=""
  fi
  # the X sockets are not in the default place :-(
  if [ -f /stand/vmunix ]; then
    xsock="/var/spool/sockets/X11/%d"
  else
    xsock="/usr/spool/sockets/X11/%d"
  fi
  x="X11R5"
  m="Motif1.2"
  cat <<EOT >> Makefile.sys
SYS_CFLAGS = $flags -I/usr/include/$x -I/usr/include/$m -I/usr/local/include
SYS_LDFLAGS = $strip-L/usr/lib/$x -L/usr/lib/$m -L/usr/local/lib
EOT
  unset flags x m
fi

#
# IRIX
#
if [ "$os" = "IRIX" ]; then
  # the linker complains when Xau is after X11:
  # ld32: ERROR 33: Unresolved data symbol "_XLockMutex_fn" ...
  cat <<EOT >> Makefile.sys
SYS_LIBS = -lX11
SYS_LDFLAGS = $strip
EOT
fi

#
# Linux 2.*
#
if [ "$os" = "Linux" ]; then
  # everything should be under /usr/X11R6
  # statically link Motif with explicit library name
  # add also extra libraries to please some linkers
  # and add -lXp when available (needed by Motif)
  # Motif could come from Lesstif which could be in /usr/local
  extra=""
  if [ -f "/usr/X11R6/lib/libXp.a" ]; then
    extra="$extra -lXp"
  fi
  if [ -f "/usr/X11R6/lib/libXpm.a" ]; then
    extra="$extra -lXpm"
  fi
  xmlib=""
  for dir in /usr/X11R6/lib /usr/local/lib; do
    if [ -f "$dir/libXm.a" ]; then
      xmlib="$dir/libXm.a"
      break
    elif [ -f "$dir/libXm.so" ]; then
      xmlib="-lXm"
      break
    fi
  done
  if [ "x$xmlib" = "x" ]; then
    echo "*** can't find the Xm library"
    exit 1
  fi
  cat <<EOT >> Makefile.sys
MOTIF_LIB = $xmlib
SYS_CFLAGS = -I/usr/X11R6/include
SYS_LDFLAGS = $strip-L/usr/X11R6/lib
SYS_LIBS = -lXext $extra
EOT
  unset extra
fi

#
# OSF1 3.* and 4.*
#
if [ "$os" = "OSF1" ]; then
  echo "SYS_LDFLAGS = $strip" >> Makefile.sys
fi

#
# SunOS 4.*
#
if [ "$os" = "SunOS" ]; then
  # the Motif stuff should be under /usr/motif12
  # the X11 include files and libraries come from /usr/local
  # statically link Motif with explicit library name
  cat <<EOT >> Makefile.sys
MOTIF_LIB = /usr/motif12/usr/lib/libXm.a
SYS_CFLAGS = -I/usr/motif12/usr/include -I/usr/local/include
SYS_LDFLAGS = $strip-L/usr/local/lib
EOT
  cc="gcc"
fi

#
# SunOS 5.*
#
if [ "$os" = "Solaris" ]; then
  # the Motif include files and libraries should be under /usr/dt
  # the X11 include files and libraries come from /usr/openwin
  # we hard-code /usr/dt/lib with -R to avoid relying on LD_LIBRARY_PATH for Motif
  cat <<EOT >> Makefile.sys
SYS_CFLAGS = -DSOLARIS -I/usr/dt/include -I/usr/openwin/include
SYS_LDFLAGS = $strip-R/usr/dt/lib -L/usr/dt/lib -L/usr/openwin/lib
SYS_LIBS = -lsocket -lnsl
EOT
fi

#
# ULTRIX 4.*
#
if [ "$os" = "ULTRIX" ]; then
  echo "SYS_LDFLAGS = $strip" >> Makefile.sys
fi

#
# end the Makefile with the CFLAGS and CC
#
cat <<EOT >> Makefile.sys
CFLAGS = ${optim}${debug}\$(SYS_CFLAGS)
EOT
if [ "x$cc" != "x" ]; then
  echo "CC = $cc" >> Makefile.sys
fi

#
# assemble the config.h
#
rm -f config.h
cat <<EOT > config.h
/*
 * system dependent config.h generated by $0
 * (ran by $USER@`hostname` on `date`)
 */

#define DEFAULT_XSOCK "$xsock"
EOT
if [ $noicc -gt 0 ]; then
  echo "#define FORBID_ICC" >> config.h
else
  echo "#undef  FORBID_ICC" >> config.h
fi
