#!/bin/bash

echo"" && echo "TMAS DisplaySystems Internet Installer for Ubuntu Budgie 22.04 LTS" && 
display_user=$(logname) && 
echo "------------------------------------------------------" && echo "Display app will be installed under user : ${display_user}" && echo "------------------------------------------------------" && echo "Running script as root user :- " && apt-get update && sleep 3 && apt-get upgrade -y && sleep 3 && apt-get dist-upgrade -y && sleep 3 && apt-get autoremove -y && sleep 3 && apt-get install x11vnc python3-pip python3-tk sqlite3 gedit unclutter chrony openssh-server -y && sleep 3 && mkdir -p /etc/x11vnc && echo "" && echo "" && echo "------------------------------------------------------" && echo "The installer will query for a Remote Desktop Password" && echo "------------------------------------------------------" && echo "" && x11vnc -storepasswd /etc/x11vnc/x11vnc.conf && echo "" && echo "" && sleep 2 && echo "[Unit]" > /lib/systemd/system/x11vnc.service && echo "Description=Start x11vnc at startup." >> /lib/systemd/system/x11vnc.service && echo "After=multi-user.target" >> /lib/systemd/system/x11vnc.service && echo "[Service]" >> /lib/systemd/system/x11vnc.service && echo "Type=simple" >> /lib/systemd/system/x11vnc.service && echo "ExecStart=/usr/bin/x11vnc -auth guess -forever -loop -noxdamage -repeat -rfbauth /etc/x11vnc/x11vnc.conf -rfbport 5800 -nevershared -tightfilexfer" >> /lib/systemd/system/x11vnc.service && echo "[Install]" >> /lib/systemd/system/x11vnc.service && echo "WantedBy=multi-user.target" >> /lib/systemd/system/x11vnc.service && sleep 2 && systemctl daemon-reload && systemctl enable x11vnc.service && systemctl start x11vnc.service && sleep 2 && chmod u+s /sbin/poweroff && chmod u+s /sbin/reboot && echo "#server X.X.X.X iburst" > /etc/chrony/sources.d/local-ntp-server.sources && ( grep -qxF 'maxdistance' /etc/chrony/chrony.conf || echo "" >> /etc/chrony/chrony.conf ) && ( grep -qxF 'maxdistance' /etc/chrony/chrony.conf || echo "maxdistance 16.0" >> /etc/chrony/chrony.conf ) && sleep 2 && curl https://vpn-tmas.ddns.net/downloads/DisplaySystems/stores/logo_128.png --output /usr/share/plymouth/tmas128.png && curl https://vpn-tmas.ddns.net/downloads/DisplaySystems/stores/logo_200.png --output /usr/share/plymouth/tmas200.png && ( [[ -f /usr/share/plymouth/themes/ubuntu-budgie-logo/backup16.png ]] || cp /usr/share/plymouth/themes/ubuntu-budgie-logo/ubuntu-budgie-logo16.png /usr/share/plymouth/themes/ubuntu-budgie-logo/backup16.png ) && ( [[ -f /usr/share/plymouth/themes/ubuntu-budgie-logo/backup16@2.png ]] || cp /usr/share/plymouth/themes/ubuntu-budgie-logo/ubuntu-budgie-logo16@2.png /usr/share/plymouth/themes/ubuntu-budgie-logo/backup16@2.png ) && ( [[ -f /usr/share/plymouth/themes/ubuntu-budgie-logo/backup@2.png ]] || cp /usr/share/plymouth/themes/ubuntu-budgie-logo/ubuntu-budgie-logo@2.png /usr/share/plymouth/themes/ubuntu-budgie-logo/backup@2.png ) && ( [[ -f /usr/share/plymouth/themes/ubuntu-budgie-logo/backup.png ]] || cp /usr/share/plymouth/themes/ubuntu-budgie-logo/ubuntu-budgie-logo.png /usr/share/plymouth/themes/ubuntu-budgie-logo/backup.png ) && cp /usr/share/plymouth/tmas128.png /usr/share/plymouth/themes/ubuntu-budgie-logo/ubuntu-budgie-logo16.png && cp /usr/share/plymouth/tmas128.png /usr/share/plymouth/themes/ubuntu-budgie-logo/ubuntu-budgie-logo.png && cp /usr/share/plymouth/tmas200.png /usr/share/plymouth/themes/ubuntu-budgie-logo/ubuntu-budgie-logo16@2.png && cp /usr/share/plymouth/tmas200.png /usr/share/plymouth/themes/ubuntu-budgie-logo/ubuntu-budgie-logo@2.png && update-grub && sleep 2 ## && 
su - display<<'EOF'
sleep 2 && echo "Running script as normal user :- " && whoami && echo "" && sleep 2 && ( grep -qxF 'export PATH' ~/.bashrc || echo "" >> ~/.bashrc ) && ( grep -qxF 'export PATH' ~/.bashrc || echo "" >> ~/.bashrc ) && ( grep -qxF 'export PATH' ~/.bashrc || echo 'export PATH="/home/${display_user}/.local/bin:\$PATH"' >> ~/.bashrc ) && sleep 2 && echo "Creating environment as user :- " && whoami && source ~/.bashrc && sleep 3 && pip3 install "fastapi[all]" starlette pydantic urllib3 asyncio certifi uvicorn requests apscheduler qrcode pillow ifaddr loguru pywebview && sleep 2 && mkdir -p ~/DisplaySystems && mkdir -p ~/DisplaySystems/logs && mkdir -p ~/DisplaySystems/stores && mkdir -p ~/DisplaySystems/public/content && mkdir -p ~/DisplaySystems/public/css && mkdir -p ~/DisplaySystems/public/js && curl https://vpn-tmas.ddns.net/downloads/DisplaySystems/dataManager.py --output ~/DisplaySystems/dataManager.py && curl https://vpn-tmas.ddns.net/downloads/DisplaySystems/dataManager-ws.py --output ~/DisplaySystems/dataManager-ws.py && curl https://vpn-tmas.ddns.net/downloads/DisplaySystems/displayManager.py --output ~/DisplaySystems/displayManager.py && curl https://vpn-tmas.ddns.net/downloads/DisplaySystems/eventManager.py --output ~/DisplaySystems/eventManager.py && curl https://vpn-tmas.ddns.net/downloads/DisplaySystems/fileManager.py --output ~/DisplaySystems/fileManager.py && curl https://vpn-tmas.ddns.net/downloads/DisplaySystems/logManager.py --output ~/DisplaySystems/logManager.py && curl https://vpn-tmas.ddns.net/downloads/DisplaySystems/stores/background.png --output ~/DisplaySystems/stores/background.png && curl https://vpn-tmas.ddns.net/downloads/DisplaySystems/stores/background2.png --output ~/DisplaySystems/stores/background2.png && curl https://vpn-tmas.ddns.net/downloads/DisplaySystems/registrationManager.py --output ~/DisplaySystems/registrationManager.py && curl https://vpn-tmas.ddns.net/downloads/DisplaySystems/statusManager.py --output ~/DisplaySystems/statusManager.py && curl https://vpn-tmas.ddns.net/downloads/DisplaySystems/systemManager.py --output ~/DisplaySystems/systemManager.py && curl https://vpn-tmas.ddns.net/downloads/DisplaySystems/config.json --output ~/DisplaySystems/config.json && curl https://vpn-tmas.ddns.net/downloads/DisplaySystems/stores/icon.png --output ~/DisplaySystems/stores/icon.png && curl https://vpn-tmas.ddns.net/downloads/DisplaySystems/stores/splash.png --output ~/DisplaySystems/stores/splash.png && curl https://vpn-tmas.ddns.net/downloads/DisplaySystems/application.sh --output ~/DisplaySystems/application.sh && echo "[Desktop Entry]" > ~/.config/autostart/displaysystems.desktop && echo "Type=Application" >> ~/.config/autostart/displaysystems.desktop && echo "Exec=/home/${display_user}/DisplaySystems/application.sh" >> ~/.config/autostart/displaysystems.desktop && echo "Hidden=false" >> ~/.config/autostart/displaysystems.desktop && echo "NoDisplay=false" >> ~/.config/autostart/displaysystems.desktop && echo "X-GNOME-Autostart-enabled=true" >> ~/.config/autostart/displaysystems.desktop && echo "Name[en_US]=TMAS Display Systems" >> ~/.config/autostart/displaysystems.desktop && echo "Name=TMAS Display Systems" >> ~/.config/autostart/displaysystems.desktop && echo "Comment[en_US]=TMAS Public Information Display Systems" >> ~/.config/autostart/displaysystems.desktop && echo "Comment=TMAS Public Information Display Systems" >> ~/.config/autostart/displaysystems.desktop && chmod 755 ~/DisplaySystems/application.sh
EOF && echo "Please select the display type to complete the installation:" && echo "" && read -p "  1. Public Internet Displays with SSL (Default)          2. Local Intranet Display without SSL " display_type  && echo "" && echo "Installing display type: ${display_type}" && 

if [ $display_type == '2' ] then 
cp /home/${display_user}/DisplaySystems/dataManager-ws.py /home/${display_user}/DisplaySystems/dataManager.py
fi && rm -rf /home/${display_user}/DisplaySystems/dataManager-ws.py