#!/bin/bash

# Define paths
INSTALL_PATH="/Applications"
APP_PATH="$INSTALL_PATH/AutoSubs.app"
RESOURCES_PATH="$APP_PATH/Contents/Resources/resources"
OLD_UTILITY_PATH="/Library/Application Support/Blackmagic Design/DaVinci Resolve/Fusion/Scripts/Utility"

# Remove legacy system-level Lua scripts if present
rm -f "$OLD_UTILITY_PATH/AutoSubs V2.lua"
rm -f "$OLD_UTILITY_PATH/AutoSubs.lua"

# pkg scripts run as root, so $HOME is the installer account's — resolve the
# console user's home and run user-scoped writes as them. With no logged-in
# console user (SSH/MDM/Setup Assistant installs) there is no user to target,
# so skip per-user files entirely; the app installs the Resolve scripts itself
# on first launch.
CONSOLE_USER="$(stat -f %Su /dev/console)"
if [ -z "$CONSOLE_USER" ] || [ "$CONSOLE_USER" = "root" ]; then
    echo "AutoSubs: no console user; skipping per-user Resolve and Adobe setup"
    exit 0
fi
USER_HOME="$(dscl . -read "/Users/$CONSOLE_USER" NFSHomeDirectory | awk '{print $2}')"
if [ ! -d "$USER_HOME" ]; then
    echo "AutoSubs: could not resolve home for $CONSOLE_USER; skipping per-user Resolve and Adobe setup"
    exit 0
fi
AS_USER=(sudo -u "$CONSOLE_USER")

SCRIPTS_DIR="$USER_HOME/Library/Application Support/Blackmagic Design/DaVinci Resolve/Fusion/Scripts"
UTILITY_PATH="$SCRIPTS_DIR/Utility"

# Write the Resolve bridge launcher now so it is in place before the app first
# runs. The app also rewrites it on startup (resolve_scripts.rs), which keeps
# it current across updates. `tee` runs as the console user so the app
# (running as them) can rewrite the file. The bundled copy has the Lua modules
# inlined, which Resolve 21.1.2 needs because it can't load files.
"${AS_USER[@]}" mkdir -p "$UTILITY_PATH"
sed -e "s|\[\[__AUTOSUBS_RESOURCES_FOLDER__\]\]|[[${RESOURCES_PATH}]]|g" \
    -e "s|\[\[__AUTOSUBS_APP_EXECUTABLE__\]\]|[[${APP_PATH}]]|g" \
    "$RESOURCES_PATH/AutoSubs.bundled.lua" | "${AS_USER[@]}" tee "$UTILITY_PATH/AutoSubs.lua" > /dev/null

# 3.10.0 installed a startup scriptlib that ran the bridge via fusion:Execute,
# which blocks Fusion's script executor for the whole Resolve session.
rm -f "$SCRIPTS_DIR/AutoSubs.scriptlib"

# Remove legacy user-level Lua scripts if present
rm -f "$UTILITY_PATH/AutoSubs V2.lua"

# Open from Resolve (resolve_launcher.rs): a login item that opens the app when
# the Resolve script runs. On by default; the app writes the marker below when
# the user turns it off. Same plist the app writes, so it doesn't rewrite it.
# bootstrap starts it now (RunAtLoad); bootout first replaces a loaded copy.
LAUNCHER_DISABLED="$USER_HOME/Library/Application Support/com.autosubs/resolve-launcher-disabled"
AGENT_LABEL="com.autosubs.resolve-launcher"
AGENT_PATH="$USER_HOME/Library/LaunchAgents/$AGENT_LABEL.plist"
if [ ! -e "$LAUNCHER_DISABLED" ]; then
    CONSOLE_UID="$(id -u "$CONSOLE_USER")"
    "${AS_USER[@]}" mkdir -p "$USER_HOME/Library/LaunchAgents"
    "${AS_USER[@]}" tee "$AGENT_PATH" > /dev/null <<PLIST
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>$AGENT_LABEL</string>
    <key>AssociatedBundleIdentifiers</key>
    <array>
        <string>com.autosubs</string>
    </array>
    <key>ProgramArguments</key>
    <array>
        <string>$APP_PATH/Contents/MacOS/autosubs</string>
        <string>--resolve-watcher</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>LimitLoadToSessionType</key>
    <string>Aqua</string>
</dict>
</plist>
PLIST
    launchctl bootout "gui/$CONSOLE_UID/$AGENT_LABEL" 2>/dev/null || true
    launchctl bootstrap "gui/$CONSOLE_UID" "$AGENT_PATH" 2>/dev/null || true
fi

# Adobe Extension Installation
PREMIERE_PATH="$USER_HOME/Library/Application Support/Adobe/CEP/extensions"
"${AS_USER[@]}" mkdir -p "$PREMIERE_PATH"
"${AS_USER[@]}" cp -r "$RESOURCES_PATH/com.autosubs.adobe" "$PREMIERE_PATH"

# Enable PlayerDebugMode for Adobe CEP extensions (CSXS 6-14)
# CSXS.13/14 are required for Premiere Pro 2026 (v26); older keys cover legacy versions.
"${AS_USER[@]}" defaults write com.adobe.CSXS.6 PlayerDebugMode 1
"${AS_USER[@]}" defaults write com.adobe.CSXS.7 PlayerDebugMode 1
"${AS_USER[@]}" defaults write com.adobe.CSXS.8 PlayerDebugMode 1
"${AS_USER[@]}" defaults write com.adobe.CSXS.9 PlayerDebugMode 1
"${AS_USER[@]}" defaults write com.adobe.CSXS.10 PlayerDebugMode 1
"${AS_USER[@]}" defaults write com.adobe.CSXS.11 PlayerDebugMode 1
"${AS_USER[@]}" defaults write com.adobe.CSXS.12 PlayerDebugMode 1
"${AS_USER[@]}" defaults write com.adobe.CSXS.13 PlayerDebugMode 1
"${AS_USER[@]}" defaults write com.adobe.CSXS.14 PlayerDebugMode 1

exit 0
