#!/bin/bash --norc

	#	mailto:gstewart@sgms-centre.com?subject=Mozilla%20external%20mailer

	urlDecode() {
		for arg; do
			echo "$arg" |perl -e '$urlEncoded = <STDIN>; $urlEncoded =~ s/%([A-Fa-f\d]{2})/chr hex $1/eg; print $urlEncoded'
		done
	}

		mailArg="$1"

	(
		echo "-==-"

		case "$mailArg" in

		*\?*)
			recipient="${mailArg%\?*}"
			extras="${mailArg#*\?}"
			;;

		*)
			recipient="$mailArg"

		esac

	#	if [[ ]]; then
	#	fi


		case "$extras" in

		*subject=*)
			subject="${extras#*subject=}"
			subject="${subject%&*}"
			subject="$(urlDecode "$subject")"
			;;

		esac


		case "$extras" in

		*body=*)
			body="${extras#*body=}"
			body="${body%&*}"
			body="$(urlDecode "$body")"
			;;

		esac


		kmailInvoke=(kmail --composer "$recipient")

		if [ "$subject" ]; then
			kmailInvoke=( "${kmailInvoke[@]}" --subject "$subject" )
		fi

		if [ "$body" ]; then
			kmailInvoke=( "${kmailInvoke[@]}" --body "$body" )
		fi

		echo "recipient=$recipient"
		echo "extras=$extras"
		echo "subject=$subject"
		echo "body=$body"
		echo "invoke=${kmailInvoke[@]}"

		echo "-==-"

		exec "${kmailInvoke[@]}"
	) >>$HOME/mailto-invoke \
	  2>&1
