#!/bin/bash
# Pruefen ob der Benutzer root ist
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
#
######################## enmserver Install ######################################
# Test-Installation aus den Git-Repository erstellen
# Achtung: nur http -> nicht für den Echtdatenbetieb geeignet ohne weitere Anpassungen!
#
# 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
#
#
##################### DATEN - INSTALLATION ######################################
APACHEPORT=7443
PHPVERSION=8.2
INSTALLPATH=/var/www/html
URL=https://github.com/SVWS-NRW/SVWS-Server
#################################################################################
#
#
### Apache2 und PHP installation
apt update && apt upgrade -y
apt install -y apache2 php php-fpm php-sqlite3
apt install -y curl zip unzip git dnsutils nmap net-tools nano mc ssh ca-certificates gnupg2 lsb-release apt-transport-https gpg
dpkg-reconfigure locales
a2enmod ssl
a2ensite default-ssl.conf
a2enmod proxy_fcgi setenvif
a2enconf php8.2-fpm
a2enmod rewrite
a2enmod headers
rm /root/.bashrc
wget -q --show-progress http://web.webolch.de/bauanleitung/downloads/linux/bash/.bashrc
wget -q --show-progress http://web.webolch.de/bauanleitung/downloads/linux/SH-Scripte/enmserver-install/crontab
wget -q --show-progress http://web.webolch.de/bauanleitung/downloads/linux/SH-Scripte/enmserver-install/enmserverUpdate.sh
wget -q --show-progress https://web.webolch.de/bauanleitung/downloads/linux/SH-Scripte/enmserver-install/update.log
mkdir -p /var/log/cron
mv update.log /var/log/cron/update.log
mv enmserverUpdate.sh /usr/local/sbin/update.sh
chmod -R 744 /usr/local/sbin/update.sh
mv crontab /etc/crontab
clear
echo
echo -e -n "${TUERKIS}OpenSSH wird fuer die root Benutzung konfiguriert. ${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
#
echo
echo -e -n "${HELLGRUEN}OK ${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/'$APACHEPORT'/g' /etc/apache2/sites-available/default-ssl.conf
sed -i 's/443/'$APACHEPORT'/g' /etc/apache2/ports.conf
#
systemctl reload apache2.service 
systemctl status apache2.service --no-pager
#
#
### optional:Java installieren und das SVWS-Server Projekt bauen
# 
#
#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 temurin-21-jdk -y
#
#cd ~
#git clone $URL
#cd SVWS-Server
#bash ./gradlew build
#
#cp ~/SVWS-Server/svws-webclient/enmserver/build/*.zip $INSTALLPATH/enmserver.zip
#
cd $INSTALLPATH
####### nächste Zeile auskommentieren, wenn optional gewählt wurde ein build erwünscht #######
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 "Installation beendet!"
