#!/bin/bash

BOLD='\033[1m'
ENDC='\033[0m'
RED='\033[0;31m'
BOLD_RED='\033[1;31m'

echo -e "hbkcd Install Script\n"

if [[ $EUID -ne 0 ]]; then
    echo -e "${BOLD_RED}ERROR:${ENDC} This script must be run as root" 
    exit 1
fi

# Package and python module dependencies
declare -a pkg_deps=("python3-setuptools" "python3-pip" "libmagic-dev" "pbzip2")
declare -A pip_deps=( [python-magic]=magic )

# Check each package dependency
echo "Checking package dependencies"
to_install=""
for package in "${pkg_deps[@]}"
do
    printf "    $package..."
    dpkg -l | grep "$package" > /dev/null
    if [ "$?" -ne 0 ]; then
        printf "${BOLD_RED}NOT INSTALLED${ENDC}\n"
        if [[ -z "$to_install" ]]; then
            to_install="$package"
        else        
            to_install="$to_install $package"
        fi
    else
        printf "${BOLD}INSTALLED${ENDC}\n"
    fi
done
printf "\n"

# Attempt to install any missing packages
if [[ -z "${to_install// }" ]]; then
    printf "All required packages are installed.\n"
else
    printf "Attempting to install missing packages: $to_install\n\n"
    sudo apt -y install "$to_install"
    if [[ "$?" -eq 0 ]]; then
        printf "\n\nPackage dependencies installed successfully.\n"
    else
        printf "\n\n${BOLD_RED}ERROR:${ENDC} unable to install package dependencies\n\n"
        exit -1
    fi 
fi
printf "\n\n"

# Check each Python module dependency
echo "Checking Python module dependencies"
to_install=""
for module in "${!pip_deps[@]}"
do
    printf "    $module..."
    python3 -c "import ${pip_deps[$module]}" 2> /dev/null
    if [ "$?" -ne 0 ]; then
        printf "${BOLD_RED}NOT INSTALLED${ENDC}\n"
        if [[ -z "$to_install" ]]; then
            to_install="$module"
        else        
            to_install="$to_install $module"
        fi
    else
        printf "${BOLD}INSTALLED${ENDC}\n"
    fi
done
printf "\n"

# Attempt to install any missing Python modules
if [[ -z "${to_install// }" ]]; then
    printf "All required Python modules are installed.\n"
else
    printf "Attempting to install missing Python modules: $to_install\n\n"
    sudo pip3 install "$to_install"
    if [[ "$?" -eq 0 ]]; then
        printf "\n\nPython module dependencies installed successfully.\n"
    else
        printf "\n\n${BOLD_RED}ERROR:${ENDC} unable to install Python module dependencies\n\n"
        exit -1
    fi 
fi
printf "\n\n"

# Install systemd config
printf "Installing hbkcd systemd service..."
cp conf/systemd/hbkcd.service /etc/systemd/system/
systemctl daemon-reload
printf "DONE\n"
printf "\n\n"

# Install hbkcd Python module
# TODO: need to figure out a clean way to detect if this fails and halt
./setup.py install
printf "\n\n"

# Install scripts and logrotate config
printf "Installing hbkcd scripts..."
install -m 755 scripts/hbkcd_add.sh /usr/local/bin/hbkcd_add
install -m 755 scripts/hbkcd_queue.sh /usr/local/bin/hbkcd_queue
install -m 755 scripts/hbkcd_remove.sh /usr/local/bin/hbkcd_remove
install -m 755 scripts/hbkcd_status.sh /usr/local/bin/hbkcd_status
printf "DONE\n"
printf "Installing logrotate config..."
install -m 644 -o root -g root conf/logrotate/hbkcd /etc/logrotate.d/hbkcd
printf "DONE\n"
printf "\n\n"

# Start or restart hbckd 
systemctl status hbkcd
result="$?"
if [ "$result" -ne 0 ]; then
    echo "Starting hbkcd"
    systemctl start hbkcd
else
    echo "Restarting hbkcd"
    systemctl restart hbkcd
fi

printf "\n${BOLD}[complete]${ENDC}\n\n"
