#!/bin/sh
#
# CONFIGURE - configure the Makefile of Tofu.
#

# Copyright (c) 2008, 2010 Sébastien Boillod <sbb at tuxfamily dot org>.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

getpath () {
    # Get the path definition, removing the excedent "/" in it.
    # $f PATH
    local path="$1"
    echo $path | sed 's/^[^=]*=//; s@//*@/@g; s@//*$@@'
}

growl () {
    # Print error message on stderr an exits with the given non-null code.
    # $f CODE, MESSAGE
    local err=$1 pr='(E)'; shift 1
    if [ "$1" ]; then
        # If there is a second argument, we print arguments with 'echo'.
        [ $err = 0 ] && pr='(W)'
        echo "$pr $*" 1>&2
    else
        # Otherwise we use 'cat' and print stdin.
        cat <&0 >&2
    fi
    [ $err -eq 0 ] || exit $err
}

usage () {
    # Print the help digest, then exit.
    # $f CODE
    local code="$1" display="cat"
    [ $code -eq 0 ] || display="growl 1"
    $display >&1 << US4G3
configure  ---  configure the installation of tofu.


    Paths definition:

      --perl=<path>     ---     the path of the Perl interpretor to use.
      --prefix=<path>   ---     install the files in <path>, "/usr/local/" is
                                the default.
      --bindir=<path>   ---     install the tofu script in <path>, by default
                                "<prefix>/bin".
      --mandir=<path>   ---     install the manual in <path>, the default path
                                is "<prefix>/man".
      --docdir=<path>   ---     install the docs in <path>, the default path is
                                "<prefix>/doc/tofu".

        NOTE: All of the paths must be specified in an ABSOLUTE way.

    Other options:

      --fhs             ---     respect the FHS specifications to build paths.
      --help            ---     print this help on stdout.


Please, send any bug reports, suggestions, feedbacks, questions, and so on,
about this install script to Seb (author/maintainer) here:

                      <sbb at tuxfamily dot org>.

US4G3
    exit 0
}

unset PFX PL BIN MAN DOC FHS
readonly CFG_SOURCES=$(cd $(dirname $0); pwd)
readonly CFG_VERSION="3.3"

for ARG in $@; do
    case $ARG in
        --bindir=/*) BIN=$(getpath $ARG) ;;
        --docdir=/*) DOC=$(getpath $ARG) ;;
        --mandir=/*) MAN=$(getpath $ARG) ;;
        --fhs) FHS=1 ;;
        --help) usage 0 ;;
        --perl=/*) PL=$(getpath $ARG) ;;
        --prefix=/*) PFX=$(getpath $ARG) ;;
        *) usage 1 ;;
    esac
done

# We build the paths.
[ "$PFX" ] || PFX="/usr/local"
[ "$BIN" ] || BIN="$PFX/bin"
[ "$PL" ] || PL=$(which perl) || exit 1

if [ "$FHS" -a ! "$MAN" ]; then
    MAN="$PFX/share/man"
elif [ ! "$MAN" ]; then
    MAN="$PFX/man"
fi

if [ "$FHS" -a ! "$DOC" ]; then
    DOC="$PFX/share/doc/tofu-$CFG_VERSION"
elif [ ! "$DOC" ]; then
    DOC="$PFX/doc/tofu-$CFG_VERSION"
fi

echo "Makefile generation..."
exec 3>Makefile
cat >&3 << EOC
#=== Generated by the configure script ========================================
SRCDIR      = $CFG_SOURCES
BINDIR      = $BIN
DOCDIR      = $DOC
MANDIR      = $MAN
PERL        = $PL
#=== Generated by the configure script ========================================
EOC
sed '/^#/d' "$CFG_SOURCES/Makefile.in" >&3

# We report the configuration.
cat >&1 << EOR

The Makefile of "tofu-$CFG_VERSION" was generated with the following settings:

    Perl interpretor:                $PL
    Path for the tofu scripts:       $BIN
    Path for the man-pages:          $MAN
    Path for the documentation:      $DOC


If these ones are convenient for you, type "make" to perform the installation.
To obtain further information about the configuration settings, please refer
to "$0 --help".

EOR

# EoF
