#!/bin/bash

# This script written by Shane Daley M0VUB. The script gracefully shutdown services while services are updated.
# We can also add items in this script for future use like updates or further log trims.
# Run updater to execute a full server update (backend)
# This script will DESTROY all docker images. It is run with SYSTEM X only!

#   Copyright (C) 2020 Shane P, Daley  M0VUB <support@gb7nr.co.uk>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.

# This script is for FreeDMR / SYSTEM X Server only which has been installed from docker_install.sh. It is NOT to be used for DOCKER-COMPOSE.
# NOT TO BE USED FOR DOCKER-COMPOSE INSTALL! PLEASE USE ALTERNATIVE UPDATE SCRIPT FOR FreeDMR / SYSTEM-X THAT HAS BEEN INSTALLED WITH DOCKER-COMPOSE.

echo Starting update.....
sleep 3
echo .
sleep 1
echo ..
sleep 1
echo ...
sleep 1
echo .....
echo Stopping FreeDMR server.....
docker container stop freedmr &&
echo Removing all docker images.....
docker rmi $(docker images -q) --force &&
sleep 10
echo Pulling latest images from HackNix.....
sleep 2
figlet "HackNix." &&
docker pull hacknix/freedmr:development-latest &&
echo Removing old containers.....
docker container rm freedmr --force &&
echo Installing new containers.....
docker run --name=freedmr -d --read-only -v /etc/freedmr/freedmr.cfg:/opt/freedmr/freedmr.cfg \
-v /var/log/freedmr/freedmr.log:/opt/freedmr/freedmr.log \
-v /etc/freedmr/rules.py:/opt/freedmr/rules.py -p 62031:62031/udp -p 62034-62046:62034-62046/udp \
-p 4321:4321/tcp hacknix/freedmr:development-latest &&
echo Flushing NAT tracking table .....
conntrack -F &&
sleep 5
echo Flushing services.....
./freedmrflush.sh &&
clear &&
echo Starting FreeDMR Server.....
sleep 3
echo .
sleep 1
echo ..
sleep 1
echo ...
sleep 1
figlet "FreeDMR." &&
sleep 2
echo Checking container is running .....
sleep 2
docker ps &&
sleep 2
echo Checking startup error logs.....
sleep 2
docker container logs freedmr &&
echo Done.
sleep 3
echo Exiting updater....
sleep 2
echo .
sleep 1
echo ..
sleep 1
echo ...
sleep 1
echo ....
sleep 1
echo FreeDMR Server is now online. Update is complete and you may now exit. AKA ShaYmez.
#
# This script has been developed by the one and only ShaYmez. Visit https://repo.radio/ShaYmez for more super scripts!
