#!/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

# Check root
printf "\n\e[1;32m%s\e[m\n" "Check user rights"
if [[ $EUID -ne 0 ]]; then
   printf "\n\e[1;31m%s\n%s\e[m\n" "This script must be run as root" "Use: su - root"
   exit 1
else
   printf "\e[1;32m%s\e[m\n" "Ok"
fi

mkdir /root/xsquare
cd /root/xsquare
## Download XSQUARE
wget https://lcdp.xsquare.ru/edu/files/pghs_xrad/6.4.5.5.5.5/xsquare.lcdp.6.4.5.5.5.5_release.zip
wget https://lcdp.xsquare.ru/edu/files/xdac/6.0.0.3/xsquare.xdac.6.0.0.3.deb
wget https://lcdp.xsquare.ru/edu/files/xreports/6.0.0.3/xsquare.xreports.6.0.0.3.deb
## Download pgsql-http client
wget https://lcdp.xsquare.ru/files/pgsql-http/v1.7.0.tar.gz
## LibreOffice
wget https://lcdp.xsquare.ru/files/libreoffice/LibreOffice_25.8.4_Linux_x86-64_deb.tar.gz

apt -y install unzip vim wget curl zip

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

##################################
echo "Install PostgreSQL"

apt -y install postgresql-15

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;\""

#######################################
## 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-15 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;"

#######################################
echo "Install PGHS + XRAD"
cd /root/xsquare
unzip xsquare.lcdp.6.4.5.5.5.5_release.zip
cd xsquare.lcdp.6.4.5.5.5.5_release

export PGPASSWORD='xrad_user';
psql -a -v ON_ERROR_STOP=1 --single-transaction -U xrad_user -h 127.0.0.1 xraddb < db/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/appdb.xsquare.pgsql

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

#######################################
echo "Install XDAC"
cd /root/xsquare
dpkg -i xsquare.xdac.6.0.0.3.deb
#######################################
echo "Install XREPORTS"
cd /root/xsquare
dpkg -i xsquare.xreports.6.0.0.3.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
##############################################
## 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"
