#!/bin/sh
# simple configure script for tla-tools
#
#  Copyright (C) 2003, 2004  Miles Bader <miles@gnu.org>
#
# 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.
#
# Written by Miles Bader <miles@gnu.org>

me=`basename $0`

# Standard make variables
prefix=${prefix:-/usr/local}
exec_prefix=${exec_prefix:-'$(prefix)'}
bindir=${bindir:-'$(exec_prefix)/bin'}

# Some messages
TRY="Try "\`"$me --help' for more information."

# sed for use before we configure $SED
BASIC_SED=${SED-sed}

CONF_VARS="srcdir prefix exec_prefix bindir"
ENV_VARS="TLA AWK ABS_AWK INSTALL SED UUIDGEN TLA_ESCAPE"
CONF_FILES="Makefile"

quoted_cmdline=""
for arg in "$@"; do
  arg_rewrite=`echo "$1" | $BASIC_SED "s@'@'\\\\''@g"`
  quoted_cmdline="$quoted_cmdline '$arg_rewrite'"
  shift
done
eval set -- "$quoted_cmdline"
quoted_cmdline="$0$quoted_cmdline"

srcdir=`echo "$0" | $BASIC_SED -n 's@/[^/]*$@@p'`
srcdir=${srcdir:-.}

# Parse command-line options
while :; do
  case "$1" in
    --help)
      echo "Configure script for tla-tools"
      echo "Usage: $me [OPTION...]"
      echo ""
      echo "      --prefix=DIR       Set install prefix (default \`$prefix')"
      echo "      --exec_prefix=DIR  Set prefix for executable files (default \`$exec_prefix')"
      echo "      --bindir=DIR       Set binary directory (default \`$bindir')"
      echo ""
      echo "      --srcdir=DIR       Set directory containing tla sources (default \`$srcdir')"
      echo ""
      echo "      --help             Display a help message and exit"
      echo ""
      echo "The following environment variables may also be defined:"
      echo ""
      echo "   AWK      awk program (should be vaguely modern; default \`nawk')"
      echo "   INSTALL  install program (default \`install')"
      echo "   TLA      tla program (default \`tla')"
      echo "   SED      GNU sed program (default \`sed')"
      echo "   UUIDGEN  UUID generator program (default \`uuidgen')"
      exit 0
      ;;
    --*)
      VAR=`echo "$1" | $BASIC_SED -e 's@^--@@' -e 's@=.*@@' -e 's@-@_@g'`
      VAL=`echo "$1" | $BASIC_SED 's@.*=@@'`
      if test x"$VAL" = x"$1"; then
	# value in next arg
	shift
	VAL="$1"
      fi
      set_var=no
      for V in $CONF_VARS; do
        if test x"$VAR" = x"$V"; then
	  eval "$VAR=\$VAL"
	  set_var=yes
	  break
	fi
      done
      if test $set_var = no; then
	 echo 1>&2 "$me: unrecognized option "\`"--$VAR'"
	 echo 1>&2 "$TRY"
	 exit 1
      fi
      shift
      ;;
    -[!-]?*)
      # split concatenated single-letter options apart
      FIRST="$1"; shift
      set -- `echo $FIRST | $BASIC_SED 's/-\(.\)\(.*\)/-\1 -\2/'` "$@"
      ;;
    -*)
      echo 1>&2 "$me: unrecognized option "\`"$1'"
      echo 1>&2 "$TRY"
      exit 1
      ;;
    *=*)
      eval "$1"; shift;;
    *)
      break;
  esac
done

USER_ENV_VARS=''
for var in $ENV_VARS; do
  if eval test x"\${$var+set}" = xset; then
    USER_ENV_VARS="$USER_ENV_VARS $var"
  fi
done

# Make variables we use
split_path=`echo $PATH | $BASIC_SED 's/:/ /g'`

# TLA
TLA=${TLA:-tla}
echo "checking for tla... $TLA"

# INSTALL
INSTALL=${INSTALL:-install}
echo "checking for install... $INSTALL"

# AWK
msg="checking for awk... "
if test x"${AWK+set}" != xset; then
  awk_progs="nawk awk"
  for bin_dir in $split_path; do
    for awk_prog in $awk_progs; do
      AWK="$bin_dir/$awk_prog"
      test -x "$AWK" && break 2
    done
  done
  if test ! -x "$AWK"; then
    echo "$msg"
    echo 1>&2 "$me: awk not found"
    exit 2
  fi
fi
echo "$msg$AWK"

# ABS_AWK (used for scripts, which need an absolute dir)
msg="checking for awk absolute path... "
if test x"${ABS_AWK+set}" != xset; then
  case "$AWK" in
    /*)
      ABS_AWK="$AWK";;
    *)
      for bin_dir in $split_path; do
	ABS_AWK="$bin_dir/$AWK"
	test -x "$ABS_AWK" && break
      done;;
  esac
fi
echo "$msg$ABS_AWK"

# SED
msg="checking for GNU sed... "
if test x"${SED+set}" != xset; then
  sed_progs="gsed sed"
  for bin_dir in $split_path; do
    for sed_prog in $sed_progs; do
      SED="$bin_dir/$sed_prog"
      if test -x "$SED"; then
	# Make sure it's GNU sed by trying the --version option
	if "$SED" --version < /dev/null 2> /dev/null | grep -i 'gnu sed' > /dev/null; then
	  break 2
	else
	  unset SED
	fi
      fi
    done
  done
  if test ! -x "$SED"; then
    echo "$msg"
    echo 1>&2 "$me: GNU sed not found"
    exit 2
  fi
fi
echo "$msg$SED"

# UUIDGEN
UUIDGEN=${UUIDGEN:-"uuidgen"}
echo "checking for uuidgen... $UUIDGEN"

# Check whether we can use tla --unescaped
if $TLA escape foo >/dev/null 2>/dev/null; then
  TLA_ESCAPE=yes
else
  TLA_ESCAPE=no
fi
echo "checking whether $TLA uses filename escapes... $TLA_ESCAPE"

#
# Output generation
#

SED_CMDS='/^#* *arch-tag:/d'
for VAR in $CONF_VARS $ENV_VARS; do
  eval VAL=\""\$$VAR"\"
  SED_CMDS="$SED_CMDS;s;@$VAR@;$VAL;"
done

for FILE in $CONF_FILES; do
  echo "$me: Generating $FILE"
  $SED "$SED_CMDS" < $srcdir/"$FILE.in" > "$FILE"
done

# Generate config.status file
(
  echo "#!/bin/sh"
  echo "# Generated by $quoted_cmdline"
  echo "srcdir='$srcdir'"
  echo "config_sed='$SED'"
  echo 'if test x"$1" = x--recheck; then'
  for VAR in $USER_ENV_VARS; do
    eval VAL=\""\$$VAR"\"
    echo "  $VAR=\${$VAR:-'`echo "$VAL" | $SED "s@'@'\\\\''@g"`'}; export $VAR"
  done
  echo "  exec $quoted_cmdline"
  echo "else"
  SED_CMDS_REWRITE=`echo "$SED_CMDS" | $SED "s@'@'\\\\''@g"`
  echo "  sed_cmds='$SED_CMDS_REWRITE'"
  echo "  for file in $CONF_FILES; do"
  echo '    echo "$0: Generating $file"'
  echo '    $config_sed "$sed_cmds" < $srcdir/"$file.in" > "$file"'
  echo "  done"
  echo "fi"
) > config.status
chmod +x config.status

# arch-tag: 384defdf-9e33-4836-a68d-01accaef63bb
