Changeset View
Changeset View
Standalone View
Standalone View
doc/examples/scd-event
#!/bin/sh | #!/bin/sh | ||||
# Sample script for scdaemon event mechanism. | # | ||||
# Sample script for the scdaemon event mechanism. | |||||
#exec >>/tmp/scd-event.log | |||||
PGM=scd-event | set -eu | ||||
reader_port= | # Uncomment to append script output to a system log file. | ||||
old_code=0x0000 | # exec >>"${TMPDIR:-/tmp}/scd-event.log" 2>&1 | ||||
new_code=0x0000 | |||||
status= | |||||
tick='`' | readonly progname='scd-event' | ||||
prev= | |||||
while [ $# -gt 0 ]; do | usage() { | ||||
arg="$1" | |||||
case $arg in | |||||
-*=*) optarg=$(echo "X$arg" | sed -e '1s/^X//' -e 's/[-_a-zA-Z0-9]*=//') | |||||
;; | |||||
*) optarg= | |||||
;; | |||||
esac | |||||
if [ -n "$prev" ]; then | |||||
eval "$prev=\$arg" | |||||
prev= | |||||
shift | |||||
continue | |||||
fi | |||||
case $arg in | |||||
--help|-h) | |||||
cat <<EOF | cat <<EOF | ||||
Usage: $PGM [options] | Usage: $progname [options] | ||||
$PGM is called by scdaemon on card reader status changes | |||||
$progname is called by scdaemon when the card reader status changes. | |||||
Options: | Options: | ||||
--reader-port N Reports change for port N | -h, --help show this help message and exit | ||||
--old-code 0xNNNN Previous status code | --reader-port N reports change for port N | ||||
--old-code 0xNNNN Current status code | --old-code 0xNNNN previous status code | ||||
--new-code 0xNNNN new status code | |||||
--status USABLE|ACTIVE|PRESENT|NOCARD | --status USABLE|ACTIVE|PRESENT|NOCARD | ||||
Human readable status code | human-readable status code | ||||
Environment: | Environment: | ||||
GNUPGHOME=DIR set to the active homedir | |||||
GNUPGHOME=DIR Set to the active homedir | |||||
EOF | EOF | ||||
exit 0 | } | ||||
;; | |||||
--reader-port) | reader_port='' | ||||
prev=reader_port | old_code='' | ||||
new_code='' | |||||
status_code='' | |||||
temp=$(getopt -o 'h' --long 'help,reader-port:,old-code:,new-code:,status:' --name "$progname" -- "$@") | |||||
eval set -- "$temp" | |||||
unset temp | |||||
while true; do | |||||
case "$1" in | |||||
-h|--help) | |||||
usage; exit 0 | |||||
;; | ;; | ||||
--reader-port=*) | --reader-port) | ||||
reader_port="$optarg" | reader_port="$2" | ||||
shift 2; continue | |||||
;; | ;; | ||||
--old-code) | --old-code) | ||||
prev=old_code | old_code="$2" | ||||
;; | shift 2; continue | ||||
--old-code=*) | |||||
old_code="$optarg" | |||||
;; | ;; | ||||
--new-code) | --new-code) | ||||
prev=new_code | new_code="$2" | ||||
;; | shift 2; continue | ||||
--new-code=*) | |||||
new_code="$optarg" | |||||
;; | ;; | ||||
--status) | --status) | ||||
prev=status | status_code="$2" | ||||
;; | shift 2; continue | ||||
--new-code=*) | |||||
status="$optarg" | |||||
;; | ;; | ||||
--) | |||||
-*) | shift; break | ||||
echo "$PGM: invalid option $tick$arg'" >&2 | |||||
exit 1 | |||||
;; | ;; | ||||
*) | *) | ||||
break | printf 'Internal error!\n' >&2 | ||||
exit 1 | |||||
;; | ;; | ||||
esac | esac | ||||
shift | |||||
done | done | ||||
if [ -n "$prev" ]; then | |||||
echo "$PGM: argument missing for option $tick$prev'" >&2 | if [ $# -gt 0 ]; then | ||||
exit 1 | printf '%s: invalid arguments\n' "$progname" >&2 && exit 1 | ||||
fi | fi | ||||
cat <<EOF | # Log the invocation and the options passed. | ||||
======================== | printf '%s: reader-port="%s" old-code="%s" new-code="%s" status="%s"\n' \ | ||||
port: $reader_port | "$progname" "$reader_port" "$old_code" "$new_code" "$status_code" | ||||
old-code: $old_code | |||||
new-code: $new_code | |||||
status: $status | |||||
EOF | |||||
if [ x$status = xUSABLE ]; then | if [ "$status_code" = 'USABLE' ]; then | ||||
gpg --batch --card-status 2>&1 | gpg --batch --card-status 2>&1 | ||||
fi | fi | ||||