#!/bin/bash

# Define the paths
CURRENT_DIR="$(dirname "$0")"

CONTROL_SURFACE_BUNDLE_NAME="LogiPlugin.bundle"
CONTROL_SURFACE_DEST_DIR="/Library/Application Support/Adobe/Common/Plug-ins/ControlSurface"
CONTROL_SURFACE_DEST_FILE="$CONTROL_SURFACE_DEST_DIR/$CONTROL_SURFACE_BUNDLE_NAME"

CEP_PLUGIN_NAME="com.loupedeck.pp.cep"
CEP_DEST_DIR="$HOME/Library/Application Support/Adobe/CEP/extensions/$CEP_PLUGIN_NAME"

source "$CURRENT_DIR/references_counter.sh"

# Uninstall the bundle if it exists and reference count is 0
decrement_control_surface_counter

if control_surface_references_count; then
    if [ -e "$CONTROL_SURFACE_DEST_FILE" ]; then
        echo "Bundle $CONTROL_SURFACE_DEST_FILE exists. Uninstall."
        rm -rf "$CONTROL_SURFACE_DEST_FILE"
    fi
fi

# Uninstall the plugin if it exists
if [ -e "$CEP_DEST_DIR" ]; then
    echo "Removing $CEP_DEST_DIR"
    rm -rf "$CEP_DEST_DIR"
fi
