#!/bin/bash
#                       /usr/local/bin/mailto
# https://crystalfaeries.net/posix/bin/mailto
# celeste:crystalfaery MAILTO 2020-08-12 12:39:15+00:00
# MAILTO operates in either of two modes, other than version and help info:
# with a valid mailto: URL as argument, invoke the mailer with it.
# with no argument, prompt for data to compose a valid mailto URL.
# remember e-mail syntax and quoting, example:
# mailto 'mailto:"celeste:crystalfaery" <celeste@crystalfaeries.net>,"selene:crystalfaery" <selene@crystalfaeries.net>'
# <a href="mailto:info@example.com?subject=subject&cc=cc@example.com">mail link</a>
# mailto: 	to set the recipient, or recipients, separate with commas
# &cc=		to set the CC recipient(s)
# &bcc= 	to set the BCC recipient(s)
# &subject= 	to set the email subject,	replace spaces with %20, use no line breaks.
# &body= 	to set the body of the message,	replace spaces with %20, and line breaks %0A.

let help=16	# 1 less than this line's # in file

cd "${HOME}"
case $# in
0)	# no options or arguments, so prompt what to do...
	echo "enter (a) valid &quot;to:&quot; address(es): "
	read TO
	echo "enter (a) valid &quot;cc:&quot; address(es): "
	read CC
	echo "enter (a) valid &quot;bcc:&quot; address(es): "
	read BCC
	echo "enter a line of text as the Subject: "
	read temp
	SUBJECT="`echo ${temp} | sed 's/ /%20/g'`"
	echo "enter text as the Body of the e-mail: "
	read temp
	BODY="`echo ${temp} | sed 's/ /%20/g; s/\n/%0A/g'`"
	exec "${0}" mailto://"${TO}"?cc="${CC}"&bcc="${BCC}"&subject="${SUBJECT}"&body="${BODY}"
	exit $?
;;
*)	# we have an option and/or argument
    case "${1}" in
    -h | --help )
	head -n $help $0
	exit
	;;
    -v | --version )
	head -n 4 $0 | tail -n 1
	exit
	;;
    *)	REPLY="$( echo ${@} | sed 's/^mailto:\/\///g; s/^mailto://g' )"
	mail "${REPLY}"
	;;
    esac
    exit $?
;;
esac
exit	$?
