#!/bin/bash

# Install the eToken PRO software from Aladdin.
# The user is supposed to obtain the rar archive first,
# for instance from 
# http://www.aladdin.ru/upload/iblock/179/eToken_PKI_Client_for_Linux_v3_65.rar

archive=etoken-3-65.3-linux-redhat-i386.tar.gz

prefix=/usr

while [ $# -gt 0 ]; do
    case $1 in
	--help|--usage)
	    cat <<EOF
$0: install the Aladdin eToken PRO software

You need to obtain the software from Aladdin yourself,
e.g. from 

 http://www.aladdin.ru/upload/iblock/179/eToken_PKI_Client_for_Linux_v3_65.rar

This RAR archive contains three .tar.gz archives for different distributions
of Linux; you need to use the Red Hat flavour, i.e.

 etoken-3-65.3-linux-redhat-i386.tar.gz

Run this script with the above tarball as a command-line argument:

 $0 etoken-3-65.3-linux-redhat-i386.tar.gz

This will generate a .deb file which can be installed with dpkg.
EOF
	    exit 0
	    ;;
	*)
	    archive=$1
	    ;;
    esac
    shift
done


if [ ! -r $archive ]; then
    echo "Can't read $archive. Stop"
    exit 1
fi

tar xfz $archive || {
    echo "Unpacking $archive failed. Stop"
    exit 1
}

oldcwd=`pwd`

cd etoken-3-65.3-linux-i386

tmpdir=`mktemp -d`

tar xCfz $tmpdir /usr/share/etoken-pro-support/debian-template.tar.gz

installprefix=$tmpdir/debian-template/etoken-pro-aladdin/$prefix
docdir=$tmpdir/debian-template/etoken-pro-aladdin/usr/share/doc

mkdir -p $docdir
mkdir -p $installprefix/bin
mkdir -p $installprefix/sbin
mkdir -p $installprefix/lib

cp LicenseAgreement.* $docdir/
cp etckdump etckinit $installprefix/bin
cp etokend etsrvd $installprefix/sbin

cp aksifdh.so.3-65.3 $installprefix/lib
cp libetokendll.so.3-65.3 $installprefix/lib
cp libetpkcs11.so.3-65.3 $installprefix/lib

ln -s aksifdh.so.3-65.3  $installprefix/lib/aksifdh.so
ln -s libetokendll.so.3-65.3 $installprefix/lib/libetokendll.so
ln -s libetpkcs11.so.3-65.3 $installprefix/lib/libetpkcs11.so

cd $tmpdir/debian-template

echo "Generating deb package..."
fakeroot dpkg-deb --build etoken-pro-aladdin etoken-pro-aladdin_3.65.3-1_i386.deb

if [ $? -eq 0 ] ; then
    echo OK
else
    echo FAILED
    echo "(sorry)"
    exit
fi

mv etoken-pro-aladdin_3.65.3-1_i386.deb $oldcwd

rm -rf $tmpdir

echo "Install the resulting package with"
echo " dpkg -i etoken-pro-aladdin_3.65.3-1_i386.deb"

echo
echo "Goodbye"

