#!/bin/bash

# Detect chroot/container (any type) and skip pacman-mirrors configuration
if [[ -f /.manjaro-chroot ]] || [[ -f /.manjaro-tools ]]; then
    echo ":: [chroot/container detected] Skipping pacman-mirrors configuration"
    else
        if [ -f /etc/pacman-mirrors.conf.pacsave ]; then
            echo "Your current config has been restored."
            echo "A new pacman-mirrors.conf is created as /etc/pacman-mirrors-new-$(date +%Y%m%d).conf"
            echo "Please verify you're not missing any changes"
            mv /etc/pacman-mirrors.conf /etc/pacman-mirrors-new-$(date +%Y%m%d).conf
            mv /etc/pacman-mirrors.conf.pacsave /etc/pacman-mirrors.conf
        fi

        if [ -f /etc/pacman.d/mirrorlist ]; then
            # fetch new data files
            pacman-mirrors --no-mirrorlist
        else
            # create mirrorlist
            pacman-mirrors -f5
        fi
    echo "Hint: Use \`pacman-mirrors\` to generate and update your pacman mirrorlist."
fi
