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

# Copyright (c) 2008 Sbastien 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, remove the excedent "/" in it.
    # getpath PATH
    local PTH=$1
    echo $PTH | sed 's/^[^=]*=//; s@//*@/@g; s@//*$@@'
}

unset PFX BIN MAN DOC USG FHS
readonly CFG_SOURCES=$(cd $(dirname $0); pwd)
readonly CFG_VERSION="2.5"
readonly CFG_DOCS="CHANGELOG PLAY playground.pl README"

for ARG in $@; do
    case $ARG in
        bindir=/*) BIN=$(getpath $ARG) ;;
        docdir=/*) DOC=$(getpath $ARG) ;;
        mandir=/*) MAN=$(getpath $ARG) ;;
        fhs) FHS=1 ;;
        help) USG=0 ;;
        prefix=/*) PFX=$(getpath $ARG) ;;
        *) echo "Error: '$ARG' isn't an allowed option."; USG=1 ;;
    esac
done

if [ "$USG" ]; then
    cat >&1 << US4G3
configure  ---  configure the installation of tofu.


    Paths definition:

        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/man1".
        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 $USG
fi

# We build the paths.
[ "$PFX" ] || PFX="/usr/local"
[ "$BIN" ] || BIN="$PFX/bin"

if [ "$FHS" -a ! "$MAN" ]; then
    MAN="$PFX/share/man/man1"
elif [ ! "$MAN" ]; then
    MAN="$PFX/man/man1"
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..."

# We make a config.mk file which will contain the paths.
cat > config.mk << EOC
SRCDIR = $CFG_SOURCES
BINDIR = $BIN
MANDIR = $MAN
DOCDIR = $DOC
DOCS = $CFG_DOCS
PLAY = $(echo $DOC/PLAY | sed 's@/@\\/@g')
EOC

install -m 0644 $CFG_SOURCES/Makefile.in Makefile

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

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


    Path of the tofu script:        $BIN
    Path of the man-page:           $MAN
    Path of the documentation:      $DOC


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

EOR

# EoF
