#!/bin/bash

# -----------------------------------------------------------------------------
# Description:
# This script resets the web GUI banners and logos on the system. It performs 
# the following actions:
# - Verifies if the setup flag exists before proceeding.
# - Removes the partner logo symbolic link if it exists.
# - Restores the original web GUI banner if it was replaced by a partner banner.
# - Adjusts the display banner configuration if no custom banner is set.
# - Optionally deletes itself and an adjacent file named "activate_code_setup" 
#   when the "--delete" flag is passed.
#
# Usage:
# 1. Run the script for testing:
#    ./usr/local/emhttp/plugins/dynamix.my.servers/scripts/activation_code_remove
#    ./usr/local/emhttp/plugins/dynamix.my.servers/scripts/activation_code_remove --debug
#
# 2. Run the script with the delete flag to self-delete, should be done in the plugin's removal script:
#    ./usr/local/emhttp/plugins/dynamix.my.servers/scripts/activation_code_remove --delete
#
# 3. Run the script and remove the setup flag after completion for a fresh state:
#    ./usr/local/emhttp/plugins/dynamix.my.servers/scripts/activation_code_remove --debug --remove-setup-flag
#
# Prerequisites:
# - The setup flag file must exist at "/boot/config/activation/.done".
#
# Options:
# --delete   Deletes the script itself and related script & php files.
# --debug    Enables debug mode to display additional information.
# --remove-setup-flag  Removes the setup flag after the script completes.
#
# -----------------------------------------------------------------------------

# Default flags
DEBUG_MODE=false
DRY_RUN=false
REMOVE_SETUP_FLAG=false
# Check for flags
for arg in "$@"; do
  case $arg in
    --debug)
      DEBUG_MODE=true
      ;;
    --dry-run)
      DRY_RUN=true
      ;;
    --remove-setup-flag)
      REMOVE_SETUP_FLAG=true
      ;;
    *)
      # You can optionally handle other arguments if needed
      ;;
  esac
done

# Debug function
debug_echo() {
  if [[ $DEBUG_MODE == true ]]; then
    echo "[DEBUG] [activation_code_remove]: $1"
  fi
}

ACTIVATION_DIR="/boot/config/activation"
ACTIVATION_SETUP_FLAG="$ACTIVATION_DIR/.done"

debug_echo "Checking for setup flag at $ACTIVATION_SETUP_FLAG"

# only proceed if ACTIVATION_SETUP_FLAG exists
if [[ -f "$ACTIVATION_SETUP_FLAG" ]]; then
  debug_echo "Setup flag found, proceeding with removal"

  # Restore the original .set-password files modified for the welcome modal
  WELCOME_MODAL_INJECT_FILE="/usr/local/emhttp/plugins/dynamix/include/.set-password.php"
  WELCOME_MODAL_INJECT_BK_FILE="$WELCOME_MODAL_INJECT_FILE.bak"
  rm -f "$WELCOME_MODAL_INJECT_FILE"
  mv -f "$WELCOME_MODAL_INJECT_BK_FILE" "$WELCOME_MODAL_INJECT_FILE"

  debug_echo "Restored .set-password file"

  # Remove the partner logo symbolic link if it exists
  WEBGUI_IMAGES_DIR="/usr/local/emhttp/webGui/images"

  PARTNER_LOGO="$WEBGUI_IMAGES_DIR/partner-logo.svg"
  debug_echo "Checking for partner logo at $PARTNER_LOGO"
  if [[ -L "$PARTNER_LOGO" ]]; then
    rm -f "$PARTNER_LOGO"
    debug_echo "Partner logo symbolic link removed"
  fi

  # restores the original webgui banner if it was replaced by the partner banner
  WEBGUI_BANNER_OG="$WEBGUI_IMAGES_DIR/banner.png"
  WEBGUI_BANNER_BK="$WEBGUI_BANNER_OG-"

  debug_echo "Checking for original webgui banner backup at $WEBGUI_BANNER_BK"
  if [[ -f $WEBGUI_BANNER_BK ]]; then
    cp -f "$WEBGUI_BANNER_BK" "$WEBGUI_BANNER_OG"
    rm -f "$WEBGUI_BANNER_BK"
    debug_echo "Original webgui banner restored"

    # if there's not a custom banner, set display banner to no aka empty string
    # otherwise leave this display setting alone to keep the user's custom banner
    CUSTOM_BANNER="/boot/config/plugins/dynamix/banner.png"

    if [[ ! -f "$CUSTOM_BANNER" ]]; then
      debug_echo "No custom banner found, setting display banner to no"
      CONFIG_FILE="/boot/config/plugins/dynamix/dynamix.cfg"
      CONFIG_SECTION="display"
      CONFIG_KEY="banner"
      CONFIG_NEW_VALUE=""

      sed -i -E "/\[$CONFIG_SECTION\]/,/^\[/ s/^($CONFIG_KEY=).*/\1$CONFIG_NEW_VALUE/" "$CONFIG_FILE"
      debug_echo "Display banner configuration set to no"
    fi
  fi

  if [[ $REMOVE_SETUP_FLAG == true ]]; then
    debug_echo "Removing setup flag"
    rm -f "$ACTIVATION_SETUP_FLAG"
    debug_echo "Setup flag removed"
  fi

  if [[ $DRY_RUN == false ]]; then
    debug_echo "Deleting activation code related setup and php files"
    FILES_TO_DELETE=(
      "/usr/local/emhttp/plugins/dynamix.my.servers/include/activation-code-extractor.php"
      "/usr/local/emhttp/plugins/dynamix.my.servers/include/partner-logo.php"
      "/usr/local/emhttp/plugins/dynamix.my.servers/include/welcome-modal.php"
      "/usr/local/emhttp/plugins/dynamix.my.servers/scripts/activation_code_remove"
      "/usr/local/emhttp/plugins/dynamix.my.servers/scripts/activation_code_setup"
    )

    for file in "${FILES_TO_DELETE[@]}"; do
      rm -f "$file"
      debug_echo "Deleted $file"
    done
  fi

  debug_echo "Removal complete"
else
  debug_echo "Setup flag not found, doing nothing"
fi
