#!/bin/sh

root=$(pwd)

wget -q -N https://toastielab.dev/EllieBotDevs/ellie-bash-installer/raw/branch/v5/detectOS.sh
bash detectOS.sh > /dev/null || exit $?

# remove old backup but store the database
if [ -d elliebot_old/output/data/ ]; then
    if [ -f elliebot_old/output/data/EllieBot.db ]; then
        if [ ! -d elliebot_db_backups/ ]; then
            mkdir elliebot_db_backups
        fi
        date_now=$(date +%s)
        cp elliebot_old/output/data/EllieBot.db elliebot_db_backups/EllieBot-"$date_now".db

        if [ -f elliebot_old/output/creds.yml ]; then
            cp elliebot_old/output/creds.yml elliebot_db_backups/creds-"$date_now".yml
        fi
    fi
fi
rm -rf elliebot_old 1>/dev/null 2>&1

# make a new backup
mv -fT elliebot elliebot_old 1>/dev/null 2>&1

# clone new version
git clone -b v5 --recursive --depth 1 https://toastielab.dev/EllieBotDevs/elliebot

wget -q -N https://toastielab.dev/EllieBotDevs/ellie-bash-installer/raw/branch/v5/rebuild.sh
bash rebuild.sh

cd "$root"
rm "$root/e-download.sh"
exit 0