#!/bin/sh
set -e

LIBRARY=/usr/lib/opensc-pkcs11.so
NAME="OpenSC PKCS #11 Module"

add_support() {
	local SQL="$1"
	local DBDIR="$2"
	local PROG="$3"
	if [ ! -d "$DBDIR" ]
	then
		return
	fi
	if modutil -list "$NAME" -dbdir "$SQL$DBDIR" > /dev/null 2>&1 ||
		[ $? -eq 32 ]
	then
		echo Already supported in "$PROG"
	else
		echo Adding support to "$PROG"
		modutil -add "$NAME" -libfile $LIBRARY -dbdir "$SQL$DBDIR"
	fi
}

DBDIR=~/.pki/nssdb
add_support "sql:" "$DBDIR" "user PKI"

DBDIR=$(ls -d ~/.mozilla/firefox/*.default 2> /dev/null || true)
add_support "" "$DBDIR" Iceweasel
