#!/bin/sh
#
# Copyright 2004-2008 Crypto-Pro. All rights reserved.
#
# This is proprietary information of
# Crypto-Pro company.
#
# Any part of this file can not be copied, 
# corrected, translated into other languages,
# localized or modified by any means,
# compiled, transferred over a network from or to
# any computer system without preliminary
# agreement with Crypto-Pro company
#
# ---------------------------------------------------
#
# This script installs CryptoPro JCP v.1.0
# 
# Usage:
#   install.sh <path_to_JRE> [-noupdate | -jcsp] [<serial_number> <company_name>]
#
# Example:
#   install.sh /usr/java/jre6 XXXXX-XXXXX-XXXXX-XXXXX-XXXXX "Your Company"
#   install.sh /usr/java/jre6 XXXXX-XXXXX-XXXXX-XXXXX-XXXXX "Your Company"
#

if [ -z "$1" ]; then
  printf "USAGE:\n"
  printf "  install.sh path_to_JRE [-noupdate | -jcsp] [serial_number company_name]\n"
  printf "  Example:\n"
  printf "    install.sh /usr/java/jre6 XXXXX-XXXXX-XXXXX-XXXXX-XXXXX \"Your Company\"\n"
  printf "    install.sh /usr/java/jre6 -jcsp XXXXX-XXXXX-XXXXX-XXXXX-XXXXX\n"
  exit 1
fi

printf "Params: $*\n"

INSTALL_JCSP=0
OLDPATH="`pwd`"
cd "`dirname "$0"`"
DISTPATH="`pwd`"
cd "$OLDPATH"

[ -r "$DISTPATH/JCP.jar" ] && [ -r "$DISTPATH/JCPinst.jar" ] || {
  printf "Script must be in the installer folder\n"
  exit 1
}

JREDIR=$1
JAVACMD="$JREDIR/bin/java"
[ -x "$JAVACMD" ] || {
  printf "File not found: $JAVACMD\n"
  exit 1
}

"$JAVACMD" -version

if [ "$2" = "-noupdate" ]; then
  printf "NoUpdate option ON\n"
  shift
  if [ -r "$JREDIR/lib/ext/JCP.jar" ]; then
    printf "JCP exists - will not update\n"
    exit 0
  fi
fi

if [ "$2" = "-jcsp" ]; then
  printf "Install JCSP\n"
  INSTALL_JCSP=1
fi

unalias mktemp > /dev/null 2>&1
if [ -x "`which mktemp 2>/dev/null`" ] || type mktemp >/dev/null 2>&1; then
  DELFILESLST=`mktemp /tmp/jcp_installer.XXXXXX` || {
    printf "mktemp failed\n"
    exit 1
  }
else
  DELFILESLST=/tmp/jcp_installer.$$
  touch "$DELFILESLST"
fi

if [ $INSTALL_JCSP -eq 0 ]; then
  if [ -n "$2" ]; then
    printf "License: $2 $3\n"
    "$JAVACMD" -Xbootclasspath/p:"$DISTPATH/JCP.jar" ru.CryptoPro.JCP.tools.License -serial "$2" -company "$3"
  else
    "$JAVACMD" -Xbootclasspath/p:"$DISTPATH/JCP.jar" ru.CryptoPro.JCP.tools.License
  fi
else
  if [ -n "$3" ]; then
    printf "JCSP License: $3\n"
    "$JAVACMD" -Xbootclasspath/p:"$DISTPATH/JCPinst.jar:$DISTPATH/JCP.jar:$DISTPATH/JCSP.jar" ru.CryptoPro.JCSP.JCSPLicense -serial "$3"
  else
    "$JAVACMD" -Xbootclasspath/p:"$DISTPATH/JCPinst.jar:$DISTPATH/JCP.jar:$DISTPATH/JCSP.jar" ru.CryptoPro.JCSP.JCSPLicense
  fi
fi

if [ $INSTALL_JCSP -eq 0 ]; then
  "$JAVACMD" -cp "$DISTPATH/JCPinst.jar" ru.CryptoPro.Install.VariantOne -uninstall all -verbose -skipfiles "$DELFILESLST"
fi

if [ -r "$DELFILESLST" ]; then
  sed "s/.*/'&'/" "$DELFILESLST" | xargs rm -f
  rm -f "$DELFILESLST"
fi

if [ $INSTALL_JCSP -eq 0 ]; then
  for i in JCP.jar ASN1P.jar forms_rt.jar asn1rt.jar JCP.jar Oscar.jar JCryptoP.jar JCPinst.jar JCPxml.jar JCPRequest.jar cpSSL.jar OCF.jar JCPRevCheck.jar JCPRevTools.jar CAdES.jar XMLDSigRI.jar JCSP.jar libcspjni.so; do
    rm -f "$JREDIR/lib/ext/$i"
    if [ -r "$JREDIR/lib/ext/$i" ]; then
      printf "JVM is running or jar-files are locked:\n"
      printf "$JREDIR/lib/ext/$i\n"
      printf "Stop all processes that use Java and retry\n"
      exit 1
    fi
  done
fi

if [ $INSTALL_JCSP -eq 0 ]; then
  if [ -n "$2" ]; then 
    "$JAVACMD" -cp "$DISTPATH/JCPinst.jar" ru.CryptoPro.Install.VariantTwo -install -verbose -serial "$2" -company "$3"
  else
    "$JAVACMD" -cp "$DISTPATH/JCPinst.jar" ru.CryptoPro.Install.VariantTwo -install -verbose
  fi
else
  if [ -n "$3" ]; then 
    "$JAVACMD" -cp "$DISTPATH/JCSP.jar" ru.CryptoPro.JCSP.JCSPInstaller -install -verbose -serial "$3"
  else
    "$JAVACMD" -cp "$DISTPATH/JCSP.jar" ru.CryptoPro.JCSP.JCSPInstaller -install -verbose
  fi 
  cp "$DISTPATH/libcspjni.so" "$JREDIR/lib/ext/libcspjni.so"
fi

