Some packages are in multiple categories (one example is devel/libgetopt). This broke the IF statement because ${CATEGORIES} got expanded to "cat1 cat2". The proper variable to use is PKGPATH.