#! /bin/sh
#
# Configuration script for MiRA software.
#
#------------------------------------------------------------------------------
#
# This file is part of MiRA: a Multi-aperture Image Reconstruction
# Algorithm <https://github.com/emmt/MiRA>.
#
# Copyright (C) 2016, Éric Thiébaut.
#
# MiRA is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License version 2 as published by the Free
# Software Foundation.
#
# MiRA 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.
#
#------------------------------------------------------------------------------

cfg_progname=$(basename "$0")
cfg_topdir=$(dirname "$0")
cfg_tmpdir=$cfg_topdir
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_die () { echo >&2 "$cfg_progname: $*"; exit 1; }

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

cfg_help () {
    cat <<EOF
usage: $cfg_progname [-h|--help] [--yorick=PATH_TO_YORICK]
options:
  --yorick=PATH        Path to Yorick executable [$cfg_yorick].
  --incdir=DIR         Destination directory for MiRA code files [Y_SITE/i].
  --bindir=DIR         Destination directory for MiRA executable [$cfg_bindir].
  --mandir=DIR         Destination directory for man pages [$cfg_mandir].
  --debug              Turn debug mode on (for this script).
  -h, --help           Print this help and exit.
EOF
}

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

cfg_yorick=yorick
cfg_incdir=
cfg_bindir=
cfg_mandir=
while test $# -ge 1; do
    cfg_arg=$1
    shift
    case "$cfg_arg" in
        -h | --help )
            cfg_help
            exit 0
            ;;
        --debug)
            cfg_debug=yes
            ;;
        --yorick=*)
            cfg_yorick=$(cfg_opt_value "$cfg_arg")
            ;;
        --incdir=*)
            cfg_incdir=$(cfg_opt_value "$cfg_arg")
            ;;
        --bindir=*)
            cfg_bindir=$(cfg_opt_value "$cfg_arg")
            ;;
        --mandir=*)
            cfg_mandir=$(cfg_opt_value "$cfg_arg")
            ;;
        *)
            cfg_die "Unknown option \"$cfg_arg\""
    esac
done

# 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=//' -e 's,/*$,/,')
cfg_ysite=$(sed < "$cfg_tmpfile" -e '/^Y_SITE=/!d;s/^Y_SITE=//' -e 's,/*$,/,')
cfg_ymkdir=$cfg_yhome
echo >&2 "Yorick home directory ----------> $cfg_yhome"
echo >&2 "Yorick site directory ----------> $cfg_ysite"

if test "x$cfg_incdir" = "x"; then
    cfg_incdir=${cfg_ysite}i
fi
echo >&2 "MiRA code files directory ------> $cfg_incdir"

if test "x$cfg_bindir" = "x"; then
    cfg_bindir_mesg="(will not be installed)"
else
    cfg_bindir_mesg="$cfg_bindir"
fi
echo >&2 "MiRA executable directory ------> $cfg_bindir_mesg"

if test "x$cfg_mandir" = "x"; then
    cfg_mandir_mesg="(will not be installed)"
else
    cfg_mandir_mesg="$cfg_mandir"
fi
echo >&2 "MiRA manual pages ---------------> $cfg_mandir_mesg"

# Create the configration file.
cfg_dst="./install.cfg"
cat >"$cfg_dst" <<EOF
# Configuration for installing MiRA software.

# INCDIR is the directory where to install MiRA code source (nothing is
# installed if empty).
INCDIR=$cfg_incdir

# BINDIR is the directory where to install MiRA command (nothing is installed
# if empty).
BINDIR=$cfg_bindir

# MANDIR is the directory where to install MiRA manula pages (nothing is
# installed if empty).
MANDIR=$cfg_mandir

# YORICK is the path for Yorick interpreter.
YORICK=$cfg_yorick
EOF
echo >&2 "Installation settings ----------> $cfg_dst"

#------------------------------------------------------------------------------
