#
# Copyright (c) 1999-2001 Easysoft Ltd. All rights reserved.
#
# This script allows the user to obtain license for the product
#
# Calling format:
#
# install_license product product_install_path
#
# product - the product name used in output (spaces allowed)
# product_install_dir - base easysoft install path
#
# returns:
#
# 0 = success
# 1 = invalid command line
# 2 = environment variable not defined
# 3 = file not found
# 4 = create file/dir failure
# 5 = permission denied
#
#
# You MUST set and export ECHO and TESTEXISTS before calling
# this script.
#
# $Id: install_license,v 1.7 2004/02/02 15:16:53 martin Exp $
#
# $Log: install_license,v $
# Revision 1.7  2004/02/02 15:16:53  martin
# Fix bz 579 - prompt not reissued if incorrect reponse to
# 	Would you like to request a OOB Server license now
#
# Revision 1.6  2003/03/18 09:44:56  martin
# Fix bug ID 2075 - license requests should be emailed to license@easysoft.com
# 	not support@easysoft.com.
#
# Revision 1.5  2001/11/23 11:47:56  martin
# Add DYLD_LIBRARY_PATH for MAC OS X, darwin.
#
# Revision 1.4  2001/04/04 10:31:00  martin
# If the user does not request a license now tell them how to do it
# 	at a later date.
#
# Revision 1.3  2000/04/19 13:37:16  jason
# Added LIBPATH for AIX and SHLIB_PATH for HP
#
# Revision 1.2  2000/03/21 10:30:16  martin
# Missed the save of pwd.
#
# Revision 1.1  2000/03/20 10:23:41  martin
# Initial revision
#
#
if [ "$ECHO" = "" ];then
  echo "error - ECHO not defined, expect it to be exported"
  exit 2
fi
if [ "$TESTEXISTS" = "" ];then
  echo "error - TESTEXISTS not defined, expect it to be exported"
  exit 2
fi
cwd=`pwd`
#
# Check command line
#
cmdline_error=""
if [ $# -ne 2 ];then
  cmdlinestr="incorrect number of parameters"
  cmdline_error="1"
else
  PRODUCTLONG="$1"
  PRODUCTINSTALLPATH="$2"
fi
#
# If command line error
#
if [ "$cmdline_error" = "1" ];then
  $ECHO "\"$*\""
  $ECHO "Invalid command line, $cmdlinestr"
  $ECHO "Usage:"
  $ECHO "install_license product product_install_path"
  exit 1
fi
#
# Check for an existing license dir and file. If one does not exist make
# the license dir and copy a blank license file in to it.
#
if [ ! "$TESTEXISTS" "$PRODUCTINSTALLPATH/license" ]; then
  if ( mkdir "$PRODUCTINSTALLPATH/license" )
  then
      $ECHO "$PRODUCTINSTALLPATH/license directory created"
  else
      $ECHO "Failed to create $PRODUCTINSTALLPATH/license"
      exit 4
  fi
  if [ ! "$TESTEXISTS" "$PRODUCTINSTALLPATH/license/licenses" ]; then
    if ( cp licenses_template "$PRODUCTINSTALLPATH/license/licenses" )
    then
      $ECHO "License template created"
    else
      $ECHO "Failed to copy license template"
      exit 4
    fi
  fi
fi
if ( cp licclient "$PRODUCTINSTALLPATH/license" )
then
  $ECHO "License client installed"
else
  $ECHO "Failed to copy license client"
  exit 4
fi
if ( cp licshell "$PRODUCTINSTALLPATH/license" )
then
  $ECHO "License client shell installed"
else
  $ECHO "Failed to copy license client shell"
  exit 4
fi
cd $cwd
#
# Delete existing product database.
#
LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$PRODUCTINSTALLPATH/lib"
export LD_LIBRARY_PATH
DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:$PRODUCTINSTALLPATH/lib"
export DYLD_LIBRARY_PATH
LIBPATH="$LIBPATH:$PRODUCTINSTALLPATH/lib"
export LIBPATH
SHLIB_PATH="$SHLIB_PATH:$PRODUCTINSTALLPATH/lib"
export SHLIB_PATH
$PRODUCTINSTALLPATH/license/licclient semcreated 2>/dev/null

if [ $? -eq 0 ];then
cat <<EOF

The installation needs to reset the Easysoft product database.
If you have any Easysoft products running on this machine it is
wise to stop them first or they may unexpectedly terminate. If they
do unexpectedly stop no harm will result other than any applications
using them will have to be restarted.

Press <enter> when you are ready to proceed.

EOF
  read answer
  $PRODUCTINSTALLPATH/license/licclient semdelete
  if [ $? -ne 0 ];then
    $ECHO "Warning: Failed to update Easysoft product database"
    $ECHO "Please report this problem to Easysoft support"
  fi
fi
#
# Run the license app.
#
if [ ! -x "$PRODUCTINSTALLPATH/license/licclient" ];then
  $ECHO "WARNING: licclient Licencing Application not found at"
  $ECHO "$PRODUCTINSTALLPATH/license/licclient"
  $ECHO "WARNING: Cannot currently license $PRODUCTLONG"
else
  answer="hoho"
  while [ "$answer" != "" -a "$answer" != "y" -a $answer != "n" ]
  do
    $ECHO "Would you like to request a $PRODUCTLONG license now (y/n) [y]: \c"
    read answer
    if [ "$answer" = "" ];then
      answer="y"
    fi
    if [ "$answer" = "n" ];then
      cat <<EOF

You can run the licshell script in 
$PRODUCTINSTALLPATH/license/
at a later date to obtain a $PRODUCTLONG license.

EOF
    fi
    if [ "$answer" = "y" ];then
      $ECHO "Running the License Application"
      $PRODUCTINSTALLPATH/license/licclient
    fi
    if [ -r licenses.out ];then
      $ECHO "It looks like licenses were obtained via the license application"
      $ECHO "These were placed in ./licenses.out."
      $ECHO "Would you like these added to the license database now (y/n) [y]: \c"
      addanswer="hoho"
      while [ "$addanswer" != "" -a "$addanswer" != "y" -a "$addanswer" != "n" ]
      do
        read addanswer
        if [ "$addanswer" = "" ];then
          addanswer="y"
        fi
        if [ "$addanswer" = "y" ];then
          cat licenses.out >> $PRODUCTINSTALLPATH/license/licenses
	  if [ $? -ne 0 ];then
            $ECHO "WARNING: Failed to install licenses"
          fi
        fi
      done
    fi
    if [ -r license_request.txt ];then
      $ECHO "It looks like a license request file was created via the license application"
      $ECHO "Please mail the license_request.txt file to license@easysoft.com"
      $ECHO "to obtain your trial or full licenses"
    fi
  done
fi
exit 0
