#!/usr/bin/env bash
# Usage: scripts/new.sh [p|s]
# SPDX-FileCopyrightText: 2024 Julien Moutinho (adh14) <julm+rezine@autogeree.net>
# SPDX-License-Identifier: AGPL-3.0-or-later
# shellcheck disable=SC2086
# shellcheck disable=SC2154
set -eux

rfcCat=$1; shift
test $# -le 0 || declare "$@"

rfcLastAdvocacy=$(find "${0%/*}"/rfcs -maxdepth 1 -type d -name "$rfcCat*" | sort -n | tail -1)
rfcLastNum=$(realpath --relative-to "${0%/*}"/rfcs "$rfcLastAdvocacy")
rfcLastNum=${rfcLastNum#"$rfcCat"}
rfcNum=$((rfcLastNum + 1))
rfcDir="rfcs/$rfcCat$rfcNum"
rfcAdvocacy="$rfcDir/advocacy.md"

mkdir -p "$rfcDir"
cat >>"$rfcAdvocacy" <<EOF
---
title: ""
subtitle: ""
title-prefix: "\${rfcOrga} RFC\${rfcTag}"
date: \${rfcDate}
author:
  - ${rfcAuthor-"$(git config user.name) <<$(git config user.email)>>"}
To:
Cc:
In-Reply-To:
SPDX-FileCopyrightText: $(date +%Y) ${rfcAuthor-"$(git config user.name) <$(git config user.email)>"}
SPDX-License-Identifier: CC-BY-4.0
RFC-Branch: ${rfcBranch}
RFC-Category: $(case "$rfcCat" in (p) echo problème;; (s) echo solution;; esac)
RFC-Compasses:
$(printf -- "  - %s\n" ${rfcCompasses})
RFC-Phase: 
RFC-Status: brouillon
...

EOF
cat >&2 <<EOF
New RFC draft: $rfcAdvocacy

To preview:       ${0%/*}/html.sh $rfcDir
To tag and push:  ${0%/*}/tag.sh  $rfcDir
To send mail:     ${0%/*}/send.sh $rfcDir
EOF