#!/bin/bash
#
# Install Script for the HID Global OMNIKEY CCID driver Smartcard reader series.
#
# Version v4.2.8
# HID Global
# https://www.hidglobal.com
#

echo
echo Installing HID Global OMNIKEY CCID driver Smartcard reader driver ...
echo

sysconfdir="/etc"
inifile="${sysconfdir}/omnikey.ini"

#
# copy the pcsc-shared library to its proper place
#

if [ "$1" = "-d" ]; then
    dropdir=$2
else
#
# check if pcsc-lite is installed
#
localpcsc=`which pcscd`
    if [ -n "${localpcsc}" ]; then
        echo "PCSC-Lite found: ${localpcsc}"
        dropdir=$($localpcsc -v | grep usbdropdir | sed 's/^.*usbdropdir=\(.*\) .*$/\1/' | cut -f1 -d' ')
    else
        echo "PCSC-Lite is not found in current path!"
        echo "Retry with appropriate user or go visit"
        echo "http://alioth.debian.org/projects/pcsclite/"
        echo "and install the latest version of pcsc-lite."
    fi
fi

if [ -z "${dropdir}" ]; then
    dropdir="/usr/lib/pcsc/drivers/"
fi

if [ ! -d "$dropdir" ]; then 
    mkdir -p $dropdir 
fi

# Delete older driver versions
rm -rf ${dropdir}/ifdok*

bundle=$(ls -A | grep -e *.bundle)
echo Copying ${bundle} to ${dropdir} ...
cp -r ./${bundle} ${dropdir}

if [ -e "${inifile}" ]; then
    echo "Backing up ${inifile}"
    mv -vf "${inifile}" "${inifile}.bak"
fi
install -m0600 ./omnikey.ini ${inifile}

if [ -e /etc/SuSE-release ]; then
    install -m0644 ./z98_omnikey.rules /etc/udev/rules.d/10-omnikey.rules
else
    install -m0644 ./z98_omnikey.rules /etc/udev/rules.d/z98_omnikey.rules
fi

# remove old drivers rule
if [ -e /etc/udev/rules.d/10-omnikey_rfid.rules ]; then
    rm -f /etc/udev/rules.d/10-omnikey_rfid.rules
fi
if [ -e /etc/udev/rules.d/z98_omnikey_rfid.rules ]; then
    rm -f /etc/udev/rules.d/z98_omnikey_rfid.rules
fi

if [ -e ./ok_pcscd_hotplug.sh  ]; then
    install -m0744 ./ok_pcscd_hotplug.sh /lib/udev/ok_pcscd_hotplug.sh
fi

#if [ -e ./ifdok_bugreport ]; then
#    install -m0755 ./ifdok_bureport /usr/sbin/
#fi


echo
echo "Installation finished!"
