# === SYSTEM SETUP ===
sudo apt update && sudo apt install -y \
  pulseaudio pulseaudio-utils pavucontrol \
  libgtk-3-dev gtk-3-examples \
  xauth dbus-x11 \
  samba

# === SAMBA CONFIGURATION ===
sudo nano /etc/samba/smb.conf
# Add at the end of the file:
# [RaspberryPiNAS]
#    path = /home/pi/Public
#    comment = Pi Share
#    browseable = yes
#    writeable = yes
#    guest ok = no
#    valid users = pi

# Set permissions
chmod 755 /home/pi
mkdir -p /home/pi/Public
chmod 777 /home/pi/Public
chown -R pi:pi /home/pi/Public

# Set Samba password for pi
sudo smbpasswd -a pi

# Restart Samba
sudo systemctl restart smbd

# === PULSEAUDIO SYSTEM-WIDE SERVICE ===
sudo nano /etc/systemd/system/pulseaudio.service
# Paste this content:
# [Unit]
# Description=PulseAudio System-wide Daemon
# After=sound.target network.target
#
# [Service]
# Type=simple
# ExecStart=/usr/bin/pulseaudio --system --disallow-exit --disallow-module-loading=0 --daemonize=no
# Restart=always
#
# [Install]
# WantedBy=multi-user.target

# === PULSEAUDIO MODULES FOR LEXICON LAMBDA USB ===
sudo nano /etc/pulse/system.pa
# Comment out:
# load-module module-udev-detect
# load-module module-detect
# Add this at the bottom:
# load-module module-alsa-sink device=hw:1,0 sink_name=LambdaOutput sink_properties=device.description="Lexicon_Lambda_USB_Output"
# load-module module-alsa-source device=hw:1,0 source_name=LambdaInput source_properties=device.description="Lexicon_Lambda_USB_Input"
# load-module module-native-protocol-unix auth-anonymous=1 socket=/tmp/pulseaudio.socket

# Set client default socket
sudo nano /etc/pulse/client.conf
# Add this line:
# default-server = unix:/tmp/pulseaudio.socket

# Enable and start PulseAudio system-wide
sudo systemctl daemon-reload
sudo systemctl enable pulseaudio.service
sudo systemctl restart pulseaudio.service

# === X11 & GUI FIXES FOR SSH/MOBAXTERM ===
sudo nano /etc/ssh/sshd_config
# Ensure these lines are present:
# X11Forwarding yes
# X11UseLocalhost no
# XAuthLocation /usr/bin/xauth

# Restart SSH
sudo systemctl restart ssh

# Ensure xauth is installed
sudo apt install -y xauth

# === BASHRC: MAKE GUI EXPORTS PERSISTENT ===
nano ~/.bashrc
# Add this at the end:
# export GDK_BACKEND=x11
# export LIBGL_ALWAYS_INDIRECT=1
# export NO_AT_BRIDGE=1
# export $(dbus-launch)

# Reload bashrc immediately
source ~/.bashrc

# === TESTING GUI ===
# (Reconnect via MobaXterm SSH with X11 forwarding enabled before running below)

# Run pavucontrol GUI
pavucontrol &

# Optional: Test GTK GUI rendering
gtk3-demo &

# Check audio devices
pactl list sinks short
pactl list sources short
