#!/bin/bash
docker_app_kill()
{
    echo 'killing savee process...';
    if kill "$(cat /app/savee/pid.info 2> /dev/null)" &> /dev/null;
    then
        rm /app/savee/pid.info;
        echo 'killing savee process successfull'; 
    else
        echo 'killing savee process failed'; 
        false;
    fi
}
docker_app_start()
{
    echo 'starting savee process...';
    if /app/savee/app/Savee 2> /dev/null & echo "$!" > /app/savee/pid.info 2> /dev/null;
    then
        echo 'starting savee process successfull';
    else
        echo 'starting savee process failed';
        false;
    fi
}
docker_stop()
{
    echo 'stoping savee docker container';
    if docker-compose -f /srv/docker/savee/savee-latest.yaml down &> /dev/null;
    then
        echo 'savee docker container stopped';
    else
        echo 'stoping of savee docker container failed';
    fi
}
docker_start()
{
    echo 'starting savee docker container';
    if docker-compose -f /srv/docker/savee/savee-latest.yaml down &> /dev/null;
    then
        echo 'savee docker container started';
    else
        echo 'starting of savee docker container failed';
    fi
}
docker_restart()
{
    docker_stop;
    docker_start;
}
ubuntu_savee_stop()
{
    echo 'stopping savee service...';
    if systemctl stop savee &> /dev/null; 
    then
        echo 'stopping savee service successfull';
    else
        echo 'stopping savee service failed';
    fi
}
ubuntu_savee_start()
{
    echo 'starting savee service...';
    if systemctl start savee &> /dev/null; 
    then
        echo 'starting savee service successfull';
    else
        echo 'starting savee service failed';
    fi
}
ubuntu_restart()
{
    echo 'starting restart savee service';
    if systemctl restart savee &> /dev/null; 
    then
        echo 'starting restart savee service successfull';
    else
        echo 'starting restart savee service failed';
    fi
}
update_app_files()
{
    echo 'deleting old app files...'; 
    if rm -R !app_folder!/* &> /dev/null; 
    then
        echo 'successfully done';
        echo 'copying new app files...'; 
        if cp -rf !update_folder!/app/. !app_folder! &> /dev/null; 
        then
            echo 'successfully done';
            echo 'rights at new app files...'; 
            if chmod 777 !app_folder!/Savee &> /dev/null; 
            then
                echo 'successfully done';
            else
                echo 'setting rights to new app files failed';
            fi
        else
            echo 'copying new app files failed';
        fi
    else
        echo 'deleting old app files failed'; 
    fi
    mkdir !app_folder!/wwwroot/data/plans &> /dev/null 
}
update_data_files()
{
    echo 'copying data folder data...'; 
    if cp -rf !update_folder!/data/. !data_folder! &> /dev/null;
    then
        echo 'successfully done';
    else
        echo 'copying new data files failed';
    fi
}
update_config_files()
{
    echo 'copying config folder data...'; 
    if cp -rf !update_folder!/config/. !config_folder! &> /dev/null;
    then
        echo 'successfully done';
    else
        echo 'copying new config files failed';
    fi
}
update_scripts_files()
{
    echo 'copying scripts folder data...'; 
    if cp -rf !update_folder!/scripts/. !scripts_folder! &> /dev/null;
    then
        echo 'successfully done';
    else
        echo 'copying new scripts files failed';
    fi
}
main()
{
    #!main_body!#
}
main;