#!/usr/bin/env bash
# sign-xpi -- sign Mozilla XPI addon/extension archives

. lib.bash || exit

cert=""
nssdb="sql:$HOME/.pki/nssdb"
force=0
do_list=0

have signtool  || err "missing 'signtool' from NSS"
have zip       || err "missing 'zip' tool"
(( ! errors )) || exit

while getopts ":c:d:fl" OPT; do
	case $OPT in
	c) cert=$OPTARG;;
	d) nssdb=$OPTARG;;
	f) force=1;;
	l) do_list=1;;
	*) lib:die_getopts;;
	esac
done; shift $((OPTIND-1))

if (( do_list )); then
	signtool -d "$nssdb" -l
	exit
fi

if [[ ! $cert ]]; then
	die "certificate name (-c) not specified"
fi

if [[ ! -f install.rdf ]]; then
	die "current directory does not contain a valid Mozilla extension"
fi

out_file="${1:-${PWD##*/}}.xpi"

(shopt -s nullglob; rm -f *.xpi)

signtool -d "$nssdb" -k "$cert" -Z "$out_file" .

lib:echo "Created \"$out_file\""
