#!/usr/bin/env bash
# sign_posts.sh — Sign Hugo posts and insert a signature shortcode.
# Usage:
# ./scripts/sign_posts.sh -k "you@example.com"
# Options:
# -k, --key KeyID or email for gpg --local-user (optional; default = gpg default key)
# -c, --content Content dir (default: content)
# -i, --include Comma-separated subdirs under content to process (default: posts,PhD_journey)
# -n, --dry-run Show actions without changing files
set -euo pipefail
KEYID="55A2A5DE84792BACC6CAEE3FB5882850E04C8D2A"
CONTENT_DIR="content"
INCLUDE_DIRS=("posts" "PhD_journey" "about")
DRYRUN=0
usage() {
sed -n '2,30p' "$0" | sed 's/^# \{0,1\}//'
}
while [[ $# -gt 0 ]]; do
case "$1" in
-k|--key) KEYID="${2:-}"; shift 2 ;;
-c|--content) CONTENT_DIR="${2:-}"; shift 2 ;;
-i|--include) IFS=',' read -r -a INCLUDE_DIRS <<< "${2:-}"; shift 2 ;;
-n|--dry-run) DRYRUN=1; shift ;;
-h|--help) usage; exit 0 ;;
*) echo "Unknown arg: $1"; usage; exit 1 ;;
esac
done
command -v gpg >/dev/null 2>&1 || { echo "gpg not found in PATH"; exit 1; }
# Ensure shortcode exists (don’t overwrite if user customized it)
SHORTCODE_PATH="layouts/shortcodes/sig.html"
if [[ ! -f "$SHORTCODE_PATH" ]]; then
echo "Creating $SHORTCODE_PATH"
[[ $DRYRUN -eq 1 ]] || mkdir -p "$(dirname "$SHORTCODE_PATH")"
[[ $DRYRUN -eq 1 ]] || cat > "$SHORTCODE_PATH" <<'EOF'
{{- /* Usage: {{< sig >}} or {{< sig "name.asc" >}}. Reads from .Page.File.Dir */ -}}
{{- $name := .Get 0 | default "signature.asc" -}}
{{- $dir := .Page.File.Dir -}}
{{- $sig := readFile (print $dir $name) | htmlEscape -}}
OpenPGP signature (.asc)
{{$sig}}
Verify: gpg --verify {{ $name }} {{ .Page.File.Path }}