#!/bin/bash
if [ -z $CI ]; then
    echo "Only run in CI"
    exit 1
fi

cd "${0%/*}/.."
ROOT="$(pwd)"
TMP="${ROOT}/.tmp"
CACHE="${TMP}/cache"
PACMAN_EXTRA="$*"

# Prepare directories
sudo rm -rf "$ROOT"/{out,out2,out.SHA512}
sudo install -o nobody -d "$ROOT"/{out,out2,out-debug}
sudo install -o nobody -d "$TMP"{,/pkgs,/ignore,/aur}
sudo install -o nobody -d "$CACHE"/srcdest

# Restore pacman cache if exists
if [ -d "$CACHE"/pkgcache ]; then
    sudo cp "$CACHE"/pkgcache/* /var/cache/pacman/pkg
fi
if [ -d "$CACHE"/pkgdb ]; then
    sudo cp "$CACHE"/pkgdb/* /var/lib/pacman/sync
fi

# Update pacman cache
echo "Updating package databases..."
sudo pacman -Sy
echo "Initializing pacman keyring"
sudo pacman-key --init
echo "Updating pacman keyring..."
sudo pacman -S --noconfirm --needed archlinux-keyring
echo "Updating installed and installing extra packages..."
sudo pacman -Syyu --noconfirm git pacman-contrib $PACMAN_EXTRA
echo "Cleaning shared cache..."
sudo paccache -rk1

# some helpers
enable_pkgs() {
    MSG="$1"; shift
    for _pkg in $*; do
        echo " ${_pkg}${MSG:+: $MSG}" > "$TMP"/pkgs/$_pkg
        rm -f "$TMP"/ignore/$_pkg
    done
}

disable_pkgs() {
    MSG="$1"; shift
    for _pkg in $*; do
        echo " ${_pkg}${MSG:+: $MSG}" > "$TMP"/ignore/$_pkg
        rm -f "$TMP"/pkgs/$_pkg
    done
}

get_pkgs() {
    (cd "$TMP"/pkgs && ls -1)
}

final() {
    echo "Cleaning up ..."

    # fetch all dependencies to pacman cache
    export PACKAGES="$(get_pkgs)"
    export SRCDEST="$CACHE"/srcdest
    "$ROOT"/tools/syncdeps-all.sh $MAKEPKG_ARGS

    # Save pacman cache
    sudo mkdir -p "$CACHE"/pkgdb
    sudo cp -r /var/lib/pacman/sync/* "$CACHE"/pkgdb

    # print report
    echo -e "\nPackages to skip:"
    cat "$TMP"/ignore/* 2>/dev/null
    echo -e "\nPackages to build:"
    cat "$TMP"/pkgs/* 2>/dev/null
    return 0
}

cd "$ROOT"/packages
ALLPKGS="$(ls -1)"
enable_pkgs "new" $ALLPKGS
trap final EXIT

source /etc/makepkg.conf

# Fetch current database
cd "$ROOT"/out
echo "Downloading proaudio package database from arch.osamc.de..."
curl -fO --progress-bar "https://arch.osamc.de/proaudio/${CARCH}/proaudio.db.tar.gz"
cd "$ROOT"/packages

# Flag packages to ignore during build

# Ignore unchanged packages
for PKG in $ALLPKGS; do
    if sudo git diff --quiet $DRONE_COMMIT_BEFORE..$DRONE_COMMIT_AFTER ./$PKG; then
        disable_pkgs "unchanged" $PKG
    fi
done

# Ignore packages where the current version is already in the repo
echo "Examining package database to determine which package(s) to build..."
if [[ -f "$ROOT"/out/proaudio.db.tar.gz ]]; then
    DB_PKGS=($(bsdtar -xOf "$ROOT"/out/proaudio.db.tar.gz '*/desc' | sed -n '/^%FILENAME%$/ {n;p}'))
    for PKG in $(get_pkgs); do
        IGN=true

        if [[ ! -f "$PKG/PKGBUILD" ]]; then
            echo "Directory $PKG does not contain a PKGBUILD file"
            continue
        fi

        PKGFILES=($(makepkg -D $PKG --packagelist MAKEPKG_LINT_PKGBUILD=0))
        # FIXME: only remove -debug when it is a suffix. A package name like "my-debug-machine" should be kept.
        for PKGPATH in "${PKGFILES[@]/\-debug}"; do
            PKGFILE=$(basename $PKGPATH)
            if [[ ! " ${DB_PKGS[@]} " =~ " $PKGFILE " ]]; then
                IGN=false
            fi
        done
        if $IGN; then
            disable_pkgs "current version already in repo" $PKG
        fi
    done
fi

# If a single package is provided in the PACKAGE variable, ignore all others
if [[ -n "$PACKAGE" && -e $PACKAGE ]]; then
    disable_pkgs "not selected" $ALLPKGS
    enable_pkgs "building single selected package" $PACKAGE
    exit 0
fi

# ignore packages that don't support our architecture
for PKG in $(get_pkgs); do
    ARCH="$(. "$PKG"/PKGBUILD; echo "${arch[@]}")"
    if [[ "$ARCH" != "any" && ! " $ARCH " =~ "$CARCH" ]]; then
        disable_pkgs "unsupported architecture" $PKG
    fi
done
