#!/bin/sh
#
#  Configures the system after files are copied: includes setting permissions, creating symlinks, and optionally registering a systemd service.

set -e

case "$1" in
    configure)
        # Ensure correct permissions on the application directory
        chmod -R 755 /opt/CuttingBoard
        
        # Make the launcher executable (just in case)
        chmod +x /opt/CuttingBoard/launchCuttingBoard.sh
        
        # Create a symlink in /usr/bin for easy execution
        ln -sf /opt/CuttingBoard/launchCuttingBoard.sh /usr/bin/CuttingBoard
        
        # Create a systemd service file (optional but recommended for servers)
#        cat > /etc/systemd/system/CuttingBoard.service <<EOF
#[Unit]
#Description=Java CuttingBoard Application
#After=network.target
#
#[Service]
#Type=simple
#ExecStart=/opt/CuttingBoard/launchCuttingBoardService.sh
#Restart=on-failure
#User=root
#WorkingDirectory=/opt/CuttingBoard
#
#[Install]
#WantedBy=multi-user.target
#EOF

        # Reload systemd daemon to recognize the new service
#        systemctl daemon-reload
        
        # Enable the service to start on boot (optional, depends on policy)
        # systemctl enable CuttingBoard.service
        
        echo "Successfully installed 'CuttingBoard'. Run 'CuttingBoard' to start."
        ;;
    
    abort-upgrade|abort-remove|abort-deconfigure)
        # Handle failed upgrades or removals gracefully
        ;;
esac

exit 0