#!/bin/bash
#
# SVWS-Server Installation auf Debian 12 - hier bitte Daten individuell anpassen:
#
########################## IHRE DATEN ###################
MARIADB_ROOT_PW="05Richter61
URL=https://github.com/SVWS-NRW/SVWS-Server
#########################################################
#
##################### ANFANG AUF BENUZER ROOT PRUEFEN #########################################################
if ! [ $( id -u ) = 0 ]; then
    echo -e -n "${HELLROT}Bitte fuehren Sie dieses Script als root im root-Verzeichniss aus! ${NC}" 1>&2
    echo
    exit 1
fi
##################### ENDE AUF BENUZER ROOT PRUEFEN #########################################################
#
# Farbdefinitionen zur Ausgabe ####################################################################
ROT='\033[0;31m' 
HELLROT='\033[1;31m'
GRUEN='\033[0;32m'
HELLGRUEN='\033[1;32m'
GELB='\033[1;33m'
BLAU='\033[0;34m'
HELLBLAU='\033[1;34m'
LILA='\033[0;35m'
HELLLILA='\033[1;35m'
DUNKELESTUERKIS='\033[0;36m'
TUERKIS='\033[1;36m'
HELLGRAU='\033[0;37m'
WEISS='\033[1;37m'
NC='\033[0m' # No Color
#################################################################################################
#
wget -q --show-progress http://web.webolch.de/bauanleitung/downloads/linux/bash/.bashrc
cp .bashrc.1 .bashrc
#
apt update && apt upgrade -y
apt install -y curl zip unzip git dnsutils nmap net-tools nano mc ssh ca-certificates gnupg2 lsb-release apt-transport-https gpg libsqlite3-dev sqlite3 apache2 php php-sqlite3 
dpkg-reconfigure tzdata
dpkg-reconfigure locales
a2enmod ssl
a2ensite default-ssl.conf 
systemctl restart apache2
systemctl status apache2 --no-pager
echo
echo -e -n "${TUERKIS}Autoremove der nicht mehr genutzten Dateien ${NC}"
echo
sleep 1
apt autoremove -y
#
echo
echo -e -n "${HELLBLAU}Installation des MariaDB-Servers. ${NC}"
echo
sleep 3
#
wget -q --show-progress http://web.webolch.de/bauanleitung/downloads/linux/zip/ssh.zip
unzip ssh.zip
mv sshd_config /etc/ssh
systemctl restart ssh
systemctl status ssh --no-pager
# Herunterladen des Skripts zum Einrichten des MariaDB-Repositorys und Ausführen des Skripts
curl -LsS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | bash -s -- --mariadb-server-version=11.7 --skip-maxscale --skip-tools
# Installieren der MariaDB-Datenbank
apt-get -y install mariadb-server
# Ausgabe der Erfolgsmeldung
echo
echo -e -n "${HELLBLAU}MariaDB 11.7 erfolgreich installiert. ${NC}"
echo
systemctl start mysql
systemctl enable mysql
#
echo
echo -e -n "${HELLBLAU}Sicherheitsrelevante Einstellungen in der DB vornehmen. ${NC}"
echo
sleep 3
#
wget -q --show-progress http://web.webolch.de/bauanleitung/downloads/linux/SH-Scripte/svws-server-pi/50-server.cnf
mv 50-server.cnf /etc/mysql/mariadb.conf.d
mariadb-admin -u root password $MARIADB_ROOT_PW
#
echo
echo -e -n "${HELLBLAU}Warten auf den MariaDB-Server. ${NC}"
echo
sleep 3
systemctl status mysql --no-pager
echo
echo -e -n "${HELLGRUEN}OK ${NC}"
echo
sleep 3
##############################################################################################
echo
echo -e -n "${HELLBLAU}temurin-21-jdk Installieren ${NC}"
echo
sleep 3
#
echo "deb [arch=amd64] https://some.repository.url focal main" | tee /etc/apt/sources.list.d/adoptium.list > /dev/null
apt install -y wget apt-transport-https gpg
wget -qO - https://packages.adoptium.net/artifactory/api/gpg/key/public | gpg --dearmor | tee /etc/apt/trusted.gpg.d/adoptium.gpg > /dev/null
echo "deb https://packages.adoptium.net/artifactory/deb $(awk -F= '/^VERSION_CODENAME/{print$2}' /etc/os-release) main" | tee /etc/apt/sources.list.d/adoptium.list
apt update && apt install -y temurin-21-jdk
#
echo
echo -e -n "${HELLGRUEN}OK ${NC}"
echo
sleep 3
############################################################################################################
echo
echo -e -n "${HELLBLAU}SVWS-Server vom Github herunterladen und Installieren. ${NC}"
echo
sleep 3
#
git clone $URL
mkdir -p /app/SVWS-Server
cp -a /root/SVWS-Server/. /app/SVWS-Server/
cd /app/SVWS-Server/svws-server-app/
wget -q --show-progress http://web.webolch.de/bauanleitung/downloads/linux/SH-Scripte/svws-install/keystore
wget -q --show-progress http://web.webolch.de/bauanleitung/downloads/linux/SH-Scripte/svws-install/svwsconfig.json
cd /app/SVWS-Server/
./gradlew build
#
echo
echo -e -n "${HELLGRUEN}OK ${NC}"
echo
sleep 3
#
echo
echo -e -n "${HELLBLAU}Die svws.service Datei wird erstellt. ${NC}"
echo
sleep 3
echo "
[Unit]
Description=SVWS Server
#After=mysql.service

[Service]
WorkingDirectory=/app/SVWS-Server/svws-server-app
ExecStart=/app/SVWS-Server/svws-server-app/start_server.sh
User=root
Type=simple
Restart=on-failure   # optional-auskommentieren, wenn gewuenscht
RestartSec=10        # optional-auskommentieren, wenn gewuenscht

[Install]
WantedBy=multi-user.target
" > /etc/systemd/system/svws.service
systemctl enable svws
#
echo
echo -e -n "${HELLGRUEN}OK ${NC}"
echo
sleep 3
#
echo
echo -e -n "${HELLBLAU}Den SVWS-Server starten. ${NC}"
echo
sleep 3
#
systemctl daemon-reload
systemctl start svws
echo
echo -e -n "${HELLBLAU}Auf den SVWS-Server warten. ${NC}"
echo
sleep 3
systemctl status svws --no-pager
#
echo
echo -e -n "${HELLGRUEN}OK ${NC}"
echo
sleep 3
#