#! /bin/sh
#
#  RLdev: RealLive-compatible development toolkit
#  Simple configuration script avoiding autoconf horrors
#  Copyright (C) 2006 Haeleth
#  Revised 2009-2011 by Richard 23
#
#  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., 59 Temple
#  Place - Suite 330, Boston, MA  02111-1307, USA.
#

# moderately portable echo
case "`echo 'x\c'`" in
'x\c')
  echo_n="echo -n"
  nobr=
  ;;
*)
  echo_n="echo"
  nobr="\c"
  ;;
esac

check_exists() {
  $echo_n "Checking for $1... ${nobr}"
  found=0
  if eval $2; then
    return 0
  else
    echo "not found"
    if test -z $3; then
      echo "Error: $1 is required for the build process. See INSTALL for details." >&2
      exit 2
    else
      return 1
    fi
  fi
}

normalise() {
  # Replace single digits with double: e.g. "1.2.5" -> "1.02.05", "1.2.10" -> "1.02.10".
  echo $1 | sed 's/\.\([0-9]\)\./.0\1./g;s/\.\([0-9]\)$/.0\1/g'
}

check_version() {
  $echo_n "Checking for $1 >= $2... ${nobr}"
  if test -n "$3"; then estr="$3"; else estr="ocamlfind query $1 -format %v"; fi
  if eval $estr >/dev/null 2>&1; then
    ver=`$estr`
    echo $ver
    v1=`normalise $ver`
    v2=`normalise $2`
    if test `expr "$v1" ">=" "$v2"` = 0; then
      if test -z $4; then
        echo "Error: $1 $2 or better is required for the build process. See INSTALL for details." >&2
        exit 2
      else
        return 2
      fi
    fi
  else
    echo "not found"
    if test -z $4; then
      echo "Error: $1 is required for the build process. See INSTALL for details." >&2
      exit 2
    else
      return 1
    fi
  fi
  return 0
}

prev=
prefix=/usr/local
enable_vaconv=true
enable_rlxml=true
default_encoding=UTF-8
for opt
do
  if test -n "$prev"; then
    eval "$prev=\$opt"
    prev=
    continue
  fi
  arg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
  case $opt in
    --prefix=* | -prefix=*)
      prefix=$arg ;;
    --prefix | -prefix)
      prev=prefix
      ;;

    --enable-vaconv | -enable-vaconv | --with-vaconv | -with-vaconv)
      enable_vaconv=true ;;
    --disable-vaconv | -disable-vaconv | --without-vaconv | -without-vaconv | --no-vaconv | -no-vaconv)
      enable_vaconv=false ;;
    --vaconv=* | -vaconv=*)
      enable_vaconv=$arg ;;
    --vaconv | -vaconv)
      prev=enable_vaconv
      ;;

    --enable-rlxml | -enable-rlxml | --with-rlxml | -with-rlxml)
      enable_rlxml=true ;;
    --disable-rlxml | -disable-rlxml | --without-rlxml | -without-rlxml | --no-rlxml | -no-rlxml)
      enable_rlxml=false ;;
    --rlxml=* | -rlxml=*)
      enable_rlxml=$arg ;;
    --rlxml | -rlxml)
      prev=enable_rlxml
      ;;
      
    --default-encoding=* | -default-encoding=* | --encoding=* | -encoding=*)
      default_encoding=$arg ;;
    --default-encoding | -default-encoding | --encoding | -encoding)
      prev=default_encoding
      ;;

    -h | -help | --help)
      cat <<__ENDHELP
Usage: $0 <options>

Options:
  --help           display this message
  --prefix=DIR     install in prefix DIR (default is /usr/local)
  --no-vaconv      don't build the Vaconv utility even if possible
  --no-rlxml       don't build the RlXml utility even if possible
  --encoding=ENC   use encoding ENC as default encoding (default is Shift_JIS)
__ENDHELP
      exit 0
      ;;
  esac
done

if test -n "$prev"; then
  opt=--`echo $prev | sed 's/_/-/g'`
  echo "Error: missing argument to $opt" >&2
  exit 2
fi

check_version "Objective Caml" 3.09 "ocamlc -version"
native=false
if check_exists "native compiler" "eval ocamlopt 2>/dev/null" optional; then echo "found"; native=true; fi
$echo_n "Checking compiler type... ${nobr}"
cat > .temp.ml <<_END_TEMP_ML
print_endline Sys.os_type;
match Sys.os_type with
  | "Cygwin" | "Win32" -> exit 0
  | _ -> exit 1
_END_TEMP_ML
if ocaml .temp.ml; then windows=true; else windows=false; fi
rm -f .temp.ml

check_exists findlib "ocamlfind query findlib -format %v"
check_exists omake "omake --version | head -1 | sed -e 's/^.*\( [^:]*\).*$/\1/'"
check_version extlib 1.4 "" optional || {
  check_version extlib 1.3
  echo "Extlib 1.4 is required to build RLdev, but some versions of 1.4 claim to be 1.3. The build may succeed, but check this if it fails."
}
check_version ulex 0.8

case $enable_vaconv in
  true | yes | y | on | 1)
    vaconv=true ;;
  *)
    vaconv=false ;;
esac
case $enable_rlxml in
  true | yes | y | on | 1)
    rlxml=true ;;
  *)
    rlxml=false ;;
esac

if $vaconv || $rlxml; then
  if check_version xml-light 2.1 "" optional; then
    forcexmllight=false
  elif check_exists xml-light "[ -s `ocamlc -where`/xml-light.cma ]" optional; then
    echo "found unknown version"
    echo "Unable to determine version of xml-light. The build may succeed; if it fails, reconfigure with the --no-vaconv and --no-rlxml options."
    forcexmllight=true
  else
    echo "Unable to use xml-light; Vaconv and RlXML will not be built. See INSTALL for details." >&2
    vaconv=false
    rlxml=false
    forcexmllight=false
  fi
fi
if $vaconv; then
  checkpng() {
    pkg-config libpng12 --exists && pkg-config libpng12 --modversion
  }
  if check_version libpng 1.2.5 checkpng optional; then
    libpng_cflags=`pkg-config libpng12 --cflags`
    libpng_ldflags=`pkg-config libpng12 --libs`
  else
    echo "Unable to confirm existence of libpng on your system. Vaconv may not build correctly." >&2
    libpng_cflags=
    libpng_ldflags="-lpng -lz"
  fi
fi

ucenc=`echo $default_encoding | tr a-z- A-Z_`
case $ucenc in
  SJS | SJIS | SHIFTJIS | SHIFT_JIS | CP932 | MS_KANJI)
    default_encoding=CP932 ;;
  EUC | EUCJP | EUC_JP)
    default_encoding=EUC-JP ;;
  UTF8 | UTF_8)
    default_encoding=UTF-8 ;;
  *)
    echo "Error: default character encoding must be CP932 (SHIFT_JIS), EUC-JP, or UTF-8." >&2
    exit 2 ;;
esac

hevea=true
# workaround for weird quoting problems
if eval "hevea -version" >/dev/null 2>&1; then
  hvtest="echo `hevea -version | head -1 | sed -e 's/^hevea \([0-9+.]\+\).*$/\1/g'`"
else hvtest="hevea -version"; fi
check_version hevea 1.08 "$hvtest" optional || {
  echo "The \`omake dochtml' option will be disabled." >&2
  hevea=false
}

pdflatex=true
if eval "pdflatex -version" >/dev/null 2>&1; then
  lttest="pdflatex -version | head -1"
else lttest=false; fi
check_exists pdflatex "$lttest" optional || {
  echo "The \`omake docpdf' option will be disabled." >&2
  pdflatex=false
}

cfgfile=common/config.cfg
echo Writing $cfgfile...
cat > $cfgfile <<_END_CONFIG_H
(* Automatically generated by configure *)

DEFINE DEFAULT_ENCODING = "$default_encoding"
_END_CONFIG_H
if test -n "$prefix"; then
  echo "DEFINE PREFIX = \"$prefix/share/rldev/lib\"" >> $cfgfile
fi

echo Writing config.inc...
cat > config.inc <<_END_CONFIG
# Automatically generated by configure

ENABLE_WIN32=$windows
ENABLE_HEVEA=$hevea
ENABLE_PDFLATEX=$pdflatex
ENABLE_RLXML=$rlxml
ENABLE_VACONV=$vaconv
FORCE_XMLLIGHT=$forcexmllight
PNG_CFLAGS=$libpng_cflags
PNG_LDFLAGS=$libpng_ldflags
_END_CONFIG
if test "$native" = "false"; then
  cat >> config.inc <<_END_CONFIG
NATIVE_ENABLED = false
BYTE_ENABLED = true
OCAMLFLAGS += -custom
_END_CONFIG
fi
if test -n "$prefix"; then
  vaconv_exe=
  rlxml_exe=
  if $vaconv; then vaconv_exe=" vaconv\$(EXE)"; fi
  if $rlxml; then rlxml_exe=" rlxml\$(EXE)"; fi
  cat >> config.inc <<_END_CONFIG

.PHONY: install uninstall
install:
  ./install-sh -d $prefix/bin
  foreach(f, \$(addprefix \$(EXEDIR)/,kprl\$(EXE) rlc\$(EXE)$vaconv_exe$rlxml_exe))
    ./install-sh -t $prefix/bin \$(f)
  ./install-sh -d $prefix/share/rldev/lib
  foreach(f, \$(glob ../lib/*))
    ./install-sh -m 644 -t $prefix/share/rldev/lib \$(f)
  ./install-sh -d $prefix/share/rldev/rtl
  foreach(f, \$(glob ../rtl/*))
    ./install-sh -m 644 -t $prefix/share/rldev/rtl \$(f)

uninstall:
  \$(RM) \$(addprefix $prefix/bin/,kprl\$(EXE) rlc\$(EXE)$vaconv_exe$rlxml_exe)
  \$(RM) -r $prefix/share/rldev
_END_CONFIG
else
  cat >> config.inc <<_END_CONFIG

.PHONY: install uninstall
install uninstall:
  eprintln(ERROR: configured with empty prefix: automatic installation is disabled.)
  exit(2)
_END_CONFIG
fi

echo "Finished configuring RLdev."
echo
echo "-- Configuration summary --"
$echo_n "Programs: kprl, rlc${nobr}"; if $vaconv; then $echo_n ", vaconv${nobr}"; fi; if $rlxml; then echo ", rlxml"; else echo; fi
$echo_n "Compiler: ${nobr}"; if $native; then echo "native code"; else echo "bytecode"; fi
$echo_n "Manuals: ${nobr}"; if $hevea && $pdflatex; then echo "html, pdf"; elif $hevea; then echo "html"; elif $pdflatex; then echo "pdf"; else echo "none"; fi
echo
if test -n "$prefix"; then
  echo "Now run \`omake' and then \`omake install' to build and install the package."
else
  echo "Now run \`omake' to build the package, then install it as you see fit."
fi
