#!/usr/local/bin/bash
# $Id: frontend.in,v 1.7 2003/02/03 23:09:34 jmmv Exp $
# bt_doc's frontend.
#
# buildtool
# Copyright (c) 2002, Julio Merino.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 3. Neither the name of the author nor the names of contributors may
#    be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

show_menu() {
    clear
cat <<EOF
BUILDTOOL                                        PACKAGE DOCUMENTATION
======================================================================


Package: $BT_PKG_NAME-$BT_PKG_VERSION
Comment: $BT_PKG_COMMENT
Maintainer: $BT_PKG_MAINTAINER
Homepage: $BT_PKG_HOMEPAGE

Documentation menu
------------------

    q. Exit bt_doc
    i. Read Buildtool's manual.
    l. Read package's license '$BT_PKG_LICENSE'
EOF
    num=1
    docsfile=`cat ./buildtool.d/docs | sed -e "s/ /-space-/g"`
    docs=""
    for d in $docsfile; do
        d=`echo $d | sed -e "s/-space-/ /g"`
        doc=`echo $d | sed -e "s/\(.*\) - \(.*\)/\1/"`
        descr=`echo $d | sed -e "s/\(.*\) - \(.*\)/\2/"`
        docs="$docs $doc"
        echo "    $num. $doc - $descr"
        num=$(($num + 1))
    done
    echo
    printf "Your choice> "
}

err_doc() {
    echo
    echo "Cannot open documentation file '$*'"
    echo "Press RETURN to continue"
    read tmp
}

if [ x"$__BUILDTOOL" != x"yes" ]; then
    echo "bt_doc: this program must be run through buildtool"
    exit 1
fi

if [ ! -f "./buildtool.d/defs" ]; then
    echo "bt_doc: cannot find buildtool.d/defs file"
    exit 1
fi

if [ ! -f "./buildtool.d/docs" ]; then
    echo "bt_doc: cannot find buildtool.d/docs file"
    exit 1
fi

. "./buildtool.d/defs"

if [ -z "$BT_PAGER" ]; then
    if [ -z "$PAGER" ]; then
        BT_PAGER=more
    else
        BT_PAGER=$PAGER
    fi
fi

while :; do
    show_menu
    read opt

    if [ "$opt" = "q" ]; then
        echo
        echo "Have fun building $BT_PKG_NAME-$BT_PKG_VERSION!"
        exit 0
    elif [ "$opt" = "i" ]; then
        info buildtool
    elif [ "$opt" = "l" ]; then
        license="/usr/local/share/buildtool-0/licenses/$BT_PKG_LICENSE"
        if [ ! -f "$license" ]; then
            license="buildtool.d/licenses/$BT_PKG_LICENSE"
        fi
        if [ ! -f "$license" ]; then
            err_doc "$BT_PKG_LICENSE"
        else
            clear
            $BT_PAGER $license
        fi
    else
        num=1
        for d in $docs; do
            if [ "$num" = "$opt" ]; then
                if [ ! -f "$d" ]; then
                    err_doc "$d"
                else
                    clear
                    $BT_PAGER "$d"
                fi
            fi
            num=$(($num + 1))
        done
    fi
done

# NOTREACHED

# Local Variables: ***
# mode: shell-script ***
# End: ***
