#!/bin/sh
############################################################################
#
# MODULE:	g.manual
# AUTHOR(S):	Markus Neteler
# PURPOSE:	Display the HTML/MAN pages
# COPYRIGHT:	(C) 2003 by the GRASS Development Team
#
#		This program is free software under the GNU General Public
#		License (>=v2). Read the file COPYING that comes with GRASS
#		for details.
#
#############################################################################

#%Module
#%  description: Display the HTML man pages of GRASS
#%  keywords: general, manual, help
#%End
#%flag
#%  key: i
#%  description: Display index
#%End
#%flag
#%  key: m
#%  description: Display as MAN text page instead of HTML page in browser
#%End
#%option
#% key: entry
#% type: string
#% description: Manual entry to be displayed
#% required : no
#%End

if  [ -z "$GISBASE" ] ; then
    echo "You must be in GRASS GIS to run this program." >&2
    exit 1
fi

if [ "$1" != "@ARGS_PARSED@" ] ; then
    exec g.parser "$0" "$@"
fi

check_docbase()
{
    #hack for cygwin:
    if [ "$OSTYPE" = "cygwin" ] ; then
        if  [ -z "$CYGWIN_ROOT" ] ; then
            CYGWIN_ROOT="\cygwin"
        fi
        for i in C D E F G H I J K L M N O P Q R S T U V W X Y Z NOT_FOUND
        do
            j="$i:$CYGWIN_ROOT$1"
            if [ -f "$j"/docs/html/index.html ] ; then
                break
            fi
        done
        if [ "$i" = "NOT_FOUND" ] ; then
            g.message -e "Cannot find GRASS document base."
            exit 1
        fi
        echo "$j"
    else
        if [ -f "$1"/docs/html/index.html ] ; then
            echo "$1"
        fi
    fi
    echo ""
}

start_browser()
{
  DOC_BASE="$GRASS_DOC_BASE/docs/html"
  if test ! -f "$DOC_BASE/$1.html" -a -n "$1" ; then
    DOC_BASE=""
    IFS=":"
    for ADDON_PATH in "$GRASS_ADDON_PATH" ; do
      if [ -f "$ADDON_PATH/docs/html/$1.html" ] ; then
        DOC_BASE="$ADDON_PATH/docs/html"
	break
      fi
    done
    unset IFS
    if [ -z "$DOC_BASE" ] ; then
      g.message -e "No HTML manual page entry for <$1>."
      exit 1
    fi
  fi
  if [ -z "$GRASS_VERBOSE" ] || [ "$GRASS_VERBOSE" -gt 1 ] ; then
    g.message "Starting browser <$BROWSERNAME> for module $1..."
  fi
  "$GRASS_HTML_BROWSER" file://"$DOC_BASE/$1.html"
}

start_man()
{
  DOC_BASE="$GRASS_DOC_BASE/man/man1"
  if [ -z "$1" ] ; then
    g.message -e "Programmer error"
    exit 1
  fi

  if [ ! -f "$DOC_BASE/$1.1" ] && [ ! -f "$DOC_BASE/$1.1.gz" ] ; then
    DOC_BASE=""
    IFS=":"
    for ADDON_PATH in "$GRASS_ADDON_PATH" ; do
      if [ -f "$ADDON_PATH/docs/man/man1/$1.1" ] || \
	 [ -f "$ADDON_PATH/docs/man/man1/$1.1.gz" ] ; then
        DOC_BASE="$ADDON_PATH/docs/man/man1"
        break
      fi
    done
    unset IFS
    if [ -z "$DOC_BASE" ] ; then
       g.message -e "No man page entry for <$1>."
       exit 1
    fi
  fi

  if [ -f "$DOC_BASE/$1.1" ] ; then
    man "$DOC_BASE/$1.1"
  elif [ -f "$DOC_BASE/$1.1.gz" ] ; then
    man "$DOC_BASE/$1.1.gz"
  else
    g.message -e "No man page entry for <$1>."
  fi
}

GRASS_DOC_BASE=`check_docbase "$GISBASE"`

#hack for MacOSX:
ISMAC=`echo "$GRASS_HTML_BROWSER" | grep "html_browser_mac.sh"`
if [ ! -z "$ISMAC" ] ; then
    BROWSERNAME=`echo "$GRASS_HTML_BROWSER_MACOSX" | cut -d . -f 3`
#hack for Cygwin:
elif [ "$OSTYPE" = "cygwin" ] ; then
    BROWSERNAME=`basename "$GRASS_HTML_BROWSER" | sed 's/\.exe$//i'`
else
    BROWSERNAME="$GRASS_HTML_BROWSER"
fi

#keep order!
#first test for index...
if [ $GIS_FLAG_I -eq 1 ] ; then
  if [ $GIS_FLAG_M -eq 1 ] ; then
    start_man index
    exit 0
  else
    start_browser index
    exit 0
  fi
fi

#... then for help parameter:
if [ -z "$GIS_OPT_ENTRY" ] ; then
 g.manual help
 exit 0
fi

if [ $GIS_FLAG_M -eq 1 ] ; then
  start_man "$GIS_OPT_ENTRY"
else
  start_browser "$GIS_OPT_ENTRY"
fi

exit 0
