#!/bin/sh
set -eu

root_dir=$(cd "$(dirname "$0")" && pwd)
. "$root_dir/../shared/build-lib.sh"

src_html="$root_dir/template.html"
output_dir="$root_dir/dist"
output_html="$output_dir/mdedit.html"

# Vendor files (bundled dependencies — no CDN required at runtime)
# Note: Tailwind is NOT a vendor file — it's replaced by css/tailwind-utils.css,
# a hand-written subset of only the utility classes used in template.html.
toastui_js="$root_dir/../shared/vendor/toastui-editor-all.min.js"
toastui_css="$root_dir/../shared/vendor/toastui-editor.min.css"

mkdir -p "$output_dir"
ensure_exists "$src_html"
ensure_exists "$toastui_js"
ensure_exists "$toastui_css"

css_temp=$(mktemp)
js_raw=$(mktemp)
js_temp=$(mktemp)
toastui_js_safe=$(mktemp)
cleanup() { rm -f "$css_temp" "$js_raw" "$js_temp" "$toastui_js_safe"; }
trap cleanup EXIT

# CSS files to concatenate in order
concat_files \
  "css/tailwind-utils.css" \
  "../shared/fonts.css" \
  "../shared/base.css" \
  "../shared/toast.css" \
  "../shared/nav.css" \
  "../shared/logo.css" \
  "css/base.css" \
  "css/editor.css" \
  "css/toc.css" \
  "css/markdown.css" \
  > "$css_temp"

# JavaScript files to concatenate in order
concat_files \
  "../shared/vendor/jszip.min.js" \
  "../shared/vendor/docx-preview.min.js" \
  "../shared/vendor/xlsx.full.min.js" \
  "../shared/vendor/utif.min.js" \
  "../shared/zddc.js" \
  "../shared/zddc-source.js" \
  "../shared/theme.js" \
  "../shared/toast.js" \
  "../shared/nav.js" \
  "../shared/logo.js" \
  "../shared/preview-lib.js" \
  "js/app.js" \
  "js/utils.js" \
  "js/front-matter.js" \
  "js/file-ops.js" \
  "js/file-system.js" \
  "js/file-tree.js" \
  "js/editor.js" \
  "js/toc.js" \
  "js/resizer.js" \
  "js/events.js" \
  "js/main.js" \
  "../shared/help.js" \
  > "$js_raw"

# Escape '</' in app JS and the Toast UI vendor JS so neither can prematurely
# close the inline <script> blocks they get embedded in.
escape_js_close_tags "$js_raw"     "$js_temp"
escape_js_close_tags "$toastui_js" "$toastui_js_safe"

compute_build_label "mdedit" "${1:-}" "${2:-}"

# Process template:
#   - Strip the Tailwind CDN <script> tag (css/tailwind-utils.css replaces it)
#   - Replace CDN <link> for Toast UI CSS with inline bundled CSS
#   - Replace CDN <script src="...toastui..."> with inline bundled Toast UI JS
#   - Inject custom CSS/JS at {{CSS_PLACEHOLDER}} and {{JS_PLACEHOLDER}}
#   - Substitute {{BUILD_LABEL}}
awk \
  -v css_file="$css_temp" \
  -v js_file="$js_temp" \
  -v toastui_js="$toastui_js_safe" \
  -v toastui_css="$toastui_css" \
  -v build_label="$build_label" \
  -v is_red="$is_red" \
  -v favicon_uri="$favicon_data_uri" \
  '
  /\{\{CSS_PLACEHOLDER\}\}/ {
    while ((getline line < css_file) > 0) print line
    close(css_file)
    next
  }
  /\{\{JS_PLACEHOLDER\}\}/ {
    while ((getline line < js_file) > 0) print line
    close(js_file)
    next
  }
  /\{\{BUILD_LABEL\}\}/ {
    if (is_red == "1") {
      gsub(/\{\{BUILD_LABEL\}\}/, "<span style=\"color:red;font-weight:bold\">" build_label "</span>")
    } else {
      gsub(/\{\{BUILD_LABEL\}\}/, build_label)
    }
    print
    next
  }
  /\{\{FAVICON\}\}/ {
    gsub(/\{\{FAVICON\}\}/, favicon_uri)
    print
    next
  }
  /<script src="https:\/\/cdn\.tailwindcss\.com"/ {
    # Stripped: Tailwind utility classes are in css/tailwind-utils.css instead
    next
  }
  /<link rel="stylesheet" href="https:\/\/uicdn\.toast\.com\/editor\/[^"]*\/toastui-editor\.min\.css"/ {
    # Inline the bundled Toast UI CSS
    print "<style>"
    while ((getline line < toastui_css) > 0) print line
    close(toastui_css)
    print "</style>"
    next
  }
  /<script src="https:\/\/uicdn\.toast\.com\/editor\/[^"]*\/toastui-editor/ {
    # Inline the bundled Toast UI JS (already passed through escape_js_close_tags
    # so its content cannot contain a literal </script> sequence). We close with
    # the real </script> because only that exact string terminates a script
    # block per the HTML5 spec.
    print "<script>"
    while ((getline line < toastui_js) > 0) print line
    close(toastui_js)
    print "</script>"
    next
  }
  { print }
' "$src_html" > "$output_html"

echo "Wrote $output_html ($(wc -c < "$output_html") bytes)"

if [ "$is_release" = "1" ]; then
    promote_release "mdedit"
fi
