#!/bin/bash
##################################
# Installation eine SVWS-Servers #
# mit Apache2                    #
# und Samba mit Schild3          #
# Erstellt am 6.8.2024 von TR    #
##################################
### Farbdefinitionen ####
#   TEXT in Weiss       #
#   DATEIEN in Gelb     #
#   ORDNER in HellBlau  #
#########################
# 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
#
## [Variablen]
##
#
VERSIONSNR=0.9.0	# Download dieser Version des SVWS-Servers
SCHILD_VERSIONSNR=3.0.89	# Download dieser Version der zum SVWS-Servers passenden (bitte pruefen) SCHILD3 Oberflaeche
ROOTPWD=05Richter61
SNR=101010
FQDN=svws
#
##
## [Softwaredownload]
##

#
echo
echo -e -n "${WEISS}Update und Upgrede des Debian ${NC}"
echo
sleep 2
apt-get update && apt upgrade -y
echo
echo -e -n "${WEISS}Autoremove der nich mehr Benoetigten Dateien ${NC}"
echo
sleep 2
apt-get autoremove -y
# Jenach Anwendung oder Bedarf benoetigt man nicht alle Softwarepakete.
echo
echo -e -n "${WEISS}Installation der folgenden Pakete ${GELB}net-tools dnsutils nmap samba ufw zip git ssh apache2 mc ${NC}"
echo
sleep 2
apt-get install net-tools dnsutils nmap samba ufw zip git ssh apache2 mc -y
# Download SVWS-Server
wget https://github.com/SVWS-NRW/SVWS-Server/releases/download/v$VERSIONSNR/install-$VERSIONSNR.sh
# Wahrscheinlich verhindert ein Bug den Dowload -- daher eun Workaround:
wget https://github.com/SVWS-NRW/SVWS-Server/releases/download/v$VERSIONSNR/linux-installer-$VERSIONSNR.tar.gz
#
##
## [SVWS Konfiguration]
##
# Ggf koennen hier noch Anpassungen fuer den Betrieb des SVWS-Severs gemacht werden. Zum Beispiel zur Migration der Datenbank, verwendetem Keystore etc.
tee -a /root/.env  <<EOF
CREATE_MariaDB=j
CREATE_KEYSTORE=j
CREATE_TESTDATA=j
MariaDB_ROOT_PASSWORD=$ROOTPWD
MariaDB_DATABASE=svwsdb
MariaDB_HOST=localhost
MariaDB_USER=svwsadmin
MariaDB_PASSWORD=$ROOTPWD
APP_PATH=/opt/app/svws
CONF_PATH=/etc/app/svws/conf
APP_PORT=8443
SVWS_TLS_KEYSTORE_PATH=/etc/app/svws/conf/keystore
SVWS_TLS_KEYSTORE_PASSWORD=$ROOTPWD
SVWS_TLS_KEY_ALIAS=
INPUT_common_name=svws-server
INPUT_organizational_unit=$SNR
INPUT_organization=$FQDN
INPUT_locality=NRW
INPUT_state=DE
INPUT_country=DE
validity_days=7000
TMP_DIR=/tmp/svws
#
# Hier kann ggf. Ihre Schuldatenbank hinterlegtr werden und ggf auch automatisch migriert werden.
# Des Weiteren gibt es noch die Moeglichkein per CURL ein Backup einzuspielen oder eine Migration zu starten.
# Dazu bitte die naechsten beiden Zeilen benutzen und die dritte natuerlich loeschen.
#MDBFILE=/tmp/svws/databases/YourSchoolDatabase.mdb
#INIT_EMPTY_DB=N
INIT_EMPTY_DB=J
#
EOF
##
##
##
# [Installation SVWS-Server]
echo
echo -e -n "${WEISS}Installation des SVWS-SERVERS ${NC}"
echo
sleep 2
bash install-$VERSIONSNR.sh
##
## [Installation Schild3]
##
# Samba einrichten
echo
echo -e -n "${WEISS}Installation des SAMBA-SERVERS und erstellen von SchiILD3 ${NC}"
echo
sleep 2
mkdir /netzlaufwerk/
mkdir /netzlaufwerk/Schild_3
mkdir /netzlaufwerk/Schild_3_Arbeitsverzeichnis
mkdir /netzlaufwerk/Schild_3/Connection-Files
mkdir /netzlaufwerk/installationfiles
wget https://github.com/SVWS-NRW/Schild3-BetaTest/releases/download/v$SCHILD_VERSIONSNR/Setup_SchILD3_v$SCHILD_VERSIONSNR.zip
mv /root/Setup_SchILD3_v$SCHILD_VERSIONSNR.zip /netzlaufwerk/installationfiles/
#mv Schild-Reports.zip /netzlaufwerk/installationfiles
unzip /netzlaufwerk/installationfiles/Setup_SchILD3_v$SCHILD_VERSIONSNR.zip -d /netzlaufwerk/Schild_3/
git clone https://github.com/SVWS-NRW/SVWS-TestMDBs /netzlaufwerk/installationfiles/SVWS-TestMDBs/
tee -a /etc/samba/smb.conf <<EOF

[netzlaufwerk]
path = /netzlaufwerk
public = yes
writable = yes
comment = SVWS Freigabe
printable = no
guest ok = yes
EOF
#
#
#
# Schild 3 Connection Files
tee -a /netzlaufwerk/Schild_3/Connection-Files/server.con.example <<EOF
[SchILDconnection]
ProviderName=MYSQL
ProviderType=MARIADB
server=$IP
Port=3306
Database=svwsdb
Username=svwsadmin
Password=AAjcM0WrUSlfbBR5EtcPSyTzso2858zMPOY0Ih3nUlm31S3aD3Fs/LwxUHYCFjs8iLU=
Direct=0
SVWSServerURL=$IP
EOF
#
cp /netzlaufwerk/Schild_3/Connection-Files/server.con.example /netzlaufwerk/Schild_3/Connection-Files/svwsdb.con
#
chmod -R 777 /netzlaufwerk/
#
systemctl restart smbd.service
echo
echo -e "${HELLGRUEN} Ausgabe des Status des Samba-Servers !?!"
echo
systemctl status smbd.service --no-pager
#