#!/bin/awk -f
# Usage: find / -type f 2>/dev/null | nipkg.awk /var/log/packages/* -
#        don't forget the final "-"

BEGIN {
    FS = OFS = "/"
    cmd="date +%d%b%y-%H%M%S"
    cmd | getline stamp
    LOG = "./nipkgs." stamp
    close(cmd)
    PREVNAME = ""
    IGNORE = " var home dev proc root srv sys tmp mnt "
    MATCH = 0
}
(FILENAME == "-") {
    sub(/^\/+/,"")
    if ((index(IGNORE, " "$1" ") == 0) && ! ($0 in DB)) {
        printf("Found %d unregistered file(s).\r",++MATCH) >"/dev/stderr"
        print >LOG
    }
    next
}
(FILENAME != PREVNAME) {
    PREVNAME = FILENAME
    RECORD = 0
    next
}
(RECORD) {
    ($0 !~ /^(usr\/share\/doc|usr\/share\/man)\//) || sub(/share\//,"") # skip usr/share symlinks
    ($0 ~ /^lib\/incoming\// sub(/incoming\//,"")) # /lib/incoming is /lib
    ($0 ~ /\/$/) || DB[$0] # skip dirs
    next
}
/^FILE LIST:/ {
    RECORD++
    next
}
END {
    close(LOG)
    print "" >"/dev/stderr"
    print "See the details in "LOG >"/dev/stderr"
}
