#!/bin/ash

# =============================================================================
# Verify PGP signed file
# Copyright (C) SFR 2016
# License: GNU GPL Version 3 or later
# Requirements: gpg2/gpg, Xdialog
# =============================================================================

export TEXTDOMAIN=fatdog
export OUTPUT_CHARSET=UTF-8

APPTITLE="$(gettext "PGP Signature Verification")"
ICON_OK="/usr/local/lib/X11/pixmaps/ok.xpm"
ICON_ERROR="/usr/local/lib/X11/pixmaps/error.xpm"

which gpg2 >/dev/null 2>&1 && GPG=gpg2 || GPG=gpg

# -----------------------------------------------------------------------------

[ -e "$1" ] || exit 1

# Ask for file containing signed data if one couldn't be found automatically
if [ -e "${1%.*}" ]; then
	FILE="${1%.*}"
else
	FILE=$(Xdialog --stdout \
				   --title "${APPTITLE}" \
				   --backtitle "$(gettext "Select file containing signed data:")" \
				   --fselect "$(dirname "$1")" 0 0)
	[ $? -ne 0 ] && exit
fi

# "Please wait" dialog, useful for large files, like ISOs, etc.
Xdialog --title "${APPTITLE}" \
		--no-buttons \
		--infobox "$(gettext "Please wait, verifying...")" 0 0 999999999 & XPID=$!

# Verify
RESULT="$(${GPG} --verify "${1}" "${FILE}" 2>&1)"

# Icon depends on return code
if [ $? -eq 0 ]; then
	ICON="${ICON_OK}"
else
	ICON="${ICON_ERROR}"
fi

# Close "Please wait" dialog
kill -0 $XPID 2>/dev/null && kill $XPID

# Show the result
Xdialog --title "${APPTITLE}" \
		--icon "${ICON}" \
		--backtitle "$(gettext "Signature in:") ${1}\n$(gettext "Signed data in:") ${FILE}" \
		--infobox "${RESULT}" 0 0 999999999

exit

###############################################################################
