#! /bin/sh
#
#! /bin/sh -x
###############################################################################
# Copyright (c) 2000-2021 Ericsson Telecom AB
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v2.0
# which accompanies this distribution, and is available at
# https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
###############################################################################

###############################################################################
#  Shell script: ttcn3_help
#  Part of     : TITAN
#  Purpose     : opening help pages
#  Content     :
#  The followings can be configured
#  browser: the application which opens the help page
#           (netscape by default)
#  browser_options: command line options for the browser
#  help_format: extension of the help files
#  help_root: root of the help files
#
#  Return value: 1 on error
#                0 on success
###############################################################################

# check the number of arguments
if test "`expr $# != 1`" = "1"
then echo "Error: exactly one argument is expected"; exit 1;
fi

# check for necessary environment variables
if test "${TTCN3_DIR}" = "" 
then echo "Error: TTCN3_DIR environment variable is not set"; exit 1;
fi

# file extension for help files
help_format=".html";
# path of the help pages
help_root="${TTCN3_DIR}/help";

# check for browser
if test "${TTCN3_BROWSER}" = "" 
then browser="netscape"; browser_options="-no-about-splash ";
else browser="${TTCN3_BROWSER}"; browser_options=" ";
fi

# check for already running browsers
# Netscape
if test "`echo ${browser} | grep netscape`" != ""
then {
  username=`whoami`
  # avoid listing grep command as well
  pslist=`/bin/ps -u ${username} -f`
  if test "`echo ${pslist} | grep netscape`" != ""
  then browser_options="-remote openURL(file:"; endtag=")"
  else browser_options="-no-about-splash "; endtag=""
  fi
}
# Mozilla
elif test "`echo ${browser} | grep mozilla`" != ""
then {
  username=`whoami`
  # avoid listing grep command as well
  pslist=`/bin/ps -u ${username}`
  if test "`echo ${pslist} | grep mozilla`" != ""
  then browser_options="-remote openURL(file:"; endtag=")"
  else browser_options="file:"; endtag=""
  fi
}
# Opera
elif test "`echo ${browser} | grep opera`" != ""
then {
  username=`whoami`
  # avoid listing grep command as well
  pslist=`/bin/ps -u ${username} -f`
  if test "`echo ${pslist} | grep opera`" != ""
  then browser_options="-remote openURL(file:"; endtag=")"
  else endtag=""
  fi
}
# Lynx should be executed in a new terminal
elif test "`echo ${browser} | grep lynx`" != ""
then {
  browser="xterm -e ${browser}"
  endtag=""
}
else endtag=""
fi

#echo "browser=$browser"
#echo "options=$browser_options"
#echo "endtag =$endtag"

# the file tree looks like
# $TTCN3_DIR/help/titan_main.html
# $TTCN3_DIR/help/titan_index.html
# $TTCN3_DIR/help/info/<keyword>.html

##### check if it is a document name #####
if test -f ${help_root}/$1
then exec ${browser} ${browser_options}${help_root}/$1${endtag}
elif test -f ${help_root}/info/$1
then exec ${browser} ${browser_options}${help_root}/info/$1${endtag}

##### check if it is the keyword 'main' #####
elif test $1 = "main"
then exec ${browser} ${browser_options}${help_root}/titan_main${help_format}${endtag}

##### check if it is a keyword #####
elif test -f ${help_root}/info/$1${help_format}
then exec ${browser} ${browser_options}${help_root}/info/$1${help_format}${endtag}

# otherwise open the index file
else exec ${browser} ${browser_options}${help_root}/titan_index${help_format}${endtag}
fi

