#!/bin/bash

# Exit on command errors and treat unset variables as an error
set -eu

app=$YNH_APP_INSTANCE_NAME

# Retrieve arguments
domain=$YNH_APP_ARG_DOMAIN
path=$YNH_APP_ARG_PATH
admin=$YNH_APP_ARG_ADMIN
#language=$YNH_APP_ARG_LANGUAGE

# Source YunoHost helpers
. /usr/share/yunohost/helpers

# Save app settings
ynh_app_setting_set "$app" admin "$admin"
#ynh_app_setting_set "$app" language "$language"

# Check domain/path availability
sudo yunohost app checkurl "${domain}${path}" -a "$app" \
    || ynh_die "Path not available: ${domain}${path}"

# install jetty 	
sudo apt-get install jetty8 libjetty-extra -y -qq

## activer le démarrage de jetty
sudo sed -i "s/NO_START=1/NO_START=0/g" /etc/default/jetty8

# installation des dépendances pour la compilation de guacamole
SRC_DIR="/home/admin/gacamole_src"
if [ ! -d $SRC_DIR ]
then
mkdir $SRC_DIR
fi

check_dep () {
if ! ynh_package_is_installed $DEP 
  then
  echo "$DEP" >> $SRC_DIR/src_dep.lst
fi
}

if [ -f $SRC_DIR/src_dep.lst ]
	then 
	rm -rf $SRC_DIR/src_dep.lst
fi
DEP=libcairo2-dev check_dep
DEP=libpng12-dev check_dep
DEP=libossp-uuid-dev check_dep
DEP=libfreerdp-dev check_dep
DEP=libpango1.0-dev check_dep
DEP=libssh2-1-dev check_dep
DEP=libtelnet-dev check_dep
DEP=libvncserver-dev check_dep
DEP=libpulse-dev check_dep
DEP=libssl-dev check_dep
DEP=libvorbis-dev check_dep
DEP=libwebp-dev check_dep

if [ -f $SRC_DIR/src_dep.lst ]
	then 
		sudo apt-get -y install `cat $SRC_DIR/src_dep.lst` -y -qq
fi



tar -xzf ../sources/guacamole-server-0.9.9.tar.gz -C $SRC_DIR
tar -xzf ../sources/guacamole-auth-jdbc-0.9.9.tar.gz -C $SRC_DIR
tar -xzf ../sources/mysql-connector-java-5.1.38.tar.gz -C $SRC_DIR
sudo mkdir -p /etc/guacamole/{lib,extensions}
sudo mkdir -p /etc/guacamole/lib
sudo mv ../sources/guacamole-0.9.9.war /etc/guacamole
sudo mv ../conf/guacamole.properties $SRC_DIR
cd $SRC_DIR
cd guacamole-server-0.9.9
./configure --with-init-dir=/etc/init.d
make
sudo make install
sudo ldconfig
sudo systemctl enable guacd
sudo ln -s /etc/guacamole/guacamole.war /var/lib/jetty8/webapps/
sudo cp ../mysql-connector-java-5.1.38/mysql-connector-java-5.1.38-bin.jar /etc/guacamole/lib/
sudo cp ../guacamole-auth-jdbc-0.9.9/mysql/guacamole-auth-jdbc-mysql-0.9.9.jar /etc/guacamole/extensions/

## manual initialisation SQL (remove in the futur)
#mysql -u root -pMYSQLROOTPASSWORD
#create database guacamole_db;
#create user 'guacamole_user'@'localhost' identified by 'PASSWORD';
#GRANT SELECT,INSERT,UPDATE,DELETE ON guacamole_db.* TO 'guacamole_user'@'localhost';
#flush privileges;
#quit
#cat guacamole-auth-jdbc-0.9.9/mysql/schema/*.sql | mysql -u root -pMYSQLROOTPASSWORD guacamole_db


sudo rm -rf /usr/share/jetty8/.guacamole
sudo ln -s /etc/guacamole /usr/share/jetty8/.guacamole

# Restart Tomcat Service
sudo service jetty8 restart

# Generate MySQL password and create database
dbuser=$app
dbname=$app
dbpass=$(ynh_string_random 12)
ynh_app_setting_set "$app" mysqlpwd "$dbpass"
ynh_mysql_create_db "$dbname" "$dbuser" "$dbpass"
sed -i "s/CHANGEME/$dbpass/g" $SRC_DIR/guacamole.properties
sudo mv $SRC_DIR/guacamole.properties /etc/guacamole
# Load initial SQL into the new database
ynh_mysql_connect_as "$dbuser" "$dbpass" "$dbname" < "$SRC_DIR/guacamole-auth-jdbc-0.9.9/mysql/schema/001-create-schema.sql"
ynh_mysql_connect_as "$dbuser" "$dbpass" "$dbname" < "$SRC_DIR/guacamole-auth-jdbc-0.9.9/mysql/schema/002-create-admin-user.sql"
# Modify Nginx configuration file and copy it to Nginx conf directory
sed -i "s@YNH_WWW_PATH@$path@g" ../conf/nginx.conf
sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/$app.conf

# clean
cd
rm -rf $SRC_DIR
sudo /etc/init.d/guacd restart
sudo service nginx reload
