#!/usr/bin/env bash
# SPDX-FileCopyrightText: 2024 Julien Moutinho (adh14) <julm+rezine@autogeree.net>
# SPDX-License-Identifier: AGPL-3.0-or-later
# shellcheck disable=SC2046
# shellcheck disable=SC2086
# shellcheck disable=SC2317
# To push and send mails:
#   scripts/send.sh rfcs/p1
# To push without sending mails:
#   scripts/send.sh rfcs/p1 no_git_send_email=set git_push=--force
set -eux
. "${0%/*}"/lib.sh
rfcDir=$1; shift
test $# -le 0 || declare "$@"

coverLetter=$(mktemp)
removeAtExit+=("$coverLetter")
mimepart_alternative=$(uuidgen)
mimepart_related=$(uuidgen)

rfcDirMetadata
rfcRevision
rfcTag
#rfcPDF -o "$rfcDir/${rfcRepo}-${rfcTag}".pdf

git_push_remote=${git_push_remote-origin}

if test ! "${no_git_send_email:+set}"; then
{ # coverLetter

rfcPlain \
  $(rfcMetadata) \
  --template "${0%/*}"/../styles/rfc.header.mail \
  -o -

cat <<EOF
Content-Disposition: inline
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary="--==_mimepart_alternative_$mimepart_alternative"
Content-Transfer-Encoding: UTF-8
EOF

cat <<EOF

----==_mimepart_alternative_$mimepart_alternative
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: UTF-8


::: attention
Vous lisez actuellement ce mél dans son format de texte brut,
vous _devriez_ le lire dans son format HTML, qui est plus soigné.
:::

EOF

rfcMarkdown -o -
cat <<EOF

----==_mimepart_alternative_$mimepart_alternative
Content-Type: multipart/related; boundary="--==_mimepart_related_$mimepart_related"
Content-Transfer-Encoding: UTF-8

----==_mimepart_related_$mimepart_related
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: UTF-8

EOF
cat "$rfcDir"/index.html
cat <<EOF
----==_mimepart_related_$mimepart_related

----==_mimepart_alternative_$mimepart_alternative

EOF

# FIXME: the PDF rendition is not good
#cat <<EOF
#----==_mimepart_mixed_$mimepart_mixed
#Content-Type: application/pdf; name="RFC$rfcTag.pdf"
#Content-Transfer-Encoding: base64
#Content-Disposition: attachment; filename*0="RFC$rfcTag."; filename*1=pdf
#
#EOF
#base64 --wrap 76 "$rfcDir/$rfcRepo-$rfcTag".pdf

} >"$coverLetter"

patchesDir=$(mktemp -d)
removeAtExit+=("$patchesDir")
git format-patch "${git_push_remote}/${rfcBranch}..$rfcTag" \
  --output-directory "$patchesDir" \
  "$rfcDir" ':!*.html'

rfcAdvocacyMeta=$(mktemp --suffix ".json")
removeAtExit+=("$rfcAdvocacyMeta")
pandoc -o "$rfcAdvocacyMeta" --to plain \
   --template "$rfcRoot"/styles/metadata.json \
  "$rfcDir"/advocacy.md
git send-email ${git_send_email-} \
  --to "$(jq -r .To <"$rfcAdvocacyMeta" | sed -e '/^null$/d')" \
  --cc "$(jq -r .Cc <"$rfcAdvocacyMeta" | sed -e '/^null$/d')" \
  --confirm cc \
  --8bit-encoding UTF-8 \
  --cover-letter "$coverLetter" \
  "$patchesDir"
fi

if test ! "${no_git_push:+set}"; then
  git push ${git_push-} --follow-tags "$git_push_remote" "$rfcTag"
  rfcTagRev=$(git rev-list "$rfcTag" -1)
  git push ${git_push-} "$git_push_remote" "$rfcTagRev:refs/heads/$rfcBranch"
  git push ${git_push-} "$git_push_remote" "$rfcTagRev:refs/heads/$rfcBranchRevision"
  git push ${git_push-} "$git_push_remote"
fi