#!/bin/csh -f
#+
#OSIRIS library of scripts
#
#NAME
#     help - prints help for each command
#
#SYNOPSIS
#     help [command|category]
#
#DESCRIPTION
# tries to find the command or alias "command," and if it is located
# prints out the comment lines in the script.
#
# If no argument is passed, a generic command summary is printed.
# 
# There are three kinds of help provided:
#
#     help "command"   will provide help on each command.
#
#     help "category"  will provide help on one of the following categories:
#
#                      ao      adaptive optics commands
#                      det     detector commands
#                      disp    display commands
#                      info    informational commands
#                      mot     motor commands
#                      tel     telescope commands
#
#     The third type of help comes into play if the argument you have provided 
#     is neither an OSIRIS command or one of the categories above.  In this
#     situation the command tries to determine whether the argument is an
#     alias or some other command in the user's PATH.  If the latter, it
#     will attempt to print out the man page entry for that command.
#
#-
#
# Modification History:
# 20041005 - MB: Copied from NIRC2 and started adapting for OSIRIS
#

if ($#argv == 0) then
    cat $FAC_SCRIPTS/info/help.cat
    exit 0

else

# Check to see if the argument is a category name.  Category names are
# limited to the list below:

    set catList = ( ao det disp info mot tel )
    set Com = "$1"

    switch ("$Com")
    case "$catList[1]":
    case "$catList[2]":
    case "$catList[3]":
    case "$catList[4]":
    case "$catList[5]":
    case "$catList[6]":
        set helpFile = "$FAC_SCRIPTS/$Com/commands.$Com"
        if ( -e $helpFile ) then
            cat $helpFile
            exit 0
        else
            echo "Cannot find the summary help file $helpFile."
            exit 1
        endif
        breaksw
    endsw

# Check to see whether we can find the file.

    set TestCom = `which $Com`
    set Alias = `echo "$TestCom" | grep "aliased to"`
    set Error = `echo "$TestCom" | grep "no $Com in"`

    if ("$Error" != "") then
        echo ""
        echo Command \"$Com\" not found.
        echo ""
        exit
    else if ("$Alias" != "") then
        echo ""
        echo \"$Com\" is an alias...
        echo "$TestCom"
        echo ""
        exit
    else

# See if this is a script by checking the first character of the first line.

        set isScript = `head -1 $TestCom | grep "^#"`
        if ("$isScript" != "") then
            echo ""
#            echo Help for command \"$Com\"...
#            echo ""

# Check for the comment style (does it use +/- or not).

            set style = `awk '/^#\+$/' $TestCom`
            if ("$style" == "") then
                awk 'substr($0,1,1)!="#"{exit}{print}' $TestCom
            else
                awk '/#\+/{ok=1;next}/#-/{exit}ok==1{print}substr($0,1,1)!="#"{exit}' $TestCom | sed 's/^#/ /'
            endif
        else

# Use the man entry if one exists.

            set mantest = `man -l $Com | head -1 | grep -v "No manual entry for"`
            if ("$mantest" != "") then
                echo ""
                echo From the man page for \"$Com\"...
                echo ""
                man $Com
                exit
            else
               echo ""
               echo \"$Com\" appears to be an executable with no man page entry.
               echo ""
            endif
        endif

    endif
endif
