#!/bin/bash

file="/etc/rc.local"
line_to_modify="/opt/scripts/auto-config/auto-config"
manual=$1

# Check if the file exists and is readable
if [ -f "$file" ] && [ -r "$file" ]; then
    # Check if the line is present and determine if it needs to be commented or uncommented
    if grep -qF "$line_to_modify" "$file"; then
        if [ "$manual" = "true" ] && ! grep -qF "#$line_to_modify" "$file"; then
            # Manual is active, and the line is not already commented; comment the line
            sudo sed -i "s|$line_to_modify|#$line_to_modify|" "$file"
            echo "Line commented."
        elif [ "$manual" = "false" ] && grep -qF "#$line_to_modify" "$file"; then
            # Manual is not active, and the line is commented; uncomment the line
            sudo sed -i "s|#$line_to_modify|$line_to_modify|" "$file"
            echo "Line uncommented."
        else
            echo "No changes needed."
        fi
    else
        echo "Line not found."
    fi
else
    echo "File not found or not readable."
fi
