#!/bin/bash

# Script d'installation pour tester le logiciel "Ruches"
# ver: 0.3
# Auteur script: nuxbsd
# Mail: nuxbsd@gmail.com
# https://nuxbsd.legtux.org/

# Logiciel "Ruches"
# path: https://gitlab.com/ruches/
# Auteur du logiciel "Ruches": ooioo
# Mail: ooioo@free.fr / anne-marie@les-abeilles-de-la-lisette.fr
# Site: https://les-abeilles-de-la-lisette.fr/wordpress/

#Info PASSWORD pour TEST
: '
TOMCAT
vers: 9.0.68 
path: http://127.0.0.1:8080/manager/html
user: admin
pass: admin

PHPPGADMIN
vers: 7.13.0
path: http://localhost/phppgadmin/
user: postgres 
pass: postgres

Les Abeilles de la bas
path: http://127.0.0.1:8080/ruches/
user: admin
pass: admin'


function clean {
clear
}

#SUPPRESSION DES INSTANCES
function del {
rm -rf /opt/tomcat 
rm -rf /tmp/git/
rm -rf /etc/phppgadmin/
rm -rf /var/lib/postgresql/
}

#INSTALLATION DES PAQUETS
function package {
apt clean
apt update
apt purge -y postgresql-13 postgresql postgresql-contrib apache2 php8.1 php8.1-fpm php8.1-pgsql php8.1 libapache2-mod-php8.1 phppgadmin
apt install -y git maven ntp libtcnative-1 xz-utils curl openjdk-17-jdk postgresql-13 postgresql postgresql-contrib apache2 php8.1 php8.1-fpm php8.1-pgsql php8.1 libapache2-mod-php8.1 phppgadmin 
}

#TOMCAT
function tomcat {
cd /opt/
curl -O https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.68/bin/apache-tomcat-9.0.68.tar.gz
tar xzvf apache-tomcat-9.0.68.tar.gz -C /opt/
mv /opt/apache-tomcat-9.0.68/ /opt/tomcat
groupadd tomcat
useradd -s /bin/false -g tomcat -d /opt/tomcat/conf tomcat
chown -R tomcat:tomcat /opt/tomcat/
chmod -R u+x /opt/tomcat/bin
./tomcat/bin/catalina.sh start && ./tomcat/bin/catalina.sh stop

#/opt/tomcat/conf/tomcat-users.xml
sed -i '$d' /opt/tomcat/conf/tomcat-users.xml
echo -e "<role rolename=\"admin\"/>\n<role rolename=\"admin-gui\"/>\n<role rolename=\"manager\"/>\n<role rolename=\"manager-gui\"/>" >> /opt/tomcat/conf/tomcat-users.xml;
echo -e "<user username=\"admin\" password=\"admin\" roles=\"admin,admin-gui,manager,manager-gui\"/>" >> /opt/tomcat/conf/tomcat-users.xml;
echo -e "</tomcat-users>" >> /opt/tomcat/conf/tomcat-users.xml;

#opt/tomcat/webapps/manager/META-INF/context.xml
cp /opt/tomcat/webapps/manager/META-INF/context.xml /opt/tomcat/webapps/manager/META-INF/context.xml.bak
sed -i '21,22d' /opt/tomcat/webapps/manager/META-INF/context.xml

#/opt/tomcat/webapps/host-manager/META-INF/context.xml
cp /opt/tomcat/webapps/host-manager/META-INF/context.xml /opt/tomcat/webapps/host-manager/META-INF/context.xml.bak
sed -i '21,22d' /opt/tomcat/webapps/host-manager/META-INF/context.xml

#/etc/systemd/system/tomcat.service
rm /etc/systemd/system/tomcat.service
echo -e "[Unit]
Description=Tomcat
After=network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment=\"JAVA_HOME=/usr/lib/jvm/java-1.17.0-openjdk-amd64\"
Environment=\"JAVA_OPTS=-Djava.security.egd=file:///dev/urandom\"
Environment=\"CATALINA_BASE=/opt/tomcat\"
Environment=\"CATALINA_HOME=/opt/tomcat\"
Environment=\"CATALINA_PID=/opt/tomcat/temp/tomcat.pid\"
Environment=\"CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC\"
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
[Install]
WantedBy=multi-user.target" > /etc/systemd/system/tomcat.service;
}

#POSTGRES
function postgres {
#/etc/phppgadmin/config.inc.php modification ligne 105 & 111
cp /etc/phppgadmin/config.inc.php /etc/phppgadmin/config.inc.php.bak
# suppressions des tabulations
sed -r -i "s/\t//ig" /etc/phppgadmin/config.inc.php
# modification des lignes 105 & 111
sed -i 's/$conf\['\''extra_login_security'\''] = true;/$conf\['\''extra_login_security'\''] = false;/' /etc/phppgadmin/config.inc.php
sed -i 's/$conf\['\''owned_only'\''] = false;/$conf\['\''owned_only'\''] = true;/' /etc/phppgadmin/config.inc.php

#/etc/apache2/conf-enabled/phppgadmin.conf
> /etc/apache2/conf-enabled/phppgadmin.conf
echo -e "Alias /phppgadmin /usr/share/phppgadmin
<Directory /usr/share/phppgadmin>
<IfModule mod_dir.c>
DirectoryIndex index.php
</IfModule>
AllowOverride None
# Only allow connections from localhost:
#Require local
Require all granted
#ou alors
#Require local
#Require ip 10.0.0.0/24
 <IfModule mod_php.c>
  php_flag magic_quotes_gpc Off
  php_flag track_vars On
  #php_value include_path .
</IfModule>
<IfModule !mod_php.c>
  <IfModule mod_actions.c>
    <IfModule mod_cgi.c>
      AddType application/x-httpd-php .php
      Action application/x-httpd-php /cgi-bin/php
    </IfModule>
    <IfModule mod_cgid.c>
      AddType application/x-httpd-php .php
      Action application/x-httpd-php /cgi-bin/php
    </IfModule>
  </IfModule>
</IfModule>
 </Directory>" > /etc/apache2/conf-enabled/phppgadmin.conf;
}

#GIT
function gitlab {
git clone https://gitlab.com/ruches/ruches.git /tmp/git/ruches
git clone https://gitlab.com/ruches/ruches-doc.git /tmp/git/ruches-doc
}

#MAVEN
function maven {
cd /tmp/git/ruches/
mvn package -DskipTests
cp /tmp/git/ruches-doc/docs/install/context.xml /opt/tomcat/conf/Catalina/localhost/ruches.xml
cp /tmp/git/ruches/target/ruches.war.original /opt/tomcat/webapps/ruches.war
chown -R tomcat:tomcat /opt/tomcat/
chmod -R u+x /opt/tomcat/bin
}

#ACTIVATION DES SERVICES APACHE
function service {
a2enmod proxy_fcgi setenvif && a2enconf php8.1-fpm

#START SERVICE
chown -R tomcat:tomcat /opt/tomcat/
systemctl daemon-reload && systemctl enable tomcat.service && systemctl start tomcat.service
systemctl restart php8.1-fpm
systemctl restart apache2
systemctl restart postgresql
}

function info {
#POUR LA DATABASE
clear
echo -e "\n\nTous les composants sont installés !\n####################################";
echo -e "ATTENION: Ceci est une installation fonctionnelle pour des tests\n\t\n\t-Apache n'est utilisé que pour utiliser phpPgAdmin\n\t-le module vhosts pour apache n'est pas fonctionnel";
echo -e "\n\t##########################################################\n\t# Pour terminer l'installation suivez les lignes içi bas #\n\t##########################################################";
echo -e "\n1.Definir le mot de passe pour l'utilisateur postgres\n*****************************************************";
echo -e "user:postgres pass:user:postgres\n";
echo -e "su - postgres\npsql\npostgres=# \password postgres\nSaisir le nouveau mot de passe de l'utilisateur « postgres » :\nSaisissez-le à nouveau :\npostgres=# exit";
echo -e "\n2.Créer la database ruches\n**************************";
echo -e "> En ligne de commande:\nsu -\nsu - postgres\npsql -c 'create database ruches;'\npsql -c \"alter user postgres with password 'postgres';\"";
echo -e "psql -f /tmp/git/ruches-doc/sql/structure.sql ruches";
echo -e "psql -f /tmp/git/ruches-doc/sql/ruches_donnees_min.sql ruches";
echo -e "psql -c \"alter user postgres with password 'postgres';\"";
echo -e "\n> Par l'interface web:\nAllez sur http://localhost/phppgadmin/\n\t-login: postgres\n\t-pass: postgres\nAllez sur database ruches\n\t-onglet SQL\n\t-Parcourir\n\t-importé: /tmp/git/ruches-doc/sql/structure.sql\n\t-importé: /tmp/git/ruches-doc/sql/ruches_donnees_min.sql";
echo -e "http://localhost:8080/";
echo -e "http://localhost:8080/manager/html\n\tlogin: admin\n\tpass:admin\n\ton peut constater que:\n\t\t-/ruches est présent dans la colone PATH\n\t\t-true est présent dans la colone Running";
echo -e "\n\thttp://localhost:8080/ruches/ est donc bien accessible :-)\n";
}

function status {
systemctl status tomcat.service && systemctl status php8.1-fpm && systemctl status apache2 && systemctl status postgresql
}

#APPEL DE FONCTION
clean
del
package
tomcat
postgres
gitlab
maven
service
info

