#!/bin/sh 
#
# License display script
#
#
#  ********************************************
#    COPYRIGHT (C) 2005  by RSA Security Inc.
#            ---ALL RIGHTS RESERVED---
#  ********************************************
#

############################################################
# Set default values and commands for the shell script.
############################################################

HERE=`dirname $0`
QUESTION1="$HERE/question1.txt"
QUESTION2="$HERE/question2.txt"
LICENSE1="$HERE/license1.txt"
LICENSE2="$HERE/license2.txt"
 
echo_no_nl_mode="unknown"

##############################################################################
# Trap 
#	this will trap any escape characters, and allow the program to abort normally 
#	by calling abort_installation
##############################################################################
trap 'trap "" 1 2 15; abort_installation' 1 2 15

##############################################################################
# echo_no_nl()
#       Echo a string with no carriage return (if possible).  Must set
#       $echo_no_nl to "unknown" before using for the first time.
##############################################################################
echo_no_nl()
{
  if [ "$echo_no_nl_mode" = "unknown" ] ; then
     echo_test=`echo \\\c`
     if [ "$echo_test" = "\c" ] ; then
        echo_no_nl_mode="-n"
     else
        echo_no_nl_mode="\c"
     fi
  fi
 
  if [ "$echo_no_nl_mode" = "\c" ] ; then
     echo $* \\c
  else
     echo -n "$*"
  fi
}

##############################################################################
# echo_wrap()
#       Echo a string with  preserving the formatting but not putting 
#       new line after last line.
##############################################################################
echo_wrap()
{

    echo "$1" | awk '{ 
                           a[NR] = $0; 
                     } 
                     END {
                         for(i = 1; i <= NR; i++) {
                             printf "%s",a[i]
                             if(i != NR) printf "\n"
                             else printf " "
                         }
                     }'

}

##############################################################################
# getAcceptDecline()
# Gets either a "Accept" or a "Decline" in the traditional (a/d) pattern 
#               $1      The default value if user hits <Enter> (TRUE/FALSE)
#               $2      The string to print to prompt the user
# The variable $AORD is set in accordance to what the user entered.
##############################################################################
getAcceptDecline()
{
#####################################################
# Set up line parameters as $1 and $2 are overwritten
#####################################################
  AorDdef=$1
  AorDprompt=$2
  echo_no_nl "$AorDprompt"
 
  AORD=""
  until [ -n "$AORD" ] ;
  do
    read AORD
    case "$AORD"
    in
      a|A )  AORD=TRUE;;
      d|D )  AORD=FALSE;;
      ''  )  AORD=$AorDdef;;
      *   )  echo ""
             echo "Please enter 'A', 'D' or '<return>' "
             AORD="";;
    esac
  done
 
}


##############################################################################
# getyesorno()
# Gets either a "yes" or a "no" in the traditional (y/n) pattern 
#               $1      The default value if user hits <Enter> (TRUE/FALSE)
#               $2      The string to print to prompt the user
# The variable $YESORNO is set in accordance to what the user entered.
##############################################################################
getyesorno()
{
#####################################################
# Set up line parameters as $1 and $2 are overwritten
#####################################################
  yesornodef=$1
  yesornoprompt=$2

 
  YESORNO=""
  until [ -n "$YESORNO" ] ;
  do
    echo ""
    echo_wrap "$yesornoprompt"
    read YESORNO
 
    case "$YESORNO"
    in
      y|Y )  YESORNO=TRUE;;
      n|N )  YESORNO=FALSE;;
      ''  )  YESORNO=$yesornodef;;
      *   )  echo ""
             echo "Please enter 'y', 'n' or '<return>' "
             YESORNO="";;
    esac
  done
 
}


############################################################
# Display the License and Copyright files.
############################################################
startup_screen()
{

    # Changed so that we could have both license files shown
    if [ -f $QUESTION1 ] ; then
        qst="`cat $QUESTION1`"
    else
        echo ""
        echo "The License Agreement question file could not be found in the current directory."
        abort_installation
    fi

    getyesorno TRUE "$qst"
    if [ "$YESORNO" = TRUE ] ; then
        if [ -f $LICENSE1 ] ; then
            echo ""
            more $LICENSE1
            touch /tmp/RSA_LICENSE1_COUNTRY_OF_ORIGIN
        else
            echo ""
            echo "The License Agreement text file could not be found in the current directory."
            echo "Installation is aborting..."
            abort_installation
        fi
    else
        # Ask another question
        if [ -f $QUESTION2 ] ; then
            qst="`cat $QUESTION2`"
        else
            echo ""
            echo "The License Agreement question file could not be found in the current directory."
            abort_installation
        fi

        getyesorno TRUE "$qst"
        if [ "$YESORNO" = TRUE ] ; then
            if [ -f $LICENSE2 ] ; then
                echo ""
                more $LICENSE2 
                touch /tmp/RSA_LICENSE2_COUNTRY_OF_ORIGIN
            else
                echo ""
                echo "The License Agreement text file could not be found in the current directory."
                echo "Installation is aborting..."
                abort_installation
            fi
        else
            echo ""
            echo "The RSA Products can be ordered from only two listed locations."
            echo "Installation is aborting..."
            abort_installation
        fi
    fi
# We will end up here only if user select "y" as answer to one of the questions
    echo ""
    echo "Do you accept the License Terms and Conditions"
    getAcceptDecline FALSE "stated above? ([A]ccept/[D]ecline) [D] "
    if [ "$AORD" = FALSE ] ; then
	abort_installation;
    fi
}


##############################################################################
# abort_installation()
##############################################################################
abort_installation()
{
  exit 1
}

startup_screen


