#!/bin/zsh -feu

# See if the source package should be recompiled, to produce deb packages.
# possibly limited to a `reprepro' distro.

# todo:
# if only 1 reprepro has it, I'm fine. don't list the others.

usage()
{
    setopt POSIX_ARGZERO
    cat <<EOF
usage: ${0##*/} [+-hdi] [-r repository][--] source-package
-d     debug
-r     repository/distribution
-i     installed
EOF
    unsetopt POSIX_ARGZERO
}

if [[ -z ${REPREPRO_BASE_DIR-} ]]; then
    cecho red "REPREPRO_BASE_DIR undefined"
    exit 1
fi

# todo:
# I should take the `suite' from the debian/changelog.
# and GIT obviously.
reprepro-distros()
{
    db5.3_dump -l $REPREPRO_BASE_DIR/db/release.caches.db
}

reprepro-packages()
{
    distro=$1
    ARCH=$2
    echo $REPREPRO_BASE_DIR/dists/$distro/main/binary-$ARCH/Packages
}


# Given a file which can be queried by grep-available(1),
# compare the given @git-sha and the one of the package inside (its dgit field).
function check_packages_file()
{
    local srcpkg=$1
    local file=$2
    local sha=$3
    # grep Dgit /home/michal/reprepro/dists/gauche-new/main/binary-amd64/Packages
    # -FSource:Package  ...... hey what is that?
    # for reprepro there is alternative:
    #         reprepro listfilter gauche-new '$Source (==gauche)'
    # if [ $DEBUG = y ]; then set -x;fi
    command=(grep-available --field=Source:Package --show-field=Package,Version,Dgit \
             --exact-match $srcpkg $file)
    if [ $DEBUG = y ]; then set +x;fi

    # set -x
    output=$(eval $command) || :
    # echo $output >&2
    version_sha=$(echo $output |sed -n  -e  '/Dgit: /s///p' )

    if [ $DEBUG = y ]; then echo $version_sha >&2; fi
    if [ -n "$version_sha" -a "$version_sha" = $sha ]; then
        return 0
    fi
    # found=$status
    # not found => exit 5
    if [ -n "$version_sha" ]; then
        cecho yellow "dgit: $version_sha"
    fi
    [ $DEBUG = y ] && echo $output >&2
    return 1
}

ARCH=$(dpkg-architecture --query DEB_BUILD_ARCH)
DEBUG=n
#file=()
typeset -a distros
distros=()
while getopts :adhir: OPT; do
    case $OPT in
        h|+h)
            usage
            exit 0
            ;;
        i)
            distros=("installed")
            ;;
        r)
            distros+=($OPTARG)
            ;;
        d)
            DEBUG=y
            ;;
	a)
	    distros=("available")
	    ;;
        *)
            usage
            exit 2
    esac
done
shift OPTIND-1
OPTIND=1

if [ $# -lt 1 ]; then
    srcpkg=$(deb-pkg-name .)
else
    srcpkg=$1;
    shift
fi

if [[ -z $distros ]]; then
    distros=( $(reprepro-distros) )
fi

sha=$(git rev-parse HEAD)
[ $DEBUG = y ] && cecho red "git: $sha $srcpkg" >&2
#set -x

foreach distro ($distros) {
    [ $DEBUG = y ] && cecho blue "looking at $distro, searching for $srcpkg" >&2
    if [ $distro = "installed" ]; then
        file=/var/lib/dpkg/status
        # /var/lib/dpkg/available
    elif [ $distro = "available" ]; then
	file=/var/lib/dpkg/available
    else
            file=$(reprepro-packages $distro $ARCH)
    fi

    if check_packages_file $srcpkg $file $sha; then
        cecho green "found identical"
        exit 0
    fi
    # found=$status
    # not found => exit 5
}
# compare with the local/git version:
exit 1
# return yes/no  = 0 1
