#!/bin/bash
# This script uninstalls Klipper

KLIPPER_NAME="$1"
KLIPPER_DIR="klipper"
ROOT_DIR="/opt/${KLIPPER_DIR}"
SOCKET_DIR="/var/lib/${KLIPPER_DIR}/sockets"
SRC_DIR="${ROOT_DIR}/${KLIPPER_NAME}"

# Step 1: Stop host software
stop_software() {
  printf "\nStopping Klipper host software..\n"
  /etc/init.d/klipper_${KLIPPER_NAME} stop
  update-rc.d klipper_${KLIPPER_NAME} disable
  update-rc.d klipper_${KLIPPER_NAME} remove
  systemctl daemon-reload
}

# Step 2: Remove Klipper folder
remove_klipper() {
  printf "\nRemoving Klipper...\n"
  rm -r ${SRC_DIR}
  printf "\nRemoving Klipper Socket...\n"
  rm -r ${SOCKET_DIR}/${KLIPPER_NAME}
}

# Step 3: Remove startup script
remove_script() {
  printf "\nremoving system start script...\n"
  rm /etc/init.d/klipper_${KLIPPER_NAME}
}

# Step 4: Report finished
finish_text() {
  printf "\n\n###### ${KLIPPER_NAME} removed! ######\n\n"
}

if [ "$#" -eq "0" ]; then
  echo 'Please start the script with a printer name!'
  exit 1
fi

stop_software
remove_klipper
remove_script
finish_text
