#
# Copyright (c) 1999-2005 Easysoft Ltd. All rights reserved.
#
# $Id: install_intro 24 2005-02-11 09:08:42Z martin $
#
# This script does the introductory work for an install e.g. display license
# notice and ask if the installer accepts it.
#
# Calling format:
#
# install_intro text_notice_file [skip_licensing_flag license_file]
#
# returns:
#
# 0 = success
# 1 = invalid command line
# 2 = environment variable not defined
# 3 = file not found
# 99 = installation aborted at user request
#
if [ "$ECHO" = "" ];then
  echo "error - ECHO not defined, expect it to be exported"
  exit 2
fi
#
# Check command line
#
SKIPLICENSING=""
LICENSEFILE=""
cmdline_error=""
if [ $# -lt 1 ];then
  cmdlinestr="Missing text notice file argument"
  cmdline_error="1"
else
  NOTICEFILE="$1"
  if [ $# -gt 1 ];then
    SKIPLICENSING="$2"
    if [ $# -eq 3 ]; then
	LICENSEFILE=$3
    fi
  fi
  if [ ! -r "$NOTICEFILE" ];then
    $ECHO "File $NOTICEFILE not found or unreadable"
    exit 3
  fi
fi
#
# If command line error
#
if [ "$cmdline_error" = "1" ];then
  $ECHO "Invalid command line, $cmdlinestr"
  $ECHO "Usage:"
  $ECHO "install_intro text_notice_file [skip_licensing license_file]"
  exit 1
fi
#
# Display the notice file
#
cat $NOTICEFILE
#
# Make sure the installer agrees to the licensing terms
#
if [ "$SKIPLICENSING" = "" ];then
  if [ "$LICENSEFILE" != "" ];then
      if [ ! -r "$LICENSEFILE" ]; then
	  $ECHO "License file $LICENSEFILE not found";
      else
	  $ECHO "Press the return key to read license"
	  read press
	  PG=""
	  if [ "$PAGER" != "" ];then
	      PG="$PAGER"
	  else
	      more < /dev/null
	      if [ $? -ne 0 ]; then
		  less < /dev/null
		  if [ $? -ne 0 ]; then
		      PG="cat"
		  else
		      PG="less"
		  fi
	      else
		  PG="more"
	      fi
	  fi
	  "$PG" "$LICENSEFILE"
      fi
  fi
  answer=hoho
  while [ "$answer" != "q" -a "$answer" != "no" -a "$answer" != "yes" ]
  do
    if [ "$LICENSEFILE" = "" ];then
	$ECHO "Do you accept the license? (q=quit to read license, yes, no): \c"
    else
	$ECHO "Do you accept the license? (q=quit, yes, no): \c"
    fi
    read answer
    if [ "$answer" = "q" ];then
      $ECHO "Installation terminated at user request"
      exit 99
    elif [ "$answer" = "yes" ];then
      $ECHO "License accepted by user"
    elif [ "$answer" = "no" ];then
      $ECHO "License not accepted by user - installation terminated"
      exit 99
    else
     $ECHO "You must answer \"yes\", \"no\" or (q)uit"
    fi
  done
fi
exit 0