#!/bin/bash

# Get the list of scheduled tasks from crontab
crontab_list=$(crontab -l 2>/dev/null)

if [ -z "$crontab_list" ]; then
    echo "No crontab entries found."
    exit 1
fi

# Extract script paths ending with ".sh" from crontab entries and check their executability
non_executable_files=()
while IFS= read -r line; do
    script_path=$(echo "$line" | grep -oE '/[^ ]+\.sh\b')
    
    if [ -n "$script_path" ]; then
        if [ -f "$script_path" ]; then
            if [ -x "$script_path" ]; then
                printf "%-100s %s\n" "$script_path" "is executable."
            else
                printf "%-100s %s\n" "$script_path" "is found but not executable."
                non_executable_files+=("$script_path")
            fi
        else
            printf "%-100s %s\n" "$script_path" "does not exist."
        fi
    fi
done <<< "$crontab_list"

# Prompt user to change permissions of non-executable files
if [ ${#non_executable_files[@]} -gt 0 ]; then
    read -p "Do you want to change permissions of the non-executable files? (yes/no): " answer
    if [ "$answer" = "yes" ]; then
        for file in "${non_executable_files[@]}"; do
            chmod +x "$file"
            echo "Changed permissions of $file to executable."
        done
    else
        echo "No permissions were changed."
    fi
fi
