#!/bin/sh

testpkg(){
    apt-cache showpkg "$1" |
        perl -ne 'if(/^([a-z]+):/i){$s=$1}elsif($s eq"Versions"&&/^[^\s]/){while(/\(([^\)]+)\)/g){push(@s,$1)}}END{exit((!grep{$_ ne"/var/lib/dpkg/status"}(@s))&&grep{$_ eq"/var/lib/dpkg/status"}(@s))}'
}

dpkg-query --show --showformat '${db:Status-Abbrev}${Package}\n' | while read line; do
    if echo "$line" | grep -q ^i; then
        testpkg "${line#???}" || echo "${line#???}"
    fi
done