#!/bin/bash

#
# Last update 26 Jan 2021
#

if [[ "$EUID" -ne 0 ]]; then
    echo "Please run as root or sudo"
    exit 13 || return 13 # EACCES
fi

if [[ "$PWD" == "/opt/domtime" ]]; then
    echo "Please run /opt/domtime/remove.sh from a folder outside /opt/domtime"
    exit 13 || return 13 # EACCES
fi

echo # blank line 
echo "This script will remove dtlinux from your system"
echo # blank line

# prompt for confirmation

read -p "Are you sure? Press Y to continue: " -n 1 -r
echo # blank line

if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo "Removal cancelled"
    exit 125 || return 125 # ECANCELED
fi

if [ -f /opt/domtime/etc/dtlinux.license ]; then
    echo # blank line
    echo "NOTICE:"
    echo # blank line
    echo "  This is a licensed copy of dtlinux. If you proceed with removal, and then"
    echo "  want to reinstall, you will have to obtain and apply a new license key."
    echo # blank line
    read -p "Are you sure? Press Y to continue: " -n 1 -r
    echo # blank line
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        echo "Removal cancelled"
        exit 125 || return 125 # ECANCELED
    fi
fi

echo # blank line
echo "Removing dtlinux..."

#
# Read location of log files for display at the end
#

LOGPATH=""
DRIFTPATH=""

if [ -f /etc/opt/domtime/dtlinux.conf ]; then
    LOGPATH=`awk '{ if($1 == "log:logPath") print $3}'     /etc/opt/domtime/dtlinux.conf`
    DRIFTPATH=`awk '{ if($1 == "log:driftPath") print $3}' /etc/opt/domtime/dtlinux.conf`
fi

if [[ $LOGPATH == $DRIFTPATH ]]; then
    DRIFTPATH=""
fi

#
# Figure out where systemd/system lives
#

SYSTEMDPATH="/usr/lib/systemd/system/"

if [[ ! -d $SYSTEMDPATH ]]; then
    SYSTEMDPATH="/lib/systemd/system"

    if [[ ! -d $SYSTEMDPATH ]]; then
        echo "Could not find either /usr/lib/systemd/system or /lib/systemd/system"
        echo "Verify this is a systemd distro"
        exit 2 || return 2 # ENOENT
    fi
fi

#
# Kill dtcheck if it happens to be running
#

DTCHECK_PID=$(pidof dtcheck)

if [[ ! -z $DTCHECK_PID ]]; then
    echo "    Stopping dtcheck"
    /bin/kill $DTCHECK_PID
fi

#
# Figure out if dtlinux was installed from a TGZ, RPM, or DEB
#

RPM=0
DEB=0
RET=0

rpm -q dtlinux >/dev/null 2>&1

if [ 0 -eq $? ]; then
    RPM=1

elif [ -f /etc/debian_version ]; then
    apt-cache show dtlinux >/dev/null 2>&1
    if [ 0 -eq $? ]; then
        DEB=1
    fi
fi

if [ $DEB -eq 1 ]; then

    # A debian package is installed
    echo "    DEB detected. Using apt to remove it"
    echo Y | apt-get remove --purge dtlinux >/dev/null 2>&1
    RET=$?
    if [ 0 -ne $RET ]; then
        echo "Something went wrong."
        echo "apt-get remove dtlinux failed with error code $RET"
        exit $RET || return $RET
    fi

elif [ $RPM -eq 1 ]; then

    # An RPM package is installed
    echo "    RPM detected. Using rpm to remove it"
    rpm -e dtlinux >/dev/null 2>&1
    RET=$?
    if [ 0 -ne $RET ]; then
        echo "Something went wrong."
        echo "rpm -e dtlinux failed with error code $RET"
        exit $RET || return $RET
    fi

else
    # Neither DEB nor RPM

    if systemctl -q is-active dtlinux.service; then
        echo "    Stopping dtlinux service"
        systemctl -q stop dtlinux.service
        systemctl -q disable dtlinux.service
    fi

    #
    # Remove service unit file
    #

    if [[ -f $SYSTEMDPATH/dtlinux.service ]]; then
        echo "    Removing service unit file"
        rm -f $SYSTEMDPATH/dtlinux.service
        systemctl -q daemon-reload
    fi
fi

#
# Remove folders and files that remain
#

if [ -d /opt/domtime ]; then
    echo "    Removing /opt/domtime"
    rm -rf /opt/domtime # /opt/domtime and all its files and folders
fi

if [ -d /etc/opt/domtime ]; then
    echo "    Removing /etc/opt/domtime"
    rm -rf /etc/opt/domtime # /etc/opt/domtime and all its files and folders
fi

if [ -d /var/opt/domtime ]; then
    echo "    Removing /var/opt/domtime"
    rm -f /var/opt/domtime/* # /var/opt/domtime, but not subfolders with content
fi

if [ -d /var/run/domtime ]; then
    echo "    Removing /var/run/domtime"
    rm -rf /var/run/domtime # folder for PID lock file
fi

#
# Remove man pages
#

if [ -f /usr/share/man/man8/dtlinux.8.gz ]; then
    echo "    Removing dtlinux man page"
    rm -f /usr/share/man/man8/dtlinux.8.gz
fi

if [ -f /usr/share/man/man3/dtcheck.3.gz ]; then
    echo "    Removing dtcheck man page"
    rm -f /usr/share/man/man3/dtcheck.3.gz
fi

#
# Remove soft links in /usr/bin to dtcheck and dtlinux
#

if [ -e /usr/bin/dtcheck ]; then
    echo "    Removing soft link to dtcheck in /usr/bin"
    rm -f /usr/bin/dtcheck
fi

if [ -e /usr/bin/dtlinux ]; then
    echo "    Removing soft link to dtlinux in /usr/bin"
    rm -f /usr/bin/dtlinux
fi

if [ -e /usr/bin/dtlinux-update ]; then
    echo "    Removing softlink to update.sh in /usr/bin/"
fi

echo "dtlinux removed"
echo # blank line

if [[ ! -z $LOGPATH && -d $LOGPATH ]]; then
    echo "    You may want to delete log files from $LOGPATH"
fi

if [[ ! -z $DRIFTPATH && -d $DRIFTPATH ]]; then
    echo "    You may want to delete drift files from $DRIFTPATH"
fi

if [[ -d /var/opt/domtime/backup ]]; then
    echo "    You may want to remove /var/opt/domtime/backup"
fi

echo # blank line
echo "Don't forget to re-enable your former time service"

exit 0 || return 0 # exit with success code

#
# End of script
#
