#! /bin/bash
#
# Executed after cron.unchecked

set -e
set -o pipefail
set -u

export SCRIPTVARS=/srv/security-master.debian.org/dak/config/debian-security/vars
. $SCRIPTVARS
DISTS="oldoldstable oldstable oldstable-kfreebsd stable testing"

if [ -e $ftpdir/Archive_Maintenance_In_Progress ]; then
    exit 0
fi

dists=
now=$(date +%s)
check=$(( now - 3*60 ))

for dist in $DISTS; do
    smodtime=$(stat -c "%Y" $base/build-queues/dists/buildd-$dist/updates/*/source/Sources.gz | sort -n | tail -1)
    pmodtime=$(stat -c "%Y" $base/build-queues/dists/buildd-$dist/updates/*/binary-*/Packages.gz | sort -n | tail -1)

    if [ ${smodtime} -gt ${check} ] || [ ${pmodtime} -gt ${check} ]; then
        # Packages/Sources changed in the last minutes
        dists="${dists} ${dist}"
    else
        continue
    fi
done

if [ ! -z "${dists}" ]; then
    for d in ${dists}; do
        case ${d} in
            oldoldstable)
                send=wheezy
                ;;
            oldstable)
                send=jessie
                ;;
            oldstable-kfreebsd)
                send=jessie-kfreebsd
                ;;
            stable)
                send=stretch
                ;;
            testing)
                send=buster
                ;;
            *)
                send=unknown
                ;;
        esac

        ssh -n wbadm@buildd trigger.security $send
    done
fi
