<commit_before>--- portmaster.orig	2012-01-27 19:10:47.000000000 +0100
+++ portmaster	2012-01-31 03:07:29.361097824 +0100
@@ -292,7 +292,13 @@
 			 unset -v PM_URB_LIST;
 			 $PM_SU_CMD /usr/bin/nice /usr/bin/make $PM_MAKE_ARGS $*; ); }
 pm_mkdir_s        () { $PM_SU_CMD /bin/mkdir -p $1; }
-pm_pkg_delete_s   () { $PM_SU_CMD /usr/sbin/pkg_delete $*; }
+pm_pkg_delete_s   () { 
+	if [ -z "$use_pkgng" ]; then
+		$PM_SU_CMD /usr/sbin/pkg_delete $*;
+	else
+		$PM_SU_CMD /usr/local/sbin/pkg delete -y $*;
+	fi
+}
 pm_rm_s           () { $PM_SU_CMD /bin/rm $*; }
 pm_rmdir_s        () { $PM_SU_CMD /bin/rmdir $*; }
 pm_unlink_s       () { [ -e "$1" ] && $PM_SU_CMD /bin/unlink $1; }
@@ -496,12 +502,16 @@
 
 	pattern=`globstrip $1`
 
-	glob_dirs=`find $pdb -maxdepth 1 -type d -name ${pattern}\*`
+	if [ -z "$use_pkgng" ]; then
+		glob_dirs=`find $pdb -maxdepth 1 -type d -name ${pattern}\*`
+	else
+		glob_dirs=`pkg query -g "%n-%v" ${pattern}\*`
+	fi
 	case "$glob_dirs" in
 	# Match a newline in multiple responses from find
 	*'
 '*)		return 2 ;;
-	$pdb/*)	return ;;
+	*)	return ;;
 	esac
 
 	unset glob_dirs
@@ -511,8 +521,12 @@
 origin_from_pdb () {
 	local o
 
-	o=`grep -m1 '@comment ORIGIN:' $pdb/$1/+CONTENTS 2>/dev/null` && {
-		echo ${o#@comment ORIGIN:}; return 0; }
+	if [ -z "$use_pkgng" ]; then
+		o=`grep -m1 '@comment ORIGIN:' $pdb/$1/+CONTENTS 2>/dev/null` && {
+			echo ${o#@comment ORIGIN:}; return 0; }
+	else
+		pkg query "%o" $1; return 0;
+	fi
 
 	case "$1" in bsdpan-*) return 3 ;; esac
 
@@ -666,6 +680,7 @@
 	o)	REPLACE_ORIGIN=oopt ;;
 	p)	fail 'The -p option has been deprecated' ;;
 	r)	PM_URB=ropt
+		#TODO
 		if [ -d "$pdb/$OPTARG" ]; then
 			glob_dirs=$OPTARG
 		else
@@ -818,7 +833,12 @@
 		fi
 		unset INDEXFILE INDEXDIR
 
-		PM_INDEX_PORTS=`pkg_version -Ivl\< $PM_INDEX | cut -f1 -d\<`
+		if [ -z "$use_pkgng" ]; then
+			pkg_version="pkg_version"
+		else
+			pkg_version="pkg version"
+		fi
+		PM_INDEX_PORTS=`$pkg_version -Ivl\< $PM_INDEX | cut -f1 -d\<`
 		export PM_INDEX_PORTS
 
 		if [ -z "$pd" -o "$pd" != /usr/ports ]; then
@@ -875,6 +895,10 @@
 iport_from_origin () {
 	local sn dir
 
+	if [ -n "$use_pkgng" ]; then
+		pkg info -q ${1} || return 1
+		return
+	fi
 	sn=${1#*/} ; sn=${sn%-*} ; sn=${sn%%[0-9]*}
 
 	if ! dir=`grep -l "@comment ORIGIN:${1}$" $pdb/${sn}*/+CONTENTS 2>/dev/null`; then
@@ -903,6 +927,7 @@
 	done
 }
 
+# useless with pkgng
 check_dependency_files () {
 	# Global: grep_deps
 	local origin iport ro_opd
@@ -985,6 +1010,7 @@
 	fi
 }
 
+# also useless with pkgng
 update_contents () {
 	local IFS delete contents origin n_port old_origin iport new_cont
 	local o_seen line d_missing d_origin d_iport prev_line answer
@@ -1129,35 +1155,62 @@
 	echo '############' > $DI_FILES		# Make the file > 0 bytes
 	echo "===>>> Gathering distinfo list for installed ports"
 	echo ''
-	for pkg in ${pdb}/*; do
-		[ -d $pkg ] || continue
-		iport=${pkg#$pdb/}
+	if [ -z "$use_pkgng" ]; then
+		for pkg in ${pdb}/*; do
+			[ -d $pkg ] || continue
+			iport=${pkg#$pdb/}
 
-		origin=`origin_from_pdb $iport` || continue
+			origin=`origin_from_pdb $iport` || continue
 
-		if [ ! -d "$pd/$origin" ]; then
-			find_moved_port $origin $iport nonfatal >/dev/null
-			[ -n "$moved_npd" ] || continue
-			origin=$moved_npd
-		fi
+			if [ ! -d "$pd/$origin" ]; then
+				find_moved_port $origin $iport nonfatal >/dev/null
+				[ -n "$moved_npd" ] || continue
+				origin=$moved_npd
+			fi
 
-		origin="${pd}/${origin}"
+			origin="${pd}/${origin}"
 
-		if [ -s "${origin}/distinfo" ]; then
-			distinfo="${origin}/distinfo"
-		else
-			pm_cd $origin || continue
-			distinfo=`pm_make -V DISTINFO_FILE`
-			[ -n "$distinfo" ] || fail "No DISTINFO_FILE in $origin"
-		fi
+			if [ -s "${origin}/distinfo" ]; then
+				distinfo="${origin}/distinfo"
+			else
+				pm_cd $origin || continue
+				distinfo=`pm_make -V DISTINFO_FILE`
+				[ -n "$distinfo" ] || fail "No DISTINFO_FILE in $origin"
+			fi
 
-		if [ -s "$distinfo" ]; then
-			grep '^SHA256 ' $distinfo | while read disc1 f disc2; do
-				f=${f#(} ; f=${f%)}
-				echo $f >> $DI_FILES
-			done
-		fi
-	done
+			if [ -s "$distinfo" ]; then
+				grep '^SHA256 ' $distinfo | while read disc1 f disc2; do
+					f=${f#(} ; f=${f%)}
+					echo $f >> $DI_FILES
+				done
+			fi
+		done
+	else
+		pkg query -a "%n-%v %o" | while read pkg origin; do
+			if [ ! -d "$pd/${origin}" ]; then
+				find_moved_port $origin $pkg nonfatal >/dev/null
+				[ -n "$moved_npd" ] || continue
+				origin=$moved_npd
+			fi
+
+			origin="${pd}/${origin}"
+
+			if [ -s "${origin}/distinfo" ]; then
+				distinfo="${origin}/distinfo"
+			else
+				pm_cd $origin || continue
+				distinfo=`pm_make -V DISTINFO_FILE`
+				[ -n "$distinfo" ] || fail "No DISTINFO_FILE in $origin"
+			fi
+
+			if [ -s "$distinfo" ]; then
+				grep '^SHA256 ' $distinfo | while read disc1 f disc2; do
+					f=${f#(} ; f=${f%)}
+					echo $f >> $DI_FILES
+				done
+			fi
+		done
+	fi
 
 	# Tell safe_exit that we are done
 	pm_unlink ${DI_FILES}-e
@@ -1206,6 +1259,24 @@
 	local pkg
 	pm_v "===>>> Sorting ports by category"
 
+	if [ -n "$use_pkgng" ]; then
+		pkg query -a "%n-%v %?d %?r" | while read pkg dep rdep; do
+			if [ $rdep -eq 0 ]; then
+				if [ $dep -eq 0 ]; then
+					branches="$branches ${pkg}"
+				else
+					trunks="$trunks ${pkg}"
+				fi
+			else
+				if [ $dep -eq 0 ]; then
+					leaves="$leaves ${pkg}"
+				else
+					roots="$roots ${pkg}"
+				fi
+			fi
+		done
+		return
+	fi
 	for pkg in $pdb/*; do
 		if [ -s "$pkg/+REQUIRED_BY" ]; then
 			if grep -ql '^@pkgdep ' $pkg/+CONTENTS 2>/dev/null; then
@@ -1272,6 +1343,7 @@
 update_required_by () {
 	# Global: grep_deps
 	local do_update
+	[ -n "$use_pkgng" ] && /bin/unlink $grep_deps && unset grep_deps
 
 	if [ -e "$pdb/$1/+REQUIRED_BY" ]; then
 		sort $pdb/$1/+REQUIRED_BY | cmp -s $grep_deps - || do_update=do_update
@@ -1323,12 +1395,19 @@
 	init_packages_var
 
 	echo "===>>> Checking for stale packages"
+	local ext
+	ext=tbz
+	[ -n "$use_pkgng" ] && ext=txz
 	for package in `find $PACKAGES -type f | sort`; do
-		pkg_dir=${package##*/} ; pkg_dir=${pkg_dir%\.tbz} ; echo ''
+		pkg_dir=${package##*/} ; pkg_dir=${pkg_dir%\.${ext}} ; echo ''
 
-		origin=`tar -O -zxvf $package '+CONTENTS' 2>/dev/null | grep '@comment ORIGIN:'` ||
-			fail Empty origin in $package
-		origin=${origin#@comment ORIGIN:}
+		if [ -z "$use_pkgng" ]; then
+			origin=`tar -O -zxvf $package '+CONTENTS' 2>/dev/null | grep '@comment ORIGIN:'` ||
+				fail Empty origin in $package
+			origin=${origin#@comment ORIGIN:}
+		else
+			origin=`pkg query "%o" $package` || fail Empty origin in $package
+		fi
 
 		if [ -z "$PM_INDEX" ]; then
 			if [ -d "$pd/$origin" ]; then
@@ -1344,8 +1423,21 @@
 		fi
 
 		if [ -n "$port_ver" ]; then
-			if [ "${port_ver}.tbz" = "${package##*/}" ]; then
+			if [ "${port_ver}.${ext}" = "${package##*/}" ]; then
 				echo "===>>> ${package##*/} is up to date"
+				if [ -n "$use_pkgng" ]; then
+					if pkg info -e ${pkg_dir}; then
+						echo "	===>>> $pkg_dir is not installed"
+						echo "	===>>> Path: ${package}"
+						get_answer_yn y "\n\t===>>> Delete stale package: ${package##*/}"
+						case "$?" in
+							0)	echo "	===>>> Deleting $package"
+								pm_unlink_s $package ;;
+						esac
+					fi
+					unset port_ver
+					continue
+				fi
 				if [ ! -d "${pdb}/${pkg_dir}" ]; then
 					echo "	===>>> $pkg_dir is not installed"
 					echo "	===>>> Path: ${package}"
@@ -1406,6 +1498,7 @@
 fi	# [ -n "$CLEAN_PACKAGES" ]
 
 if [ -n "$CHECK_DEPENDS" ]; then
+	[ -n "$use_pkgng" ] && exit 0 # this is useless with pkgng"
 	PM_VERBOSE=pmv_check_depends
 IFS='
 '
@@ -1446,6 +1539,7 @@
 fi
 
 if [ -n "$CHECK_PORT_DBDIR" ]; then
+	[ -n "$use_pkgng" ] && exit 0 # useless with pkgng
 	[ -d "$port_dbdir" ] ||
 		fail 'PORT_DBIR is empty, or the directory $port_dbdir does not exist'
 
@@ -1610,14 +1704,19 @@
 				unset port_ver
 			fi
 		else
-			case `pkg_version -t $iport $port_ver` in
+			if [ -z "$use_pkgng" ]; then
+				pkg_version="pkg_version"
+			else
+				pkg_version="pkg version"
+			fi
+			case `$pkg_version -t $iport $port_ver` in
 			\<)	do_update=upd_lt ;;
 			=)	;;	# Can be reached if same version with different options
 			\>)	if [ -n "$PM_VERBOSE" ]; then
 					echo "	===>>> Port version $port_ver does not"
 					echo "	===>>> seem newer than installed $iport"
 				fi ;;
-			*)	fail "pkg_version -t $iport $port_ver gave an unexpected result"
+			*)	fail "$pkg_version -t $iport $port_ver gave an unexpected result"
 			esac
 
 			[ -z "$do_update" ] && {
@@ -1689,7 +1788,13 @@
 	fi
 
 	pm_cd $pkgdir || fail "Cannot cd into $pkgdir to create a package"
-	if $PM_SU_CMD pkg_create -b $2; then
+	local pkg_create
+	if [ -z "$use_pkgng" ]; then
+		pkg_create="pkg_create -b"
+	else
+		pkg_create="pkg create "
+	fi
+	if $PM_SU_CMD $pkg_create $2; then
 		if [ "$1" = "$pbu" ]; then
 			if [ -n "$BACKUP" ]; then
 				echo "	===>>> Package saved to $1" ; echo ''
@@ -2028,19 +2133,28 @@
 fi
 
 if [ -n "$EXPUNGE" ]; then
-	if [ ! -d "$pdb/$EXPUNGE" ]; then
-		find_glob_dirs $EXPUNGE
-		case $? in
-		1)	fail "No such directory/port: $pdb/$EXPUNGE" ;;
-		2)	echo "===>>> $EXPUNGE matched multiple ports"
-			fail "The -e option works with only one port at a time" ;;
-		0)	EXPUNGE=${glob_dirs#$pdb/}
-			unset glob_dirs ;;
-		esac
+	if [ -z "$use_pkgng" ]; then
+		if [ ! -d "$pdb/$EXPUNGE" ]; then
+			find_glob_dirs $EXPUNGE
+			case $? in
+			1)	fail "No such directory/port: $pdb/$EXPUNGE" ;;
+			2)	echo "===>>> $EXPUNGE matched multiple ports"
+				fail "The -e option works with only one port at a time" ;;
+			0)	EXPUNGE=${glob_dirs#$pdb/}
+				unset glob_dirs ;;
+			esac
+		fi
+	else
+		pkg info -e $EXPUNGE || fail "No such port: $EXPUNGE"
+		EXPUNGE=`pkg query "%n-%v" $EXPUNGE`
 	fi
 
 	origin=`origin_from_pdb $EXPUNGE`
-	deplist=`grep -l DEPORIGIN:$origin$ $pdb/*/+CONTENTS`
+	if [ -z "$use_pkgng" ]; then
+		deplist=`grep -l DEPORIGIN:$origin$ $pdb/*/+CONTENTS`
+	else
+		deplist=`pkg query "%rn-%rv" $origin`
+	fi
 	if [ -n "$deplist" ]; then
 		echo "===>>> Warning: Ports with dependencies on ${EXPUNGE}:"
 		for dep in $deplist; do
@@ -2048,7 +2162,8 @@
 		done
 		get_answer_yn n "\n\t===>>> Delete this dependency data"
 		case "$?" in
-		0)	for f in $deplist; do
+		0)	[ -n "$use_pkgng" ] && exit 0
+			for f in $deplist; do
 				update_contents delete $f $origin
 			done ;;
 		*)	exit 1 ;;
@@ -2058,8 +2173,13 @@
 	[ -n "$BACKUP" ] && { init_packages ; pm_pkg_create $pbu $EXPUNGE; }
 	[ -z "$DONT_SCRUB_DISTFILES" ] && { delete_all_distfiles $origin; delete_dist_list; }
 
-	echo "===>>> Running pkg_delete -f $EXPUNGE"
-	pm_pkg_delete_s -f $EXPUNGE || fail 'pkg_delete failed'
+	if [ -z "$use_pkgng" ]; then
+		pkg_delete="pkg_delete"
+	else
+		pkg_delete="pkg delete"
+	fi
+	echo "===>>> Running $pkg_delete -f $EXPUNGE"
+	pm_pkg_delete_s -f $EXPUNGE || fail "$pkg_delete failed"
 
 	echo '' ; echo "===>>> Running ${0##*/} -s $ARGS"
 	exec $0 -s $ARGS
@@ -2069,13 +2189,22 @@
 if [ -n "$CLEAN_STALE" ]; then
 	[ -z "$no_del_list" ] && export no_del_list=':'
 
-	for file in `find $pdb -type f -name \+REQUIRED_BY -empty` ; do
+	#TODO: will use pkg autoremove when it works
+	if [ -z "$use_pkgng" ]; then
+		find_stale_ports="find $pdb -type f -name \+REQUIRED_BY -empty"
+	else
+		find_stale_ports="pkg query -a \"%?r %n-%v\" | awk '/^0/ { print \$2 }'"
+	fi
+	for file in `eval $find_stale_ports` ; do
 		iport="${file%/+REQUIRED_BY}" ; iport=${iport#$pdb/}
 
 		case "$no_del_list" in *:${iport}:*) continue ;; esac
 
 		origin=`origin_from_pdb $iport`
-		deplist=`grep -l DEPORIGIN:$origin$ $pdb/*/+CONTENTS`
+		deplist=""
+		if [ -z "$use_pkgng" ]; then
+			deplist=`grep -l DEPORIGIN:$origin$ $pdb/*/+CONTENTS`
+		fi
 		if [ -n "$deplist" ]; then
 			echo ''
 			echo "===>>> Warning: Unrecorded dependencies on ${iport}:"
@@ -2088,22 +2217,32 @@
 			continue
 		fi
 
-		pkg_info $iport
+		if [ -z "$use_pkgng" ]; then
+			pkg_info $iport
+			pkg_delete="pkg_delete"
+		else
+			pkg info -f $iport
+			pkg_delete="pkg delete"
+		fi
 
 		get_answer_yn n "\t===>>> ${iport} is no longer depended on, delete"
 		case "$?" in
 		0)	[ -n "$BACKUP" ] && { init_packages ; pm_pkg_create $pbu $iport; }
 			[ -z "$DONT_SCRUB_DISTFILES" ] && { delete_all_distfiles $origin; delete_dist_list; }
 
-			echo "===>>> Running pkg_delete -f $iport"
-			pm_pkg_delete_s -f $iport || fail 'pkg_delete failed'
+			echo "===>>> Running $pkg_delete -f $iport"
+			pm_pkg_delete_s -f $iport || fail "$pkg_delete failed"
 
 			exec $0 -s $ARGS ;;
-		*)	get_answer_yn n "\t===>>> Delete this dependency data"
-			case "$?" in
-			0)	pm_unlink_s $file ;;
-			*)	no_del_list="${no_del_list}${iport}:" ;;
-			esac ;;
+		*)	if [ -z "$use_pkgng" ]; then
+				get_answer_yn n "\t===>>> Delete this dependency data"
+				case "$?" in
+				0)	pm_unlink_s $file ;;
+				*)	no_del_list="${no_del_list}${iport}:" ;;
+				esac
+			else
+				no_del_list="${no_del_list}${iport}:"
+			fi ;;
 		esac
 	done
 	exit 0
@@ -2243,7 +2382,12 @@
 			fail "No entry for $origin in $PM_INDEX"
 	fi
 
-	case `pkg_version -t $iport $new_port 2>/dev/null` in
+	if [ -z "$use_pkgng" ]; then
+		pkg_version="pkg_version"
+	else
+		pkg_version="pkg version"
+	fi
+	case `$pkg_version -t $iport $new_port 2>/dev/null` in
 	\<)	build_l="${build_l}\tUpgrade $iport to $new_port\n" ;;
 	=)	build_l="${build_l}\tRe-install $iport\n" ;;
 	\>)	build_l="${build_l}\tDowngrade $iport to $new_port\n" ;;
@@ -2466,7 +2610,11 @@
 				fail "Cannot cd to $d_port"
 			fi
 			for glob in $conflicts; do
-				confl_p=`pkg_info -I $glob 2>/dev/null`
+				if [ -z "$use_pkgng" ]; then
+					confl_p=`pkg_info -I $glob 2>/dev/null`
+				else
+					confl_p=`pkg query -g "%n-%v" $glob 2>/dev/null`
+				fi
 				if [ -n "$confl_p" ]; then
 					confl_p=${confl_p%% *}
 					echo ''
@@ -2601,6 +2749,10 @@
 		done
 
 		for dep in $build_only_dl_g; do
+			if [ -n "$use_pkgng" ]; then
+				[ "`pkg query "%?r" $dep`" = "0" ] || temp_bodlg="$temp_bodlg $dep"
+				continue
+			fi
 			grep -q "@comment DEPORIGIN:${dep#$pd/}$" $pdb/*/+CONTENTS ||
 				temp_bodlg="$temp_bodlg $dep"
 		done
@@ -2635,7 +2787,14 @@
 	echo "===>>> Begin $verb ports that depend on $PM_URB_IPORTS"
 	echo ''
 
+	#TODO
 	for origin in $PM_URB_ORIGINS; do
+		if [ -n "$use_pkgng" ]; then
+			for req_by in `pkg query "%rn-%rv" ${origin}`; do
+				PM_URB_LIST="${PM_URB_LIST} ${req_by}"
+			done
+			continue
+		fi
 		for req_by in `grep -l DEPORIGIN:${origin}$ $pdb/*/+CONTENTS`; do
 			req_by="${req_by%/+CONTENTS}"
 			req_by="${req_by##*/}"
@@ -2806,9 +2965,16 @@
 		*/*)	origin=$port ;;
 		*)	# If an installed version does not exist at this
 			# point it probably got updated as a dependency
-			if [ ! -d "$pdb/$port" ]; then
-				numports=$(( $numports - 1 ))
-				continue
+			if [ -z "$use_pkgng" ]; then
+				if [ ! -d "$pdb/$port" ]; then
+					numports=$(( $numports - 1 ))
+					continue
+				fi
+			else
+				if ! pkg info -e $port; then
+					numports=$(( $numports - 1 ))
+					continue
+				fi
 			fi
 			origin=`origin_from_pdb $port` ;;
 		esac
@@ -3036,7 +3202,12 @@
 	/*)	echo '' ; no_valid_port ;;
 	*/*)	portdir=$argv ;;
 	\.)	portdir=${PWD##*/ports/} ;; # Not always $pd, could be symlink
-	*)	[ -d "$pdb/$argv" ] && upg_port=$argv ;;
+	*)	if [ -z "$use_pkgng" ]; then
+			[ -d "$pdb/$argv" ] && upg_port=$argv
+		else
+			pkg info -e $argv && upg_port=`pkg query "%n-%v" $argv`
+		fi
+		;;
 	esac
 
 	if [ -z "$portdir" -a -z "$upg_port" ]; then
@@ -3069,11 +3240,20 @@
 
 	case "$arg2" in
 	*/*)	ro_opd=$arg2 ; ro_upg_port=`iport_from_origin $ro_opd` ;;
-	*)	if [ -d "$pdb/$arg2" ]; then
-			ro_upg_port=$arg2
+	*)	if [ -z "$use_pkgng" ]; then
+			if [ -d "$pdb/$arg2" ]; then
+				ro_upg_port=$arg2
+			else
+				find_glob_dirs $arg2 && ro_upg_port=${glob_dirs#$pdb/}
+				unset glob_dirs
+			fi
 		else
-			find_glob_dirs $arg2 && ro_upg_port=${glob_dirs#$pdb/}
-			unset glob_dirs
+			if pkg info -e $arg2; then
+				ro_upg_port=$arg2
+			else
+				find_glob_dirs $arg2 && ro_upg_port=${glob_dirs}
+				unset glob_dirs
+			fi
 		fi
 		[ -n "$ro_upg_port" ] && ro_opd=`origin_from_pdb $ro_upg_port`
 	esac
@@ -3525,7 +3705,12 @@
 		pm_v "===>>> Available package ($latest_pv) matches the current version"
 	elif [ -n "$latest_pv" -a -n "$PM_PACKAGES_NEWER" ]; then
 		if [ -n "$upg_port" ]; then
-			case `pkg_version -t $upg_port $latest_pv` in
+			if [ -z "$use_pkgng" ]; then
+				pkg_version="pkg_version"
+			else
+				pkg_version="pkg version"
+			fi
+			case `$pkg_version -t $upg_port $latest_pv` in
 			\<)	use_package=up_newer
 				pm_v "===>>> Available package ($latest_pv)"
 				pm_v "       is newer than installed ($upg_port)" ;;
@@ -3541,7 +3726,12 @@
 			pm_v "===>>> There is a package available ($latest_pv)"
 		fi
 	elif [ -n "$latest_pv" ]; then
-		case `pkg_version -t $new_port $latest_pv` in
+		if [ -z "$use_pkgng" ]; then
+			pkg_version="pkg_version"
+		else
+			pkg_version="pkg version"
+		fi
+		case `$pkg_version -t $new_port $latest_pv` in
 		\<)	# Could happen if ports tree is out of date
 			use_package=up_old_tree
 			pm_v "===>>> Available package ($latest_pv)"
@@ -3640,7 +3830,12 @@
 		    grep -v ^$LOCALBASE_COMPAT > $ldconfig_out
 
 		unset temp
-		for file in `pkg_info -q -L $UPGRADE_PORT |
+		if [ -z "$use_pkgng" ]; then
+			pkglist="pkg_info -q -L"
+		else
+			pkglist="pkg query %Fp"
+		fi
+		for file in `$pkglist $UPGRADE_PORT |
 		    sort - $ldconfig_out | uniq -d`; do
 			temp="${temp}$file "
 		done
@@ -3743,29 +3938,32 @@
 	fi
 fi
 
-for file in $preserve_port_files; do
-	mv $file ${file}-new
-	mv ${preserve_dir}/${file##*/} $file
-	oldmd5="MD5:`md5 -q $file`"
-
-	new_cont=`pm_mktemp contents`
-	while read left right; do
-		case "$left" in
-		@cwd)		short_file="${file#${right}/}" ;;
-		$short_file)	found_it=found_it ; continue;;
-		@comment)	if [ -n "$found_it" ]; then
-					echo -e "${short_file}-new\n$left $right"
-					echo -e "$short_file\n@comment $oldmd5"
-					unset found_it
-					continue
-				fi ;;
-		esac
-		echo "$left $right"
-	done < $pdb/$new_port/+CONTENTS > $new_cont
-	pm_install_s $new_cont $contents
-	pm_unlink $new_cont
-	unset file oldmd5 new_cont left right short_file
-done
+#TODO
+if [ -z "$use_pkgng" ]; then
+	for file in $preserve_port_files; do
+		mv $file ${file}-new
+		mv ${preserve_dir}/${file##*/} $file
+		oldmd5="MD5:`md5 -q $file`"
+
+		new_cont=`pm_mktemp contents`
+		while read left right; do
+			case "$left" in
+			@cwd)		short_file="${file#${right}/}" ;;
+			$short_file)	found_it=found_it ; continue;;
+			@comment)	if [ -n "$found_it" ]; then
+						echo -e "${short_file}-new\n$left $right"
+						echo -e "$short_file\n@comment $oldmd5"
+						unset found_it
+						continue
+					fi ;;
+			esac
+			echo "$left $right"
+		done < $pdb/$new_port/+CONTENTS > $new_cont
+		pm_install_s $new_cont $contents
+		pm_unlink $new_cont
+		unset file oldmd5 new_cont left right short_file
+	done
+fi
 if [ -n "$preserve_dir" ]; then
 	rmdir $preserve_dir 2>/dev/null
 	unset preserve_dir preserve_port_files
@@ -3781,7 +3979,12 @@
 temp=`find $LOCALBASE_COMPAT -type d -empty 2>/dev/null`
 if [ -z "$temp" ] && [ -d "$LOCALBASE_COMPAT" ]; then
 	unset files
-	for file in `pkg_info -q -L $new_port`; do
+	if [ -z "$use_pkgng" ]; then
+		pkglist="pkg_info -q -L"
+	else
+		pkglist="pkg query %Fp"
+	fi
+	for file in `$pkglist $new_port`; do
 		[ -f "${LOCALBASE_COMPAT}/${file##*/}" ] &&
 			files="${files}${LOCALBASE_COMPAT}/${file##*/} "
 	done
@@ -3844,7 +4047,8 @@
 	/bin/unlink $dist_list_temp ; unset ds dist_list_temp
 fi
 
-if [ -n "$use_package" ]; then
+# pkgng does not need this
+if [ -n "$use_package" -a -z "$use_pkgng" ]; then
 	if grep -q DEPORIGIN $pdb/$new_port/+CONTENTS; then
 		echo "===>>> Updating dependencies for $new_port to match installed versions"
 		update_contents $pdb/$new_port/+CONTENTS
@@ -3879,27 +4083,29 @@
 	echo ''
 fi
 
-check_dependency_files $portdir $new_port
-if [ -s "$grep_deps" ]; then
-	echo "===>>> Updating dependency entry for $new_port in each dependent port"; pm_v
-	while read d_port; do
-		pm_v "===>>> $d_port"
-		dp_cont=$pdb/$d_port/+CONTENTS
-		[ -e "$dp_cont" ] || continue
-
-		if [ -n "$ro_opd" ]; then
-			grep -ql "DEPORIGIN:$ro_opd$" $dp_cont &&
-				update_contents $dp_cont $portdir $new_port $ro_opd
-		fi
-		# Do this one last so it can get deleted as a duplicate
-		# if ro_opd is present.
-		if grep -ql "DEPORIGIN:$portdir$" $dp_cont; then
-			update_contents $dp_cont $portdir $new_port
-		fi
-	done < $grep_deps
-	unset d_port dp_cont do_update
+if [ -z $use_pkgng ]; then
+	check_dependency_files $portdir $new_port<commit_msg>Fix listing of installed ports by categories<commit_after>--- portmaster.orig	2012-01-31 03:17:17.681285114 +0100
+++ portmaster	2012-01-31 04:36:56.231159190 +0100
@@ -292,7 +292,13 @@
 			 unset -v PM_URB_LIST;
 			 $PM_SU_CMD /usr/bin/nice /usr/bin/make $PM_MAKE_ARGS $*; ); }
 pm_mkdir_s        () { $PM_SU_CMD /bin/mkdir -p $1; }
-pm_pkg_delete_s   () { $PM_SU_CMD /usr/sbin/pkg_delete $*; }
+pm_pkg_delete_s   () { 
+	if [ -z "$use_pkgng" ]; then
+		$PM_SU_CMD /usr/sbin/pkg_delete $*;
+	else
+		$PM_SU_CMD /usr/local/sbin/pkg delete -y $*;
+	fi
+}
 pm_rm_s           () { $PM_SU_CMD /bin/rm $*; }
 pm_rmdir_s        () { $PM_SU_CMD /bin/rmdir $*; }
 pm_unlink_s       () { [ -e "$1" ] && $PM_SU_CMD /bin/unlink $1; }
@@ -496,12 +502,16 @@
 
 	pattern=`globstrip $1`
 
-	glob_dirs=`find $pdb -maxdepth 1 -type d -name ${pattern}\*`
+	if [ -z "$use_pkgng" ]; then
+		glob_dirs=`find $pdb -maxdepth 1 -type d -name ${pattern}\*`
+	else
+		glob_dirs=`pkg query -g "%n-%v" ${pattern}\*`
+	fi
 	case "$glob_dirs" in
 	# Match a newline in multiple responses from find
 	*'
 '*)		return 2 ;;
-	$pdb/*)	return ;;
+	*)	return ;;
 	esac
 
 	unset glob_dirs
@@ -511,8 +521,12 @@
 origin_from_pdb () {
 	local o
 
-	o=`grep -m1 '@comment ORIGIN:' $pdb/$1/+CONTENTS 2>/dev/null` && {
-		echo ${o#@comment ORIGIN:}; return 0; }
+	if [ -z "$use_pkgng" ]; then
+		o=`grep -m1 '@comment ORIGIN:' $pdb/$1/+CONTENTS 2>/dev/null` && {
+			echo ${o#@comment ORIGIN:}; return 0; }
+	else
+		pkg query "%o" $1; return 0;
+	fi
 
 	case "$1" in bsdpan-*) return 3 ;; esac
 
@@ -666,6 +680,7 @@
 	o)	REPLACE_ORIGIN=oopt ;;
 	p)	fail 'The -p option has been deprecated' ;;
 	r)	PM_URB=ropt
+		#TODO
 		if [ -d "$pdb/$OPTARG" ]; then
 			glob_dirs=$OPTARG
 		else
@@ -818,7 +833,12 @@
 		fi
 		unset INDEXFILE INDEXDIR
 
-		PM_INDEX_PORTS=`pkg_version -Ivl\< $PM_INDEX | cut -f1 -d\<`
+		if [ -z "$use_pkgng" ]; then
+			pkg_version="pkg_version"
+		else
+			pkg_version="pkg version"
+		fi
+		PM_INDEX_PORTS=`$pkg_version -Ivl\< $PM_INDEX | cut -f1 -d\<`
 		export PM_INDEX_PORTS
 
 		if [ -z "$pd" -o "$pd" != /usr/ports ]; then
@@ -875,6 +895,10 @@
 iport_from_origin () {
 	local sn dir
 
+	if [ -n "$use_pkgng" ]; then
+		pkg info -q ${1} || return 1
+		return
+	fi
 	sn=${1#*/} ; sn=${sn%-*} ; sn=${sn%%[0-9]*}
 
 	if ! dir=`grep -l "@comment ORIGIN:${1}$" $pdb/${sn}*/+CONTENTS 2>/dev/null`; then
@@ -903,6 +927,7 @@
 	done
 }
 
+# useless with pkgng
 check_dependency_files () {
 	# Global: grep_deps
 	local origin iport ro_opd
@@ -985,6 +1010,7 @@
 	fi
 }
 
+# also useless with pkgng
 update_contents () {
 	local IFS delete contents origin n_port old_origin iport new_cont
 	local o_seen line d_missing d_origin d_iport prev_line answer
@@ -1129,35 +1155,62 @@
 	echo '############' > $DI_FILES		# Make the file > 0 bytes
 	echo "===>>> Gathering distinfo list for installed ports"
 	echo ''
-	for pkg in ${pdb}/*; do
-		[ -d $pkg ] || continue
-		iport=${pkg#$pdb/}
+	if [ -z "$use_pkgng" ]; then
+		for pkg in ${pdb}/*; do
+			[ -d $pkg ] || continue
+			iport=${pkg#$pdb/}
 
-		origin=`origin_from_pdb $iport` || continue
+			origin=`origin_from_pdb $iport` || continue
 
-		if [ ! -d "$pd/$origin" ]; then
-			find_moved_port $origin $iport nonfatal >/dev/null
-			[ -n "$moved_npd" ] || continue
-			origin=$moved_npd
-		fi
+			if [ ! -d "$pd/$origin" ]; then
+				find_moved_port $origin $iport nonfatal >/dev/null
+				[ -n "$moved_npd" ] || continue
+				origin=$moved_npd
+			fi
 
-		origin="${pd}/${origin}"
+			origin="${pd}/${origin}"
 
-		if [ -s "${origin}/distinfo" ]; then
-			distinfo="${origin}/distinfo"
-		else
-			pm_cd $origin || continue
-			distinfo=`pm_make -V DISTINFO_FILE`
-			[ -n "$distinfo" ] || fail "No DISTINFO_FILE in $origin"
-		fi
+			if [ -s "${origin}/distinfo" ]; then
+				distinfo="${origin}/distinfo"
+			else
+				pm_cd $origin || continue
+				distinfo=`pm_make -V DISTINFO_FILE`
+				[ -n "$distinfo" ] || fail "No DISTINFO_FILE in $origin"
+			fi
 
-		if [ -s "$distinfo" ]; then
-			grep '^SHA256 ' $distinfo | while read disc1 f disc2; do
-				f=${f#(} ; f=${f%)}
-				echo $f >> $DI_FILES
-			done
-		fi
-	done
+			if [ -s "$distinfo" ]; then
+				grep '^SHA256 ' $distinfo | while read disc1 f disc2; do
+					f=${f#(} ; f=${f%)}
+					echo $f >> $DI_FILES
+				done
+			fi
+		done
+	else
+		pkg query -a "%n-%v %o" | while read pkg origin; do
+			if [ ! -d "$pd/${origin}" ]; then
+				find_moved_port $origin $pkg nonfatal >/dev/null
+				[ -n "$moved_npd" ] || continue
+				origin=$moved_npd
+			fi
+
+			origin="${pd}/${origin}"
+
+			if [ -s "${origin}/distinfo" ]; then
+				distinfo="${origin}/distinfo"
+			else
+				pm_cd $origin || continue
+				distinfo=`pm_make -V DISTINFO_FILE`
+				[ -n "$distinfo" ] || fail "No DISTINFO_FILE in $origin"
+			fi
+
+			if [ -s "$distinfo" ]; then
+				grep '^SHA256 ' $distinfo | while read disc1 f disc2; do
+					f=${f#(} ; f=${f%)}
+					echo $f >> $DI_FILES
+				done
+			fi
+		done
+	fi
 
 	# Tell safe_exit that we are done
 	pm_unlink ${DI_FILES}-e
@@ -1203,25 +1256,45 @@
 }
 
 ports_by_category () {
-	local pkg
+	local pkg dep rdep
 	pm_v "===>>> Sorting ports by category"
 
-	for pkg in $pdb/*; do
-		if [ -s "$pkg/+REQUIRED_BY" ]; then
-			if grep -ql '^@pkgdep ' $pkg/+CONTENTS 2>/dev/null; then
-				branches="$branches ${pkg#$pdb/}"
+	if [ -z "$use_pkgng" ]; then
+		for pkg in $pdb/*; do
+			if [ -s "$pkg/+REQUIRED_BY" ]; then
+				if grep -ql '^@pkgdep ' $pkg/+CONTENTS 2>/dev/null; then
+					branches="$branches ${pkg#$pdb/}"
+				else
+					trunks="$trunks ${pkg#$pdb/}"
+				fi
 			else
-				trunks="$trunks ${pkg#$pdb/}"
+				if grep -ql '^@pkgdep ' $pkg/+CONTENTS 2>/dev/null; then
+					leaves="$leaves ${pkg#$pdb/}"
+				else
+					[ -d "$pkg" ] || continue
+					roots="$roots ${pkg#$pdb/}"
+				fi
 			fi
-		else
-			if grep -ql '^@pkgdep ' $pkg/+CONTENTS 2>/dev/null; then
-				leaves="$leaves ${pkg#$pdb/}"
+		done
+	else
+		while read pkg dep rdep; do
+			if [ $rdep -eq 1 ]; then
+				if [ $dep -eq 1 ]; then
+					branches="$branches $pkg"
+				else
+					trunks="$trunks $pkg"
+				fi
 			else
-				[ -d "$pkg" ] || continue
-				roots="$roots ${pkg#$pdb/}"
+				if [ $dep -eq 1 ]; then
+					leaves="$leaves $pkg"
+				else
+					roots="$roots $pkg"
+				fi
 			fi
-		fi
-	done
+		done << EOF
+`pkg query -a "%n-%v %?d %?r"`
+EOF
+	fi
 }
 
 delete_empty_dist_subdirs () {
@@ -1272,6 +1345,7 @@
 update_required_by () {
 	# Global: grep_deps
 	local do_update
+	[ -n "$use_pkgng" ] && /bin/unlink $grep_deps && unset grep_deps
 
 	if [ -e "$pdb/$1/+REQUIRED_BY" ]; then
 		sort $pdb/$1/+REQUIRED_BY | cmp -s $grep_deps - || do_update=do_update
@@ -1323,12 +1397,19 @@
 	init_packages_var
 
 	echo "===>>> Checking for stale packages"
+	local ext
+	ext=tbz
+	[ -n "$use_pkgng" ] && ext=txz
 	for package in `find $PACKAGES -type f | sort`; do
-		pkg_dir=${package##*/} ; pkg_dir=${pkg_dir%\.tbz} ; echo ''
+		pkg_dir=${package##*/} ; pkg_dir=${pkg_dir%\.${ext}} ; echo ''
 
-		origin=`tar -O -zxvf $package '+CONTENTS' 2>/dev/null | grep '@comment ORIGIN:'` ||
-			fail Empty origin in $package
-		origin=${origin#@comment ORIGIN:}
+		if [ -z "$use_pkgng" ]; then
+			origin=`tar -O -zxvf $package '+CONTENTS' 2>/dev/null | grep '@comment ORIGIN:'` ||
+				fail Empty origin in $package
+			origin=${origin#@comment ORIGIN:}
+		else
+			origin=`pkg query "%o" $package` || fail Empty origin in $package
+		fi
 
 		if [ -z "$PM_INDEX" ]; then
 			if [ -d "$pd/$origin" ]; then
@@ -1344,8 +1425,21 @@
 		fi
 
 		if [ -n "$port_ver" ]; then
-			if [ "${port_ver}.tbz" = "${package##*/}" ]; then
+			if [ "${port_ver}.${ext}" = "${package##*/}" ]; then
 				echo "===>>> ${package##*/} is up to date"
+				if [ -n "$use_pkgng" ]; then
+					if pkg info -e ${pkg_dir}; then
+						echo "	===>>> $pkg_dir is not installed"
+						echo "	===>>> Path: ${package}"
+						get_answer_yn y "\n\t===>>> Delete stale package: ${package##*/}"
+						case "$?" in
+							0)	echo "	===>>> Deleting $package"
+								pm_unlink_s $package ;;
+						esac
+					fi
+					unset port_ver
+					continue
+				fi
 				if [ ! -d "${pdb}/${pkg_dir}" ]; then
 					echo "	===>>> $pkg_dir is not installed"
 					echo "	===>>> Path: ${package}"
@@ -1406,6 +1500,7 @@
 fi	# [ -n "$CLEAN_PACKAGES" ]
 
 if [ -n "$CHECK_DEPENDS" ]; then
+	[ -n "$use_pkgng" ] && exit 0 # this is useless with pkgng"
 	PM_VERBOSE=pmv_check_depends
 IFS='
 '
@@ -1446,6 +1541,7 @@
 fi
 
 if [ -n "$CHECK_PORT_DBDIR" ]; then
+	[ -n "$use_pkgng" ] && exit 0 # useless with pkgng
 	[ -d "$port_dbdir" ] ||
 		fail 'PORT_DBIR is empty, or the directory $port_dbdir does not exist'
 
@@ -1610,14 +1706,19 @@
 				unset port_ver
 			fi
 		else
-			case `pkg_version -t $iport $port_ver` in
+			if [ -z "$use_pkgng" ]; then
+				pkg_version="pkg_version"
+			else
+				pkg_version="pkg version"
+			fi
+			case `$pkg_version -t $iport $port_ver` in
 			\<)	do_update=upd_lt ;;
 			=)	;;	# Can be reached if same version with different options
 			\>)	if [ -n "$PM_VERBOSE" ]; then
 					echo "	===>>> Port version $port_ver does not"
 					echo "	===>>> seem newer than installed $iport"
 				fi ;;
-			*)	fail "pkg_version -t $iport $port_ver gave an unexpected result"
+			*)	fail "$pkg_version -t $iport $port_ver gave an unexpected result"
 			esac
 
 			[ -z "$do_update" ] && {
@@ -1689,7 +1790,13 @@
 	fi
 
 	pm_cd $pkgdir || fail "Cannot cd into $pkgdir to create a package"
-	if $PM_SU_CMD pkg_create -b $2; then
+	local pkg_create
+	if [ -z "$use_pkgng" ]; then
+		pkg_create="pkg_create -b"
+	else
+		pkg_create="pkg create "
+	fi
+	if $PM_SU_CMD $pkg_create $2; then
 		if [ "$1" = "$pbu" ]; then
 			if [ -n "$BACKUP" ]; then
 				echo "	===>>> Package saved to $1" ; echo ''
@@ -2028,19 +2135,28 @@
 fi
 
 if [ -n "$EXPUNGE" ]; then
-	if [ ! -d "$pdb/$EXPUNGE" ]; then
-		find_glob_dirs $EXPUNGE
-		case $? in
-		1)	fail "No such directory/port: $pdb/$EXPUNGE" ;;
-		2)	echo "===>>> $EXPUNGE matched multiple ports"
-			fail "The -e option works with only one port at a time" ;;
-		0)	EXPUNGE=${glob_dirs#$pdb/}
-			unset glob_dirs ;;
-		esac
+	if [ -z "$use_pkgng" ]; then
+		if [ ! -d "$pdb/$EXPUNGE" ]; then
+			find_glob_dirs $EXPUNGE
+			case $? in
+			1)	fail "No such directory/port: $pdb/$EXPUNGE" ;;
+			2)	echo "===>>> $EXPUNGE matched multiple ports"
+				fail "The -e option works with only one port at a time" ;;
+			0)	EXPUNGE=${glob_dirs#$pdb/}
+				unset glob_dirs ;;
+			esac
+		fi
+	else
+		pkg info -e $EXPUNGE || fail "No such port: $EXPUNGE"
+		EXPUNGE=`pkg query "%n-%v" $EXPUNGE`
 	fi
 
 	origin=`origin_from_pdb $EXPUNGE`
-	deplist=`grep -l DEPORIGIN:$origin$ $pdb/*/+CONTENTS`
+	if [ -z "$use_pkgng" ]; then
+		deplist=`grep -l DEPORIGIN:$origin$ $pdb/*/+CONTENTS`
+	else
+		deplist=`pkg query "%rn-%rv" $origin`
+	fi
 	if [ -n "$deplist" ]; then
 		echo "===>>> Warning: Ports with dependencies on ${EXPUNGE}:"
 		for dep in $deplist; do
@@ -2048,7 +2164,8 @@
 		done
 		get_answer_yn n "\n\t===>>> Delete this dependency data"
 		case "$?" in
-		0)	for f in $deplist; do
+		0)	[ -n "$use_pkgng" ] && exit 0
+			for f in $deplist; do
 				update_contents delete $f $origin
 			done ;;
 		*)	exit 1 ;;
@@ -2058,8 +2175,13 @@
 	[ -n "$BACKUP" ] && { init_packages ; pm_pkg_create $pbu $EXPUNGE; }
 	[ -z "$DONT_SCRUB_DISTFILES" ] && { delete_all_distfiles $origin; delete_dist_list; }
 
-	echo "===>>> Running pkg_delete -f $EXPUNGE"
-	pm_pkg_delete_s -f $EXPUNGE || fail 'pkg_delete failed'
+	if [ -z "$use_pkgng" ]; then
+		pkg_delete="pkg_delete"
+	else
+		pkg_delete="pkg delete"
+	fi
+	echo "===>>> Running $pkg_delete -f $EXPUNGE"
+	pm_pkg_delete_s -f $EXPUNGE || fail "$pkg_delete failed"
 
 	echo '' ; echo "===>>> Running ${0##*/} -s $ARGS"
 	exec $0 -s $ARGS
@@ -2069,13 +2191,22 @@
 if [ -n "$CLEAN_STALE" ]; then
 	[ -z "$no_del_list" ] && export no_del_list=':'
 
-	for file in `find $pdb -type f -name \+REQUIRED_BY -empty` ; do
+	#TODO: will use pkg autoremove when it works
+	if [ -z "$use_pkgng" ]; then
+		find_stale_ports="find $pdb -type f -name \+REQUIRED_BY -empty"
+	else
+		find_stale_ports="pkg query -a \"%?r %n-%v\" | awk '/^0/ { print \$2 }'"
+	fi
+	for file in `eval $find_stale_ports` ; do
 		iport="${file%/+REQUIRED_BY}" ; iport=${iport#$pdb/}
 
 		case "$no_del_list" in *:${iport}:*) continue ;; esac
 
 		origin=`origin_from_pdb $iport`
-		deplist=`grep -l DEPORIGIN:$origin$ $pdb/*/+CONTENTS`
+		deplist=""
+		if [ -z "$use_pkgng" ]; then
+			deplist=`grep -l DEPORIGIN:$origin$ $pdb/*/+CONTENTS`
+		fi
 		if [ -n "$deplist" ]; then
 			echo ''
 			echo "===>>> Warning: Unrecorded dependencies on ${iport}:"
@@ -2088,22 +2219,32 @@
 			continue
 		fi
 
-		pkg_info $iport
+		if [ -z "$use_pkgng" ]; then
+			pkg_info $iport
+			pkg_delete="pkg_delete"
+		else
+			pkg info -f $iport
+			pkg_delete="pkg delete"
+		fi
 
 		get_answer_yn n "\t===>>> ${iport} is no longer depended on, delete"
 		case "$?" in
 		0)	[ -n "$BACKUP" ] && { init_packages ; pm_pkg_create $pbu $iport; }
 			[ -z "$DONT_SCRUB_DISTFILES" ] && { delete_all_distfiles $origin; delete_dist_list; }
 
-			echo "===>>> Running pkg_delete -f $iport"
-			pm_pkg_delete_s -f $iport || fail 'pkg_delete failed'
+			echo "===>>> Running $pkg_delete -f $iport"
+			pm_pkg_delete_s -f $iport || fail "$pkg_delete failed"
 
 			exec $0 -s $ARGS ;;
-		*)	get_answer_yn n "\t===>>> Delete this dependency data"
-			case "$?" in
-			0)	pm_unlink_s $file ;;
-			*)	no_del_list="${no_del_list}${iport}:" ;;
-			esac ;;
+		*)	if [ -z "$use_pkgng" ]; then
+				get_answer_yn n "\t===>>> Delete this dependency data"
+				case "$?" in
+				0)	pm_unlink_s $file ;;
+				*)	no_del_list="${no_del_list}${iport}:" ;;
+				esac
+			else
+				no_del_list="${no_del_list}${iport}:"
+			fi ;;
 		esac
 	done
 	exit 0
@@ -2243,7 +2384,12 @@
 			fail "No entry for $origin in $PM_INDEX"
 	fi
 
-	case `pkg_version -t $iport $new_port 2>/dev/null` in
+	if [ -z "$use_pkgng" ]; then
+		pkg_version="pkg_version"
+	else
+		pkg_version="pkg version"
+	fi
+	case `$pkg_version -t $iport $new_port 2>/dev/null` in
 	\<)	build_l="${build_l}\tUpgrade $iport to $new_port\n" ;;
 	=)	build_l="${build_l}\tRe-install $iport\n" ;;
 	\>)	build_l="${build_l}\tDowngrade $iport to $new_port\n" ;;
@@ -2466,7 +2612,11 @@
 				fail "Cannot cd to $d_port"
 			fi
 			for glob in $conflicts; do
-				confl_p=`pkg_info -I $glob 2>/dev/null`
+				if [ -z "$use_pkgng" ]; then
+					confl_p=`pkg_info -I $glob 2>/dev/null`
+				else
+					confl_p=`pkg query -g "%n-%v" $glob 2>/dev/null`
+				fi
 				if [ -n "$confl_p" ]; then
 					confl_p=${confl_p%% *}
 					echo ''
@@ -2601,6 +2751,10 @@
 		done
 
 		for dep in $build_only_dl_g; do
+			if [ -n "$use_pkgng" ]; then
+				[ "`pkg query "%?r" $dep`" = "0" ] || temp_bodlg="$temp_bodlg $dep"
+				continue
+			fi
 			grep -q "@comment DEPORIGIN:${dep#$pd/}$" $pdb/*/+CONTENTS ||
 				temp_bodlg="$temp_bodlg $dep"
 		done
@@ -2635,7 +2789,14 @@
 	echo "===>>> Begin $verb ports that depend on $PM_URB_IPORTS"
 	echo ''
 
+	#TODO
 	for origin in $PM_URB_ORIGINS; do
+		if [ -n "$use_pkgng" ]; then
+			for req_by in `pkg query "%rn-%rv" ${origin}`; do
+				PM_URB_LIST="${PM_URB_LIST} ${req_by}"
+			done
+			continue
+		fi
 		for req_by in `grep -l DEPORIGIN:${origin}$ $pdb/*/+CONTENTS`; do
 			req_by="${req_by%/+CONTENTS}"
 			req_by="${req_by##*/}"
@@ -2806,9 +2967,16 @@
 		*/*)	origin=$port ;;
 		*)	# If an installed version does not exist at this
 			# point it probably got updated as a dependency
-			if [ ! -d "$pdb/$port" ]; then
-				numports=$(( $numports - 1 ))
-				continue
+			if [ -z "$use_pkgng" ]; then
+				if [ ! -d "$pdb/$port" ]; then
+					numports=$(( $numports - 1 ))
+					continue
+				fi
+			else
+				if ! pkg info -e $port; then
+					numports=$(( $numports - 1 ))
+					continue
+				fi
 			fi
 			origin=`origin_from_pdb $port` ;;
 		esac
@@ -3036,7 +3204,12 @@
 	/*)	echo '' ; no_valid_port ;;
 	*/*)	portdir=$argv ;;
 	\.)	portdir=${PWD##*/ports/} ;; # Not always $pd, could be symlink
-	*)	[ -d "$pdb/$argv" ] && upg_port=$argv ;;
+	*)	if [ -z "$use_pkgng" ]; then
+			[ -d "$pdb/$argv" ] && upg_port=$argv
+		else
+			pkg info -e $argv && upg_port=`pkg query "%n-%v" $argv`
+		fi
+		;;
 	esac
 
 	if [ -z "$portdir" -a -z "$upg_port" ]; then
@@ -3069,11 +3242,20 @@
 
 	case "$arg2" in
 	*/*)	ro_opd=$arg2 ; ro_upg_port=`iport_from_origin $ro_opd` ;;
-	*)	if [ -d "$pdb/$arg2" ]; then
-			ro_upg_port=$arg2
+	*)	if [ -z "$use_pkgng" ]; then
+			if [ -d "$pdb/$arg2" ]; then
+				ro_upg_port=$arg2
+			else
+				find_glob_dirs $arg2 && ro_upg_port=${glob_dirs#$pdb/}
+				unset glob_dirs
+			fi
 		else
-			find_glob_dirs $arg2 && ro_upg_port=${glob_dirs#$pdb/}
-			unset glob_dirs
+			if pkg info -e $arg2; then
+				ro_upg_port=$arg2
+			else
+				find_glob_dirs $arg2 && ro_upg_port=${glob_dirs}
+				unset glob_dirs
+			fi
 		fi
 		[ -n "$ro_upg_port" ] && ro_opd=`origin_from_pdb $ro_upg_port`
 	esac
@@ -3525,7 +3707,12 @@
 		pm_v "===>>> Available package ($latest_pv) matches the current version"
 	elif [ -n "$latest_pv" -a -n "$PM_PACKAGES_NEWER" ]; then
 		if [ -n "$upg_port" ]; then
-			case `pkg_version -t $upg_port $latest_pv` in
+			if [ -z "$use_pkgng" ]; then
+				pkg_version="pkg_version"
+			else
+				pkg_version="pkg version"
+			fi
+			case `$pkg_version -t $upg_port $latest_pv` in
 			\<)	use_package=up_newer
 				pm_v "===>>> Available package ($latest_pv)"
 				pm_v "       is newer than installed ($upg_port)" ;;
@@ -3541,7 +3728,12 @@
 			pm_v "===>>> There is a package available ($latest_pv)"
 		fi
 	elif [ -n "$latest_pv" ]; then
-		case `pkg_version -t $new_port $latest_pv` in
+		if [ -z "$use_pkgng" ]; then
+			pkg_version="pkg_version"
+		else
+			pkg_version="pkg version"
+		fi
+		case `$pkg_version -t $new_port $latest_pv` in
 		\<)	# Could happen if ports tree is out of date
 			use_package=up_old_tree
 			pm_v "===>>> Available package ($latest_pv)"
@@ -3640,7 +3832,12 @@
 		    grep -v ^$LOCALBASE_COMPAT > $ldconfig_out
 
 		unset temp
-		for file in `pkg_info -q -L $UPGRADE_PORT |
+		if [ -z "$use_pkgng" ]; then
+			pkglist="pkg_info -q -L"
+		else
+			pkglist="pkg query %Fp"
+		fi
+		for file in `$pkglist $UPGRADE_PORT |
 		    sort - $ldconfig_out | uniq -d`; do
 			temp="${temp}$file "
 		done
@@ -3743,29 +3940,32 @@
 	fi
 fi
 
-for file in $preserve_port_files; do
-	mv $file ${file}-new
-	mv ${preserve_dir}/${file##*/} $file
-	oldmd5="MD5:`md5 -q $file`"
-
-	new_cont=`pm_mktemp contents`
-	while read left right; do
-		case "$left" in
-		@cwd)		short_file="${file#${right}/}" ;;
-		$short_file)	found_it=found_it ; continue;;
-		@comment)	if [ -n "$found_it" ]; then
-					echo -e "${short_file}-new\n$left $right"
-					echo -e "$short_file\n@comment $oldmd5"
-					unset found_it
-					continue
-				fi ;;
-		esac
-		echo "$left $right"
-	done < $pdb/$new_port/+CONTENTS > $new_cont
-	pm_install_s $new_cont $contents
-	pm_unlink $new_cont
-	unset file oldmd5 new_cont left right short_file
-done
+#TODO
+if [ -z "$use_pkgng" ]; then
+	for file in $preserve_port_files; do
+		mv $file ${file}-new
+		mv ${preserve_dir}/${file##*/} $file
+		oldmd5="MD5:`md5 -q $file`"
+
+		new_cont=`pm_mktemp contents`
+		while read left right; do
+			case "$left" in
+			@cwd)		short_file="${file#${right}/}" ;;
+			$short_file)	found_it=found_it ; continue;;
+			@comment)	if [ -n "$found_it" ]; then
+						echo -e "${short_file}-new\n$left $right"
+						echo -e "$short_file\n@comment $oldmd5"
+						unset found_it
+						continue
+					fi ;;
+			esac
+			echo "$left $right"
+		done < $pdb/$new_port/+CONTENTS > $new_cont
+		pm_install_s $new_cont $contents
+		pm_unlink $new_cont
+		unset file oldmd5 new_cont left right short_file
+	done
+fi
 if [ -n "$preserve_dir" ]; then
 	rmdir $preserve_dir 2>/dev/null
 	unset preserve_dir preserve_port_files
@@ -3781,7 +3981,12 @@
 temp=`find $LOCALBASE_COMPAT -type d -empty 2>/dev/null`
 if [ -z "$temp" ] && [ -d "$LOCALBASE_COMPAT" ]; then
 	unset files
-	for file in `pkg_info -q -L $new_port`; do
+	if [ -z "$use_pkgng" ]; then
+		pkglist="pkg_info -q -L"
+	else
+		pkglist="pkg query %Fp"
+	fi
+	for file in `$pkglist $new_port`; do
 		[ -f "${LOCALBASE_COMPAT}/${file##*/}" ] &&
 			files="${files}${LOCALBASE_COMPAT}/${file##*/} "
 	done
@@ -3844,7 +4049,8 @@
 	/bin/unlink $dist_list_temp ; unset ds dist_list_temp
 fi
 
-if [ -n "$use_package" ]; then
+# pkgng does not need this
+if [ -n "$use_package" -a -z "$use_pkgng" ]; then
 	if grep -q DEPORIGIN $pdb/$new_port/+CONTENTS; then
 		echo "===>>> Updating dependencies for $new_port to match installed versions"
 		update_contents $pdb/$new_port/+CONTENTS
@@ -3879,27 +4085,29 @@
 	echo ''
 fi
 
-check_dependency_files $portdir $new_port
-if [ -s "$grep_deps" ]; then
-	echo "===>>> Updating dependency entry for $new_port in each dependent port"; pm_v
-	while read d_port; do
-		pm_v "===>>> $d_port"
-		dp_cont=$pdb/$d_port/+CONTENTS
-		[ -e "$dp_cont" ] || continue
-
-		if [ -n "$ro_opd" ]; then
-			grep -ql "DEPORIGIN:$ro_opd$" $dp_cont &&
-				update_contents $dp_cont $portdir $new_port $ro_opd
-		fi
-		# Do this one last so it can get deleted as a duplicate
-		# if ro_opd is present.
-		if grep -ql "DEPORIGIN:$portdir$" $dp_cont; then
-			update_contents $dp_cont $portdir $new_port
-		fi
-	done < $grep_deps
-	unset d_port dp_cont do_update
+if [ -z $use_pkgng ]; then
+	check_dependency_files $portdir $new_port
{'fraction_non_alphanumeric': 0.19476046837517516, 'fraction_numerical': 0.026078901740968576, 'mean_word_length': 3.0144927536231885, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 268, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1643315', 'n_tokens_mistral': 20799, 'n_tokens_neox': 18371, 'n_words': 4339}