#!/bin/bash

Main()
{
    # Create endeavouros-mirrorlist.offline for offline install.
    # Prefer mirrors that work well worldwide.
    #
    # Assumptions:
    #  - official /etc/pacman.d/endeavouros-mirrorlist exists
    #  - the format of country comments is static and unique, e.g.:
    #      ## Germany

    # Note: the original mirrorlist can be either
    #  - not ranked (then it includes mirror definitions in alphabetical country order)
    #  - ranked (is OK as-is)

    local mirrorlist="/etc/pacman.d/endeavouros-mirrorlist"
    local offline="${mirrorlist}.offline-install"

    if [ "$(grep -w "eos-rankmirrors" "$mirrorlist")" ] ; then
        # a ranked list exists
        cp "$mirrorlist" "$offline"
    else
        # a non-ranked list, prefer German mirrors
        printf "# EndeavourOS mirrorlist for offline install only.\n\n" > "$offline"
        sed -n '/^## Germany/,/^$/p' "$mirrorlist"  >> "$offline"
        sed -n '/^## [A-Z].*/,$p'    "$mirrorlist"  >> "$offline"
    fi

}

Main "$@"
