#! /bin/sh -e

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

# Helpers
## Function to log messages
log_helper() {
    LOG_FILE=/home/ubuntu/miniprintserver-update.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 \
    apt-get -fuy -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" \
    $APT_CMD
}

## Function to cancel current update
cancel_update() {
    msg="Canceling Update $1"
    log_helper "$msg ... "
    rm -rf source && rm -rf deployment
    rm -rf $1*
    mv source.back source && mv deployment.back deployment
    sudo systemctl stop miniprintserver
    sudo systemctl start miniprintserver
    sleep 10
    sudo systemctl is-active --quiet miniprintserver
    if [ "$?" -ne 0 ]
    then
        log_helper "$msg failed, exiting."
    fi
    log_helper "$msg ... done"
}

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

# Changing to miniprintserver directory
msg="Changing to miniprintserver directory"
cd /home/ubuntu/miniprintserver
log_helper "$msg ... "

# 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."

# Checking if the server is up to date
msg="Checking if the server is up to date"
log_helper "$msg ... "
if [ -f "$app_last_version.zip" ]
then
    log_helper "The server is up to date. Current version is $app_last_version. Exiting."
    exit
fi
log_helper "The server needs to be updated to $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 "$app_last_version.zip" -d $app_last_version
if [ "$?" -ne 0 ]
then
    log_helper "$msg failed, exiting."
    exit
fi
log_helper "$msg ... done"

# Changing owner to ubuntu
msg="Changing owner to ubuntu"
log_helper "$msg ... "
chown -R ubuntu:ubuntu $app_last_version
if [ "$?" -ne 0 ]
then
    log_helper "$msg failed, exiting."
    exit
fi
log_helper "$msg ... done"

# Backing up current links
msg="Backing up current links"
log_helper "$msg ... "
mv deployment deployment.back && mv source source.back
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 ... "
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"

# Copying configuration files
msg="Copying configuration files"
log_helper "$msg ... "
cp source.back/instance/config.py source/instance/ && chown ubuntu:ubuntu source/instance/config.py
if [ "$?" -ne 0 ]
then
    log_helper "$msg failed, exiting."
    exit
fi
log_helper "$msg ... done"

# Restarting all miniprintserver services
msg="Restarting all miniprintserver services"
log_helper "$msg ... "
sudo systemctl restart miniprintserver
if [ "$?" -ne 0 ]
then
    log_helper "$msg failed, exiting."
    cancel_update $app_last_version
    exit
fi
log_helper "$msg ... done"

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

# Update finished successfully
log_helper "Update finished successfully!"
