#!/bin/bash
# Ubuntu Belpic
# script installs Beid-tools (Belpic package) from Edgy repositories to Ubuntu Dapper Drake
# a good deal of other packages get replaced as well due to dependencies.
###########################################################################################


# must be root to do this
if [ "$(whoami)" != "root" ]; then
	echo "$(whoami) is not root. aborting. try again as root / sudo"
	exit
fi

# see if Edgy has repo in sources.list
grep 'edgy' /etc/apt/sources.list
if [ "$?" != "0" ]; then
	echo "adding 'Edgy Eft' repositories"; echo ; sleep 3
	REMOVE_EDGY="yes"
	cp /etc/apt/sources.list /etc/apt/sources.list.orig
	(echo "deb http://be.archive.ubuntu.com/ubuntu/ edgy main universe  # Edgy for ACR38U and Belpic") >> /etc/apt/sources.list
	apt-get update
fi

# check that card reader is plugged in and detected by OS
lsusb |grep "Smart Card"
if [ "$?" != "0" ]; then 
	echo "no smartcard reader detected. Plug in the Card Reader and press a key"; 
	read DUMMY
fi

# install drivers
lsusb |grep "ACR3"
if [ "$?" != "0" ]; then 
	echo "ok, card reader found, installing drivers"
	sleep 1
	apt-get -y -t edgy install libacr38u libacr38ucontrol0 
else 
	echo "no ACRU38 found";
	echo "$0 will continue to install Belpic software, "
	echo "but this may not work untill you've installed drivers for your Card Reader"
	read DUMMY
fi

#install Belpic tools, pcsc daemon, ...
apt-get -y -t edgy install beid-tools
apt-get -y -t edgy install pcscd
apt-get -y -t edgy install libpcsclite-dev 

#install GUI
apt-get -y -t edgy install beidgui

#install the rest of the Belpic package
apt-get -y -t edgy install libbeid2-dev
apt-get -y -t edgy install libbeid2
apt-get -y -t edgy install libbeidlibopensc2-dev
apt-get -y -t edgy install libbeidlibopensc2

#test for 'so far, so good'
beid-tool -l | grep "pcsc"
if [ "$?" = "0" ]; then 
	echo; echo "found card reader with pcsc driver. so far, so good"
	echo "insert a card in the card reader, then press any key"
	read DUMMY
	beid-tool -a | grep "Belpic smartcards"
	beid-tool -a | grep "Card ATR"
else
	echo;echo " not sure this is going to work" ; echo;
	sleep 5
fi

if [ "$REMOVE_EDGY" = "yes" ]; then
	#restore original apt sources, without edgy repo
	mv -f /etc/apt/sources.list.orig /etc/apt/sources.list 
	apt-get update 
fi 

