#!/bin/bash
#
# Script to uninstall BeyondCron and optionally deletes associated data and users
#
# usage: uninstall
#
# Copyright 2023 BeyondCron Pty Ltd and related entities / All rights reserved.

cd /tmp || exit 1

configError=0
commands=()

lowercase() {
  echo "$*" | tr '[:upper:]' '[:lower:]'
}

findCommand() {
  for command in "$@"
  do
    which $command 2> /dev/null && return
  done
  echo "$0: could not find any of the following commands: $*" >&2
  configError=1
}

trim() {
  echo $* | xargs
}

ask() {
  prompt=$1
  while :
  do
    read -p  "$prompt [Y/N/Q]: " askAnswer
    case $(lowercase "$askAnswer") in
      y|yes)
        eval "$2=y"
        break
        ;;
      n|no)
        eval "$2=n"
        break
        ;;
      q|quit) exit 0;;
    esac
  done
}

# check that user is root
if [ "$(id -u)" -ne 0 ]
then
  echo "$0: you must be root to run this command" >&2
  exit 1
fi

# confirm which packaging system is in use
packageCommand=$(findCommand dpkg rpm)
if [ $configError -ne 0 ]
then
  exit 1
fi

# remove BeyondCron package
case $packageCommand in
*dpkg)
  $packageCommand -s beyondcron > /dev/null 2>&1
  if [ $? -eq 0 ]
  then
    removePackageCommand="$packageCommand --purge beyondcron"
  fi
  ;;
*rpm)
  $packageCommand -qi beyondcron > /dev/null 2>&1
  if [ $? -eq 0 ]
  then
    removePackageCommand="$packageCommand --erase beyondcron"
  fi
  ;;
esac

if [ -n "$removePackageCommand" ]
then
  if [ -z "$removePackage" ]
  then
    ask "Would you like to remove the the package beyondcron" removePackage
  fi
  case $removePackage in
  y) commands+=("$removePackageCommand");;
  n) exit 0;;
  esac
fi

# remove data directories
for name in agent server web
do
  dir=/var/bc-$name
  if [ -d $dir ]
  then
    dataDirs="$dataDirs $dir"
  fi
done

dataDirs=$(trim $dataDirs)
if [ -n "$dataDirs" ]
then
  if [ -z "$removeData" ]
  then
    ask "Would you like to remove the BeyondCron data directories $dataDirs" removeData
  fi
  if [ "$removeData" = 'y' ]
  then
    commands+=("rm -rf $dataDirs")
  fi
fi

# remove log directories
logDirs='/var/log/bc-web'
if [ -d "$logDirs" ]
then
  if [ -z "$removeLogs" ]
  then
    ask "Would you like to remove the BeyondCron log directories $logDirs" removeLogs
  fi
  if [ "$removeLogs" = 'y' ]
  then
    commands+=("rm -rf $logDirs")
  fi
fi

# remove BeyondCron users
if [ -z "$dataDirs" ] || [ "$removeData" = 'y' ]
then
  for name in bc-daemon bc-web
  do
    if [ "$(grep -c $name /etc/passwd)" -eq 1 ]
    then
      users="$users $name"
    fi
  done

  users=$(trim $users)
  if [ -n "$users" ]
  then
    if [ -z "$removeUsers" ]
    then
      ask "Would you like to delete the BeyondCron users $users" removeUsers
    fi
    if [ "$removeUsers" = 'y' ]
    then
      for user in $users
      do
        commands+=("userdel -r $user")
      done
    fi
  fi
fi

# remove BeyondCron sudo configuration
if [ -z "$users" ] || [ "$removeUsers" = 'y' ]
then
  sudoFile=/etc/sudoers.d/90-beyondcron
  if [ -f $sudoFile ]
  then
    if [ -z "$removeSudoConfig" ]
    then
      ask "Would like to remove $sudoFile" removeSudoConfig
    fi
    if [ "$removeSudoConfig" = 'y' ]
    then
      commands+=("rm $sudoFile")
    fi
  fi
fi

if [ ${#commands[@]} -gt 0 ]
then
  echo
  echo "The following commands will be executed:"
  echo
  for command in "${commands[@]}"
  do
    echo "  $command"
  done
  if [ -z "$removeConfirm" ]
  then
    echo
    ask "Are you sure that you wish to continue" removeConfirm
  fi
  if [ "$removeConfirm" = 'y' ]
  then
    echo
    for command in "${commands[@]}"
    do
      $command
    done
    echo
  fi
fi

# end