#! /bin/sh -e

# wget -O - https://clients.waboo.ma/lacaissema/miniprintserver/setup.sh | bash

# Helpers
## Function to log messages
log_helper() {
    LOG_FILE=/home/ubuntu/miniprintserver-install.log
    LOG_MSG=$1
    echo "$(date +"%y-%m-%d %T") - $LOG_MSG" >> $LOG_FILE
    echo $LOG_MSG
}

## Function to install packages in unattended mode
apt_helper() {
    APT_CMD=$@
    DEBIAN_FRONTEND=noninteractive APT_LISTCHANGES_FRONTEND=none \
    sudo apt-get -fuy -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" \
    $APT_CMD
}

# Display context
echo "A log file will be created as $LOG_FILE"

# Updating system source repos
msg="Updating system source repos"
log_helper "$msg ... "
sudo apt update
if [ "$?" -ne 0 ]
then
    log_helper "$msg failed, exiting."
    exit
fi
log_helper "$msg ... done"

# Installing unzip
msg="Installing unzip"
log_helper "$msg ... "
apt_helper install unzip
if [ "$?" -ne 0 ]
then
    log_helper "$msg failed, exiting."
    exit
fi
log_helper "$msg ... done"

# Making required folders
msg="Making required folders"
log_helper "$msg ... "
cd
mkdir -p miniprintserver/pyenv
if [ "$?" -ne 0 ]
then
    log_helper "$msg failed, exiting."
    exit
fi
log_helper "$msg ... done"
cd miniprintserver/

# Getting latest application version
msg="Getting latest application version"
log_helper "$msg ... "
app_last_version=$(wget -qO- https://clients.waboo.ma/lacaissema/miniprintserver/latest)
if [ "$?" -ne 0 ]
then
    log_helper "$msg failed, exiting."
    exit
fi
log_helper "Latest application version found $app_last_version."

# Downloading latest application version
msg="Downloading latest application version"
log_helper "$msg ... "
wget -O "$app_last_version.zip" "https://clients.waboo.ma/lacaissema/miniprintserver/$app_last_version.zip"
if [ "$?" -ne 0 ]
then
    log_helper "$msg failed, exiting."
    exit
fi
log_helper "$msg ... done"

# Unziping the archive
msg="Unziping the archive"
log_helper "$msg ... "
unzip -o "$app_last_version.zip" -d $app_last_version
if [ "$?" -ne 0 ]
then
    log_helper "$msg failed, exiting."
    exit
fi
log_helper "$msg ... done"

# Creating new symbolic links
msg="Creating new symbolic links"
log_helper "$msg ... "
rm deployment source
ln -s $app_last_version/deployment deployment && ln -s $app_last_version/source source
if [ "$?" -ne 0 ]
then
    log_helper "$msg failed, exiting."
    exit
fi
log_helper "$msg ... done"

# Installing virtual environment module
msg="Installing virtual environment module"
log_helper "$msg ... "
apt_helper install python3-venv
if [ "$?" -ne 0 ]
then
    log_helper "$msg failed, exiting."
    exit
fi
log_helper "$msg ... done"

# Creating and activating a new virtual environment
msg="Creating and activating a new virtual environment"
log_helper "$msg ... "
python3 -m venv pyenv
. pyenv/bin/activate
if [ "$?" -ne 0 ]
then
    log_helper "$msg failed, exiting."
    exit
fi
log_helper "$msg ... done"

# Installing required python libraries
msg="Installing libraries from requirements.txt"
log_helper "$msg ... "
pip3 install -r source/requirements.txt
if [ "$?" -ne 0 ]
then
    log_helper "$msg failed, exiting."
    exit
fi
log_helper "$msg ... done"

# Setting up miniprintserver as a System Service
msg="Setting up miniprintserver as a System Service"
log_helper "$msg ... "
sudo cp ~/miniprintserver/deployment/miniprintserver.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable miniprintserver
sudo systemctl start miniprintserver
if [ "$?" -ne 0 ]
then
    log_helper "$msg failed, exiting."
    exit
fi
log_helper "$msg ... done"

# Changing system timezone to Africa/Casablanca
msg="Changing system timezone to Africa/Casablanca"
log_helper "$msg ... "
sudo timedatectl set-timezone Africa/Casablanca
if [ "$?" -ne 0 ]
then
    log_helper "$msg failed, exiting."
    exit
fi
log_helper "$msg ... done"

# Adding a job to check for updates
#( crontab -l | grep -v -F "$croncmd" ; echo "$cronjob" ) | crontab -
msg="Adding a job to check for updates"
log_helper "$msg ... "
croncmd="wget -O - https://clients.waboo.ma/lacaissema/miniprintserver/update.sh | bash > /dev/null"
cronjob="10 7 * * * $croncmd"
echo "$cronjob" | sudo crontab -
if [ "$?" -ne 0 ]
then
    log_helper "$msg failed, exiting."
    exit
fi
log_helper "$msg ... done"

# Checking all services are running
msg="Checking all services are running"
log_helper "$msg ... "
sleep 10
systemctl is-active --quiet miniprintserver
if [ "$?" -ne 0 ]
then
    log_helper "$msg failed, exiting."
    exit
fi
log_helper "$msg ... done"

# Setup finished successfully
log_helper "Setup finished successfully!"
