#! /bin/sh
#*******************************************************************************
# E.S.O. - ACS project
#
# "@(#) $Id: acsMan,v 1.62 2004/09/21 08:59:32 mpasquat Exp $" 
#
# who        when       what
# --------  ----------  ----------------------------------------------
# psivera   2000-10-06  created
#

#************************************************************************
#   NAME
#   acsMan - invoke tkman browser on the ACS man pages
# 
#   SYNOPSIS
#
#        acsMan  [-verbose] [<tkmanOption> ...] [<manpage>]
#
# 
#   DESCRIPTION
#   These utilities are used to access the on-line documentation of the ACS
#   software.
#
#   The utilities spawns the tkman browser giving to it as scope for 
#   where to lacate man pages the following list:
#
#      - if src/ is the current directory, the current module
#
#      - if INTROOT is defined, the current integration area
#
#      - the ACS root area.
#
#   The tkman browser offers the following main advantages:
#      
#      - hypertext links to other man pages (click on a word in the text
#        which corresponds to a man page, and you jump there), 
#      
#      - better navigation within long man pages with searches and jumps
#        to section headers.
#      
#      - possibility to select a portion of text and copy it somewhere else.
#      
#      - enable/disable of directory in the man-search path.
#      
#   See presentation page for a complete description of tkman features.
#
#
#   -verbose        if any, print out current INTROOT and module.
#                   By default, output is produced only on error.
#
#   <tkmanOption>   any tkman options. See tkman documentation.
#
#   <manpage>       if specified, the manpage is shown at the beginning
#                   instead of tkman presentation page
#
#   FILES
#                     
#
#   ENVIRONMENT
#   MODROOT  <read>  the root directory of the current module
#
#   INTROOT  <read>  the root directory of the current integration area
#
#   ACSROOT  <read>  the root directory of the ACS root area
#                    (it should have been set at login time)
#
#   RETURN VALUES
#
#   CAUTIONS 
#   I suggest the use of tkman with the "Mono" option enabled (in the lower 
#   right corner) to preserve the allignement of drawings.
#   
#   Parameter interpretation is position dependend. If used, they must
#   specified in the right order.
#
#   acsMan can recognize the current module ONLY if the current directory
#   is set to "src/"
#
#   SEE ALSO 
#   tkman
#
#   BUGS
#   On HP, the "Print" option in the "Occasionals" menu does not work
#   because the "psroff" command in not available. For the time being,
#   use te1 to print.
#----------------------------------------------------------------------

# if Linux: disable the bash builtin command 'echo'.
if [ "`uname`" = "Linux" ]
then
    enable -n echo
fi

if [ "$1" = "-verbose" ]
then
    VERBOSE=on
    shift
    echo "acsMan:"
fi


#----------------------------------------------------------------------
# Put the ACS root area man pages in the scope
#----------------------------------------------------------------------

if [ -z "$ACSROOT" -a -z "$INTLIST" ] 
then
    echo "acsMan: ERROR - ACSROOT/INTLIST not defined."
    echo ""
    echo "        ----> It should have been defined at loggin time."
    echo "              Ask your system manager to check login files"
    echo ""
    exit
fi

MANPATH=""
dir_acs=""
if [ ! -z "$ACSROOT" -a  -d $ACSROOT/man -a -d $ACSROOT/vw/man ] 
then
   MANPATH=$ACSROOT/man:$ACSROOT/vw/man
   dir_acs="OK"
fi


# Separation of dirs
item_list=`echo $INTLIST | sed s/:/\ /g`

dir_intlist=""
for item in $item_list
do
  if [ -d $item/man -a -d $item/vw/man ]
  then
     MANPATH=$item/man:$item/vw/man:${MANPATH}
     dir_intlist="OK"
  fi
done

if [ -z "$dir_acs" -a -z "$dir_intlist" ]
then
    echo "acsMan: ERROR - $[ACSROOT|INTLIST][/vw/]/man does not exist or it is nor a directory."
    echo ""
    echo "        ----> It should have been correctly defined at loggin time."
    echo "              Ask your system manager to check login files"
    echo ""
    exit 
fi


#----------------------------------------------------------------------
# if any, add the current integration area to the scope
#----------------------------------------------------------------------

if [ "$INTROOT" != "" ] 
then
    if [ -d $INTROOT/man  -a  -d $INTROOT/vw/man ] 
    then
        MANPATH=$INTROOT/man:$INTROOT/vw/man:${MANPATH}
    else
        echo "acsMan: WARNING - $INTROOT has not the standard structure."
        echo "                  (man and/or vw/man missing)"
        echo ""
        echo "        ----> check you directory structure (getTemplate)"
        echo ""
    fi
else
    INTROOT=none
fi

if [ "$VERBOSE" = "on" ]
then 
    echo "  - current integration area: >>$INTROOT<<"
fi

#----------------------------------------------------------------------
# if current directory is src/ try to figure out the module name 
#----------------------------------------------------------------------

CURRENT_DIR=`pwd`
CURRENT_NAME=`basename $CURRENT_DIR`

if [ "$CURRENT_NAME" = "src" ]
then

    #
    # find module name. There are two cases:
    #    - normal modules: the module name is the parent directory
    #    - multiplatform modules: the module is structured as more than one 
    #                     submodules. Each submodule has the normal module
    #                     directory tree but fixed names are used:
    #                        <mod>/lcu    for the LCU part
    #                        <mod>/ws     for the ws  part
    PARENT_DIR=`dirname $PWD`
    PARENT_NAME=`basename $PARENT_DIR`
    if [ "$PARENT_NAME" = "lcu" -o "$PARENT_NAME" = "ws" ]
    then
        GRANDPARENT_DIR=`dirname $PARENT_DIR`
        GRANDPARENT_NAME=`basename $GRANDPARENT_DIR`
        MODROOT=$GRANDPARENT_DIR
        MANPATH=$MODROOT/ws/man:$MODROOT/lcu/man:${MANPATH}
    else
        MODROOT=$PARENT_DIR
        MANPATH=$MODROOT/man:${MANPATH}
    fi
else 
    MODROOT=none
fi

if [ "$VERBOSE" = "on" ]
then 
    echo "  - current module          : >>$MODROOT<<"
fi

export MANPATH


#----------------------------------------------------------------------
# invoke the tkman browser. Remaining input parameters are passed to tkman.
#----------------------------------------------------------------------
tkman -title "acsMan - manual page browser"  $* &

if [ "$VERBOSE" = "on" ]
then
    echo " . . . opening tkman .\c"
    sleep 1; echo " .\c"
    sleep 1; echo " .\c"
    sleep 1; echo " .\c"
    sleep 1; echo " .\c"
    sleep 1; echo " .\n"
fi

#
# ___oOo___#
