#!/bin/bash

# functions
_info(){
    echo -e "\e[0;33m"
    echo ""
    echo $1
    echo ""
    echo -e "\e[0;0m"
}
#---------------------------------

# setting time zone
timedatectl set-timezone Europe/Prague
_info "Timezone set to Europe/Prague"

# docker install
_info "Docker installation started"

sudo apt-get remove docker docker-engine docker.io containerd runc
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg lsb-release -y
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io -y

_info "Docker installation finished"
#---------------------------------

#docker-compose install
_info "Docker-Compose installation started" 

sudo curl -L "https://github.com/docker/compose/releases/download/v2.8.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

_info "Docker-Compose installation finished"
#---------------------------------

#crontab install
_info "Crontab installation check"

sudo apt-get -y install cron

_info "Crontab installation check"
#---------------------------------

#pipe for executing commands from containers in host configuration
_info "Pipe installation started"

mkdir -p /srv/docker/pipe
mkfifo /srv/docker/pipe/dockerpipe
echo '#!/bin/bash' > /srv/docker/pipe/execpipe.sh
echo 'while true; do eval "$(cat /srv/docker/pipe/dockerpipe)"; done' >> /srv/docker/pipe/execpipe.sh
chmod 777 /srv/docker/pipe/execpipe.sh
( ( crontab -l || true ); echo '@reboot /srv/docker/pipe/execpipe.sh'; ) | crontab -
/srv/docker/pipe/execpipe.sh &

_info "Pipe installation finished and started"
#---------------------------------

#savee docker container configuration and start
_info "Docker Savee config started"

wget -c https://repo.nikatron.com/install/savee/docker/savee-latest.yaml -P /srv/docker/savee/
docker login https://docker.nikatron.com
docker-compose -f /srv/docker/savee/savee-latest.yaml pull
docker-compose -f /srv/docker/savee/savee-latest.yaml up -d

_info "Docker Savee config finished - Savee container started"
#---------------------------------