#!/bin/bash

echo -n $( dirname -- "$( readlink -f -- "$0" )" ) > pwd.txt

CWD=$(cat pwd.txt)

# packages=$(cat packages.txt)
packages=$(pacman -Qqn | sort)

for package in ${packages}
do
    echo "$package"
    if [ -d "$package" ]; then
        cd "$CWD/$package"
        git pull
        cd "$CWD"
        sleep 3
    else
        access_location=$(curl -ILs https://gitlab.archlinux.org/archlinux/packaging/packages/${package} | grep location | awk '{print $2}')
        if [[ "$access_location" == *"gitlab.archlinux.org/users"* ]]; then
            echo ""
        else
            git clone "https://gitlab.archlinux.org/archlinux/packaging/packages/${package}.git"
        fi
        sleep 5
    fi
done

echo "Ready"
