#!/bin/bash

# --- KONFIGURACIJA ---
TEMPLATE_FILE="footer-template.html"
HTML_FILES=$(find . -name "*.html" ! -path "./$TEMPLATE_FILE")

# --- PREVERJANJA ---
if [ ! -f "$TEMPLATE_FILE" ]; then
    echo "NAPAKA: Predloga '$TEMPLATE_FILE' ne obstaja."
    exit 1
fi

# --- GLAVNA LOGIKA ---
echo "Začenjam s posodabljanjem nog..."

template_content=$(<"$TEMPLATE_FILE")

for file in $HTML_FILES; do
    awk '
        BEGIN { in_footer = 0; replaced = 0 }
        /<footer>/ {
            if (!in_footer) {
                while ((getline line < "footer-template.html") > 0) {
                    print line
                }
                close("footer-template.html")
                in_footer = 1
                replaced = 1
            }
        }
        /\/footer>/ {
            in_footer = 0
            next
        }
        !in_footer { print }
        END { exit !replaced }
    ' "$file" > "$file.tmp"

    if [ $? -eq 0 ]; then
        mv "$file.tmp" "$file"
        echo "Uspešno posodobljena noga v datoteki: $file"
    else
        rm -f "$file.tmp"
        echo "NAPAKA: Noga v datoteki '$file' ni bila zamenjana. Preverite značko '<footer>'."
    fi
done

echo "Posodabljanje zaključeno."
