#!/bin/bash

__UTAH_SCRIPT_DESCRIPTION="Test script for scheduler.cron implementation"
echo "Setting up scheduled task..."
# Create Utah cron directory
_utah_cron_dir="$HOME/.utah/cron"
mkdir -p "${_utah_cron_dir}"
_utah_cron_script="${_utah_cron_dir}/job_$(date +%s)_$$.sh"

# Generate the cron job script
cat > "${_utah_cron_script}" << 'EOF'
#!/bin/bash
# Generated by Utah - scheduler.cron("0 */6 * * *")
echo "Running every 6 hours..."
echo "Performing maintenance tasks..."
EOF

# Make the script executable
chmod +x "${_utah_cron_script}"

# Check if similar cron job already exists
_utah_cron_pattern="0 \*/6 \* \* \*.*utah.*job_"
if ! crontab -l 2>/dev/null | grep -q "${_utah_cron_pattern}"; then
    # Add to crontab
    (crontab -l 2>/dev/null; echo "0 */6 * * * ${_utah_cron_script}") | crontab -
    echo "Cron job installed: 0 */6 * * * ${_utah_cron_script}"
else
    echo "Similar cron job already exists"
fi
# Create Utah cron directory
_utah_cron_dir="$HOME/.utah/cron"
mkdir -p "${_utah_cron_dir}"
_utah_cron_script="${_utah_cron_dir}/job_$(date +%s)_$$.sh"

# Generate the cron job script
cat > "${_utah_cron_script}" << 'EOF'
#!/bin/bash
# Generated by Utah - scheduler.cron("0 0 * * *")
echo "Daily maintenance"
echo "Cleaning up temporary files..."
EOF

# Make the script executable
chmod +x "${_utah_cron_script}"

# Check if similar cron job already exists
_utah_cron_pattern="0 0 \* \* \*.*utah.*job_"
if ! crontab -l 2>/dev/null | grep -q "${_utah_cron_pattern}"; then
    # Add to crontab
    (crontab -l 2>/dev/null; echo "0 0 * * * ${_utah_cron_script}") | crontab -
    echo "Cron job installed: 0 0 * * * ${_utah_cron_script}"
else
    echo "Similar cron job already exists"
fi
# Create Utah cron directory
_utah_cron_dir="$HOME/.utah/cron"
mkdir -p "${_utah_cron_dir}"
_utah_cron_script="${_utah_cron_dir}/job_$(date +%s)_$$.sh"

# Generate the cron job script
cat > "${_utah_cron_script}" << 'EOF'
#!/bin/bash
# Generated by Utah - scheduler.cron("0 2 * * 1")
echo "Weekly report generation"
reportDate="Monday Report"
echo "Generating: ${reportDate}"
EOF

# Make the script executable
chmod +x "${_utah_cron_script}"

# Check if similar cron job already exists
_utah_cron_pattern="0 2 \* \* 1.*utah.*job_"
if ! crontab -l 2>/dev/null | grep -q "${_utah_cron_pattern}"; then
    # Add to crontab
    (crontab -l 2>/dev/null; echo "0 2 * * 1 ${_utah_cron_script}") | crontab -
    echo "Cron job installed: 0 2 * * 1 ${_utah_cron_script}"
else
    echo "Similar cron job already exists"
fi
echo "Scheduler setup complete!"
