#!/bin/bash
#
# SVWS-Server Installation auf Debian 12 - hier bitte Daten individuell anpassen:
#
########################## IHRE DATEN ###################
INSTALLPATH=/var/www/html
PHPVERSION=8.2
Apache2Port=7443
URL=https://github.com/SVWS-NRW/SVWS-Server
#########################################################
#
##################### 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
#
# 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
#################################################################################################
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}Webnotenmanager (wenom) installieren ${NC}"
echo
sleep 3
#
sed -i "s|;extension=pdo_sqlite.*$|extension=pdo_sqlite|" /etc/php/${PHPVERSION}/apache2/php.ini
#
echo "
<Directory ${INSTALLPATH}>
   Options Indexes FollowSymLinks Includes ExecCGI
   AllowOverride All
   Require all granted
</Directory>" >> /etc/apache2/apache2.conf
#
### DocumentRoot anpassen
mkdir -p ${INSTALLPATH}/public
sed -i "s|DocumentRoot.*$|DocumentRoot ${INSTALLPATH}/public|" /etc/apache2/sites-available/000-default.conf
sed -i "s|DocumentRoot.*$|DocumentRoot ${INSTALLPATH}/public|" /etc/apache2/sites-available/default-ssl.conf
sed -i 's/443/'$Apache2Port'/g' /etc/apache2/sites-available/default-ssl.conf
sed -i 's/443/'$Apache2Port'/g' /etc/apache2/ports.conf
#
systemctl reload apache2.service
systemctl status apache2.service --no-pager
#
cd /root/SVWS-Server
mv svws-webclient/enmserver/build/*.zip $INSTALLPATH/enmserver.zip
cd $INSTALLPATH
unzip enmserver.zip
#####################################################################
##wget -q --show-progress https://wenom.svws-nrw.de/enmserver.zip ###
##unzip enmserver.zip                                             ###
#####################################################################
chmod -R 777 $INSTALLPATH
chown -R www-data:www-data $INSTALLPATH
curl --request GET --url http://localhost/api/setup --header "Content-Type: application/x-www-form-urlencoded"
cp /var/www/html/db/client.sec /var/www/html/public/secret.html
#
echo
echo -e -n "${HELLGRUEN}OK ${NC}"
echo
sleep 3
#
echo -e -n "${HELLGRUEN}Die Installation ist abgeschlossen und steht zum Aufruf unter ${GELB}https://${SERVERNAME}:8443 ${HELLGRUEN}bereit. ${NC}"
echo
echo -e -n "${HELLTURKIS}Die Installation ist abgeschlossen und der SchemaAdmin steht zum Aufruf unter ${GELB}https://${SERVERNAME}:8443/admin ${HELLGRUEN}bereit. ${NC}"
echo
