#!/bin/csh -f

# Set Variables 
# "man_dir" to the path name of the LEDA manual directory
# "awk_cmd" to your nawk (new awk) command (nawk or gawk)

set man_dir = /LEDA/SRC/man
#set awk_cmd = gawk
set awk_cmd = nawk

if (! -d $man_dir) then
echo    ""
echo -n "Cannot find LEDA manual directory,"
echo    " change variable 'man_dir' in '$0' \!"
echo    ""
exit 1
endif


set extract_cmd = cat

if ("$1" == "-t") then
  set view_cmd = "$OPENWINHOME/bin/textedit -read_only -position 0 0"
  set filter_cmd = "sed s/_//g"
  shift
else
   set view_cmd = "less -+M -+m -e -n -P$1"
  set filter_cmd = cat
endif


if ($1 == "") then
clear
echo "Lman - print LEDA manual pages"
echo " "
echo "Syntax: "
echo " "
echo "    lman   T   [op]"
echo " "
echo "Arguments: "
echo " "
echo "    T   :  name of a LEDA data type"
echo " "
echo "    op  :  name of an operation of data type T or one of the section names"
echo "           definition, creation, operations, or implementation"
echo " "
echo "Usage: "
echo " "
echo "    lman  T        prints the manual page for data type T (piped through less)."
echo " "
echo "    lman  T  op    prints the manual entry for operation T::op or section"
echo "                   op of the manual page for T (if op is a section name)."
echo " "
echo " "
exit 1
endif

set tex_file = $man_dir/$1.tex
set awk_script = $man_dir/Lman.awk

if (-f $tex_file) then
    $extract_cmd $tex_file | $awk_cmd -f $awk_script | $filter_cmd > /tmp/lman$$
    $view_cmd /tmp/lman$$
    rm -f /tmp/lman$$
else
   echo "$0": LEDA data type \"$1\" not found
   exit 1
endif

exit 0


#if (-f $tex_file) then
#  if ($2 != "") then
#    while ($2 != "")
#      extract_man $tex_file | $awk_cmd -f $awk_script $2 
#      shift
#    end
#  else
#   if ($less == 1) then
#    extract_man $tex_file | $awk_cmd -f $awk_script | less -+M -+m -e -n -P"LEDA Manual ($1)"
#   else
#    if ($textedit == 1) then
#     extract_man $tex_file | $awk_cmd -f $awk_script | sed s/_//g > /tmp/lman$$
#     textedit -read_only /tmp/lman$$
#     rm -f /tmp/lman$$
#    else
#     $awk_cmd -f $awk_script $tex_file
#    endif
#   endif
#  endif
#else
#   echo "$0": LEDA data type \"$1\" not found
#   exit 1
endif

exit 0
