#! /bin/sh
#! *sh* 10/91
#! Fhelp
#! enter the Ferret Users Guide at the indicated line number
#! If no line number is given then coach on usage

# no argument given: explain the ropes
if [ $# -eq 0 ]; then
     echo ' '
     echo '    *** Fhelp - Interactive help for FERRET ***'
     echo ' '
     echo '    Fhelp enters the FERRET Users Guide at a given line number'
     echo '          or at the first occurrence of a given string'
     echo ' '
     echo '        Correct usage is:  Fhelp line_number'
     echo '                      or:  Fhelp string'
     echo '             For example:  Fhelp "getting started"'
     echo ' '
     echo '    When reading the Users Guide use standard "more" commands:'
     echo '    ? = help   b = back 1 page   CR = next line  space = next page'
     echo ' '
     echo ' '
     echo '    Also available: Fapropos'
     echo '    Fapropos scans the FERRET Users Guide for a character string'
     echo '    and reports the lines where it occurs'
     echo ' '
     echo '        Correct usage is:  Fapropos  string'
     echo ' '
     exit
fi

# too many arguments: explain the syntax
if [ $# -gt 1 ]; then
     echo " "
     echo "    *** Syntax error in command entered ***"
     echo " "
     echo "        Correct usage is:  Fhelp line_number"
     echo " "
     echo ' '
     echo '    Use Fapropos to scan the FERRET Users Guide for a character string'
     echo '    and determine the lines where it occurs'
     echo ' '
     echo '        Correct usage is:  Fapropos  string'
     exit
fi

# did user enter a line number or a string
if echo $1 | grep -E -q '^[0-9]+$' ; then
#    line number - enter the FERRET manual 2 lines before the requested line
     startpos=`echo "$1 - 2" | bc`
     more -d +${startpos} ${FER_DIR}/doc/ferret_users_guide.txt
else
#    string: use grep for case-insensitive search and start 2 lines before the first occurence
     startpos=`grep -in "$1" ${FER_DIR}/doc/ferret_users_guide.txt | head -1 | sed 's/^\([0-9]*\):.*$/\1/'`
     if [ -n "${startpos}" ]; then
         startpos=`echo "${startpos} - 2" | bc`
         more -d +${startpos} ${FER_DIR}/doc/ferret_users_guide.txt
     fi
fi
