#!/bin/sh
set -e

# Inject custom CSS into app.css if it exists
if [ -f /opt/app/lib/asciinema-1.0.0/priv/static/assets/custom.css ]; then
  echo "Injecting custom CSS into app.css..."
  cat /opt/app/lib/asciinema-1.0.0/priv/static/assets/custom.css >> /opt/app/lib/asciinema-1.0.0/priv/static/assets/app.css
  # Also append to the hashed version
  CSS_FILE=$(ls /opt/app/lib/asciinema-1.0.0/priv/static/assets/app-*.css 2>/dev/null | head -1)
  if [ -n "$CSS_FILE" ]; then
    cat /opt/app/lib/asciinema-1.0.0/priv/static/assets/custom.css >> "$CSS_FILE"
    echo "Custom CSS injected successfully"

    # Remove pre-compressed .gz files so updated CSS is served
    rm -f /opt/app/lib/asciinema-1.0.0/priv/static/assets/app.css.gz
    rm -f /opt/app/lib/asciinema-1.0.0/priv/static/assets/app-*.css.gz
    echo "Removed pre-compressed CSS files to force serving updated version"
  fi
fi

# Execute the original entrypoint
exec /sbin/tini -- /opt/app/bin/server "$@"
