#! /bin/sh
#
# Configuration script for a Yorick plugin.
#
#------------------------------------------------------------------------------
#
# Copyright (C) 2012 Éric Thiébaut <eric.thiebaut@univ-lyon1.fr>
#
# This software is governed by the CeCILL-C license under French law and
# abiding by the rules of distribution of free software.  You can use, modify
# and/or redistribute the software under the terms of the CeCILL-C license as
# circulated by CEA, CNRS and INRIA at the following URL
# "http://www.cecill.info".
#
# As a counterpart to the access to the source code and rights to copy,
# modify and redistribute granted by the license, users are provided only
# with a limited warranty and the software's author, the holder of the
# economic rights, and the successive licensors have only limited liability.
#
# In this respect, the user's attention is drawn to the risks associated with
# loading, using, modifying and/or developing or reproducing the software by
# the user in light of its specific status of free software, that may mean
# that it is complicated to manipulate, and that also therefore means that it
# is reserved for developers and experienced professionals having in-depth
# computer knowledge. Users are therefore encouraged to load and test the
# software's suitability as regards their requirements in conditions enabling
# the security of their systems and/or data to be ensured and, more
# generally, to use and operate it in the same conditions as regards
# security.
#
# The fact that you are presently reading this means that you have had
# knowledge of the CeCILL-C license and that you accept its terms.
#
#------------------------------------------------------------------------------

# The following default values are specific to the package.  They can be
# overwritten by options on the command line.
cfg_cflags=
cfg_deplibs=
cfg_ldflags=

# The other values are pretty general.
cfg_tmpdir=.
cfg_tmpfile="$cfg_tmpdir/cfg-$$"
cfg_debug=no

cfg_on_exit () {
  if test "$cfg_debug" = "no"; then
    rm -f "$cfg_tmpfile" "$cfg_tmpfile.i"
  fi
}

trap cfg_on_exit 0

cfg_progname=$0
cfg_srcdir=$(dirname "$0")
#cfg_path=$(readlink -fn "$@")
#cfg_srcdir=$(dirname "$cfg_path")

cfg_die () { echo >&2 "$cfg_progname: $*"; exit 1; }

cfg_opt_value () { echo "$*" | sed 's/^--[^=]*=//'; }

cfg_despace () { echo "$*" | sed 's/ /\\ /g'; }

cfg_subst_macro () {
  local s
  local t
  s=$*
  for t in "/" "%" "@" "," "-"; do
    case "$s" in
      *$t* )
        ;;
      * )
        break
    esac
  done
  if test "$t" = "-"; then
    cfg_die "No valid separator found"
  fi
  s='[ 	]*'
  echo "s${t}^${s}${1}${s}=.*${t}${1}=${2}${t}"
}

cfg_help () {
  cat <<EOF
usage: $cfg_progname [-h|--help] [--yorick=PATH_TO_YORICK]
options:
  --arch=OS-CPU        Architecture [$cfg_arch].
  --yorick=PATH        Path to Yorick executable [$cfg_yorick].
  --cflags=CFLAGS      Additional compiler flags [$cfg_cflags], for instance:
                         --cflags='-Isomedir'
  --deplibs=DEPLIBS    Flags for dependencies [$cfg_deplibs], for instance:
                         --deplibs='-Lsomedir -lsomelib'
  --ldflags=LDFLAGS    Additional linker flags [$cfg_ldflags].
  --debug              Turn debug mode on (for this script).
  -h, --help           Print this help and exit.
EOF
}

if cmp -s "./Makefile" "$cfg_srcdir/Makefile"; then
  cfg_inplace=yes
else
  cfg_inplace=no
fi

cfg_os=$(uname -s | tr A-Z a-z)
if test "x$cfg_os" = "x"; then
  cfg_os="unknown"
fi
cfg_cpu=$(uname -m | tr A-Z a-z)
if test "x$cfg_cpu" = "x"; then
  cfg_cpu="unknown"
else
  case "$cfg_cpu" in i?86 ) cfg_cpu="ix86";; esac
fi
cfg_arch=${cfg_os}-${cfg_cpu}
cfg_yorick=yorick
cfg_prefix=/usr/local
while test $# -ge 1; do
  cfg_arg=$1
  shift
  case "$cfg_arg" in
    -h | --help )
      cfg_help
      exit 0
      ;;
    --debug)
      cfg_debug=yes
      ;;
    --arch=*)
      cfg_arch=$(cfg_opt_value "$cfg_arg")
      ;;
    --cflags=*)
      cfg_cflags=$(cfg_opt_value "$cfg_arg")
      ;;
    --deplibs=*)
      cfg_deplibs=$(cfg_opt_value "$cfg_arg")
      ;;
    --ldflags=*)
      cfg_ldflags=$(cfg_opt_value "$cfg_arg")
      ;;
    --yorick=*)
      cfg_yorick=$(cfg_opt_value "$cfg_arg")
      ;;
    *)
      cfg_die "Unknown option \"$cfg_arg\""
  esac
done

case "$cfg_arch" in
  mswin )
    cfg_exe_sfx=.exe
    ;;
  * )
    cfg_exe_sfx=
esac

# Search Yorick in the path:
if test "x$cfg_yorick" = "xyorick"; then
  cfg_save_IFS=$IFS
  IFS=":"
  for cfg_dir in $PATH; do
    cfg_file=$cfg_dir/yorick$cfg_exe_sfx
    if test -r "$cfg_file" -a -x "$cfg_file" -a ! -d "$cfg_file"; then
      cfg_yorick=$cfg_file
      break
    fi
  done
  IFS=$cfg_save_IFS
fi
if test "x$cfg_yorick" = "xyorick" \
  -o ! -f "$cfg_yorick" \
  -o ! -x "$cfg_yorick"; then
  echo >&2 "Yorick excutable not found."
  echo >&2 "Try to specify the path with option --yorick=..."
  exit 1
fi
echo >&2 "Yorick executable --------> $cfg_yorick"

# Get the Y_HOME and Y_SITE variables.
cat >"$cfg_tmpfile.i" <<EOF
write, format = "Y_HOME=%s\nY_SITE=%s\n", Y_HOME, Y_SITE;
quit;
EOF
"$cfg_yorick" -batch "$cfg_tmpfile.i" > "$cfg_tmpfile"

cfg_yhome=$(sed < "$cfg_tmpfile" -e '/^Y_HOME=/!d;s/^Y_HOME=//')
cfg_ysite=$(sed < "$cfg_tmpfile" -e '/^Y_SITE=/!d;s/^Y_SITE=//')
cfg_ymkdir=$cfg_yhome
echo >&2 "Yorick home directory ----> $cfg_yhome"
echo >&2 "Yorick site directory ----> $cfg_ysite"

# Create the Makefile.
cfg_dst="./Makefile"
if test "$cfg_inplace" = "yes"; then
  cfg_src="$cfg_dst.bak"
  mv -f "$cfg_dst" "$cfg_src"
else
  cfg_src="$cfg_srcdir/Makefile"
fi

cfg_s0=$(cfg_subst_macro "Y_EXE"       "$cfg_yorick")
cfg_s1=$(cfg_subst_macro "Y_MAKEDIR"   "$cfg_ymkdir")
cfg_s2=$(cfg_subst_macro "Y_EXE_HOME"  "$cfg_yhome")
cfg_s3=$(cfg_subst_macro "Y_EXE_SITE"  "$cfg_ysite")
cfg_s4=$(cfg_subst_macro "PKG_CFLAGS"  "$cfg_cflags")
cfg_s5=$(cfg_subst_macro "PKG_DEPLIBS" "$cfg_deplibs")
cfg_s6=$(cfg_subst_macro "PKG_LDFLAGS" "$cfg_ldflags")
cfg_s7=$(cfg_subst_macro "srcdir"      "$cfg_srcdir")
sed < "$cfg_src" > "$cfg_dst" \
  -e "$cfg_s0;$cfg_s1;$cfg_s2;$cfg_s3;$cfg_s4;$cfg_s5;$cfg_s6;$cfg_s7"

if test "$cfg_inplace" = "yes"; then
  rm -f "$cfg_src"
fi

echo "Makefile has been updated."
echo "You can run 'make' and 'make install' now."
