#! /bin/bash

echo ' Script para Debian wheezy '
echo '###########################'


function instalacion_paquetes() {
	
    echo 'Instalación de los paquetes'
    if [ "$1" = "1" ]; then
        echo 'Tarjeta inteligente'
        if [ "$2" = "32" ]; then
            echo 'Seccion para arquitectura 32 bits'
	    echo 'Se requiere usuario administrador'
            su - -c "aptitude install openssl pcscd opensc && \
            /home/$USERNAME/driver-device/repositorio-tibisay.sh && \
            apt-key add  /home/$USERNAME/driver-device/apt-seguridad.gpg.asc && \
            aptitude update && \
            dpkg -i /home/$USERNAME/driver-device/libssl0.9.8_0.9.8o-7_i386.deb && \
            aptitude install esteidfirefoxplugin && \
            dpkg -i /home/$USERNAME/driver-device/libclassicclient_6.1.0-b07_i386_fii.deb && \
	    cd /usr/lib/i386-linux-gnu && \
            mv opensc-pkcs11.so opensc-pkcs11.so.original && \
            ln -s /usr/lib/ClassicClient/libgclib.so /usr/lib/i386-linux-gnu/opensc-pkcs11.so && \ "   
        else
            echo 'Seccion para arquitectura 64 bits'
            echo 'Se requiere usuario administrador'
	    su - -c "aptitude install openssl pcscd opensc && \
            /home/$USERNAME/driver-device/repositorio-tibisay.sh && \
            apt-key add /home/$USERNAME/driver-device/apt-seguridad.gpg.asc && \
            aptitude update && \
            dpkg -i /home/$USERNAME/driver-device/libssl0.9.8_0.9.8o-7_amd64.deb && \
            aptitude install esteidfirefoxplugin && \
            dpkg -i /home/$USERNAME/driver-device/libclassicclient_6.1.0-b07_amd64.deb && \
	    cd /usr/lib/x86_64-linux-gnu && \
            mv opensc-pkcs11.so opensc-pkcs11.so.original && \
            ln -s /usr/lib/ClassicClient/libgclib.so /usr/lib/x86_64-linux-gnu/opensc-pkcs11.so "
        fi
    else
        echo 'eToken'
        if [ "$2" = "32" ]; then
            echo 'Seccion para arquitectura 32 bits'
	    echo 'Se requiere usuario administrador'
            su - -c "aptitude install openssl pcscd opensc && \
            /home/$USERNAME/driver-device/repositorio-tibisay.sh && \
            apt-key add  /home/$USERNAME/driver-device/apt-seguridad.gpg.asc && \
            aptitude update && \
            dpkg -i /home/$USERNAME/driver-device/libssl0.9.8_0.9.8o-7_i386.deb && \
            aptitude install esteidfirefoxplugin && \ 
            dpkg -i /home/$USERNAME/driver-device/SafenetAuthenticationClient-8.1.0-4_i386.deb && \
            cd /usr/lib/i386-linux-gnu && \
            mv opensc-pkcs11.so opensc-pkcs11.so.original && \ 
            ln -s /usr/lib32/libeTPkcs11.so /usr/lib/i386-linux-gnu/opensc-pkcs11.so "
        else
            echo 'Seccion para arquitectura 64 bits'
            echo 'Se requiere usuario administrador'
            su - -c "aptitude install openssl pcscd opensc && \
            /home/$USERNAME/driver-device/repositorio-tibisay.sh && \
            apt-key add  /home/$USERNAME/driver-device/apt-seguridad.gpg.asc && \
            aptitude update && \
            dpkg -i /home/$USERNAME/driver-device/libssl0.9.8_0.9.8o-7_amd64.deb && \ 
            aptitude install esteidfirefoxplugin && \
            dpkg -i /home/$USERNAME/driver-device/SafenetAuthenticationClient-8.1.0-4_amd64.deb && \
            cd /usr/lib/x86_64-linux-gnu && \
            mv opensc-pkcs11.so opensc-pkcs11.so.original && \ 
            ln -s /usr/lib64/libeTPkcs11.so /usr/lib/x86_64-linux-gnu/opensc-pkcs11.so "
        fi
    fi
}

pc_version=$(lsb_release -a | grep Codename) # almacena la versión de Debian instalada
var_version=${pc_version:10}
pc_arq=$(uname -r)  #almacena la arquitectura del cpu

arq="amd64"
vers="jessie"
#vers="wheezy"

if [ "$var_version" != "$vers" ]; then # Verifica si la versión de Debian es Wheezy
    echo 'No es debian wheezy... Se debetiene la ejecución del script..!'
    exit
else 
    echo 'debian wheezy'
fi

echo 'Creando el directorio driver-divice'
mkdir -p /home/$USERNAME/driver-device
cd /home/$USERNAME/driver-device
wget --no-check-certificate https://tibisay.cenditel.gob.ve/murachi/raw-attachment/wiki/WikiStart/repositorio-tibisay.sh
chmod 755 repositorio-tibisay.sh
wget http://tibisay.cenditel.gob.ve/repositorio/apt-seguridad.gpg.asc
chmod 755 apt-seguridad.gpg.asc

rep="0"

while [ "$rep" == "0" ]; do

    echo 'Intraduzca (1) si va utilizar tarjeta inteligente (2) si va utilizar etoken: '
    read opt
    
    if [ "$opt" = "1" -o "$opt" = "2" ]; then
        rep="1"
        if [ "$opt" = "1" ]; then
            echo 'Descargando el driver de la tarjeta inteligente Gemalto'
        
            if [ $(expr index "$pc_arq" "$arq") = 0 ]; then
                echo 'Tarjeta inteligente (arquitectura de 32 bits)'
                wget --no-check-certificate https://tibisay.cenditel.gob.ve/murachi/raw-attachment/wiki/WikiStart/libssl0.9.8_0.9.8o-7_i386.deb
                chmod 755 libssl0.9.8_0.9.8o-7_i386.deb
	            wget --no-check-certificate https://tibisay.cenditel.gob.ve/murachi/raw-attachment/wiki/WikiStart/libclassicclient_6.1.0-b07_i386_fii.deb
                chmod 755 libclassicclient_6.1.0-b07_i386_fii.deb
                wget --no-check-certificate https://tibisay.cenditel.gob.ve/murachi/raw-attachment/wiki/WikiStart/libhal1_0.5.14-8_i386.deb
                chmod 755 libhal1_0.5.14-8_i386.deb 
                #wget --no-check-certificate https://150.188.246.53/drivers/gemalto/linux/libclassicclient_6.1.0-b07_i386_fii.deb
                instalacion_paquetes 1 32
        
            else
                echo 'Tarjeta inteligente (arquitectura de 64 bits)'
                wget --no-check-certificate https://tibisay.cenditel.gob.ve/murachi/raw-attachment/wiki/WikiStart/libssl0.9.8_0.9.8o-7_amd64.deb
                chmod 755 libssl0.9.8_0.9.8o-7_amd64.deb
                wget --no-check-certificate https://tibisay.cenditel.gob.ve/murachi/raw-attachment/wiki/WikiStart/libclassicclient_6.1.0-b07_amd64.deb
                #wget --no-check-certificate https://150.188.246.53/drivers/gemalto/linux/libclassicclient_6.1.0-b07_amd64.deb
               chmod 755 libclassicclient_6.1.0-b07_amd64.deb
               instalacion_paquetes 1 64    
            fi 

       else 
            echo 'Descargando el driver del dispositivo eToken'
            if [ $(expr index "$pc_arq" "$arq") = 0 ]; then
                echo 'eToken (arquitectura de 32 bits)'
                wget --no-check-certificate https://tibisay.cenditel.gob.ve/murachi/raw-attachment/wiki/WikiStart/libssl0.9.8_0.9.8o-7_i386.deb
                chmod 755 libssl0.9.8_0.9.8o-7_i386.deb
	            wget --no-check-certificate https://tibisay.cenditel.gob.ve/murachi/raw-attachment/wiki/WikiStart/SafenetAuthenticationClient-8.1.0-4_i386.deb
	            chmod 755 SafenetAuthenticationClient-8.1.0-4_i386.deb
                wget --no-check-certificate https://tibisay.cenditel.gob.ve/murachi/raw-attachment/wiki/WikiStart/libhal1_0.5.14-8_i386.deb
                #wget http://ftp.us.debian.org/debian/pool/main/h/hal/libhal1_0.5.14-8_i386.deb
                chmod 755 libhal1_0.5.14-8_i386.deb
                instalacion_paquetes 2 32
        
           else
               echo 'eToken (arquitectura de 64 bits)'
               wget --no-check-certificate https://tibisay.cenditel.gob.ve/murachi/raw-attachment/wiki/WikiStart/libssl0.9.8_0.9.8o-7_amd64.deb
               chmod 755 libssl0.9.8_0.9.8o-7_amd64.deb
               wget --no-check-certificate https://tibisay.cenditel.gob.ve/murachi/raw-attachment/wiki/WikiStart/SafenetAuthenticationClient-8.1.0-4_amd64.deb 
               chmod 755 SafenetAuthenticationClient-8.1.0-4_amd64.deb
               wget --no-check-certificate https://tibisay.cenditel.gob.ve/murachi/raw-attachment/wiki/WikiStart/libhal1_0.5.14-8_amd64.deb
               #wget http://ftp.us.debian.org/debian/pool/main/h/hal/libhal1_0.5.14-8_amd64.deb
               chmod 755 libhal1_0.5.14-8_amd64.deb
               instalacion_paquetes 2 64        
           fi 
       fi
    else echo "debe ingresar el número indicado" 
    fi
done
