#!/bin/sh

. autorepo-config
branch=$AUTOREPO_BRANCH
aptconfremote=

LANG=C
export LANG

get_remote_apt_conf()
{
    local target conffile
    target=$1
    aptconfremote=
    for conffile in \
    "$AUTOREPO_LOCAL_APT_DIR/check.conf.$branch.$target" \
    "$AUTOREPO_LOCAL_APT_DIR/check.conf.$target" \
    "$AUTOREPO_GLOBAL_APT_DIR/apt.conf.$branch.$target" \
	; do
	if [ -e "$conffile" ]; then
	    aptconfremote="$conffile"
	    return 0
	fi
    done
}

check_target()
{
    local target
    target=$1
    tmp=$TMPDIR
    difffile=$AUTOREPO_HOME/unmets.diff.$target
    rm -f "$difffile"
    get_remote_apt_conf $target
    aptconflocal="$AUTOREPO_LOCAL_APT_DIR/apt.conf.$target"
    WORKDIR1=$tmp/WD1
    WORKDIR2=$tmp/WD2

    cleanup_tmpdir()
    {
	rm -f $tmp/unmets.{old,new}
	rm -rf $WORKDIR1 $WORKDIR2
    }

    cleanup_tmpdir
    mkdir -p $WORKDIR1 $WORKDIR2
    mkaptbox --apt-config=$aptconflocal $WORKDIR2
    $WORKDIR2/aptbox/apt-cache unmet > $tmp/unmets.new
    if [ -s "$tmp/unmets.new" ]; then
	mkaptbox --apt-config=$aptconfremote $WORKDIR1
	$WORKDIR1/aptbox/apt-cache unmet > $tmp/unmets.old
	diff -U0 $tmp/unmets.{old,new} | tee "$difffile"
    fi
    cleanup_tmpdir
    [ -s "$difffile" ] || rm -f "$difffile"
}

check_target x86_64
check_target i586
