#!/bin/sh

# Generic xdg-email replacement for macOS
# Sends e-mail via Mail.app

OSASCRIPT="/usr/bin/osascript"

# Configure this to your e-mail address
from="Vorname Nachname <email@example.com>"

while [ ${#} -gt 0 ] ; do
 arg="${1}"
 shift

 case "${arg}" in
  --utf8)
   ;;

  --help)
   ;;

  --manual)
   ;;

  --version)
   ;;

  --to)
   if [ -z "${1}" ] ; then
    exit 1
   fi
   to="${1}"
   shift
   ;;

  --cc)
   if [ -z "${1}" ] ; then
    exit 1
   fi
   shift
   ;;

  --bcc)
   if [ -z "${1}" ] ; then
    exit 1
   fi
   shift
   ;;

  --subject)
   if [ -z "${1}" ] ; then
    exit 1
   fi
   subject="${1}"
   shift
   ;;

  --body)
   if [ -z "${1}" ] ; then
    exit 1
   fi
   body="${1}"
   shift
   ;;

  --attach)
   if [ -z "${1}" ] ; then
    exit 1
   fi
   shift
   ;;

  *)
   if [ -z "${1}" ] ; then
    to="${arg}"
   else
    exit 1
   fi
   ;;
 esac
done

${OSASCRIPT} <<EOS
tell application "Mail"
 activate
 set newMessage to (a reference to (make new outgoing message))
 tell newMessage
  make new to recipient at beginning of to recipients with properties {address:"${to:-""}"}
  set the sender to "${from}"
  set the subject to "${subject:-""}"
  set the content to "${body:-""}"
 end tell
end tell
EOS

exit
