#/bin/bash
# By David PA7LIM - 2026 copyright
# Version 30-march-2026
# This script installs the necessary dependencies for the project
# Check if script is run as bluedv user
# update 20 March 2026: Added alsa-utils as package
#	Added a new QX-18 microphone to the udev rules
#
if [ "$(whoami)" != "bluedv" ]; then
    echo "ERROR: This script must be run as user 'bluedv'"
    echo "Please switch to user bluedv with: su - bluedv"
    echo "Then run this script again."
    exit 1
fi
# Update package lists and upgrade existing packages
sudo apt update; sudo apt upgrade -y
# Install PortAudio and ALSA development libraries
sudo apt install -y portaudio19-dev libasound2-dev alsa-utils
#python3-pip python3-dev

#sudo pip3 install adafruit-circuitpython-ili9341
# Install required SPI and GPIO libraries
#sudo pip3 install adafruit-circuitpython-busio adafruit-circuitpython-digitalio RPi.GPIO

# check if user bluedv exists, if not, give message and cancel installation
if id "bluedv" &>/dev/null; then
    echo "User bluedv exists, continuing installation..."
else
    echo "User bluedv does not exist. Please create the user with 'sudo adduser bluedv' and run this script again."
    exit 1
fi

# enable SPI interface for ILI9341 TFT display
sudo raspi-config nonint do_spi 0

# Configure /boot/config.txt for ILI9341 display
echo "# ILI9341 TFT Display Configuration
dtoverlay=spi0-1cs
gpio=25=op  # RST pin
gpio=24=op  # DC pin
gpio=8=op   # CE0 pin" | sudo tee -a /boot/config.txt

# Create udev rule for the UACDemo microphone
#echo 'SUBSYSTEM=="input", ATTRS{name}=="Jieli Technology UACDemoV1.0 Keyboard", KERNEL=="event*", SYMLINK+="mymic"
#SUBSYSTEM=="input", ATTRS{name}=="RunSheng Technol USB Composite Device Keyboard", KERNEL=="event*", SYMLINK+="mymic"' \
#| sudo tee /etc/udev/rules.d/99-uacdemo.rules

# New rules updated 20 March 2026
sudo tee /etc/udev/rules.d/99-uacdemo.rules >/dev/null <<'EOF'
SUBSYSTEM=="input", ATTRS{name}=="Jieli Technology UACDemoV1.0 Keyboard", KERNEL=="event*", SYMLINK+="mymic"
SUBSYSTEM=="input", ATTRS{name}=="UACDemoV1.0 UACDemoV1.0 Keyboard", KERNEL=="event*", SYMLINK+="mymic"
SUBSYSTEM=="input", ATTRS{name}=="RunSheng Technol USB Composite Device Keyboard", KERNEL=="event*", SYMLINK+="mymic"
EOF

# Reload udev rules to apply the new rule
sudo udevadm control --reload-rules

# Download main program and unzip it in /usr/local/bluedv.
wget https://software.pa7lim.nl/BlueDV-Radio/bluedv-radio-linux-full.tar.gz -O /tmp/main_program.gz
# remove old installation if exists
sudo rm -rf /usr/local/bluedv
# Make new installation directory
sudo mkdir -p /usr/local/bluedv

sudo chown bluedv:bluedv /usr/local/bluedv
# Extract the downloaded program into the installation directory

tar xvfz /tmp/main_program.gz -C /usr/local/bluedv
# Set execute permissions on the main program
sudo chmod +x /usr/local/bluedv/bluedv-radio

# create service file to start the program at boot. Check for staged updates in /tmp/bluedvupdate before starting
echo '[Unit]
Description=BlueDV Radio Service
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=bluedv
Group=bluedv
WorkingDirectory=/usr/local/bluedv

# Check if update is staged in /tmp/bluedvupdate and apply it
ExecStartPre=/bin/bash -c "if [ -f /tmp/bluedvupdate/bluedv-radio ]; then cp /tmp/bluedvupdate/bluedv-radio /usr/local/bluedv/bluedv-radio && chmod +x /usr/local/bluedv/bluedv-radio && rm -rf /tmp/bluedvupdate; fi"

# Start BlueDV
ExecStart=/usr/local/bluedv/bluedv-radio

Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target' | sudo tee /etc/systemd/system/bluedv.service
# Reload systemd to recognize the new service
sudo systemctl daemon-reload
# Enable the service to start on boot
sudo systemctl enable bluedv.service
#
echo "Installation complete."
echo "Make sure you insert your AMBE stick and microphone/Speakers before restarting your Raspberry Pi."
echo "To restart, type 'sudo reboot'."
