#!/bin/bash

# Include scripts
scriptPath=$(dirname "${BASH_SOURCE[0]}")

. "$scriptPath/common.sh"

logFolder="${HOME}/installation"
logFile="${logFolder}/uninstall.log"

touch $logFile

# Confirm user if he wants to unistall
zenity --question --width=500 --height=350 \
--title "Are you sure" \
--text "Are you sure you want to unistall all kis packages ? (statistics and counters will be saved in /kis/backup)"

if [ $? != 0 ]; then
    exit 1
fi

( (

sudo /kis/app/scripts/stop-services.sh

echo "======================================================================"
echo "REMOVING PACKAGES ===================================================="
echo "======================================================================"
echo "# REMOVING PACKAGES"

dependencies_file="/kis/app/dependencies"

sudo rm -f $dependencies_file_old

# Remove all service packages with the correct version

line_number=$(wc -l $dependencies_file | awk '{ print $1 }')

step=$((100 / line_number))
echo "line number : ${line_number}, step : ${step}"

echo "# Starting uninstalling dependencies..."

( (
percent=0
end_of_file=0
packagesToInstall=""
while [[ $end_of_file == 0 ]]; do
  read -r package
  # the last exit status is the 
  # flag of the end of file
  end_of_file=$?

  name=$(echo "$package" | cut -d"=" -f1)

  if [ -z $name ]; then
    continue
  fi

  percent=$((percent+step))
  if [ $percent -gt 99 ]; then
    percent=99
  fi
  echo "# ${percent}% : Checking $package..."
  echo "${percent}"
  packagesToInstall="${packagesToInstall} ${name}"

done < "$dependencies_file"

if [[ !(-z "${packagesToInstall}") ]]; then
  echo "# Uninstalling ${packagesToInstall}..."
  command="sudo apt remove -y${packagesToInstall}"
  echo "Executing : ${command}"
  $command > $logFolder/uninstall_deps.log
fi

echo "# FINISHED"
echo "100"

) | tee $logFile ) | 
zenity --width=500 --height=350 --progress \
  --title="Uninstalling services..." \
  --text="" \
  --percentage=0 \
  --auto-close

if [ "$?" = -1 ] ; then
    show_error "Uninstallation canceled."
fi

echo "======================================================================"
echo "REMOVING SHORTCUTS ==================================================="
echo "======================================================================"
echo "# REMOVING SHORTCUTS"

rm -f ${HOME}/Desktop/kis-launcher.desktop
rm -f ${HOME}/Desktop/kis-supervisor.desktop

rm -f ${HOME}/Desktop/verifone-configurator.desktop
rm -f ${HOME}/Desktop/payment-tool.desktop
rm -f ${HOME}/Desktop/castle-configurator.desktop

rm -f ${HOME}/.config/autostart/kis-start.desktop

rm -f ${HOME}/Desktop/hardwareApp.desktop

rm -f ${HOME}/Desktop/maintenance-configurator.desktop

rm -f ${HOME}/Desktop/ticketZ.desktop

echo "======================================================================"
echo "Saving stats + counter in /kis/backup ====================================="
echo "======================================================================"
echo "# Saving stats + counters in /kis/statsbackup"

sudo mkdir /kis/backup
sudo cp /kis/static/statistics/* /kis/backup/
sudo cp /kis/static/configuration/services/payment/counter.txt /kis/backup/counter.txt

echo "======================================================================"
echo "REMOVING /KIS SOFT FOLDERS ==========================================="
echo "======================================================================"
echo "# REMOVING /KIS SOFT FOLDERS"

sudo rm -rf /kis/app
sudo rm -rf /kis/configuration
sudo rm -rf /kis/static
sudo rm -rf /kis/data

echo "======================================================================"
echo "END =================================================================="
echo "======================================================================"
) |& tee $logFile 2>&1) | zenity --progress --title "Uninstalling..." --pulsate --text "" --auto-close