#!/bin/bash

usage()
{
    echo
    echo "Count deb-packages in Cacus and local FS"
    echo
    echo "usage: countdebs <repo>"
    echo
    exit 1
}

BRANCHES="stable prestable testing unstable"
ARCHES="i386 amd64 all"
REPO="$1"

if [ "x$REPO" == "x" ]; then
    echo "<repo> cannot be empty. Abort..."
    usage
fi

for branch in $BRANCHES; do
    echo -e -n "$REPO $branch:  \tC="
    unset PACKAGES
    for arch in $ARCHES; do
        PACKAGES=$(($PACKAGES+$(show-packages-in-cacus $REPO $branch $arch | grep Filename: | awk -F/ '{ print $NF }' | sort -u | wc -l)))
    done
    echo -e -n "$PACKAGES"
    echo -e -n "\tL="
    LOCAL_PACKAGES=$(find /repo/$REPO/$branch -name "*deb" | wc -l)
    echo $LOCAL_PACKAGES

    if [ $(($LOCAL_PACKAGES-$PACKAGES)) -gt 20 ]; then
        echo
        echo "Malformed packages in Cacus: $REPO/$branch"
        EXIT_CODE="NONZERO"
    fi
done

if [ "$EXIT_CODE" == "NONZERO" ]; then
    exit 3
fi
