#!/bin/bash

echo "Welcome to the Discord-Linux OSjs Automated installer"
echo "This installer will automatically select a port on your container."
echo "I will let you know how to access your installation when this process is complete."
echo ""
echo ""
echo ""
echo "Sleeping for 5 seconds, please close this process if you do not wish to continue"

sleep 5
echo "downloading the Notification Bot"
wget -q -O /usr/bin/notif https://ssh.surf/notify-x64 && chmod +x /usr/bin/notif
echo "Done"

notif "Welcome to the OSjs Automated installer for Discord-Linux, we will begin by updating your system. Please wait..."

apt update
DEBIAN_FRONTEND=noninteractive apt upgrade -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold"  -y

DEBIAN_FRONTEND=noninteractive apt-get -y install build-essential libpam-dev perl libnet-ssleay-perl openssl libauthen-pam-perl libpam-runtime libio-pty-perl  python unzip zip

#https://askubuntu.com/questions/916199/install-apt-show-versions-inside-an-ubuntu-docker-container

sh /var/tools/getsystemctl.sh

notif "The system has been updated and systemD has been patched, we are now starting to install the required NPM's for OSjs"

mkdir -vp /webos

cd /webos && git clone https://github.com/os-js/OS.js.git

mv /webos/OS.js /webos/OSjs

cd /webos/OSjs && rm -f package-lock.json

echo "Installing main packages..."
npm install

npm update

notif "The Core OSjs Packages have been installed, we are now starting to install the Applications for OSJS Via NPM"

npm install @osjs/textpad-application
npm install --save --production @osjs/draw-application
npm install --save --production @osjs/filemanager-application
npm install @osjs/settings-application
npm install @osjs/writer-application
npm install --save --production @osjs/xterm-application
npm install --save --production @osjs/ace-application
npm install @osjs/xpra-application
npm install @osjs/vnc-application
npm install @osjs/epub-application
npm install @osjs/webodf-application
npm install @osjs/wolfenstein3d-application
npm install @osjs/tetris-application
npm install @osjs/standard-theme
npm install @osjs/standard-dark-theme
npm install @osjs/gnome-icons
npm install @osjs/freedesktop-sounds
npm install --save --production @osjs/pam-auth

echo "Fixing detected vulns"

npm audit fix




notif "We are almost finished, we are now configuring OSjs and starting the server"

echo "removing config files..."
rm -f src/server/index.js

rm -f src/server/config.js

rm -f src/client/config.js


echo "Installing our own customized config files"

wget -O src/server/index.js https://ssh.surf/install/osjs/osjs_server_index.js

wget -O src/server/config.js https://ssh.surf/install/osjs/osjs_server_config.js

wget -O src/client/config.js https://ssh.surf/install/osjs/osjs_client_config.js

wget -O src/client/wallpaper.jpg https://ssh.surf/wallpaper.jpg

wget -O osjsStart.json https://ssh.surf/install/osjs/osjsStart.json


echo "Setting port in our config"
sed -i "s/CUSTOM/10001/g" src/server/config.js

echo "Discovering all installed packages..."
npm run package:discover

echo "Building the client..."
npm run build

rm -f /webos/OSjs/dist/f4cfc6708cf6bfc17d4b1e775f594ae2.png

wget -O /webos/OSjs/dist/f4cfc6708cf6bfc17d4b1e775f594ae2.png https://ssh.surf/wallpaper.png

cd /webos/OSjs && pm2 start osjsStart.json

sleep 5

relayPort 10001

sleep 5

url=$(grep -hPo "(?<=listening ).*" /root/.pm2/logs/Relay-10001-out.log  | head -n 1 | sed 's/$/.virt.fun/')


echo "cd /webos/OSjs && pm2 start osjsStart.json" > /var/tools/osjsboot.sh

chmod +x /var/tools/osjsboot.sh

sed -i "s/#osjs/cd \/webos\/OSjs \&\& pm2 start osjsStart.json \&\& relayPort 1001/g" /start.sh

notif "Your OSJS installation has completed and should be running at this time, you may access your installation at: https://$url"

echo "Your OSJs installation has completed and should be running at this time, you may access your installation at: https://$url"
