#!/bin/bash
##################################
####### XSQUARE (PGHS+XRAD+XDAC+XREPORTS) start installation #######
##################################
## LOGON AS root
## ssh root@my_ip -p 22
## password:xsquare
##manualy setup your locale
# dpkg-reconfigure locales
# or automaticaly

# functions
function check_user_permissions() {
   RED="\033[1;31m"
   GREEN="\033[1;32m"
   NC="\033[0m"
   echo -e "${GREEN}Check user${NC}"
   if [[ $EUID -ne 0 ]]; then
      echo -e "${RED}This script must be run as root${NC}"
      echo -e "${RED}Use: 'su - root' or 'sudo -i'${NC}"
      exit 1
   else
      echo -e "${GREEN}Ok${NC}"
   fi
}

function install_script_package_dependencies() {
   apt -y install unzip vim wget curl zip
}

function get_installation_packages() {
   mkdir /root/xsquare
   cd /root/xsquare
   ## Download XSQUARE
   wget https://xsquare.dev/files/edu/pghs_xrad/6.6.7.7.7.7/xsquare.lcdp.6.6.7.7.7.7_release.zip
   wget https://xsquare.dev/files/edu/xdac/6.6.0.4/xsquare.xdac.6.6.0.4.deb
   wget https://xsquare.dev/files/edu/xreports/6.6.1.2/xsquare.xreports.6.6.1.2.deb
   ## Download pgsql-http client
   wget https://xsquare.dev/files/edu/tools/pgsql-http/v1.7.0.tar.gz
   ## LibreOffice
   wget https://xsquare.dev/files/edu/tools/libreoffice/LibreOffice_25.8.4_Linux_x86-64_deb.tar.gz
}

function locale_configuration() {
   echo "Europe/Moscow" > /etc/timezone && \
      dpkg-reconfigure -f noninteractive tzdata && \
      sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \
      sed -i -e 's/# ru_RU.UTF-8 UTF-8/ru_RU.UTF-8 UTF-8/' /etc/locale.gen && \
      echo 'LANG="ru_RU.UTF-8"'>/etc/default/locale && \
      dpkg-reconfigure --frontend=noninteractive locales && \
      update-locale LANG=ru_RU.UTF-8
      update-locale LC_ALL=ru_RU.UTF-8

   export LANG=ru_RU.UTF-8
   env | grep LANG
}

function postgresql_installation() {
   echo "Install PostgreSQL"
   apt -y install postgresql-17

   su - postgres -c "psql -U postgres -d postgres -c \"create user xrad_user with encrypted password 'xrad_user';\""
   su - postgres -c "psql -U postgres -d postgres -c \"create user app_user with encrypted password 'app_user';\""
   su - postgres -c "psql -U postgres -d postgres -c \"CREATE DATABASE \"appdb\" WITH OWNER \"app_user\" ENCODING 'UTF8' LC_COLLATE = 'ru_RU.UTF-8' LC_CTYPE = 'ru_RU.UTF-8';\""
   su - postgres -c "psql -U postgres -d postgres -c \"CREATE DATABASE \"xraddb\" WITH OWNER \"xrad_user\" ENCODING 'UTF8' LC_COLLATE = 'ru_RU.UTF-8' LC_CTYPE = 'ru_RU.UTF-8';\""
   su - postgres -c "psql -U postgres -d postgres -c \"ALTER USER xrad_user WITH SUPERUSER;\""
   su - postgres -c "psql -U postgres -d postgres -c \"ALTER USER app_user WITH SUPERUSER;\""
}

function postgresql_http_extension_installation() {
   ## pgsql-http client to make HTTP request to XREPORTS from PostgreSQL
   ## for page http://pghs.domain/pghs/download?page=5500
   ## from wget https://github.com/pramsey/pgsql-http/archive/refs/tags/v1.7.0.tar.gz
   ## if you are not using version 17 of PostgreSQL, you can download http from the developer's website https://github.com/pramsey/pgsql-http
   apt -y install gcc dpkg-dev postgresql-server-dev-17 libcurl4-openssl-dev

   tar -xzf v1.7.0.tar.gz
   export PATH=$PATH:/usr/lib/postgresql/17/bin/
   cd pgsql-http-1.7.0
   make
   make install

   export PGPASSWORD='app_user';
   psql -U app_user -h 127.0.0.1 -d appdb -c "CREATE EXTENSION http;"
}

function pghs_xrad_simple_core_installation() {
   echo "Install PGHS + XRAD"
   cd /root/xsquare
   unzip xsquare.lcdp.6.6.7.7.7.7_release.zip
   cd xsquare.lcdp.6.6.7.7.7.7_release

   export PGPASSWORD='xrad_user';
   psql -a -v ON_ERROR_STOP=1 --single-transaction -U xrad_user -h 127.0.0.1 xraddb < db/simple-core-xraddb.xsquare.pgsql

   export PGPASSWORD='app_user';
   psql -a -v ON_ERROR_STOP=1 --single-transaction -U app_user -h 127.0.0.1 appdb < db/simple-core-appdb.xsquare.pgsql

   rm ./usr/local/xsquare.pghs/demo-config.json
   rm ./usr/local/xsquare.xrad/demo-config.json
   apt -y install nginx
   cp -R ./var /
   cp -R ./etc /
   cp -R ./usr /

   ## REMOVE LINK to defaul config
   rm -f /etc/nginx/sites-enabled/default
   systemctl restart nginx
   systemctl --no-pager status nginx

   systemctl start xsquare.pghs.service
   systemctl start xsquare.xrad.service
   systemctl enable xsquare.xrad.service
   systemctl enable xsquare.pghs.service
   systemctl --no-pager status xsquare.pghs.service
   systemctl --no-pager status xsquare.xrad.service
}


function pghs_xrad_demo_installation() {
   echo "Install PGHS + XRAD"
   cd /root/xsquare
   unzip xsquare.lcdp.6.6.7.7.7.7_release.zip
   cd xsquare.lcdp.6.6.7.7.7.7_release

   export PGPASSWORD='xrad_user';
   psql -a -v ON_ERROR_STOP=1 --single-transaction -U xrad_user -h 127.0.0.1 xraddb < db/demo-xraddb.xsquare.pgsql

   export PGPASSWORD='app_user';
   psql -a -v ON_ERROR_STOP=1 --single-transaction -U app_user -h 127.0.0.1 appdb < db/demo-appdb.xsquare.pgsql

   mv ./usr/local/xsquare.pghs/demo-config.json ./usr/local/xsquare.pghs/config.json
   mv ./usr/local/xsquare.xrad/demo-config.json ./usr/local/xsquare.xrad/config.json
   apt -y install nginx
   cp -R ./var /
   cp -R ./etc /
   cp -R ./usr /

   ## REMOVE LINK to defaul config
   rm -f /etc/nginx/sites-enabled/default
   systemctl restart nginx
   systemctl --no-pager status nginx

   systemctl start xsquare.pghs.service
   systemctl start xsquare.xrad.service
   systemctl enable xsquare.xrad.service
   systemctl enable xsquare.pghs.service
   systemctl --no-pager status xsquare.pghs.service
   systemctl --no-pager status xsquare.xrad.service
}

function xdac_installation() {
   echo "Install XDAC"
   cd /root/xsquare
   dpkg -i xsquare.xdac.6.6.0.4.deb
}

function xreports_installation() {
   echo "Install XREPORTS"
   cd /root/xsquare
   dpkg -i xsquare.xreports.6.6.1.2.deb
   ## Install Libre Office. We recommend latest - 25.8.4
   echo "Install Libre Office"

   apt -y install libxinerama1 libcairo2 libcups2 default-jre

   cd /root/xsquare
   tar -xvzf LibreOffice_25.8.4_Linux_x86-64_deb.tar.gz
   dpkg -i ./LibreOffice_25.8.4.2_Linux_x86-64_deb/DEBS/*.deb

   #set "soffice-path" in config
   soffice_path=`find / -name "soffice"`
   echo $soffice_path
   old_soffice_path='"soffice-path": ""';
   new_soffice_path='"soffice-path": "'$soffice_path'"';
   echo  $new_soffice_path
   sed -i -e "s#$old_soffice_path#$new_soffice_path#g" /usr/local/xsquare.xreports/config.json
   cat /usr/local/xsquare.xreports/config.json

   systemctl restart xsquare.xreports.service
}

function post_installation_info() {
   ##############################################
   ## Getting ip address
   your_ip=`hostname -I | awk '{print $1}'`
   ##############################################
   echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
   echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
   echo ""
   echo "XSQUARE installation has been completed"
   echo "XSQUARE - PGHS http://$your_ip:80"
   echo "XSQUARE - XRAD http://$your_ip:8080"
   echo "XSQUARE - XREPORTS http://$your_ip:8886"
   echo "XSQUARE - XDAC http://$your_ip:8887"
   echo ""
   echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
   echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
}

# installation
echo "Select the version to install:"
echo "S - Simple-core"
echo "D - Demo"
echo "0 - Exit"
echo ""
read -p "Enter your choice (S/D/0): " answer
if [[ -z $answer ]]; then
    echo "Restart the installation script and select the version"
    exit 0
elif [ $answer == "0" ]; then
    echo "Exit..."
    exit 0
elif [ $answer == "S" ] || [ $answer == "s" ]; then
    echo "Version: Simple-core"
    echo ""
    check_user_permissions
    install_script_package_dependencies
    get_installation_packages
    locale_configuration
    postgresql_installation
    postgresql_http_extension_installation
    pghs_xrad_simple_core_installation
    xdac_installation
    xreports_installation
    post_installation_info
elif [ $answer == "D" ] || [ $answer == "d" ]; then
    echo "Version: Demo"
    echo ""
    check_user_permissions
    install_script_package_dependencies
    get_installation_packages
    locale_configuration
    postgresql_installation
    postgresql_http_extension_installation
    pghs_xrad_demo_installation
    xdac_installation
    xreports_installation
    post_installation_info
else
    echo "Select one of the the version"
    echo "Installation interrupted"
    exit 0
fi

