#!/bin/sh

PHOTO_URL="me.png"
PROFILE_URL="https://mocus.dynv6.net"
AUTHOR_NAME="Ricozu"

BASE_DIR="/mnt/sdb/www/mocus"

convert_indieweb_json_to_html() {
    local input_file="$1"
    local output_file="$2"

    if [ ! -s "$input_file" ]; then
        echo "WARNUNG: $input_file existiert nicht oder ist leer. Überspringe..."
        return 1
    fi

    echo '<div class="h-feed">' > "$output_file"
    jq -r --arg photo "$PHOTO_URL" --arg profile "$PROFILE_URL" --arg author "$AUTHOR_NAME" '
      "<article class=\"h-entry\" id=\"post-\(.id)\">\n" +
      "  <a style=\"display:none;\" class=\"p-category\" href=\"#\">indieweb</a>\n" +
      "  <div class=\"p-author h-card\">\n" +
      "    <a class=\"u-url p-name\" href=\"\($profile)\">\n" +
      "      <img style=\"display:none;\" class=\"u-photo\" src=\"\($photo)\" alt=\"\($author)\"></a>\n" +
      "  </div>\n" +
      "  <h2 class=\"e-content\">\(.content)</h2>\n" +
      "  <div>\n" +
      "    <a class=\"u-url\" href=\"\($profile)/#post-\(.id)\">\n" +
      "      <time class=\"dt-published\" datetime=\"\(.created_at | strftime("%Y-%m-%dT%H:%M:%SZ"))\">\(.created_at | strftime("%a, %d %b %Y %H:%M:%S GMT"))</time>\n" +
      "    </a>\n" +
      "  </div>\n" +
      "</article>"
    ' "$input_file" >> "$output_file"
    echo '</div>' >> "$output_file"
}

convert_nostr_json_to_html() {
    local input_file="$1"
    local output_file="$2"

    if [ ! -s "$input_file" ]; then
        echo "WARNUNG: $input_file existiert nicht oder ist leer. Überspringe..."
        return 1
    fi

    jq -r '
      "<div>\n" +
      "  <div>" + .content + "</div>\n" +
      "  <div class=\"dt-published\">\n" +
      "    <span>" + (.created_at | strftime("%a, %d %b %Y %H:%M:%S GMT")) + "</span>\n" +
      "  </div>\n" +
      "</div>\n"
    ' "$input_file" > "$output_file"
}

convert_indieweb_json_to_html "$BASE_DIR/indieweb.json" "$BASE_DIR/indieweb.html"
convert_nostr_json_to_html "$BASE_DIR/nostr.json" "$BASE_DIR/nostr.html"
