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

mkdir /root/xsquare
cd /root/xsquare
## Download XSQUARE
wget https://xsquare.dev/files/edu/pghs_xrad/5.3.3.5.3.5/xsquare.lcdp.5.3.3.5.3.5_en_release.zip
wget https://xsquare.dev/files/edu/xdac/5.0.6.1/xsquare.xdac.5.0.6.1.deb
wget https://xsquare.dev/files/edu/xreports/5.0.0.12/xsquare.xreports.5.0.0.12.deb
## Download pgsql-http client
wget https://xsquare.dev/files/edu/tools/pgsql-http/v1.5.0.tar.gz
## LibreOffice
wget https://xsquare.dev/files/edu/tools/libreoffice/LibreOffice_25.2.4_Linux_x86-64_deb.tar.gz

apt -y install unzip vim wget curl open-vm-tools zip

echo "UTC" > /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 && \
    echo 'LANG="en_US.UTF-8"'>/etc/default/locale && \
    dpkg-reconfigure --frontend=noninteractive locales && \
    update-locale LANG=en_US.UTF-8

export LANG=en_US.UTF-8
env | grep LANG

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

apt -y install postgresql-15 postgresql-server-dev-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 = 'en_US.UTF-8' LC_CTYPE = 'en_US.UTF-8';\""
su - postgres -c "psql -U postgres -d postgres -c \"CREATE DATABASE \"xraddb\" WITH OWNER \"xrad_user\" ENCODING 'UTF8' LC_COLLATE = 'en_US.UTF-8' LC_CTYPE = 'en_US.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;\""
##################################
echo "Install PGHS + XRAD"

unzip xsquare.lcdp.5.3.3.5.3.5_en_release.zip
cd xsquare.lcdp.5.3.3.5.3.5_en_release

export PGPASSWORD='xrad_user';
psql -U xrad_user -h 127.0.0.1 xraddb < db/xraddb.xsquare.pgsql

export PGPASSWORD='app_user';
psql -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.5.0.6.1.deb
#######################################
echo "Install XREPORTS"
cd /root/xsquare
dpkg -i xsquare.xreports.5.0.0.12.deb
#######################################
## 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.5.0.tar.gz
apt -y install gcc dpkg-dev libcurl4-openssl-dev 

cd /root/xsquare
tar -xzf v1.5.0.tar.gz
export PATH=$PATH:/usr/lib/postgresql/15/bin/
cd pgsql-http-1.5.0
make 
make install

export PGPASSWORD='app_user';
psql -U app_user -h 127.0.0.1 -d appdb -c "CREATE EXTENSION http;"
#######################################
##Install Libre Office. We recommend 7.4.7.2 or 24.8.4
echo "Install Libre Office"

apt -y install libxinerama1 libcairo2 libcups2 default-jre

cd /root/xsquare
tar -xvzf LibreOffice_25.2.4_Linux_x86-64_deb.tar.gz
dpkg -i ./LibreOffice_25.2.4.3_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
#######################################
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"
