#!/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.
"${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.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"

# 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
