#!/bin/bash

# LibrePortal Tag Content Manager
# Handles content extraction from LibrePortal tags

# Get current tag content (works for both states).
# Single awk fork instead of grep | awk | head — called hundreds of times
# during a webui regen, so the fork savings are material. Value ends at
# the first pipe / whitespace / EOL, matching the tags_manager_update
# placeholder-capture rule (so two annotations on one line don't bleed).
tagsManagerGetTagContent()
{
    local file_path="$1"
    local tag_name="$2"

    awk -v t="$tag_name" '
        {
            marker = "#LIBREPORTAL|" t "|"
            p = index($0, marker)
            if (p == 0) next
            rest = substr($0, p + length(marker))
            n = match(rest, /[| \t\r]/)
            if (n > 0) rest = substr(rest, 1, n - 1)
            print rest
            exit
        }
    ' "$file_path"
}
