#!/bin/csh -f

# ccp4faq - query the list of Frequently Asked Questions about CCP4

# From the ofaq script by gerard kleywegt @ 930921
# debugged and use inverse video @ 940511
# changed browse option to avoid repeating list and
#     enable you to find a specific question quicker;
#     also remove and add back any trailing zeroes @ 940805
# 
# CCP4 version 1.0 10-Mar-95
#

onintr done

# define where the FAQ is:
set faq=$CDOC/ccp4.faq

# define options
set options='l(ist) b(rowse) f(ind) h(elp)'
set opts='l b f h'

set stat=0

set bold="`tput smso`"
set unbold="`tput rmso`"

set tmp=~/qqq.$$
set tmp2=~/qqq2.$$

set all=`grep '^Q\.' $faq | cut -c1-5 | grep '^Q\.'`

echo ""
echo "${bold} CCP4FAQ - query the CCP4 FAQ list ${unbold}"

if ($#argv < 1) then
  echo ""
  echo "usage: $0 option [query]"
  echo "option is one of :" $options
  echo ""
  set stat=-1
  goto done
endif

set opt=`echo $1 | cut -c1 | tr '[A-Z]' '[a-z]'`
set par=$2

foreach poss ($opts)
  if ( $opt == $poss ) goto okay
end
echo "invalid option : " $opt
echo "option must be one of : " $options
echo ""
set stat=-2
goto done

# okay
okay:

# help option
if ( $opt == "h" ) then
  echo ""
  echo "$0 $1"
  echo "options are :" $options
  echo "$0 help       ... this option"
  echo "$0 list       ... prints a list of all FAQs"
  echo "$0 browse     ... interactive session"
  echo "$0 find query ... print all info regarding keyword 'query'"
  echo ""
  goto done
endif

# for the other options we need to check if the FAQ file exists
if (! -e $faq ) then
  echo ""
  echo "faq file $faq does not exist"
  echo ""
  set stat=-3
  goto done
endif
if (! -f $faq ) then
  echo ""
  echo "faq file $faq is not a regular file"
  echo ""
  set stat=-4
  goto done
endif

# list option
if ( $opt == "l" ) then
  grep '^Q\.' $faq
  goto done
endif

# browse option
if ( $opt == "b" ) then
  retry:
    echo ""
    echo -n "Enter nr of question, Q to quit or L for list : "
    set x=$<
    set choice=`echo $x | cut -c1 | tr '[A-Z]' '[a-z]'`
    if ($x == "") goto retry
    if ($choice == "l") goto listem
    if ($choice == "q") then
      echo ""
      goto done
    endif
# remove upto two trailing zeroes (if any)
    set q=`echo $x | sed -e s/^0//`
    set x=`echo $q | sed -e s/^0//`
    set y=$x
# add upto two trailing zeroes (if question number < 100)
    if ( $x < 100 ) set y="0"$y
    if ( $x <  10 ) set y="0"$y
    set x='^Q\.'$y
    echo '/'{$x}'/,/^-----/ p' > $tmp
    echo ""
    sed -n -f $tmp $faq
    echo ""
#    echo -n "${bold} Hit RETURN : ${unbold}"
#    set x=$<
  goto retry
  listem:
    echo ""
    grep '^Q\.' $faq | more
    goto retry
endif

# find option
if ( $opt == "f" ) then
  if ( $par == "" ) then
    echo ""
    echo "no query provided"
    echo ""
    set stat=-5
    goto done
  endif
  echo ""
  set inverse="${bold}""$par""${unbold}"
  echo "Looking for : $inverse"
  @ cnt = 0
  foreach qa ( $all )
    if ("$qa" == "Q") goto forgetit
    echo '/'^{$qa}'/,/^-----/ p' > $tmp2
    set ct=`sed -n -f $tmp2 $faq | grep $par | wc -l`
    if ( $ct > 0 ) then
#     cat $tmp2
#     echo $qa
      @ cnt = $cnt + 1
      echo ""
      echo "${bold} Hit number $cnt : $qa ${unbold}"
      echo ""
      sed -n -f $tmp2 $faq | sed -e "s/$par/$inverse/g"
      echo ""
      echo -n "${bold} Hit RETURN or Q to quit: ${unbold}"
      set x=$<
      set x=`echo $x | cut -c1 | tr '[a-z]' '[A-Z]'`
      if ("$x" == "Q") goto done
    endif
    forgetit:
  end
  echo ""
  echo "${bold} Number of hits : $cnt ${unbold}"
  echo ""
endif

# done
done:
  echo ""
  if ( -e $tmp  ) /bin/rm $tmp
  if ( -e $tmp2 ) /bin/rm $tmp2
  exit $stat
