#!/bin/bash

# Custom repository URL
CUSTOM_REPO_URL="http://archive.mirror.derak.cloud/ubuntu/"

# Paths for repository lists
OLD_REPO_LIST="/etc/apt/sources.list"
NEW_REPO_LIST="/etc/apt/sources.list.d/ubuntu.sources"
BACKUP_DIR="/etc/apt/sources.list.d/backup"

# Check if user has sudo privileges
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root or with sudo"
   exit 1
fi

# Create a backup directory for existing sources if it doesn't exist
mkdir -p "$BACKUP_DIR"

# Get the current Ubuntu release version and codename (e.g., focal, jammy, dione/noble)
UBUNTU_VERSION=$(lsb_release -sr)
UBUNTU_CODENAME=$(lsb_release -sc)

# Function to update the repository list for Ubuntu 24.04+ using new format
update_new_format() {
    echo "Detected Ubuntu 24.04 or newer, using new repository file format"

    # Backup the existing sources list file
    if [ -f "$NEW_REPO_LIST" ]; then
        echo "Backing up current repository list to $BACKUP_DIR/ubuntu.sources.bak"
        cp "$NEW_REPO_LIST" "$BACKUP_DIR/ubuntu.sources.bak"
    fi

    # Create a new sources file for Ubuntu 24.04+
    echo "Creating a new repository list for Ubuntu 24.04"
    cat <<EOF > $NEW_REPO_LIST
# Main repository
Types: deb
URIs: $CUSTOM_REPO_URL
Suites: noble noble-updates noble-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

# Security updates
Types: deb
URIs: $CUSTOM_REPO_URL
Suites: noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
EOF

    echo "Repository list updated for Ubuntu 24.04 at $NEW_REPO_LIST"
}

# Function to update the repository list for Ubuntu 20.04/22.04 using old format
update_old_format() {
    echo "Detected Ubuntu $UBUNTU_VERSION ($UBUNTU_CODENAME) - using traditional /etc/apt/sources.list format"

    # Backup the current sources list
    if [ -f "$OLD_REPO_LIST" ]; then
        echo "Backing up current repository list to $BACKUP_DIR/sources.list.bak"
        cp "$OLD_REPO_LIST" "$BACKUP_DIR/sources.list.bak"
    fi

    # Replace the repository list with your custom repository for older versions
    echo "Updating repository list with custom repository"
    echo "deb $CUSTOM_REPO_URL $UBUNTU_CODENAME main restricted universe multiverse" > "$OLD_REPO_LIST"
    echo "deb $CUSTOM_REPO_URL $UBUNTU_CODENAME-updates main restricted universe multiverse" >> "$OLD_REPO_LIST"
    echo "deb $CUSTOM_REPO_URL $UBUNTU_CODENAME-backports main restricted universe multiverse" >> "$OLD_REPO_LIST"
    echo "deb $CUSTOM_REPO_URL $UBUNTU_CODENAME-security main restricted universe multiverse" >> "$OLD_REPO_LIST"

    echo "Repository list updated for Ubuntu $UBUNTU_VERSION at $OLD_REPO_LIST"
}

# Main logic to decide which format to use based on the Ubuntu version
if [[ "$UBUNTU_VERSION" == "24.04" ]]; then
    update_new_format
else
    update_old_format
fi

# Update the package index to reflect the new repositories
echo "Updating package index..."
apt update

# Completion message
echo "Repository list updated successfully to $CUSTOM_REPO_URL"
