#!/bin/bash

echo "Enter your passphrase (at least 8 caracters)"
read passphrase

echo -e "\nAdministrator certification part"
echo "--------------------------------"

echo "Insert a blank card and press Return when it is done"
read

echo -n "Creation of the certification card: "
sst create_cert_card -p "$passphrase" || exit
echo "done"

echo -n "Installation of the administrator certification key: "
sst install_cert_key -p "$passphrase" || exit
echo "done"

echo -e "\nAddition of a new user in the system"
echo "------------------------------------"

echo "Enter the user name (8 characters max)"
read username

echo "Enter the user PIN code (4 digits)"
read pin

echo "Insert a blank card and press Return when it is done"
read

echo -n "Formatting the card: "
sst format_card -p "$passphrase" || exit
echo "done"

echo -n "Changing the user PIN code: "
sst unlock_card -p "$passphrase" -c "$pin" || exit
echo "done"

echo -n "Deleting $username from the default database: "
sst delete_user -d default -u "$username"
echo "done"

echo "Adding $username in the default database"
sst create_user -p "$passphrase" -d default -u "$username" || exit

cat <<EOF
To test it, you must modify the configuration file /etc/pam.d/login or the
configuration file of the used X display manager to integrate the
pam_smartcard module.

You should use the patch available in
/usr/share/doc/smartsession/login.patch
to modify /etc/pam.d/login

Now, user $username will be able to log in on this computer with his smartcard
EOF
