#! /bin/sh

#########################################################################
#                                                                       #
#                                 OCaml                                 #
#                                                                       #
#            Xavier Leroy, projet Cristal, INRIA Rocquencourt           #
#                                                                       #
#   Copyright 1999 Institut National de Recherche en Informatique et    #
#   en Automatique.  All rights reserved.  This file is distributed     #
#   under the terms of the GNU Library General Public License, with     #
#   the special exception on linking described in file LICENSE.         #
#                                                                       #
#########################################################################

configure_options="$*"
where=`ocamlc -where 2> /dev/null || echo '/usr/local/lib/ocaml'`
tk_defs=''
tk_libs=''
tk_x11=yes
verbose=no

# The inf(), wrn(), err() functions below can be used to provide a consistent
# way to notify the user. The notification is always given to the stdout
# descriptor.
inf() {
  printf "%b\n" "$*" 1>&3
}

wrn() {
  printf "[WARNING] %b\n" "$*" 1>&3
}

err() {
  printf "[ERROR!]%b\n" "$*" 1>&3
  exit 2
}

exec 3>&1

# Parse command-line arguments

if echo "$configure_options" | grep -q -e '--\?[a-zA-Z0-9-]\+='; then
  err "Arguments to this script look like '-libdir /foo/bar', not '-libdir=/foo/bar' (note the '=')."
fi

while : ; do
  case "$1" in
    "") break;;
    -libdir|--libdir)
        where=$2; shift;;
    -tkdefs*|--tkdefs*)
        tk_defs=$2; shift;;
    -tklibs*|--tklibs*)
        tk_libs=$2; shift;;
    -tk-no-x11|--tk-no-x11)
        tk_x11=no;;
    -verbose|--verbose)
        verbose=yes;;
    *) err "Unknown option \"$1\".";;
  esac
  shift
done

export verbose

# Sanity checks

case "$where" in
  /*) ;;
   *) err "The -prefix directory must be absolute.";;
esac

# Generate the files

cd config/auto-aux
rm -f Makefile
touch Makefile

# Write options to Makefile

echo "# generated by ./configure $configure_options" >> Makefile

# Where to install

echo "WHERE=$where" >> Makefile

# Include OCaml configuration

echo "include $where/Makefile.config" >> Makefile

# Look for tcl/tk

inf "Configuring LablTk..."

if test $tk_x11 = no; then
  has_tk=true
else
  tk_x11_include=`cat $where/Makefile.config | grep '^X11_INCLUDES=' | sed -e 's/^X11_INCLUDES=//'`
  tk_x11_libs=`cat $where/Makefile.config | grep '^X11_LIBS=' | sed -e 's/^X11_LIBS=//'`
  has_tk=true
fi

cc=`cat $where/Makefile.config | grep '^NATIVECC=' | sed -e 's/^NATIVECC=//'`
cclibs=`cat $where/Makefile.config | grep '^NATIVECCLIBS=' | sed -e 's/^NATIVECCLIBS=//'`
export cc cclibs

if test $has_tk = true; then
  tcl_version=''
  tcl_version=`sh ./runtest $tk_defs $tk_x11_include tclversion.c`
  for tk_incs in \
    "-I/usr/local/include" \
    "-I/usr/include" \
    "-I/usr/local/include/tcl8.6 -I/usr/local/include/tk8.6" \
    "-I/usr/include/tcl8.6 -I/usr/include/tk8.6" \
    "-I/usr/local/include/tcl8.5 -I/usr/local/include/tk8.5" \
    "-I/usr/include/tcl8.5 -I/usr/include/tk8.5" \
    "-I/usr/local/include/tcl8.4 -I/usr/local/include/tk8.4" \
    "-I/usr/include/tcl8.4 -I/usr/include/tk8.4" \
    "-I/usr/local/include/tcl8.3 -I/usr/local/include/tk8.3" \
    "-I/usr/include/tcl8.3 -I/usr/include/tk8.3" \
    "-I/usr/local/include/tcl8.2 -I/usr/local/include/tk8.2" \
    "-I/usr/include/tcl8.2 -I/usr/include/tk8.2" \
    "-I/sw/include" \
    "-I/usr/pkg/include"
  do if test -z "$tcl_version"; then
    tk_defs="$tk_incs"
    tcl_version=`sh ./runtest $tk_defs $tk_x11_include tclversion.c`
  fi; done
  if test -n "$tcl_version" && test "x$tcl_version" != "xnone"; then
    inf "tcl.h and tk.h version $tcl_version found with \"$tk_defs\"."
    case $tcl_version in
    8.6) tclmaj=8 tclmin=6 tkmaj=8 tkmin=6 ;;
    8.5) tclmaj=8 tclmin=5 tkmaj=8 tkmin=5 ;;
    8.4) tclmaj=8 tclmin=4 tkmaj=8 tkmin=4 ;;
    8.3) tclmaj=8 tclmin=3 tkmaj=8 tkmin=3 ;;
    8.2) tclmaj=8 tclmin=2 tkmaj=8 tkmin=2 ;;
    8.1) tclmaj=8 tclmin=1 tkmaj=8 tkmin=1 ;;
    8.0) tclmaj=8 tclmin=0 tkmaj=8 tkmin=0 ;;
    7.6) tclmaj=7 tclmin=6 tkmaj=4 tkmin=2 ;;
    7.5) tclmaj=7 tclmin=5 tkmaj=4 tkmin=1 ;;
    *) wrn "This version is not known." ; has_tk=false ;;
    esac
  else
    inf "tcl.h and/or tk.h not found."
    has_tk=false
  fi
fi

system=`cat $where/Makefile.config | grep '^SYSTEM=' | sed -e 's/^SYSTEM=//'`
tkauxlibs="$cclibs"
tcllib=''
tklib=''
if test $has_tk = true; then
  if test -n "$tk_libs" && \
     sh ./hasgot $tk_libs $tk_x11_libs $tkauxlibs Tcl_DoOneEvent
  then tk_libs="$tk_libs $dllib"
  elif sh ./hasgot $tk_libs -ltcl$tclmaj.$tclmin $tkauxlibs Tcl_DoOneEvent
  then
    tk_libs="$tk_libs -ltk$tkmaj.$tkmin -ltcl$tclmaj.$tclmin $dllib"
  elif sh ./hasgot $tk_libs -ltcl$tclmaj$tclmin $tkauxlibs Tcl_DoOneEvent
  then
    tk_libs="$tk_libs -ltk$tkmaj$tkmin -ltcl$tclmaj$tclmin $dllib"
  elif test -z "$tk_libs" && tk_libs=-L/usr/local/lib && \
    sh ./hasgot $tk_libs -ltcl$tclmaj.$tclmin $tkauxlibs Tcl_DoOneEvent
  then
    tk_libs="$tk_libs -ltk$tkmaj.$tkmin -ltcl$tclmaj.$tclmin $dllib"
  elif sh ./hasgot $tk_libs -ltcl$tclmaj$tclmin $tkauxlibs Tcl_DoOneEvent
  then
    tk_libs="$tk_libs -ltk$tkmaj$tkmin -ltcl$tclmaj$tclmin $dllib"
  elif sh ./hasgot -L/sw/lib $tk_libs -ltcl$tclmaj.$tclmin $tkauxlibs \
                   Tcl_DoOneEvent
  then tk_libs="-L/sw/lib -ltk$tkmaj.$tkmin -ltcl$tclmaj.$tclmin $dllib"
  elif sh ./hasgot -L/usr/pkg/lib $tk_libs $tk_x11_libs \
        -ltk$tkmaj$tkmin -ltcl$tclmaj$tclmin -lpthread $tkauxlibs \
                   Tcl_DoOneEvent
  then
    case "$system" in
      *bsd*) tk_libs="-R/usr/pkg/lib -L/usr/pkg/lib $tk_libs $tk_x11_libs -ltk$tkmaj$tkmin -ltcl$tclmaj$tclmin -lpthread $tkauxlibs";;
      *) tk_libs="-L/usr/pkg/lib $tk_libs $tk_x11_libs -ltk$tkmaj$tkmin -ltcl$tclmaj$tclmin -lpthread $tkauxlibs";;
    esac
  else
    inf "Tcl library not found."
    has_tk=false
  fi
fi

if test $has_tk = true; then
  if sh ./hasgot $tk_libs $tk_x11_libs $tkauxlibs Tk_SetGrid; then
    inf "Tcl/Tk libraries found."
  elif sh ./hasgot -L/sw/lib $tk_libs $tk_x11_libs $tkauxlibs Tk_SetGrid; then
    case "$system" in
      *bsd*) tk_libs="-R/sw/lib -L/sw/lib $tk_libs";;
      *) tk_libs="-L/sw/lib $tk_libs";;
    esac
    inf "Tcl/Tk libraries found."
  elif sh ./hasgot -L/usr/pkg/lib $tk_libs $tk_x11_libs $tkauxlibs \
                   Tk_SetGrid; then
    case "$system" in
      *bsd*) tk_libs="-R/usr/pkg/lib -L/usr/pkg/lib $tk_libs";;
      *) tk_libs="-L/usr/pkg/lib $tk_libs";;
    esac
    inf "Tcl/Tk libraries found."
  else
    inf "Tcl library found."
    wrn "Tk library not found."
    has_tk=false
  fi
fi

if test $has_tk = true; then
  if test $tk_x11 = yes; then
    echo "TK_DEFS=$tk_defs "'$(X11_INCLUDES)' >> Makefile
    echo "TK_LINK=$tk_libs "'$(X11_LINK)' >> Makefile
  else
    echo "TK_DEFS=$tk_defs" >> Makefile
    echo "TK_LINK=$tk_libs" >> Makefile
  fi
  otherlibraries="$otherlibraries labltk"
else
  echo "TK_DEFS=" >> Makefile
  echo "TK_LINK=" >> Makefile
fi

mv Makefile ..

# Print a summary

inf
inf "** Configuration summary **"
inf
if test $has_tk = true; then
inf "Configuration for the \"labltk\" library:"
inf "        use tcl/tk version ....... $tcl_version"
inf "        options for compiling .... $tk_defs $tk_x11_include"
inf "        options for linking ...... $tk_libs $tk_x11_libs"
else
inf "The \"labltk\" library: not supported"
fi
