#! /bin/sh
#
# Configuration script for a Yorick plugin.
#
#------------------------------------------------------------------------------
#
# Copyright (C) 2012 Éric Thiébaut <eric.thiebaut@univ-lyon1.fr>
#
# This file is part of free software IPY: 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 3 of the License, or (at your
# option) any later version.
#
# IPY 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, see <http://www.gnu.org/licenses/>.
#
#------------------------------------------------------------------------------

# 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-tmp-$$"
cfg_debug=no

cfg_on_exit () {
    if test "$cfg_debug" = "no"; then
        rm -f "$cfg_tmpfile" "$cfg_tmpfile.i" "$cfg_tmpfile.c" "$cfg_tmpfile.o"
    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].
  -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
            ;;
        --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."
