#
# Copyright (c) 1999-2005 Easysoft Ltd. All rights reserved.
#
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
