#!/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 uninstalls CryptoPro JCP v.1.0
# 
# Usage:
#   uninstall.sh <path_to_JRE>
#
# Example:
#   uninstall.sh /usr/java/jre6
#

if [ -z "$1" ]; then
  printf "USAGE:\n"
  printf "  install.sh path_to_JRE\n"
  printf "Example:\n"
  printf "  uninstall.sh /usr/java/jre6\n"
  exit 1
fi

printf "Params: $*\n"

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
}

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

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

"$JAVACMD" -version

"$JAVACMD" -cp "$DISTPATH/JCPinst.jar" ru.CryptoPro.Install.VariantOne -uninstall all -verbose -rmsetting -skipfiles "$DELFILESLST"

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

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
