clear

if ! grep -q "oldstable" /etc/apt/sources.list; then
	echo "deb http://deb.debian.org/debian/ oldstable main contrib non-free-firmware" >> /etc/apt/sources.list
fi

apt update
if [ $? -ne 0 ]; then echo "apt update failed. Exiting."; exit; fi
apt upgrade
if [ $? -ne 0 ]; then echo "apt upgrade failed. Exiting."; exit; fi
apt install -y python3.11 unzip imagemagick cmatrix gcp sudo sway vim plymouth alsa-utils tmux mesa-vulkan-drivers avrdude build-essential curl gpg xwayland figlet magic-wormhole htop btop mesa-va-drivers intel-media-va-driver dkms python3-pyaudio ntfs-3g network-manager
if [ $? -ne 0 ]; then echo "apt install failed. Exiting."; exit; fi

rm -rf /media
mkdir /game/media
ln -s /game/media /media
chown -R norville:norville /game /media
if ! grep -q "norville ALL" /etc/sudoers; then echo "norville ALL=(ALL:ALL) NOPASSWD:ALL" >> /etc/sudoers; fi

wget https://spookypinball.s3.us-east-2.amazonaws.com/dev/11-automount-usb.rules
if [ $? -ne 0 ]; then echo "wget 11-automount failed. Exiting"; exit; fi
wget https://spookypinball.s3.us-east-2.amazonaws.com/dev/10-platformio-udev.rules
if [ $? -ne 0 ]; then echo "wget 10-platformio failed. Exiting."; exit; fi
wget https://spookypinball.s3.us-east-2.amazonaws.com/dev/gpg.pub
if [ $? -ne 0 ]; then echo "wget gpg.pub failed. Exiting"; exit; fi

mv 11-automount-usb.rules /etc/udev/rules.d/
mv 10-platformio-udev.rules /etc/udev/rules.d/
chown norville:norville gpg.pub
mv gpg.pub /home/norville/
su -c 'gpg --import /home/norville/gpg.pub' norville
rm /home/norville/gpg.pub

if ! grep -q "tmpfs /var/log" /etc/fstab; then

	cat /etc/fstab | sed 's/ [0-9].*[0-9]$/ 0 0/g' | sed 's/ \/ .*ext4.*$/ \/ ext4 noatime,nodiratime,ro,data=ordered,barrier=1,errors=remount-ro 0 0/' | sed 's/ \/game .*ext4.*$/ \/game ext4 noatime,nodiratime,rw,data=ordered,barrier=1 0 0/' > new_fstab
	echo "tmpfs /tmp tmpfs mode=1777,nosuid,nodev,noatime,nodiratime,size=128M 0 0" >> new_fstab
	echo "tmpfs /var/log tmpfs mode=1777,nosuid,nodev,noatime,nodiratime,size=128M 0 0" >> new_fstab
	#diff /etc/fstab new_fstab
	#read -p "press enter to continue"
	mv new_fstab /etc/fstab

fi

if ! grep -q "GRUB_HIDDEN_TIMEOUT" /etc/default/grub; then
	cat /etc/default/grub | sed 's/GRUB_TIMEOUT=5/GRUB_TIMEOUT=0\nGRUB_HIDDEN_TIMEOUT=0/' > new_grub
	mv new_grub /etc/default/grub
fi

# silent boot: plymouth splash, no kernel/systemd/grub text
sed -i 's/^GRUB_CMDLINE_LINUX_DEFAULT=.*/GRUB_CMDLINE_LINUX_DEFAULT="quiet splash loglevel=3 rd.udev.log_level=3 udev.log_level=3 systemd.show_status=false rd.systemd.show_status=false vt.global_cursor_default=0"/' /etc/default/grub
if ! grep -q "^GRUB_TIMEOUT_STYLE=" /etc/default/grub; then echo "GRUB_TIMEOUT_STYLE=hidden" >> /etc/default/grub; fi
if ! grep -q "^GRUB_TERMINAL=" /etc/default/grub; then echo "GRUB_TERMINAL=console" >> /etc/default/grub; fi
# comment out the "Loading Linux ..." / "Loading initial ramdisk ..." echoes
sed -i "s/^\techo\t'\$(echo \"\$message\" | grub_quote)'/#&/" /etc/grub.d/10_linux
# GRUB's built-in "Booting ..." line can't be turned off, so draw it black on black
echo -e '#!/bin/sh\n# Hide GRUB console text (e.g. the built-in "Booting ..." line) by drawing it black on black\ncat << EOF\nset color_normal=black/black\nEOF' > /etc/grub.d/01_hide_text
chmod 755 /etc/grub.d/01_hide_text

update-grub

if ! grep -q "Haunted-Mansion" /boot/grub/grub.cfg; then
	cat /boot/grub/grub.cfg | sed 's/Debian GNU\/Linux/Haunted-Mansion/' > new_grub
	#diff /boot/grub/grub.cfg new_grub
	#read -p "press enter to continue"
	mv new_grub /boot/grub/grub.cfg
fi

su -c 'mkdir -p /home/norville/.config/sway' norville

cat /etc/sway/config | sed 's/bindsym $mod+Shift+e exec/# bindsym $mod+Shift+e exec/' | sed 's/position top/position bottom\n    mode hide/' | sed 's|^output \* bg .*|output * bg /usr/share/plymouth/themes/spooky/splash.png fill|' > new_sway
# "plymouth quit" finishes the splash handoff started in .profile
echo -e "bindsym \$mod+Shift+e exec swaymsg exit\nseat seat0 hide_cursor 0001\nexec sudo plymouth quit --retain-splash\nexec --no-startup-id /game/code/uptest/main.x86_64" >> new_sway
#diff /etc/sway/config new_sway
#read -p "press enter to continue"
mv new_sway /home/norville/.config/sway/config
chown norville:norville /home/norville/.config/sway/config

mkdir -p /game/update /game/backup /game/log

echo -e "#!/bin/bash\nmount -o remount,rw /" > /usr/sbin/unlock-root
chmod +x /usr/sbin/unlock-root

echo -e "#!/bin/bash\nmount -o remount,ro /" > /usr/sbin/lock-root
chmod +x /usr/sbin/lock-root

mkdir -p /game/NetworkManager
rm -rf /etc/NetworkManager
ln -s /game/NetworkManager /etc/NetworkManager

#autologin:

mkdir -p /etc/systemd/system/getty@tty1.service.d
# no banner/login text, and don't reset/hang up/clear tty1 while plymouth is still showing the splash on it
echo -e "[Service]\nExecStart=\nExecStart=-/usr/sbin/agetty --autologin norville --noclear --noissue --nohostname --skip-login %I \$TERM\nTTYReset=no\nTTYVHangup=no\nTTYVTDisallocate=no" > /etc/systemd/system/getty@tty1.service.d/override.conf
su -c 'touch /home/norville/.hushlogin' norville


#ln -s /game/godot /home/norville/.local/share/godot

if [ ! -d /game/vosk ]; then
	wget https://spookypinball.s3.us-east-2.amazonaws.com/dev/vosk.zip
	if [ $? -ne 0 ]; then echo "wget vosk failed. Exiting."; exit; fi
	unzip vosk.zip
	rm vosk.zip
	rm -rf /game/vosk*
	mv vosk /game/
	mv vosk-env /game/
fi

chown -R norville:norville /game

echo "nameserver 8.8.8.8" > /etc/resolv.conf

if [ ! -f /game/code/uptest/main.x86_64 ]; then
	rm -rf /game/code/uptest
	wget https://spookypinball.s3.us-east-2.amazonaws.com/dev/uptest.zip
	if [ $? -ne 0 ]; then echo "wget uptest failed. Exiting."; exit; fi
	unzip uptest.zip -d /game/code/
	sync
	rm uptest.zip
fi

# .profile ships with the game as _profile (the game also keeps the two in sync)
cp /game/code/uptest/_profile /home/norville/.profile
if [ $? -ne 0 ]; then echo "copy .profile failed. Exiting."; exit; fi
chown norville:norville /home/norville/.profile

#boot splash:

# splash image ships with the game in uptest.zip
mkdir -p /usr/share/plymouth/themes/spooky
cp /game/code/uptest/splash.png /usr/share/plymouth/themes/spooky/
if [ $? -ne 0 ]; then echo "copy splash.png failed. Exiting."; exit; fi
chmod 644 /usr/share/plymouth/themes/spooky/splash.png
echo -e "[Plymouth Theme]\nName=Spooky\nDescription=Spooky Pinball logo on black\nModuleName=script\n\n[script]\nImageDir=/usr/share/plymouth/themes/spooky\nScriptFile=/usr/share/plymouth/themes/spooky/spooky.script" > /usr/share/plymouth/themes/spooky/spooky.plymouth
echo -e 'Window.SetBackgroundTopColor(0, 0, 0);\nWindow.SetBackgroundBottomColor(0, 0, 0);\n\nimage = Image("splash.png");\nimage = image.Scale(Window.GetWidth(), Window.GetHeight());\nsprite = Sprite(image);\nsprite.SetX(Window.GetX());\nsprite.SetY(Window.GetY());\nsprite.SetZ(-100);' > /usr/share/plymouth/themes/spooky/spooky.script
echo -e "[Daemon]\nTheme=spooky\nShowDelay=0\nDeviceTimeout=8" > /etc/plymouth/plymouthd.conf
plymouth-set-default-theme spooky
# plymouth is quit by .profile/sway instead of at the end of boot
systemctl mask plymouth-quit.service plymouth-quit-wait.service
# load i915 early so the splash comes up at native resolution
if ! grep -qx "i915" /etc/initramfs-tools/modules; then echo "i915" >> /etc/initramfs-tools/modules; fi
update-initramfs -u -k all

#convert -size 1920x1080 xc:black /game/code/uptest/splash.png

echo -e "\nalias sound_test='for x in \$(aplay -L | grep :); do echo \$x; aplay -D \$x /usr/share/sounds/alsa/Noise.wav 2>/dev/null; sleep 1; done'" >> /home/norville/.bashrc

echo -e 'pcm.!default {\n     type plug\n     slave {\n               pcm "plughw:PCH,3"\n    }\n}' > /home/norville/.asoundrc

chown -R norville:norville /game /home/norville/.asoundrc
interface="NOT FOUND"
interface=$(grep allow-hotplug /etc/network/interfaces | cut -d ' ' -f 2)
echo "WIRED INTERFACE: $interface"

cat /etc/network/interfaces | sed "s/^allow-hotplug ${interface}/# allow-hotplug ${interface}/" | sed "s/^iface ${interface} inet dhcp/# iface ${interface} inet dhcp/" > new_if
mv new_if /etc/network/interfaces

cd /game
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
if [ $? -ne 0 ]; then echo "get-pip failed. Exiting"; exit; fi
source /game/vosk-env/bin/activate
python get-pip.py
pip install sounddevice
pip install vosk
rm get-pip.py

cd /root
history -c

sync
sync
sleep 10
sync
sync
reboot
