#!/bin/bash

LOG_FILE=gpshell.log

echo "mode_201
gemXpressoPro
enable_trace
establish_context
card_connect
select -AID A000000018434D00
open_sc -security 3 -keyind 0 -keyver 0 -key 47454d5850524553534f53414d504c45 // Open secure channel
#get_status -element 20 # list of packages
get_status -element 40 # list of applets
card_disconnect
release_context
" | gpshell > $LOG_FILE

if [ $? -ne 0 ]; then
    read -p "Could not get list of applets..."
    rm $LOG_FILE
    exit 1
fi

APPLETS=`cat $LOG_FILE | grep -E '^[0-9a-f]+\s+[0-9]\s+[0-9]' | sed 's/^\([0-9a-f]*\).*$/\1/'`

echo "List of applets installed on smart card:"
for AID in $APPLETS; do
    echo -n "  AID $AID (ASCII "
    echo -n $AID | xxd -ps -r
    echo ")"
done

read -p "Press any key to exit..."
rm $LOG_FILE
