#!/usr/bin/env bash
# Disable LXDE autostart of DyzineFrame (bench / headless fixture Pis).
# Field office topbox5 keeps autostart — do not run there unless intended.
#
# Usage:
#   bash disable_dyzineframe_autostart.sh
#   bash disable_dyzineframe_autostart.sh --enable   # restore autostart

if grep -q $'\r' "${BASH_SOURCE[0]}" 2>/dev/null; then
  sed -i 's/\r$//' "${BASH_SOURCE[0]}"
  exec bash "${BASH_SOURCE[0]}" "$@"
fi

set -euo pipefail

AUTOSTART="${HOME}/.config/autostart/topboxboot.desktop"
BACKUP="${HOME}/.config/autostart/topboxboot.desktop.disabled"
SRC="${HOME}/DyzineFrame/topboxboot.desktop"

if [[ "${1:-}" == "--enable" ]]; then
  mkdir -p "${HOME}/.config/autostart"
  if [[ -f "${BACKUP}" ]]; then
    mv -f "${BACKUP}" "${AUTOSTART}"
    echo "Restored ${AUTOSTART} from backup"
  elif [[ -f "${SRC}" ]]; then
    cp -f "${SRC}" "${AUTOSTART}"
    echo "Installed ${AUTOSTART} from ~/DyzineFrame/topboxboot.desktop"
  else
    echo "FATAL: no backup or ~/DyzineFrame/topboxboot.desktop" >&2
    exit 1
  fi
  exit 0
fi

if [[ -f "${AUTOSTART}" ]]; then
  mv -f "${AUTOSTART}" "${BACKUP}"
  echo "Disabled DyzineFrame autostart: ${AUTOSTART} -> ${BACKUP}"
elif [[ -f "${BACKUP}" ]]; then
  echo "OK: DyzineFrame autostart already disabled (${BACKUP})"
else
  echo "OK: no autostart entry (${AUTOSTART})"
fi
