#!/bin/bash
. /etc/default/trigger-gitlab-on-apt-repo-update

trigger=

mkdir -p "$STORAGE_DIR"

for distro in debian ubuntu; do
    shafile="$STORAGE_DIR/$distro.sha256sums"

    echo "running wget for $distro"
    wget --quiet -r --level 2 --accept="Release" --reject="InRelease" --directory-prefix="$STORAGE_DIR/" "ftp://mirror.bytemark.co.uk/$distro/dists/"

    [ -f "$shafile" ] || sha256sum "$STORAGE_DIR/mirror.bytemark.co.uk/$distro/dists/"*"/Release" > "$shafile"

    if ! sha256sum --quiet -c "$shafile"; then
        echo "some $distro repos have changed."
        trigger="yes"
        sha256sum "$STORAGE_DIR/mirror.bytemark.co.uk/$distro/dists/"*"/Release" > "$shafile"
    else
        echo "nothing's changed with $distro"
    fi
done

if [ -n "$trigger" ]; then
    echo "triggering builds"
    for project in $PROJECTS; do
        curl -X POST -F token=$TOKEN -F ref=master $GITLAB/api/v4/projects/$project/trigger/pipeline
    done
else
    echo "not triggering builds"
fi
