#!/bin/bash

echo "" && echo "TMAS DisplaySystems online Installer for Ubuntu and Ubuntu Budgie 22.04 LTS"
display_user=$SUDO_USER && echo "" && echo "------------------------------------------------------" && echo "The installer will run the Display app under user : ${display_user}" && echo "------------------------------------------------------" && installed_os_flavour_fullname=$(cat /var/log/installer/media-info) && installed_os_flavour=(${installed_os_flavour_fullname// / })
if [ $installed_os_flavour != "Ubuntu" ] && [ $installed_os_flavour != "Ubuntu-Budgie" ]
then
echo "This installer only runs for Ubuntu & Ubuntu Budgie."
exit 0
fi
echo "" && echo "Detected Linux installer Flavour: ${installed_os_flavour_fullname}" && 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-venv python3-tk python3-pil.imagetk sqlite3 gedit unclutter chrony openssh-server ffmpeg firefox -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 "------------------------------------------------------" && echo "The installer will install NTP client settings        " && echo "------------------------------------------------------" && echo "Enter a local NTP server IP or FQDN - Defaults as none"

read fname
fname=${fname:=X.X.X.X}
if [ $fname == "X.X.X.X" ]
then
echo "#server X.X.X.X iburst" > /etc/chrony/sources.d/local-ntp-server.sources
else
echo "server ${fname} iburst" > /etc/chrony/sources.d/local-ntp-server.sources
fi

echo "Configured NTP reference servers" && ( grep -qxF "# tmas display systems" /etc/chrony/chrony.conf || echo "" >> /etc/chrony/chrony.conf ) && ( grep -qxF "# tmas display systems" /etc/chrony/chrony.conf || echo "maxdistance 16.0" >> /etc/chrony/chrony.conf ) && ( grep -qxF "# tmas display systems" /etc/chrony/chrony.conf || echo "# tmas display systems" >> /etc/chrony/chrony.conf ) && sleep 2 && 

if [ $installed_os_flavour == "Ubuntu" ]
then
apt-get install gnome-shell-extension-manager -y
fi
echo "" && update-grub && sleep 2 && su - ${display_user}<<'EOF'
sleep 2 && echo "Running script as normal user :- $(whoami)" && echo "" && sleep 2
display_working_directory_path='export PATH="/home/' && display_working_directory_path+=$(whoami) && display_working_directory_path+='/.local/bin:$PATH"'
sleep 1 && ( grep -qxF "# tmas display systems" ~/.bashrc || echo "" >> ~/.bashrc ) && ( grep -qxF "# tmas display systems" ~/.bashrc || echo "" >> ~/.bashrc ) && ( grep -qxF "# tmas display systems" ~/.bashrc || echo "$display_working_directory_path" >> ~/.bashrc ) && ( grep -qxF "# tmas display systems" ~/.bashrc || echo "# tmas display systems" >> ~/.bashrc ) && sleep 2 && echo "Creating environment as user :- $(whoami)" && echo "$display_working_directory_path" && echo "" && source ~/.bashrc && sleep 3 && mkdir -p ~/.config/autostart && mkdir -p ~/DisplaySystems && python3 -m venv ~/DisplaySystems/dsenv && source ~/DisplaySystems/dsenv/bin/activate && pip3 install "fastapi[all]" starlette pydantic urllib3 asyncio certifi uvicorn requests apscheduler qrcode pillow ifaddr loguru pywebview && sleep 2 && deactivate && sleep 2 && 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://devops-01.tmas.my/DisplaySystems/dataManager.py --output ~/DisplaySystems/dataManager.py && curl https://devops-01.tmas.my/DisplaySystems/dataManager-ws.py --output ~/DisplaySystems/dataManager-ws.py && curl https://devops-01.tmas.my/DisplaySystems/displayManager.py --output ~/DisplaySystems/displayManager.py && curl https://devops-01.tmas.my/DisplaySystems/eventManager.py --output ~/DisplaySystems/eventManager.py && curl https://devops-01.tmas.my/DisplaySystems/fileManager.py --output ~/DisplaySystems/fileManager.py && curl https://devops-01.tmas.my/DisplaySystems/logManager.py --output ~/DisplaySystems/logManager.py && curl https://devops-01.tmas.my/DisplaySystems/stores/background.png --output ~/DisplaySystems/stores/background.png && curl https://devops-01.tmas.my/DisplaySystems/stores/background2.png --output ~/DisplaySystems/stores/background2.png && curl https://devops-01.tmas.my/DisplaySystems/registrationManager.py --output ~/DisplaySystems/registrationManager.py && curl https://devops-01.tmas.my/DisplaySystems/statusManager.py --output ~/DisplaySystems/statusManager.py && curl https://devops-01.tmas.my/DisplaySystems/systemManager.py --output ~/DisplaySystems/systemManager.py && curl https://devops-01.tmas.my/DisplaySystems/config.json --output ~/DisplaySystems/config.json && curl https://devops-01.tmas.my/DisplaySystems/stores/icon.png --output ~/DisplaySystems/stores/icon.png && curl https://devops-01.tmas.my/DisplaySystems/stores/splash.png --output ~/DisplaySystems/stores/splash.png && curl https://devops-01.tmas.my/DisplaySystems/stores/dispsys32.png --output ~/DisplaySystems/stores/dispsys32.png && curl https://devops-01.tmas.my/DisplaySystems/stores/dispsys64.png --output ~/DisplaySystems/stores/dispsys64.png && curl https://devops-01.tmas.my/DisplaySystems/application.sh --output ~/DisplaySystems/application.sh && echo "[Desktop Entry]" > ~/.config/autostart/displaysystems.desktop && echo "Type=Application" >> ~/.config/autostart/displaysystems.desktop && echo "Exec=/home/$(whoami)/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]=DisplaySystems" >> ~/.config/autostart/displaysystems.desktop && echo "Name=DisplaySystems" >> ~/.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 && echo "[Desktop Entry]" > ~/Desktop/DisplaySystems.desktop && echo "Name=DisplaySystems" >> ~/Desktop/DisplaySystems.desktop && echo "Version=1.2.0" >> ~/Desktop/DisplaySystems.desktop && echo "Exec=/home/$(whoami)/DisplaySystems/application.sh" >> ~/Desktop/DisplaySystems.desktop && echo "Icon=dispsys" >> ~/Desktop/DisplaySystems.desktop && echo "Terminal=false" >> ~/Desktop/DisplaySystems.desktop && echo "Type=Application" >> ~/Desktop/DisplaySystems.desktop && install -D /home/$(whoami)/DisplaySystems/stores/dispsys32.png ~/.local/share/icons/hicolor/32x32/apps/dispsys.png && install -D /home/$(whoami)/DisplaySystems/stores/dispsys64.png ~/.local/share/icons/hicolor/64x64/apps/dispsys.png && echo "------------------------------------------------------" && echo "The installer will note the following reminders:      " && echo "------------------------------------------------------" && echo "" && echo " For Ubuntu-Budgie:                                   " && echo "1. Disable Blank Screen @ Budgie Control Center-Power " && echo "2. Disable Automatic Screen Lock @ Budgie Control Center-Displays" && echo "3. Enable Automatic Login @ Budgie Control Center-Users" && echo "4. Configure Top Panel @ Budgie Desktop Settings-Top Panel-Settings" && echo "" && echo " For Ubuntu:                                          " && echo "1. Disable Blank Screen @ Settings-Power " && echo "2. Enable Automatic Login @ Settings-Users" && echo "3. Enable Auto-Hide Dock @ Settings-Appearence" && echo "4. Configure Top Panel @ Gnome Extension Manager using 'Hide Top Bar' extension by 'tuxor1337'" && echo "5. Enable desktop shortcut @ Desktop>Right Click menu On DisplaySystems icon" && echo "" && echo "------------------------------------------------------" && echo "The installer will configure the Display as an executable" && echo "------------------------------------------------------" && echo "" && chmod 755 ~/DisplaySystems/application.sh && chmod 755 ~/Desktop/DisplaySystems.desktop && chown $(whoami):$(whoami) ~/Desktop/DisplaySystems.desktop && echo "------------------------------------------------------" && echo "The installer has completed successfully"               && echo "------------------------------------------------------"
EOF