#!/bin/bash

# Reinstalling GRUB with online check and local fallback mechanism.

echo
echo "Reinstalling Grub..."
echo

# Define the working directory for storing temporary files (ensure it's set).
workdir="${workdir:-/tmp}"  # Default to /tmp if $workdir is not set.

Online=0
file_boolean=0

# Function to check if the system has network connectivity
check_connectivity() {
    local test_ip="8.8.8.8"
    local test_count=1

    echo "Checking network connectivity..."
    if ping -c "$test_count" "$test_ip" > /dev/null 2>&1; then
        echo "System is Online."
        Online=1
    else
        echo "System is Offline."
        Online=0
    fi
}

# Function to check if the required file exists locally
check_file() {
    local file="/usr/local/share/snigdhaos/grub/grub"
    if [[ -f "$file" ]]; then
        echo "$file exists."
        file_boolean=1
    else
        echo "$file doesn't exist."
        file_boolean=0
    fi
}

# Check system connectivity and file existence
check_connectivity
check_file

# Handle online situation: download the default grub config if online
if [ "$Online" -eq 1 ]; then
    echo "Downloading Default Grub..."
    if ! sudo wget -q https://raw.githubusercontent.com/Snigdha-OS/snigdhaos-updater/master/grub -O "$workdir/etc/default/grub"; then
        echo "Failed to download the default grub file! Exiting."
        exit 1
    fi
    echo "Downloaded default grub file successfully."
fi

# Handle offline situation: use local grub file if available
if [ "$Online" -eq 0 ] && [ "$file_boolean" -eq 1 ]; then
    echo "Using local Grub config..."
    if ! sudo cp /usr/local/share/snigdhaos/grub/grub /etc/default/grub; then
        echo "Failed to copy the local grub file! Exiting."
        exit 1
    fi
    echo "Local Grub config copied successfully."
fi

# Handle offline situation with missing local file: notify user
if [ "$Online" -eq 0 ] && [ "$file_boolean" -eq 0 ]; then
    echo "No network connection and local grub file missing."
    echo "Please run this script once you are back online, or ensure the local grub file is available."
    exit 1
fi

# Regenerate the grub configuration
echo "Regenerating Grub configuration..."
if ! sudo grub-mkconfig -o /boot/grub/grub.cfg; then
    echo "Failed to regenerate Grub configuration! Exiting."
    exit 1
fi

echo
echo "Grub reinstallation and configuration completed successfully!"
echo
