#!@l_prefix@/lib/openpkg/bash
##
##  man -- OpenPKG Tool Chain "man" command
##  Copyright (c) 2000-2006 OpenPKG Foundation e.V. <http://openpkg.net/>
##  Copyright (c) 2000-2006 Ralf S. Engelschall <http://engelschall.com/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  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.
##

#   configuration
prefix="@l_prefix@"

#   minimum command line parsing
opt_h=no
while [ 1 ]; do
    case "$1" in
        -h | --help ) opt_h=yes; shift ;;
        * ) break ;;
    esac
done
if [ ".$opt_h" = .yes ]; then
    echo "openpkg:man:USAGE: openpkg man [--help] [<section>] <command>"
    exit 0
fi
if [ $# -eq 1 ]; then
    man_page="$1"
elif [ $# -eq 2 ]; then
    man_sect="$1"
    man_page="$2"
else
    echo "openpkg:man:ERROR: invalid number of arguments" 1>&2
    exit 1
fi

#   determine path to commands
openpkg_tools="${OPENPKG_TOOLS}"
openpkg_tools_cmdpath="${OPENPKG_TOOLS_CMDPATH}"
if [ ".${openpkg_tools}" != . -a ".${openpkg_tools_cmdpath}" = . ]; then
    openpkg_tools_cmdpath="${openpkg_tools}/cmd:@"
fi
cmdpath="${prefix}/libexec/openpkg"
if [ -d "${prefix}/libexec/openpkg-tools" ]; then
    cmdpath="${prefix}/libexec/openpkg-tools:${cmdpath}"
fi
if [ -d "${prefix}/libexec/openpkg-audit" ]; then
    cmdpath="${prefix}/libexec/openpkg-audit:${cmdpath}"
fi
if [ ".${openpkg_tools_cmdpath}" != . ]; then
    cmdpath=`echo "${openpkg_tools_cmdpath}" | sed -e "s;@;${cmdpath};"`
fi
openpkg_tools_cmdpath=`echo "${cmdpath}" | sed -e 's/::/:/g' -e 's/^://' -e 's/:$//'`

#   search for manual page in OpenPKG Tool Chain
man_file=""
man_type=""
OIFS="$IFS"; IFS=":"
for dir in ${openpkg_tools_cmdpath}; do
    IFS="$OIFS"
    for file in $dir/$man_page.${man_sect-"1"} $dir/$man_page.[1-9]; do
        if [ -f "$file" ]; then
            man_file="$file"
            man_type="roff"
            if [ ".$man_sect" = . ]; then
                man_sect=`echo "$man_file" |\
                    sed -e 's;^;X;' -e 's;^X.*\.\([1-9]\)$;\1;' -e 's;^X.*;;'`
            fi
            break
        fi
    done
    if [ ".$man_type" = . ]; then
        for file in $dir/$man_page.pod $dir/$man_page.pl $dir/$man_page.sh; do
            if [ -f "$file" ]; then
                if [ ".`grep '^=pod' $file`" != . ]; then
                    man_file="$file"
                    man_type="pod"
                    break
                fi
            fi
        done
    fi
    if [ ".$man_type" != . ]; then
        break
    fi
done
IFS="$OIFS"
if [ ".$man_type" != . ]; then
    #   determine POD to Roff converter
    pod2roff=""
    if [ ".$man_type" = .pod ]; then
        pod2roff=`$prefix/lib/openpkg/shtool path -p "$prefix/bin:$PATH" pod2man`
        if [ ".$pod2roff" = . ]; then
            echo "openpkg:man:ERROR: unable to find POD to Roff converter (pod2man)" 1>&2
            exit 1
        fi
        if [ ".$man_sect" = . ]; then
            man_sect=1
        fi
        pod2roff="$pod2roff --section=${man_sect}"
        pod2roff="$pod2roff --release=\"$man_page(${man_sect})\""
        pod2roff="$pod2roff --center=OpenPKG --date=OpenPKG"
        pod2roff="$pod2roff --quotes=none"
    fi

    #   determine Roff to ASCII converter
    roff2ascii=`$prefix/lib/openpkg/shtool path -p "$prefix/bin:$PATH" nroff groff`
    if [ ".$roff2ascii" = . ]; then
        echo "openpkg:man:ERROR: unable to find Roff to ASCII converter (nroff, groff)" 1>&2
        exit 1
    fi
    roff2ascii="$roff2ascii -man"
    case "$roff2ascii" in
        */groff ) roff2ascii="$roff2ascii -Tascii" ;;
    esac

    #   determine pager
    pager="$PAGER"
    if [ ".$pager" = . ]; then
        pager=`$prefix/lib/openpkg/shtool path -p "$prefix/bin:$PATH" less more cat`
    fi
    if [ ".$pager" = . ]; then
        echo "openpkg:man:ERROR: unable to find text viewer ($PAGER, less, more, cat)" 1>&2
        exit 1
    fi

    #   render the manual page to the TTY
    if [ ".$man_type" = .roff ]; then
        eval "$roff2ascii $man_file | $pager"
    elif [ ".$man_type" = .pod ]; then
        eval "$pod2roff $man_file | $roff2ascii | $pager"
    fi
    exit 0
fi

#   fallback to the man(1) command
MANPATH="$prefix/man:$prefix/local/man:${MANPATH-/usr/man}:/usr/man:/usr/share/man"
export MANPATH
eval "exec man $man_sect $man_page"

