pacman -Syu --noconfirm
pacman -Su pyside6 qt6-svg qt6-wayland egl-wayland libbcm2835 qt6-xcb-private-headers python-dbus python-distlib python-pip cage ttf-dejavu sudo networkmanager polkit ntfs-3g dosfstools udisks2 --noconfirm

cat > /etc/pip.conf << EOL
[global]
break-system-packages = true
EOL
pip install python-periphery   


# assume there is only one user on the machine
USERNAME=`id -nu 1000`

systemctl stop 3dprinteros.service

curl -O https://builds.3dprinteros.com/google_qtui.zip
python -m zipfile -e google_qtui.zip /home/$USERNAME/3dprinteros-client/
chown -R $USERNAME:$USERNAME /home/$USERNAME/3dprinteros-client


systemctl disable 3dprinteros.service


usermod -a -G wheel,video,power,network,users,storage $USERNAME
sed -i "s/\#.*\%wheel/\%wheel/" /etc/sudoers
echo "dtoverlay=vc4-fkms-v3d" >> /boot/config.txt


cat > /opt/3dprinteros/usb-automount.service << EOL
[Unit]
Type=simple
Description=USB mass storage Auto Mount

[Service]
ExecStart=/opt/3dprinteros/usb-automount.sh
[Install]
WantedBy = multi-user.target
EOL
systemctl link /opt/3dprinteros/usb-automount.service
systemctl enable usb-automount.service


cat << 'EOL' > /opt/3dprinteros/usb-automount.sh
#!/bin/sh

pathtoname() {
    udevadm info -p /sys/"$1" | awk -F= '/DEVNAME/ {print $2}'
}

stdbuf -oL -- udevadm monitor --udev -s block | while read -r _ _ event devpath _; do
    if [ "$event" = add ]; then
        devname=$(pathtoname "$devpath")
        udisksctl mount --block-device "$devname" --no-user-interaction
    fi
done
EOL
chmod +x /opt/3dprinteros/usb-automount.sh


cat > /etc/pam.d/$USERNAME << EOL
auth           required        pam_unix.so nullok
account        required        pam_unix.so
session        required        pam_unix.so
session        required        pam_systemd.so
EOL

cat << 'EOL' > /opt/3dprinteros/cage@.service
[Unit]
Description=Cage Wayland compositor on %I
# Make sure we are started after logins are permitted. If Plymouth is
# used, we want to start when it is on its way out.
After=systemd-user-sessions.service plymouth-quit-wait.service
# Since we are part of the graphical session, make sure we are started
# before it is complete.
Before=graphical.target
# On systems without virtual consoles, do not start.
ConditionPathExists=/dev/tty0
# D-Bus is necessary for contacting logind, which is required.
Wants=dbus.socket systemd-logind.service
After=dbus.socket systemd-logind.service
# Replace any (a)getty that may have spawned, since we log in
# automatically.
Conflicts=getty@%i.service
After=getty@%i.service

[Service]
Type=simple
ExecStart=/usr/bin/cage -s -d -- python /home/printeros/3dprinteros-client/launcher.py
ExecStartPost=+sh -c "tty_name='%i'; exec chvt $${tty_name#tty}"
Restart=on-failure
User=printeros
Group=printeros
# Log this user with utmp, letting it show up with commands 'w' and
# 'who'. This is needed since we replace (a)getty.
UtmpIdentifier=%I
UtmpMode=user
# A virtual terminal is needed.
TTYPath=/dev/%I
TTYReset=yes
TTYVHangup=yes
TTYVTDisallocate=yes
# Fail to start if not controlling the virtual terminal.
StandardInput=tty-fail

# Set up a full (custom) user session for the user, required by Cage.
PAMName=printeros

[Install]
WantedBy=graphical.target
DefaultInstance=tty7
EOL
sed -i s/printeros/$USERNAME/g /opt/3dprinteros/cage@.service 
systemctl link /opt/3dprinteros/cage@.service
systemctl enable cage@tty7.service
systemctl set-default graphical.target


#systemctl enable NetworkManager
#systemctl start NetworkManager


systemctl start usb-automount.service
systemctl start cage@tty7.service
