#!/bin/sh
#
#   Copyright (C) 1987-2000 by Jeffery P. Hansen
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Last edit by hansen on Mon Feb 26 16:42:40 2001
#
# Configure options
#  -verbose    Print compiler messages while building makefile.
#  -noedit     Do not update the config.h file.
#  -nomkmf     Do not make the imakefile/makefile.
#  -test       Do configuration tests only (same as "-noedit -nomkmf")

##################################################################
#
# Configurable parameters
#

# Note: All options (except list of compilers) are now set in config.h

# List of possible compliers
compilers="gcc cc"

##################################################################
#
# Parsing configure options
#
verbose=0
noedit=0
nomkmf=0
notests=0
for x in $*; do
  case X$x in
  X-v*)
    verbose=1
    ;;
  X-noe*)
    noedit=1
    ;;
  X-not*)
    notests=1
    ;;
  X-nom*)
    nomkmf=1
    ;;
  X-t*)
    nomkmf=1
    noedit=1
    ;;
  esac
done


##################################################################
#
# Initialize some variables (add to these as we find what we need)
#
LPATH=""
IPATH=""
LIBS=""
TCLTKOK=1

##################################################################
#
# Get current directory to set up a backup location for the
# TkGate home directory.
#
PWD=`pwd`


error()
{
  echo ""
  echo "ERROR: $1"
  echo ""
  exit
}

ccerror()
{
  X=$compilers
  echo ""
  echo "ERROR: $1"
  echo ""
  echo "Your machine does not seem to have a compiler, or the compiler"
  echo "has a non-standard name.  I tried the commands listed below: "
  echo "     $compilers"
  echo ""
  echo "If the compiler on your machine has a different name, add it to"
  echo "the list of compilers in the variable 'compilers' at the top"
  echo "of this (configure) script.  If you really do not have a compiler"
  echo "for your machine, I suggest you obtain gnu's gcc (www.gnu.org)."
  exit
}

ttxerror()
{
  echo ""
  echo "ERROR: $1"
  echo ""
  echo "If you get this message, it may be because a required component"
  echo "such as Tcl 8.0, Tk 8.0 or X11 was missing or could not be found."
  echo "See the section on 'NON STANDARD TCL/TK LOCATIONS' in the README"
  echo "file for more information."
  exit
}

goerror()
{
  echo ""
  echo "ERROR: $1"
  echo ""
  echo "Your machine does not seem to have the getopt function, used to"
  echo "parse command line options.  You can probably get a copy of it"
  echo "from GNU web site (www.gnu.org) as part of the GNU C library."
  echo ""
  exit
}

##################################################################
#
# Set a script variable
#
setconfigvar()
{
  cat > ltest.c << -eof-
#include <stdio.h>
#include "config.h"
-eof-
  echo "main() { puts($2); }" >> ltest.c
  $CC ltest.c -o ltest
  AAA=`./ltest`
  eval $1='"'${AAA}'"'
  rm -f ltest.c ltest
}

##################################################################
#
# Do special language-specific setup
#
langsetup()
{
  if echo $tcltk_ver | egrep '^.*jp$' > /dev/null; then
    echo "Configuring with EUC-based Japanese support."
    optionallibnames="$jp_optionallibnames"
    cp messages.ja.euc messages.ja
    UNICODE_JSUPPORT=0
    JSUPPORT=1
  elif [ X$lang = Xujp ]; then
    if [ X$tcltk_var = X8.0 ]; then
      echo "Unicode based-Japanese not support in this version of tcl/tk." 
      echo "Please set TKGATE_LANG in config.h to either 'en' or 'jp'."
      exit
    fi
    echo "Configuring for unicode-based Japanese support."
    cp messages.ja.utf8 messages.ja
    UNICODE_JSUPPORT=1
    JSUPPORT=1
  else
    echo "Configuring for Western language support."
    cp messages.ja.euc messages.ja
    UNICODE_JSUPPORT=0
    JSUPPORT=0
  fi
}


##################################################################
#
# Check the OS type to determine if signal() handlers need to be
# reintalled after each invocation. 
#
checkos()
{
  case `uname` in
    FreeBSD)
      RESIGNAL=0
      SIGSET=0
      ;;
    SunOS)
      RESIGNAL=1
      SIGSET=1
      ;;
    *)
      RESIGNAL=1
      SIGSET=0
      ;;
  esac
}

##################################################################
#
# findincl var file
#
# Search for the include file 'file' in the standard places and
# assign the directory path to 'var'.  Return value indicates
# if the file was found.
#
findincl()
{
  for a in $incdirs; do
    if [ -f $a/$2 ]; then
      eval $1=$a
      return 0
    fi
  done
  return 1
}

##################################################################
#
# findlib var lib
#
# Search for the library 'lib' in the standard places and
# assign the directory path to 'var'.  Return value indicates
# if the library was found.
#
findlib()
{
  for a in $libdirs; do
    if [ -f $a/lib${2}.a ]; then
      eval $1=$a
      return 0
    fi
    if [ -f $a/lib${2}.so ]; then
      eval $1=$a
      return 0
    fi
  done
  return 1
}

##################################################################
#
# union set m 
#
# Add a member to a set if it is not already a member
#
union()
{
  eval "S=\${$1}"
  for a in $S; do
    if [ "X$a" = "X$2" ]; then
      return 0
    fi
  done

  eval "$1=\"$S $2\""

  return 0
} 

##################################################################
#
# Find the compiler
#
findcc()
{
  for cc in $compilers; do
    if which "$cc" > /dev/null; then
      CC=$cc
      break
    fi
  done

  if [ X$CC = "X" ]; then
    ccerror "unable to find any compilers."
  fi

  echo "C compiler: $CC"
}

##################################################################
#
# Verify we have bison/yacc
#
findyacc()
{
  AA=/`which bison`
  BB=/`which yacc`
  if [ -f "$AA" ]; then
    echo "bison found"
    YACC="bison -y"
  elif [ -f "$BB" ]; then
    echo "yacc found"
    YACC=yacc
  else
    error "Can't find bison or yacc!"
  fi
}

##################################################################
#
# Verify we have flex/lex
#
findlex()
{
  AA=/`which flex`
  BB=/`which lex`
  if [ -f "$AA" ]; then
    echo "flex found"
    LEX="flex -l"
  elif [ -f "$BB" ]; then
    echo "lex found"
    LEX=lex
  else
    error "Can't find flex or lex!"
  fi
}

##################################################################
#
# Check for yyrestart function.
#
checkyyrestart()
  {
  cat > yyrtest.l << -eof-
%%
[09]	{ return 0; }
%%

int yyerror(char *s)
{
    return 0;
}

int yywrap()
{
    return 0;
}

int main(int argc,char *argv[])
{
    FILE *f = 0;
    yyrestart(f);
}

-eof-

  if $LEX yyrtest.l; then
    echo "lex test OK."
  else
    rm -f yyrtest.l lex.yy.c yyrtest
    echo "Unexpected error: unable to lex test file."
    exit
  fi

  if $CC -o yyrtest lex.yy.c 2> /dev/null; then
    NOYYRESTART=0
    echo "yyrestart found."
  else
    NOYYRESTART=1
    echo "yyrestart not found"
  fi

  rm -f yyrtest.l lex.yy.c yyrtest
}

##################################################################
#
# Determine which version of tcl/tk to use and set path variables
#
find_tcltk_version()
{
  tcltk_ver=""

  if [ X$lang = Xjp ]; then
    tcltk_versions="$jp_tcltk_versions $tcltk_versions"
  fi

  if [ X$tcltk_ver = X ]; then
    for v in $tcltk_versions; do
      alt_v=`echo $v | sed s/\\\\.//`
      tk_lib="tk$v"
      tcl_lib="tcl$v"

      if findincl TCLINC tcl$v/tcl.h; then
          tcl_ipre="/tcl$v"
        else
        if findincl TCLINC tcl$alt_v/tcl.h; then
          tcl_ipre="/tcl$alt_v"
	else
	  if findincl TCLINC tcl.h; then
	    tcl_ipre=""
	  else
	    continue
	  fi
	fi
      fi
      if findincl TKINC tk$v/tk.h; then
          tk_ipre="/tk$v"
        else
        if findincl TKINC tk$alt_v/tk.h; then
          tk_ipre="/tk$alt_v"
	else
	  if findincl TKINC tk.h; then
	    tk_ipre=""
	  else
	    continue
	  fi
	fi
      fi

      if findlib TCLLIB tcl$v; then
          true
        else
        if findlib TCLLIB tcl$alt_v; then
          true
	else
	  continue
	fi
        tcl_lib="tcl$alt_v"
      fi
      if findlib TKLIB tk$v; then
          true
        else
        if findlib TKLIB tk$alt_v; then
          true
	else
	  continue
	fi
        tk_lib="tk$alt_v"
      fi

      tcltk_ver=$v
      break
    done
  fi

  if [ X$tcltk_ver != X ]; then
      TKINC="$TKINC$tk_ipre"
      TCLINC="$TCLINC$tcl_ipre"

      union IPATH -I$TKINC
      union IPATH -I$TCLINC

      union LIBS -l$tk_lib
      union LPATH -L$TKLIB

      union LIBS -l$tcl_lib
      union LPATH -L$TCLLIB

      echo "Tcl/Tk Version: $tcltk_ver"
  else
      echo "Tcl/Tk Version: not found"
      TCLTKOK=0
  fi

}

##################################################################
#
# Find tcl script files
#
findtclinit()
{
  if [ X"$TCLLIB" != X"" ]; then  
      D=$TCLLIB/tcl$tcltk_ver
      if [ -f $D/init.tcl ]; then
	echo "Tcl script files found in: $D"
      else
	echo "Warning: Tcl script files not found in: $D"
	TCLTKOK=0
      fi
  else
    TCLTKOK=0
  fi
}

##################################################################
#
# Find tk script files
#
findtkinit()
{
  if [ "$TKLIB" != "" ]; then  
      D=$TKLIB/tk$tcltk_ver
      if [ -f $D/button.tcl ]; then
	echo "Tk script files found in: $D"
      else
	echo "Warning: Tk script files not found in: $D"
	TCLTKOK=0
      fi
  else
    TCLTKOK=0
  fi
}

##################################################################
#
# Find X11/Xlib.h
#

findxh()
{
  if findincl XINC X11/Xlib.h; then
    echo "found X11/Xlib.h in: $XINC"
    if [ $XINC != "/usr/include" ]; then
      union IPATH -I$XINC
    fi
  else
    ttxerror "unable to find X11/Xlib.h"
  fi
}

#################################################################
#
# Find required libraries we might need
#
findreqlibs()
{
  for n in $requiredlibnames; do
    if findlib DIR $n; then
      union LIBS -l$n
      union LPATH -L$DIR
    else
      ttxerror "Unable to find library $n"
    fi
  done
}

##################################################################
#
# Find other libraries we might need
#
findoptlibs()
{
  for n in $optionallibnames; do
    if findlib DIR $n; then
      union LIBS -l$n
      union LPATH -L$DIR
    fi
  done
}

##################################################################
#
# Check for "long long" type.
#

checklonglong()
{
  cat > lltest.c << -eof-
#include <unistd.h>

int main(int argc,char *argv[])
{
  long long a,b,c;

  a = 0x7fffffff;
  b = 3;
  c = a*b;

  if (c > a)
    return 0;
  else
    return 1;  
}
-eof-

  if $CC -o lltest lltest.c 2> /dev/null && ./lltest; then
    USELONGLONG=1
    echo "long long supported"
  else
    USELONGLONG=0
    echo "long long not supported"
  fi

  rm -f lltest lltest.c
}

##################################################################
#
# Check for optreset
#
checkoptreset()
{
  cat > ortest.c << -eof-
#include <unistd.h>

main(int argc,char *argv[])
{
  int c;
  int n = 0;
  extern int optind;

  while (argc > 0) {
    while ((c = getopt(argc,argv,"x")) >= 0) {
      if (c == 'x') n++;
    }
    argc -= optind;
    argv += optind;
#ifdef OPTRESET
    optreset = 1;
#endif
    optind = 0;
    if (argc > 0) {
      argc--;
      argv++;
    }
  }
  printf("%d\n",n);
}
-eof-

  if $CC -o ortest ortest.c -DOPTRESET 2> /dev/null; then
    echo "getopt uses optreset."
    OPTRESET=1
  elif $CC -o ortest ortest.c 2> /dev/null; then
    echo "getopt does not use optreset."
    OPTRESET=0
  else
    goerror "Can not find getopt"
  fi

  if [ X`./ortest -x c -x` = X2 ]; then
    echo "getopt ok"
  else
    echo "WARNING: getopt does handle switches after file names."
  fi
  rm -f ortest ortest.c
}

##################################################################
#
# Show some of what we found out. 
#
showvalues()
{
  echo "*************************************************************"
  echo "Configuration Results:"
  if [ X$lang = Xen ]; then
    echo "  Language Support: Western (English and European)"
  elif [ X$lang = Xjp ]; then
    echo "  Language Support: English and Japanese"
  elif [ X$lang = Xujp ]; then
    echo "  Language Support: Unicode-based Japanese"
  else
    echo "Bogus language code"
    exit
  fi

  echo "  C Compiler:       $CC"
  echo "  Tcl/Tk Version:   $tcltk_ver"
  echo "  Library Path:     $LPATH"
  echo "  Include Path:     $IPATH"
  echo "  Libraries:        $LIBS"
  echo "  Primary Home:     $homedir"
  echo "  Secondary Home:   $PWD"
  echo "  optreset usage:   $OPTRESET"
  echo "  long long usage:  $USELONGLONG"
  echo "  Ephemeral Signal: $RESIGNAL"
  echo "  Use sigset:       $SIGSET"
  echo "  Yacc:             $YACC"
  echo "  Lex:              $LEX"
  echo "  No yyrestart:     $NOYYRESTART"
  echo "*************************************************************"
  echo "* Configuration complete - type 'make' to compile."
  echo "*************************************************************"
}

##################################################################
#
# Update config.h
#
editconfigh()
{
  cat > options.h << -eof-
#ifndef TKGATE_CC
#define TKGATE_CC		$CC
#endif
#ifndef OPTRESET
#define OPTRESET		$OPTRESET
#endif
#ifndef USELONGLONG
#define USELONGLONG		$USELONGLONG
#endif

#ifndef TKGATE_LPATH
#define TKGATE_LPATH		$LPATH
#endif
#ifndef TKGATE_IPATH
#define TKGATE_IPATH		$IPATH
#endif
#ifndef TKGATE_LIBS
#define TKGATE_LIBS		$LIBS
#endif

#ifndef TKGATE_HOMEDIR
#define TKGATE_HOMEDIR		"$homedir"
#endif

#ifndef TKGATE_SECONDARYHOME
#define TKGATE_SECONDARYHOME	"$PWD"
#endif

#ifndef TKGATE_RESIGNAL
#define TKGATE_RESIGNAL		$RESIGNAL
#endif
#ifndef TKGATE_SIGSET
#define TKGATE_SIGSET		$SIGSET
#endif

#ifndef TKGATE_YACC
#define TKGATE_YACC		$YACC
#endif
#ifndef TKGATE_LEX
#define TKGATE_LEX		$LEX
#endif
#ifndef TKGATE_NOYYRESTART
#define TKGATE_NOYYRESTART	$NOYYRESTART
#endif

#ifndef TKGATE_JSUPPORT
#define TKGATE_JSUPPORT		$JSUPPORT
#endif

#ifndef TKGATE_UNICODE_JSUPPORT
#define TKGATE_UNICODE_JSUPPORT		$UNICODE_JSUPPORT
#endif

#define TKGATE_GDF		`echo gdf/*.gdf`
#define TKGATE_MESSAGES		`echo messages.*`
#define TKGATE_EXAMPLE1		`echo examples/ex1/*`
#define TKGATE_EXAMPLE2		`echo examples/ex2/*`
#define TKGATE_EXAMPLE3		`echo examples/ex3/*`
#define TKGATE_EXAMPLE4		`echo examples/ex4/*`
#define TKGATE_EXAMPLE5		`echo examples/ex5/*`
#define TKGATE_TUTORIALS	`echo examples/tutorials/*`
#define TKGATE_JATUTORIALS	`echo examples/ja-tutorials/*`
#define TKGATE_BITMAPS		`echo bitmaps/*`
#define TKGATE_SCRIPTS		`echo scripts/*.tcl`
#define TKGATE_DOCS		`echo doc/*.html doc/*.txt doc/*.gm`
#define TKGATE_DOCFIGS		`echo doc/fig/*`
-eof-

  BEGINLN=`grep -n BEGIN-AUTO config.h | sed 's/:.*//'`
  ENDLN=`grep -n END-AUTO config.h| sed 's/:.*//'`
  mv config.h aaa.h
  head -$BEGINLN aaa.h > config.h
  cat options.h >> config.h
  tail +$ENDLN aaa.h >> config.h
  mv aaa.h config.h.bak
  rm -f options.h
}

obsolete()
{
  rm -f edcmd
  echo '/BEGIN-AUTO/' >> edcmd
  echo '.,/END-AUTO/ d' >> edcmd
  echo '.-1 r options.h' >> edcmd
  echo w >> edcmd
  echo q >> edcmd
  ed config.h < edcmd > /dev/null
  echo "config.h updated."
  rm -f edcmd options.h
}

##################################################################
#
# Do the xmkmf
#
doxmkmf()
{
  echo "* doing top-level imake"
  xmkmf
  (cd src/common; echo "* doing imake of common"; xmkmf)
  (cd src/tkgate; echo "* doing imake of tkgate"; xmkmf)
  (cd src/gsim; echo "* doing imake of gsim"; xmkmf)
  (cd src/gmac; echo "* doing imake of gmac"; xmkmf)
}

domkinclude()
{
  make includes
}

domkdep()
{
  make depend
}

tcltk_ok_check()
{

  if [ $TCLTKOK != 1 ]; then
    echo ""
    echo "#############################################################################"
    echo "WARNING: I was unable to find one or more required tcl/tk components.  If you"
    echo "are sure you have tcl/tk on your system, you may need to manually configure"
    echo "the location.  Uncomment and modify the TKGATE_IPATH, TKGATE_LPATH, TKGATE_LIBS"
    echo "TCL_LIBRARY and TK_LIBRARY variables.  See the README file for more details."
    echo "#############################################################################"
    exit
  fi
}

if [ $notests = 0 ]; then 
    checkos
    findcc
    setconfigvar lang TKGATE_LANG
    setconfigvar gateversion TKGATE_VERSION
    setconfigvar homedirbase TKGATE_HOMEDIRBASE
    setconfigvar incdirs TKGATE_INCDIRS
    setconfigvar libdirs TKGATE_LIBDIRS
    setconfigvar requiredlibnames TKGATE_REQLIBNAMES
    setconfigvar optionallibnames TKGATE_OPTLIBNAMES
    setconfigvar jp_optionallibnames TKGATE_JPOPTLIBNAMES

    setconfigvar tcltk_versions TKGATE_TCLTK_VERSIONS
    setconfigvar jp_tcltk_versions TKGATE_JP_TCLTK_VERSIONS

    homedir="${homedirbase}/tkgate-$gateversion"
    find_tcltk_version
    tcltk_ok_check
    langsetup
    findtclinit
    findtkinit
    findyacc
    findlex
    checkyyrestart
    findxh
    findreqlibs
    findoptlibs
    checklonglong
    checkoptreset

    if [ $noedit = 0 ]; then
      echo "*** updating config.h"
      editconfigh
    fi
fi

if [ $nomkmf = 0 ]; then
  if [ $verbose = 1 ]; then 
    echo "*** doing xmkmf"
    doxmkmf
    echo "*** doing includes"
    domkinclude
    echo "*** doing dependencies"
    domkdep
  else
    echo "*** doing xmkmf"
    doxmkmf > /dev/null 
    echo "*** doing includes"
    domkinclude > /dev/null 
    echo "*** doing dependencies"
    domkdep > /dev/null 
  fi
fi

if [ $notests = 0 ]; then 
  showvalues
fi




