#!/bin/bash
#
# Fix Autodiscovery Jobs After RPM Upgrade
# Run this on systems where autodiscovery jobs are stuck "pending" after upgrade
#

echo "Fixing autodiscovery directory permissions..."

# Fix directory permissions
mkdir -p /usr/local/nagiosxi/html/includes/components/autodiscovery/jobs
chown apache:nagios /usr/local/nagiosxi/html/includes/components/autodiscovery/jobs
chmod 775 /usr/local/nagiosxi/html/includes/components/autodiscovery/jobs

echo "Fixing existing job file ownership..."

# Fix ownership of existing job files
cd /usr/local/nagiosxi/html/includes/components/autodiscovery/jobs
for file in *.watch *.out *.xml; do
    if [ -f "$file" ]; then
        chown apache:nagios "$file"
        chmod 664 "$file"
    fi
done 2>/dev/null

echo "Autodiscovery permissions fixed!"
echo ""
echo "Next steps:"
echo "1. Refresh the autodiscovery page in your browser"
echo "2. Try re-running an existing job or creating a new one"
echo "3. Jobs should now execute properly without getting stuck"
