#!/bin/bash

# Function to check internet connectivity
check_connectivity() {
    local test_ip="8.8.8.8"
    local test_count=1

    # Try to ping a known IP address to check if the system is online
    if ping -c "$test_count" "$test_ip" > /dev/null 2>&1; then
        echo -e "\nSystem is Online!\n"
        Online=1
    else
        echo -e "\nSystem is Offline! Unable to download snigdhaos-keyring.\n"
        Online=0
    fi
}

# Run the connectivity check
check_connectivity

# Install snigdhaos-keyring if the system is online
if [ "$Online" -eq 1 ]; then
    echo -e "\nInstalling snigdhaos-keyring...\n"
    if ! sudo pacman -Sy snigdhaos-keyring --noconfirm; then
        echo -e "\nError: Failed to install snigdhaos-keyring.\n"
        exit 1
    fi
fi

# Clean up previous pacman sync databases
echo -e "\nRemoving previous pacman databases at /var/lib/pacman/sync/*\n"
if ! sudo rm -rf /var/lib/pacman/sync/*; then
    echo -e "\nError: Failed to remove pacman sync databases.\n"
    exit 1
fi

# Remove previous GNUPG folder
echo -e "\nRemoving previous /etc/pacman.d/gnupg folder\n"
if ! sudo rm -rf /etc/pacman.d/gnupg/*; then
    echo -e "\nError: Failed to remove pacman gnupg folder.\n"
    exit 1
fi

# Initialize pacman keys
echo -e "\nInitializing pacman keys...\n"
if ! sudo pacman-key --init; then
    echo -e "\nError: Failed to initialize pacman keys.\n"
    exit 1
fi

# Populate the keyring
echo -e "\nPopulating keyring...\n"
if ! sudo pacman-key --populate archlinux; then
    echo -e "\nError: Failed to populate the keyring.\n"
    exit 1
fi

# Add the Ubuntu keyserver for extra key fetching
echo -e "\nAdding Ubuntu keyserver...\n"
echo "keyserver hkp://keyserver.ubuntu.com:80" | sudo tee --append /etc/pacman.d/gnupg/gpg.conf > /dev/null

# Synchronize the database
echo -e "\nSynchronizing Database...\n"
if ! sudo pacman -Sy; then
    echo -e "\nError: Failed to synchronize pacman database.\n"
    exit 1
fi

# Completion message
echo -e "\nTask Completed!\n"
