
#!/bin/bash

title=""
body=""
tags=""

usage() {
    echo "Usage: $0 [-t title] [-b body] [-g tags] [file]"
    echo "Options:"
    echo "  -t, --title   Title of the note"
    echo "  -b, --body    Body content of the note"
    echo "  -g, --tags    Comma-separated tags for the note"
    echo "  file          File to read the body content from"
    exit 1
}

while [[ $# -gt 0 ]]; do
    case "$1" in
        -t|--title)
            title="$2"
            shift 2
            ;;
        -b|--body)
            body="$2"
            shift 2
            ;;
        -g|--tags)
            tags="$2"
            shift 2
            ;;
        *)
            if [[ -f "$1" ]]; then
                body=$(< "$1")
            else
                echo "Error: Invalid argument '$1'"
                usage
            fi
            shift
            ;;
    esac
done

if [[ -z "$title" ]] || [[ -z "$body" ]]; then
    echo "Error: Title and body are required."
    usage
fi

json_data=$(jq -n   --arg content "<p>${body}</p>"   --arg title "$title"   --arg tags "$tags"   '{content: $content, title: $title, encrypted: false, type: "html", tags: $tags, visibility: "public"}')

response=$(curl -s 'https://pento.pockethost.io/api/custom/notes'   --compressed -X POST   -H 'Accept: application/json, text/plain, */*'   -H 'Content-Type: application/json'   --data-raw "$json_data")

  echo "$response"

noteId=$(echo "$response" | jq -r '.data.id')

echo "https://pento.page/content/$noteId"
