#!/bin/sh

. autorepo-config

LANG=C
export LANG

# for get_apt_conf_for_task
. autorepo-apt-sh-functions

check_target()
{
    local target
    target=$1
    difffile=$AUTOREPO_HOME/unmets.diff.$target
    rm -f "$difffile"
    get_apt_conf_for_task $target check
    aptconfremote="$aptconf_for_task"
    get_apt_conf_for_task $target apt
    aptconflocal="$aptconf_for_task"

    TOPDIR=`mktemp -d --tmpdir autorepo.XXXXXXXXXX`
    touch $TOPDIR/autorepo-repo-unmets.$target
    WORKDIR2=$TOPDIR/WD2
    mkdir $WORKDIR2
    mkaptbox --apt-config=$aptconflocal $WORKDIR2
    $WORKDIR2/aptbox/apt-cache unmet > $TOPDIR/unmets.new
    if [ -s "$TOPDIR/unmets.new" ]; then
	if [ "$aptconflocal" == "$aptconfremote" ]; then
	    cat "$TOPDIR/unmets.new" > "$difffile"
	else
	    WORKDIR1=$TOPDIR/WD1
	    mkdir $WORKDIR1
	    mkaptbox --apt-config=$aptconfremote $WORKDIR1
	    $WORKDIR1/aptbox/apt-cache unmet > $TOPDIR/unmets.old
	    diff -U0 $TOPDIR/unmets.{old,new} > "$difffile"
	fi
    fi
    rm -rf "$TOPDIR"
    if [ -e "$difffile" ]; then
	autorepo-filter-unmets-diff "$difffile" | tee "$difffile".0
	mv "$difffile".0 "$difffile"
	[ -s "$difffile" ] || rm -f "$difffile"
    fi
}

for arch in $GB_ARCH; do
    check_target $arch
done
