#!/usr/bin/bash

#set -x  # Enable debug mode

# Delete previous log files
> unreachable.txt
> failure.txt
> success.txt


# Check if iplist.txt exists and no arguments are provided, then set the log file accordingly
if [ -f "iplist.txt" ] && [ $# -eq 0 ]; then
    exec > iplist.log 2>&1
    #echo "Redirecting output to iplist.log"
fi

# Set the time zone to Central Time
export TZ="America/Chicago"

date
echo ________________________________________
echo

echo "Initializing"

# Function to process each IP
process_ip() {
    local ip="$1"
    echo "PINGING $ip"

    # Ping the IP and check if it's reachable
    if ping -c 1 -W 1 "$ip" &> /dev/null; then
        echo "$ip is reachable"
        MAIN "$ip"
    else
        echo "$ip" >> unreachable.txt
        echo "$ip is unreachable. Skipping..."
    fi
}


# Function to run each command and log output

run_command() {
    local command=$1
    echo "Running: $command"

    # Execute the command and capture both output and error
    #output=`$command`
    output=$(eval $command)
    #output=$(eval $command 2>&1)
    echo $output

    # Return exit status
    return $?
}

# Function to end the main process and log success or failure
end_main() {
    local ip="$1"
    if [[ $success -eq 1 ]]; then
        echo "$ip" >> success.txt
        echo "$ip - Process completed successfully"
	echo
	echo
    else
        echo "$ip" >> failure.txt
        echo "$ip - Process encountered errors"
	echo
	echo
    fi
}








# Main function to connect and execute adb commands
MAIN() {
    local ip="$1"
    echo "CONNECTING to $ip"
    adb disconnect &> /dev/null

    success=1

    # Execute predefined adb commands
    run_command "adb connect $ip:5555" || { success=0; end_main "$ip"; return; }
    run_command "adb root" || { success=0; end_main "$ip"; return; }
    run_command "adb connect $ip:5555" || { success=0; end_main "$ip"; return; }
    run_command "adb remount" || { success=0; end_main "$ip"; return; }

       # Read all lines from commands.txt into an array
    mapfile -t commands < commands.txt

    
       # Execute each command in the array
    for command in "${commands[@]}"; do
        # Skip empty lines
        if [[ -z "$command" ]]; then
            continue
        fi

        # Execute the command using the run_command function
        run_command "$command"
        if [[ $? -ne 0 ]]; then
            echo "Error executing command: $command. Exiting..."
            break  # Stop execution if a command fails
        fi
    done


    end_main "$ip"
}


# Function to process each IP
process_ip() {
    local ip="$1"
    echo "PINGING $ip"

    # Ping the IP and check if it's reachable
    if ping -c 1 -W 1 "$ip" &> /dev/null; then
        echo "$ip is reachable"
        MAIN "$ip"
    else
        echo "$ip" >> unreachable.txt
        echo "$ip is unreachable. Skipping..."
    fi
}


# Check if commands.txt exists
if [[ ! -f "commands.txt" ]]; then
    echo "Error: commands.txt not found. Please create commands.txt with the commands to execute."
    exit 1
fi

# Check if an argument is provided or if iplist.txt exists
if [[ -z "$1" ]]; then
    if [[ ! -f "iplist.txt" || ! -s "iplist.txt" ]]; then
        echo "Error: No IP address provided and iplist.txt does not exist or is empty."
        echo "Please provide an IP address as an argument or create iplist.txt."
        exit 1
    else

        mapfile -t ips < iplist.txt
        for ip in "${ips[@]}"; do
        process_ip "$ip"
        done
    fi
else
    # Argument provided, use it as the IP
    ip="$1"
    process_ip "$ip"
fi

exit 0

