#!/bin/bash
+mail_dir=$(realpath --relative-to=. "$(dirname "$0")/../mails")
+printf -v TARGET '%s/%(%Y-%m-%d+%s)T.email' "$mail_dir" -1
+
+subject="$1"
+shift
to="$1"
-cc="$2"
-subject="$3"
+shift
+readarray -t cc < <(printf '%s\n' "$@" | sort -u)
-(
- cat <<EOF
-To: $to
-Cc: $cc
-Subject: $subject
+for i in "${!cc[@]}"; do
+ e=${cc[$i]##*<}
+ e=${e%%>*}
+ if grep -q -F "<$e>" <<<"$to"; then
+ unset "cc[$i]"
+ fi
+done
-EOF
+{
+ printf 'To: %s\n' "$to"
+ if [ "${#cc[@]}" -gt 0 ]; then
+ printf 'Cc: %s\n' "${cc[@]}"
+ fi
+ printf 'Subject: %s\n\n' "$subject"
cat
-) | claws-mail --compose-from-file -
+} > "$TARGET"
+
+gvim "$TARGET"
+
+printf 'When you are finished editing you need to send %s\n' "$TARGET"
exit 0