#!/bin/bash
set -e

# This script installs, enables, and restarts the AES67 services
# Requires sudo privileges

# Copy system service file for ptp_aes67
sudo cp /home/caster/bumble-auracast/src/service/ptp_aes67.service /etc/systemd/system/ptp_aes67.service

# Copy user service file for pipewire-aes67
mkdir -p /home/caster/.config/systemd/user
cp /home/caster/bumble-auracast/src/service/pipewire-aes67.service /home/caster/.config/systemd/user/pipewire-aes67.service

# Install PipeWire user config to persist
mkdir -p /home/caster/.config/pipewire/pipewire.conf.d
cp /home/caster/bumble-auracast/src/service/pipewire/99-lowlatency.conf /home/caster/.config/pipewire/pipewire.conf.d/99-lowlatency.conf

# Reload systemd to recognize new/updated services
sudo systemctl daemon-reload
systemctl --user daemon-reload

# Enable services to start on boot
sudo systemctl enable ptp_aes67.service
systemctl --user enable pipewire-aes67.service

# Restart services
systemctl --user restart pipewire.service pipewire-pulse.service
systemctl --user restart pipewire-aes67.service
sudo systemctl restart ptp_aes67.service

# print status
sudo systemctl status ptp_aes67.service --no-pager
systemctl --user status pipewire.service --no-pager
systemctl --user status pipewire-aes67.service --no-pager


echo "AES67 services updated, enabled, restarted, and status printed successfully."
