#!/bin/bash
# File: /var/www/moduleair_pro_4g/services/setup_matrix_services.sh
# Purpose: Set up LED matrix boot display service (runs once at boot)
# to install: 
#   sudo chmod +x /var/www/moduleair_pro_4g/services/setup_matrix_services.sh
#   sudo /var/www/moduleair_pro_4g/services/setup_matrix_services.sh

echo "Setting up moduleair boot display service..."

# Create directory for logs if it doesn't exist
mkdir -p /var/www/moduleair_pro_4g/logs


# Create service file for LED Matrix Boot Display (one-shot at boot)
cat > /etc/systemd/system/moduleair-matrix-boot.service << 'EOL'
[Unit]
Description=moduleair LED Matrix Boot Display Service (runs once)
After=network.target
After=multi-user.target

[Service]
Type=oneshot
ExecStart=/var/www/moduleair_pro_4g/matrix/imageScreen/image_split_boot /var/www/moduleair_pro_4g/matrix/imageScreen/ModuleAir128x64.png
User=root
WorkingDirectory=/var/www/moduleair_pro_4g/matrix/imageScreen/
StandardOutput=append:/var/www/moduleair_pro_4g/logs/matrix_boot_service.log
StandardError=append:/var/www/moduleair_pro_4g/logs/matrix_boot_service_errors.log
TimeoutStartSec=30s
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
EOL

# Compile the boot version of the program
echo "Compiling boot version of image_split..."
cd /var/www/moduleair_pro_4g/matrix/imageScreen/

# Make sure we have the boot version source file
if [ ! -f "image_split_boot.cc" ]; then
    echo "Creating image_split_boot.cc from provided code..."
    # The code will be created separately as image_split_boot.cc
    echo "Please save the boot version code as image_split_boot.cc first"
    exit 1
fi

# Compile the boot version
g++ -I/var/www/moduleair_pro_4g/matrix/include -L/var/www/moduleair_pro_4g/matrix/lib \
    /var/www/moduleair_pro_4g/matrix/imageScreen/image_split_boot.cc \
    -o /var/www/moduleair_pro_4g/matrix/imageScreen/image_split_boot \
    -lrgbmatrix `GraphicsMagick++-config --cppflags --libs`

# Check if compilation was successful
if [ ! -f "image_split_boot" ]; then
    echo "Compilation failed! Please check dependencies and try again."
    exit 1
fi

# Make sure the matrix executable has proper permissions
echo "Setting permissions for LED matrix boot executable..."
chmod +x /var/www/moduleair_pro_4g/matrix/imageScreen/image_split_boot

# Reload systemd to recognize new service
systemctl daemon-reload

# Enable the boot service
echo "Enabling boot display service..."
systemctl enable moduleair-matrix-boot.service

echo "Setup complete!"
echo ""
echo "LED Matrix Boot Display will:"
echo "  - Run once at boot after network and multi-user target"
echo "  - Show 3-second split reveal animation"
echo "  - Display image for 5 seconds"
echo "  - Exit automatically (total ~8 seconds)"
echo ""
echo "To test the service manually:"
echo "  sudo systemctl start moduleair-matrix-boot.service"
echo ""
echo "To check the status:"
echo "  sudo systemctl status moduleair-matrix-boot.service"
echo ""
echo "To view logs:"
echo "  sudo journalctl -u moduleair-matrix-boot.service"
echo "  tail -f /var/www/moduleair_pro_4g/logs/matrix_boot_service.log"
echo ""
echo "To disable boot display:"
echo "  sudo systemctl disable moduleair-matrix-boot.service"
echo ""
echo "Note: The old repeating matrix display timer has been disabled."
echo "If you want to re-enable it, run the original setup_services.sh script."