aboutsummaryrefslogtreecommitdiff
path: root/ports-mgmt/portmaster/files/patch-portmaster
blob: 41ac74cb21a049b41c5e41174a68be8ee9efdb91 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
--- portmaster.orig	2018-01-14 21:53:23 UTC
+++ portmaster
@@ -7,10 +7,12 @@ trap trap_exit INT
 
 umask 022
 
-progcmd="$0"			# actual invocation of this program for search in ps output
-progname="${0##*/}"		# program name in messages
-program="$(realpath $0)"	# full path to program for recursive calls
+progcmd="$0"				# actual invocation of this program for search in ps output
+progname="${0##*/}"			# program name in messages
+program="$(realpath $0 || echo $0)"	# full path to program for recursive calls
 
+PKG_CMD="/usr/local/sbin/pkg-static"
+
 # Initialize crucial values for the parent, and export them for the children
 if [ -z "$PM_PARENT_PID" ]; then
 	PM_PARENT_PID=$$
@@ -45,7 +47,7 @@ if [ -z "$PM_PARENT_PID" ]; then
 	my_environment=`set`
 
 	# If we are already root, unset this to avoid potential conflict
-	[ `ps -o uid= $$` -eq 0 ] && unset PM_SU_CMD PM_SU_VERBOSE
+	[ `id -u` -eq 0 ] && unset PM_SU_CMD PM_SU_VERBOSE
 fi
 
 #=============== Begin functions we always want to have ===============
@@ -181,7 +183,7 @@ parent_exit () {
 
 		pm_v "===>>> Removing empty directories from WRKDIRPREFIX"
 		[ -n "$needws" ] && echo '' || pm_v
-		find $PM_WRKDIRPREFIX -depth -mindepth 1 -type d -empty -delete 2>/dev/null
+		find $PM_WRKDIRPREFIX -depth -mindepth 1 -maxdepth 2 -type d -empty -delete 2>/dev/null
 	fi
 
 	case "$DISPLAY_LIST" in
@@ -195,7 +197,7 @@ parent_exit () {
 
 		: ${PAGER:='less -e'}
 		(
-		pkg query "===>>> pkg-message for %n-%v\n%M" $DISPLAY_LIST
+		$PKG_CMD query "===>>> pkg-message for %n-%v\n%M" $DISPLAY_LIST
 		echo "===>>> Done displaying pkg-message files" ; echo '' ) | $PAGER ;;
 	esac
 
@@ -243,12 +245,12 @@ parent_exit () {
 	fi
 
 	if [ -n "$1" -a -n "${PM_NEEDS_UPDATE# }" -a -n "$PM_BUILDING" -a -z "$FETCH_ONLY" ]; then
-		echo "$progname <flags>${PM_NEEDS_UPDATE}" > ${TMPDIR}/portmasterfail.txt
+		echo "$progname <flags>${PM_NEEDS_UPDATE}" > ~/portmasterfail.txt
 		echo ''
 		echo "===>>> You can restart from the point of failure with this command line:"
 		echo "       $progname <flags>${PM_NEEDS_UPDATE}"
 		echo ''
-		echo "This command has been saved to ${TMPDIR}/portmasterfail.txt"
+		echo "This command has been saved to ~/portmasterfail.txt"
 		echo ''
 	fi
 }
@@ -260,7 +262,7 @@ safe_exit () {
 		parent_exit $1
 	else
 		# Save state for the parent process to read back in
-		> $IPC_SAVE
+		: > $IPC_SAVE
 		if [ -z "$PM_FIRST_PASS" ]; then
 			echo "DISPLAY_LIST='$DISPLAY_LIST'" >> $IPC_SAVE
 			echo "INSTALLED_LIST='$INSTALLED_LIST'" >> $IPC_SAVE
@@ -308,50 +310,55 @@ safe_exit () {
 	exit ${1:-0}
 } # safe_exit()
 
-flavor_part	() { expr "$1" : ".*@" >/dev/null && echo "${1#*@}"; }
-dir_part	() { echo "${1%%@*}"; }
-export_flavor	() { local flavor="$1"; if [ "$FLAVOR" != "$flavor" ]; then
-			pm_v "===>>> Setting FLAVOR to '$flavor' (was '$FLAVOR')";
-			export FLAVOR="$flavor"; fi; }
- 
+target_part	() { [ -z "${1##*:*}" ] && echo "${1#*:}" || echo install; }
+flavor_part	() { local tmp="${1%%:*}"; [ -z "${tmp##*@*}" ] && echo "${tmp#*@}"; }
+dir_part	() { local tmp="${1%%:*}"; echo "${tmp%%@*}"; }
+export_flavor	() { if [ -n "$1" ]; then export FLAVOR="$1"; else unset FLAVOR; fi; }
+
 pm_cd     () { builtin cd $1 2>/dev/null || return 1; }
 pm_cd_pd  () { [ -n "$PM_INDEX_ONLY" ] && return 2;
 			local dir=$pd/$(dir_part $1);
 			builtin cd $dir 2>/dev/null ||
 			fail "Cannot cd to port directory: $dir"; }
 pm_isdir	() { builtin test -d "$1"; }
-pm_isdir_pd	() { local dir=$pd/$(dir_part "$1");
-			builtin test -d "$dir"; }
-pm_kill   () { kill $* >/dev/null 2>/dev/null; }
+pm_isdir_pd	() { local dir=$(dir_part "$1");
+			builtin test -n "$dir" -a -d "$pd/$dir"; }
+pm_kill   () { kill "$@" >/dev/null 2>/dev/null; }
 pm_make   () { ( unset -v CUR_DEPS INSTALLED_LIST PM_DEPTH build_l PM_URB_LIST;
-		 /usr/bin/nice /usr/bin/make $PM_MAKE_ARGS $*; ); }
-pm_make_b () { /usr/bin/make $PM_MAKE_ARGS BEFOREPORTMK=bpm $*; }
+		 /usr/bin/nice /usr/bin/make $PM_MAKE_ARGS "$@"; ); }
+pm_make_b () { /usr/bin/make $PM_MAKE_ARGS BEFOREPORTMK=bpm "$@"; }
 pm_mktemp () {
 	pm_mktemp_file=`/usr/bin/mktemp -t f-${PM_PARENT_PID}-$1 2>&1` ||
 		fail "mktemp for $1 failed:\n       ${pm_mktemp_file#mktemp: }"
 }
 pm_unlink () { [ -e "$1" ] && /bin/unlink $1; }
-pm_islocked	() { local locked; [ -n "$1" ] && locked=`pkg query %k "$1"` &&
-			[ "$locked" = 1 -o -e "$pdb/$1/+IGNOREME" ]; }
+pm_islocked	() { local locked; [ -n "$1" ] && locked=`$PKG_CMD query %k "$1"` &&
+			[ "$locked" = 1 ] || [ -e "$pdb/$1/+IGNOREME" ]; }
 
 # Superuser versions for commands that need root privileges
 
-pm_find_s         () { $PM_SU_CMD /usr/bin/find $*; }
+pm_find_s         () { $PM_SU_CMD /usr/bin/find "$@"; }
 pm_install_s      () { $PM_SU_CMD /usr/bin/install -p -o root -g wheel -m 644 $1 $2; }
 pm_make_s         () { ( unset -v CUR_DEPS INSTALLED_LIST PM_DEPTH build_l PM_URB_LIST;
-			 $PM_SU_CMD /usr/bin/nice /usr/bin/make $PM_MAKE_ARGS $*; ); }
+			 $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/local/sbin/pkg delete -yf $*;
+	for f in "$@"; do
+		if [ "$($PKG_CMD query %k $f)" = 1 ]; then
+			was_locked=1
+			$PKG_CMD unlock -qy $f
+		fi
+	done
+	$PM_SU_CMD /usr/local/sbin/pkg-static delete -yf "$@";
 	for d in $* ; do
 	    pm_rm_s -rf $pdb/$d
 	done
 }
-pm_rm_s           () { $PM_SU_CMD /bin/rm $*; }
-pm_rmdir_s        () { $PM_SU_CMD /bin/rmdir $*; }
+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; }
 
-pm_v              () { [ -n "$PM_VERBOSE" ] && echo -e "$*"; }
+pm_v              () { [ -n "$PM_VERBOSE" ] && echo -e "$@"; }
 pm_sv             () { [ -n "$PM_SU_VERBOSE" ] && echo -e "===>>> SU $*"; }
 
 # Do this here so we can have a reasonably good guess.
@@ -359,7 +366,7 @@ pm_sv             () { [ -n "$PM_SU_VERBOSE" ] && echo
 if [ "$$" -eq "$PM_PARENT_PID" ]; then
 	if [ -z "$pd" ]; then
 		if [ -z "$PORTSDIR" ]; then
-			pd=`pm_make_b -f/usr/share/mk/bsd.port.mk -V PORTSDIR 2>/dev/null`
+			pd=`pm_make_b -f/usr/share/mk/bsd.port.mk -V PORTSDIR 2>/dev/null` || pd=""
 			[ -z "$pd" ] && pm_isdir /usr/ports && pd=/usr/ports
 		else
 			pd=$PORTSDIR
@@ -433,6 +440,8 @@ usage () {
 	echo ''
 	echo "$progname --list-origins"
 	echo ''
+	echo "$progname --try-broken"
+	echo ''
 	echo "$progname [--force-config|-G] [-P|-PP] [-aftv] -F"
 	echo ''
 	echo "$progname [-n|y] [-b] [-D|d] -e <name/glob of a single port in $pdb>"
@@ -513,6 +522,8 @@ usage () {
 	echo '-n answer no to all user prompts for the features below'
 	echo '-y answer yes to all user prompts for the features below'
 	echo ''
+	echo '--try-broken do not skip ports marked as broken'
+	echo ''
 	echo '[-n|y] [-b] [-D|d] -e expunge one port via pkg delete, and remove its distfiles'
 	echo '[-n|y] [-b] [-D|d] -s clean out stale ports that used to be depended on'
 	echo ''
@@ -549,7 +560,7 @@ find_glob_dirs () {
 
 	pattern=`globstrip $1`
 
-	glob_dirs=`pkg query -g "%n-%v" ${pattern}\*`
+	glob_dirs=`$PKG_CMD query -g "%n-%v" ${pattern}\*`
 	case "$glob_dirs" in
 	# Match a newline in multiple responses from find
 	*'
@@ -562,12 +573,37 @@ find_glob_dirs () {
 	return 1
 }
 
+match_flavor () {
+	local origin=$(dir_part $1)
+	local flavor	# =$(flavor_part $1)
+	local iport="$2"
+	local dir="$pd/$origin"
+
+	if [ -d "$dir" ]; then
+	local IFS=' '
+	local pkgname=${iport%-*}
+	local flavors=$(pm_make -C "$dir" -V FLAVORS)
+	for flavor in $flavors; do
+		local p=$(pm_make -C "$dir" FLAVOR=$flavor -V PKGNAME)
+		if [ -n "$p" -a "${p%-*}" = "$pkgname" ]; then
+			echo "$origin@$flavor"
+			return
+		fi
+	done
+	fi
+	echo "$1"	# should not be reached!
+}
+
 origin_from_pdb () {
-	local flavor pkgname
+	local flavor pkgname origin_flavor
 
 	pkgname="$1"
-	flavor=$(pkg annotate -Sq "$pkgname" flavor)
-	pkg query '%o'"${flavor:+@$flavor}" "$pkgname" 2>/dev/null && return
+	flavor=$($PKG_CMD annotate -Sq "$pkgname" flavor)
+	
+	if origin_flavor=$($PKG_CMD query '%o'"${flavor:+@$flavor}" "$pkgname" 2>/dev/null); then
+		match_flavor $origin_flavor $pkgname
+		return 0
+	fi
 
 	case "$pkgname" in bsdpan-*) return 3 ;; esac
 
@@ -576,7 +612,7 @@ origin_from_pdb () {
 			# An error above doesn't necessarily mean there's
 			# a problem in +MANIFEST, so don't mention it
 			echo "	===>>> No origin available for $pkgname" >&2
-			echo "	===>>> $pdb/$pkgname/+IGNOREME exists" >&2
+			echo "	===>>> $pdb/$pkgname/+IGNOREME exists or the package is locked" >&2
 			echo '' >&2
 		fi
 		return 2
@@ -684,6 +720,7 @@ for var in "$@" ; do
 	--list-origins)		LIST_ORIGINS=list_origins ;;
 	--show-work)		SHOW_WORK=show ; PM_THOROUGH=thorough ;;
 	--force-config)		export PM_FORCE_CONFIG=pm_force_config ;;
+	--try-broken)		TRY_BROKEN=try_broken; PM_MAKE_ARGS="$PM_MAKE_ARGS -DTRYBROKEN" ;;
 	--*)			echo "Illegal option $var" ; echo ''
 				echo "===>>> Try $progname --help"; exit 1 ;;
 	*)			newopts="$newopts $var" ;;
@@ -728,7 +765,7 @@ while getopts 'BCDFGHKLPRabde:fghilm:nop:r:stvwx:y' CO
 	o)	REPLACE_ORIGIN=oopt ;; # -o should take a parameter and $OPTARG should be assigned here
 	p)	fail 'The -p option has been deprecated' ;;
 	r)	PM_URB=ropt
-		if [ -d "$pdb/$OPTARG" ] && pkg info -e $OPTARG; then
+		if [ -d "$pdb/$OPTARG" ] && $PKG_CMD info -e $OPTARG; then
 			glob_dirs=$OPTARG
 		else
 			case "$OPTARG" in */*) fail 'The argument to -r must be a package name, or a glob pattern' ;; esac
@@ -789,6 +826,7 @@ if [ -n "$ALWAYS_SCRUB_DISTFILES" -a -n "$DONT_SCRUB_D
 		fail "The -d and -D options are mutually exclusive"
 fi
 
+[ -n "$NO_ACTION" ] && PM_NO_MAKE_CONFIG=no_action PM_FORCE_CONFIG=""
 [ -n "$PM_NO_MAKE_CONFIG" -a -n "$PM_FORCE_CONFIG" ] && unset PM_NO_MAKE_CONFIG
 
 if [ -n "$LIST" -o -n "$LIST_PLUS" ]; then
@@ -875,7 +913,7 @@ if [ "$$" -eq "$PM_PARENT_PID" ]; then
 		fi
 		unset INDEXFILE INDEXDIR
 
-		PM_INDEX_PORTS=`pkg version -Ivl\< $PM_INDEX | cut -f1 -d\<`
+		PM_INDEX_PORTS=`$PKG_CMD version -Ivl\< $PM_INDEX | cut -f1 -d\<`
 		export PM_INDEX_PORTS
 
 		if [ -z "$pd" -o "$pd" != /usr/ports ]; then
@@ -927,6 +965,9 @@ if [ "$$" -eq "$PM_PARENT_PID" ]; then
 	fi
 fi	# [ "$$" -eq "$PM_PARENT_PID" ]
 
+#=============== cache some build parameters in the environment ===============
+SCRIPTSDIR="$pd/Mk/Scripts" PORTSDIR="$pd" MAKE=make . "$pd/Mk/Scripts/ports_env.sh" > /dev/null
+
 #=============== Begin functions relevant to --features and main ===============
 
 # find installed port for given origin (with optional @flavor) in the pkg DB
@@ -938,11 +979,15 @@ iport_from_origin () {
 
 	origin=$(dir_part "$1")
 	flavor=$(flavor_part "$1")
-	pkgname_l=$(echo $(pkg query '%n-%v' $origin)) || return 1
+	if [ -z "$flavor" ]; then
+		unset FLAVOR
+		flavor=$(pm_make -C "${pd}/${origin}" -V FLAVOR)
+	fi
+	pkgname_l=$(echo $($PKG_CMD query '%n-%v' $origin)) || return 1
 	# if multiple flavors registered then fall back to $FLAVOR if no flavor has been passed in $1
 	[ "${pkgname_l}" != "${pkgname_l#* }" ] && : ${flavor:=$FLAVOR}
 	for pkgname in $pkgname_l; do
-		pkgflavor=$(pkg annotate -Sq "$pkgname" flavor)
+		pkgflavor=$($PKG_CMD annotate -Sq "$pkgname" flavor)
 		if [ "$pkgflavor" = "$flavor" ]; then
 			echo $pkgname
 			return 0
@@ -1002,25 +1047,6 @@ get_answer_yn () {
 	fi
 }
 
-match_flavor () {
-	local origin=$(dir_part $1)
-	local flavor=$(flavor_part $1)
-	local iport="$2"
-	local dir="$pd/$origin"
-
-	[ -n "$flavor" -a -d "$dir" ] || return
-	local IFS=' '
-	local pkgname=${iport%-*}
-	local flavors=$(pm_make -C "$dir" -V FLAVORS)
-	for flavor in $flavors; do
-		local p=$(pm_make -C "$dir" FLAVOR=$flavor -V PKGNAME)
-		if [ -n "$p" -a "${p%-*}" = "$pkgname" ]; then
-			moved_npd="$origin@$flavor"
-			break
-		fi
-	done
-}
-
 # Find the new origin for moved ports
 # Set global variable moved_npd on success
 # Return values:
@@ -1045,7 +1071,7 @@ find_moved_port () {
 					echo ''
 					echo "	===>>> The $sf port has been deleted"
 					echo "	===>>> Reason: ${moved##*|}"
-					echo "	===>>> Skipping it due to +IGNOREME file"
+					echo "	===>>> Skipping it due to +IGNOREME file or the package being locked"
 					echo ''
 				fi
 				return 0
@@ -1056,7 +1082,8 @@ find_moved_port () {
 			fi ;;
 		${sf}\|*) moved_npd=${moved#*\|}	# New port directory
 			moved_npd=${moved_npd%%\|*}
-			[ -n $(flavor_part $moved_npd) ] && match_flavor $moved_npd $iport
+			[ -n "$(flavor_part $moved_npd)" ] && moved_npd=$(match_flavor $moved_npd $iport)
+				
 			echo ''
 			echo "	===>>> The $sf port moved to $moved_npd"
 			echo "	===>>> Reason: ${moved##*|}"
@@ -1082,7 +1109,7 @@ find_moved_port () {
 }
 
 all_pkgs_by_origin () {
-	namesorigins=`pkg query -a "%n-%v %o"`
+	namesorigins=`$PKG_CMD query -a "%n-%v %o"`
 	echo "$namesorigins"
 	return
 }
@@ -1146,11 +1173,10 @@ read_distinfos_all () {
 	echo ''
 
 	for origin in ${pd}/*/*; do
-#		origin=$(dir_part "$origin")
 		case "${origin#$pd/}" in
 		Mk/*|T*|distfiles/*|packages/*|*/[Mm]akefile*|CVS/*|*/CVS|base/*) continue ;; esac
 
-		pm_isdir "$origin" ] || continue
+		pm_isdir "$origin" || continue
 
 		if [ -s "${origin}/distinfo" ]; then
 			distinfo="${origin}/distinfo"
@@ -1181,17 +1207,17 @@ ports_by_category () {
 	local pkg
 
 	pm_v "===>>> Sorting ports by category"
-	roots=`   pkg query -e "%#d = 0 && %#r = 0" "%n-%v"`
-	trunks=`  pkg query -e "%#d = 0 && %#r > 0" "%n-%v"`
-	branches=`pkg query -e "%#d > 0 && %#r > 0" "%n-%v"`
-	leaves=`  pkg query -e "%#d > 0 && %#r = 0" "%n-%v"`
+	roots=`   $PKG_CMD query -e "%#d = 0 && %#r = 0" "%n-%v"`
+	trunks=`  $PKG_CMD query -e "%#d = 0 && %#r > 0" "%n-%v"`
+	branches=`$PKG_CMD query -e "%#d > 0 && %#r > 0" "%n-%v"`
+	leaves=`  $PKG_CMD query -e "%#d > 0 && %#r = 0" "%n-%v"`
 
 	num_roots=$(echo    $(echo $roots    | wc -w))
 	num_trunks=$(echo   $(echo $trunks   | wc -w))
 	num_branches=$(echo $(echo $branches | wc -w))
 	num_leaves=$(echo   $(echo $leaves   | wc -w))
 
-	num_ports=$(echo $(pkg query -a "%n-%v" | wc -w))
+	num_ports=$(echo $($PKG_CMD query -a "%n-%v" | wc -w))
 }
 
 delete_empty_dist_subdirs () {
@@ -1279,7 +1305,7 @@ if [ -n "$CLEAN_PACKAGES" ]; then
 	for package in `find $PACKAGES -type f | sort`; do
 		pkg_dir=${package##*/} ; pkg_dir=${pkg_dir%\.*} ; echo ''
 
-		origin=`pkg query -F $package "%o" 2>/dev/null` ||
+		origin=`$PKG_CMD query -F $package "%o" 2>/dev/null` ||
 			origin=`tar -O -zxvf $package '+CONTENTS' 2>/dev/null | grep '@comment ORIGIN:'` ||
 			origin=`tar -O -zxvf $package '+MANIFEST' 2>/dev/null | grep '^origin:'` ||
 			fail "Empty origin in $package"
@@ -1302,7 +1328,7 @@ if [ -n "$CLEAN_PACKAGES" ]; then
 		if [ -n "$port_ver" ]; then
 			if [ "$port_ver" = "$pkg_dir" ]; then
 				echo "===>>> ${package##*/} is up to date"
-				if ! pkg info -e $pkg_dir; then
+				if ! $PKG_CMD info -e $pkg_dir; then
 					echo "	===>>> $pkg_dir is not installed"
 					echo "	===>>> Path: ${package}"
 					get_answer_yn y "\n\t===>>> Delete stale package: ${package##*/}"
@@ -1320,7 +1346,7 @@ if [ -n "$CLEAN_PACKAGES" ]; then
 
 			unset port_ver
 
-			if pkg info -e $pkg_dir; then
+			if $PKG_CMD info -e $pkg_dir; then
 				echo "	===>>> ${package##*/} matches the installed version"
 			else
 				echo "	===>>> ${package##*/} is not installed"
@@ -1362,7 +1388,7 @@ if [ -n "$CLEAN_PACKAGES" ]; then
 fi	# [ -n "$CLEAN_PACKAGES" ]
 
 if [ -n "$CHECK_DEPENDS" ]; then
-	pkg check -dn
+	$PKG_CMD check -dn
 	exit 0
 fi
 
@@ -1377,14 +1403,14 @@ if [ -n "$CHECK_PORT_DBDIR" ]; then
 		if ! pm_isdir_pd "$origin"; then
 			find_moved_port $origin $pkg nonfatal >/dev/null
 			[ -n "$moved_npd" ] || continue
-			origin=$moved_npd
+			origin=$(dir_part $moved_npd)
 		fi
 
 		if ! pm_cd $pd/$origin; then
 			echo "	===>>> $pd/$origin does not exist for $pkg"
 			continue
 		fi
-		unique_list="${unique_list}`make -V UNIQUENAME -V OPTIONS_NAME | tr '\n' ':'`"
+		unique_list="${unique_list}`pm_make -V OPTIONS_NAME | tr '\n' ':'`"
 	done <<-EOF
 	$(all_pkgs_by_origin)
 	EOF
@@ -1413,7 +1439,7 @@ if [ -n "$CHECK_PORT_DBDIR" ]; then
 fi
 
 if [ -n "$LIST_ORIGINS" ]; then
-	pkg query -e '%#r = 0' '%o'
+	$PKG_CMD query -e '%#r = 0' '%o'
 
 	exit 0
 fi
@@ -1423,10 +1449,11 @@ fi
 
 check_state () {
 	# Global: state
-	local state_set
+	local state_set makevars
 
+	[ -z "$TRY_BROKEN" ] && makevars="FORBIDDEN BROKEN IGNORE" || makevars="FORBIDDEN"
 	if egrep -ql '^(FORBIDDEN|BROKEN|IGNORE)' Makefile; then
-		for state in FORBIDDEN BROKEN IGNORE; do
+		for state in $makevars; do
 			state_set=`pm_make -V $state`
 			if [ -n "$state_set" ]; then
 				echo "	===>>> This port is marked $state"
@@ -1502,10 +1529,10 @@ check_for_updates () {
 	fi
 
 	if [ -z "$do_update" -a -z "$skip" -a -z "$PM_INDEX_ONLY" ] && pm_isdir "$pd/$origin"; then
-		if ! pm_cd $pd/$origin; then
+		if ! pm_cd $pd/$origin; then # <se> cannot happen - see pm_isdir test above
 			if pm_islocked "$iport"; then
 				echo "	===>>> Warning: Unable to cd to $pd/$origin"
-				echo "	===>>> Continuing due to $pdb/$iport/+IGNOREME"
+				echo "	===>>> Continuing due to $pdb/$iport/+IGNOREME or the package being locked"
 				echo ''
 				CUR_DEPS="${CUR_DEPS}${iport}:${originflavor}:"
 				return 0
@@ -1521,7 +1548,7 @@ check_for_updates () {
 		# If the port has moved and no +IGNOREME, we have to update it
 		if [ -n "$moved_npd" ]; then
 			if pm_islocked "$iport"; then
-				echo "	===>>> Continuing due to $pdb/$iport/+IGNOREME"
+				echo "	===>>> Continuing due to $pdb/$iport/+IGNOREME or the package being locked"
 				echo ''
 				CUR_DEPS="${CUR_DEPS}${iport}:${originflavor}:"
 				return 0
@@ -1545,7 +1572,7 @@ check_for_updates () {
 				unset port_ver
 			fi
 		else
-			case `pkg version -t $iport $port_ver` in
+			case `$PKG_CMD version -t $iport $port_ver` in
 			\<)	do_update=upd_lt ;;
 			=)	;;	# Can be reached if same version with different options
 			\>)	if [ -n "$PM_VERBOSE" ]; then
@@ -1569,7 +1596,7 @@ check_for_updates () {
 		if [ -z "$moved_npd" ]; then
 			echo "	===>>> New version available: $port_ver"
 			if pm_islocked "$iport"; then
-				echo "	===>>> +IGNOREME file is present for $1"
+				echo "	===>>> +IGNOREME file is present for $1 or the package is locked"
 			fi
 			pm_cd_pd $origin && check_state
 			num_updates=$(( $num_updates + 1 ))
@@ -1628,7 +1655,7 @@ pm_pkg_create () {
 	fi
 
 	pm_cd $pkgdir || fail "Cannot cd into $pkgdir to create a package"
-	if $PM_SU_CMD pkg create $format_option $2; then
+	if $PM_SU_CMD $PKG_CMD create $format_option $2; then
 		if [ "$1" = "$pbu" ]; then
 			if [ -n "$BACKUP" ]; then
 				echo "	===>>> Package saved to $1" ; echo ''
@@ -1980,7 +2007,7 @@ if [ -n "$LIST" -o -n "$LIST_PLUS" ]; then
 fi
 
 if [ -n "$EXPUNGE" ]; then
-	if ! pm_isdir "$pdb/$EXPUNGE" || ! pkg info -e $EXPUNGE; then
+	if ! pm_isdir "$pdb/$EXPUNGE" || ! $PKG_CMD info -e $EXPUNGE; then
 		find_glob_dirs $EXPUNGE
 		case $? in
 		1)	fail "No such port: $EXPUNGE" ;;
@@ -1992,7 +2019,7 @@ if [ -n "$EXPUNGE" ]; then
 	fi
 
 	origin=`origin_from_pdb $EXPUNGE`
-	deplist=`pkg query "%rn-%rv" $origin`
+	deplist=`$PKG_CMD query "%rn-%rv" $origin`
 	if [ -n "$deplist" ]; then
 		echo "===>>> Warning: Ports with dependencies on ${EXPUNGE}:"
 		echo "$deplist" | sed 's/^/	/'
@@ -2020,7 +2047,7 @@ fi
 if [ -n "$CLEAN_STALE" ]; then
 	[ -z "$no_del_list" ] && export no_del_list=':'
 
-	stale_ports=`pkg query -ae '%a = 1' '%?r %n-%v' | awk '/^0 / { print \$2 }'`
+	stale_ports=`$PKG_CMD query -ae '%a = 1' '%?r %n-%v' | awk '/^0 / { print \$2 }'`
 	for file in $stale_ports; do
 		iport="${file%/+REQUIRED_BY}" ; iport=${iport#$pdb/}
 
@@ -2040,7 +2067,7 @@ if [ -n "$CLEAN_STALE" ]; then
 			continue
 		fi
 
-		echo '' ; pkg info -f $iport
+		echo '' ; $PKG_CMD info -f $iport
 
 		get_answer_yn n "\t===>>> ${iport} is no longer depended on, delete"
 		case "$?" in
@@ -2073,7 +2100,7 @@ check_restart_and_udf () {
 	# to go out to the disk if we don't have to.
 	[ -z "$RESTART" ] && return 1
 
-	if [ ! -e "$pdb/${1}/PM_UPGRADE_DONE_FLAG" ] || ! pkg info -e $1; then
+	if [ ! -e "$pdb/${1}/PM_UPGRADE_DONE_FLAG" ] || ! $PKG_CMD info -e $1; then
 		return 1
 	else
 		already_done $1
@@ -2175,26 +2202,32 @@ update_pm_nu () {
 }
 
 update_build_l () {
-	local originflavor origin flavor iport
+	local originflavor origin flavor iport make_target
 
 	originflavor=$1 ; update_pm_nu $originflavor
 	origin=$(dir_part "$originflavor")
 	flavor=$(flavor_part "$originflavor")
 	iport="$2"
+	make_target=$3
 
 	[ -n "$PM_NO_CONFIRM" ] && return
 
 	if [ -z "$iport" ]; then
 		case "$build_l" in *\ $origin\\*) return ;; esac
-		build_l="${build_l}\tInstall $originflavor\n"
+		case $make_target in
+		install)
+			build_l="${build_l}\tInstall $originflavor\n" ;;
+		*)
+			build_l="${build_l}\tExecute make $make_target for $originflavor\n" ;;
+		esac
 		return
-	else
-		case "$build_l" in *\ $iport\ *|*\ $iport\\*) return ;; esac
 	fi
 
+	case "$build_l" in *\ $iport\ *|*\ $iport\\*) return ;; esac
+
 	find_new_port "$originflavor" # sets global variable new_port
 
-	case `pkg version -t $iport $new_port 2>/dev/null` in
+	case `$PKG_CMD 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" ;;
@@ -2203,8 +2236,9 @@ update_build_l () {
 }
 
 update_port () {
-	local deps
+	local make_target deps
 
+	make_target=$(target_part "$1")
 	if [ -n "$2" ]; then
 		echo "===>>> Launching child to update $1 to $2"
 	else
@@ -2300,7 +2334,7 @@ make_dep_list () {
 			fail "make_dep_list: Unsupported option '$dep_type'"
 		esac
 	done
-	[ -n "$var_opt" ] && make $var_opt | tr ' ' '\n' | cut -d: -f2 | sort -u
+	[ -n "$var_opt" ] && pm_make $var_opt | tr ' ' '\n' | cut -d: -f2-3 | sort -u
 }
 
 gen_dep_list () {
@@ -2368,15 +2402,17 @@ dependency_check () {
 		rundeps=`gen_dep_list run-depends-list`
 
 		for dep in $d_port_list; do
+			# strip optional make target
+			local depdir=${dep%:*}
 			# If the port is already installed, do not mark
 			# it as a build-only dependency, or it will be
 			# installed by package and/or removed
-			pkg info -e ${dep#$pd/} &&
-				run_dl="$run_dl $dep" &&
+			$PKG_CMD info -e ${depdir#$pd/} &&
+				run_dl="$run_dl $depdir" &&
 				continue
 			case "$rundeps" in
 			*" ${dep} "*|*${dep}*)
-				varname=`echo ${dep#$pd/} | sed 's#[-+/\.@]#_#g'`
+				varname=`echo ${dep#$pd/} | sed 's#[-+/\.@:]#_#g'`
 				rundep_list="$rundep_list $varname"
 				eval $varname=\"$portdir \$$varname\"
 				eval ${varname}_p=$dep
@@ -2409,7 +2445,10 @@ dependency_check () {
 
 	# Do not export, for THIS parent process only
 	[ -n "$PM_FIRST_PASS" ] && doing_dep_check=doing_dep_check
-	for d_port in $d_port_list; do
+	for d_port_target in $d_port_list; do
+		# strip optional make target
+		local d_port=${d_port_target%:*}
+		local make_target=$(target_part "$d_port_target")
 		origin="${d_port#$pd/}"
 		if [ -n "$SHOW_WORK" ]; then
 			iport=`iport_from_origin $origin`
@@ -2429,23 +2468,14 @@ dependency_check () {
 		[ -z "$PM_URB_UP" ] &&
 			case "$CUR_DEPS" in *:${origin}:*) continue ;; esac
 
-		if [ -z "$PM_INDEX_ONLY" ]; then
+		if [ -z "$PM_INDEX_ONLY" -a "$make_target" = install ]; then
 			local conflicts glob confl_p dir flavor
 			dir=$(dir_part $d_port)
 			flavor=$(flavor_part $d_port)
-			conflicts=''
-			if pm_cd "$pd/$dir"; then
-				if grep -ql ^CONFLICTS Makefile ; then
-					conflicts=`FLAVOR=$flavor pm_make_b \
-							-V CONFLICTS \
-							-V CONFLICTS_BUILD \
-							-V CONFLICTS_INSTALL`
-				fi
-			else
-				fail "Cannot cd to $dir"
-			fi
+			pm_cd "$pd/$dir" || fail "Cannot cd to $dir"
+			conflicts=`FLAVOR=$flavor pm_make_b -V CONFLICTS -V CONFLICTS_BUILD -V CONFLICTS_INSTALL`	 
 			for glob in $conflicts; do
-				confl_p=`pkg query -g "%n-%v" $glob 2>/dev/null`
+				confl_p=`$PKG_CMD query -g "%n-%v" $glob 2>/dev/null`
 				if [ -n "$confl_p" ]; then
 					confl_p=${confl_p%% *}
 					d_port="$pd/`origin_from_pdb $confl_p`"
@@ -2498,7 +2528,11 @@ dependency_check () {
 			check_for_updates $iport $origin || fail 'Update failed'
 		else
 			check_interactive $origin || continue
-			update_port $origin
+			if [ "$make_target" != "install" ]; then
+				[ -z "$PM_FIRST_PASS" ] && post_clean_l="$post_clean_l $origin"
+				origin="$origin:$make_target"
+			fi
+			update_port "$origin"
 		fi
 	done
 	[ -n "$PM_FIRST_PASS" ] && unset doing_dep_check
@@ -2587,7 +2621,7 @@ post_first_pass () {
 		done
 
 		for dep in $build_only_dl_g; do
-			[ "`pkg query "%?r" ${dep#$pd/}`" = "1" ] && continue
+			[ "`$PKG_CMD query "%?r" ${dep#$pd/}`" = "1" ] && continue
 			[ -n "$PM_DEL_BUILD_ONLY" ] &&
 				iport_from_origin ${dep#$pd/} >/dev/null && continue
 			temp_bodlg="$temp_bodlg $dep"
@@ -2629,7 +2663,7 @@ urb_update () {
 			case " $PM_URB_ORIGINS" in *" $req_by_o "*) continue ;; esac
 			PM_URB_LIST="${PM_URB_LIST} ${req_by}"
 		done <<-EOF
-		$(pkg query "%rn-%rv %ro" ${origin})
+		$($PKG_CMD query "%rn-%rv %ro" ${origin})
 		EOF
 	done
 
@@ -2642,7 +2676,7 @@ urb_update () {
 
 	for req_by in $PM_URB_LIST; do
 		# Probably not needed, but JIC
-		pkg info -e $req_by || continue
+		$PKG_CMD info -e $req_by || continue
 
 		pm_v "===>>> $req_by depends on $PM_URB_IPORTS"
 
@@ -2683,7 +2717,7 @@ multiport () {
 			else
 				fail "$pd/${port} does not exist"
 			fi ;;
-		*)	if pm_isdir "$pdb/$port" && pkg info -e $port; then
+		*)	if pm_isdir "$pdb/$port" && $PKG_CMD info -e $port; then
 				worklist_temp="$worklist_temp $port"
 			else
 				find_glob_dirs $port
@@ -2773,8 +2807,8 @@ multiport () {
 
 		num=$(( $num + 1 ))
 		init_term_printf "$port ${num}/${numports}"
-		("$program" $ARGS $port) || update_failed=update_failed
-		. $IPC_SAVE && > $IPC_SAVE
+		($program $ARGS $port) || update_failed=update_failed
+		. $IPC_SAVE && : > $IPC_SAVE
 		[ -n "$update_failed" ] && fail "Update for $port failed"
 
 		case "$PM_NEEDS_UPDATE" in
@@ -2803,7 +2837,7 @@ multiport () {
 		*/*)	origin=$port ;;
 		*)	# If an installed version does not exist at this
 			# point it probably got updated as a dependency
-			if ! pkg info -e $port; then
+			if ! $PKG_CMD info -e $port; then
 				numports=$(( $numports - 1 ))
 				continue
 			fi
@@ -2823,7 +2857,7 @@ multiport () {
 		num=$(( $num + 1 ))
 		init_term_printf "$port ${num}/${numports}"
 		("$program" $ARGS $port) || update_failed=update_failed
-		. $IPC_SAVE && > $IPC_SAVE
+		. $IPC_SAVE && : > $IPC_SAVE
 		[ -n "$update_failed" ] && fail "Update for $port failed"
 	done
 
@@ -2951,7 +2985,7 @@ all_first_pass () {
 		origin=`origin_from_pdb $iport` || {
 			case "$?" in
 			3) ;;
-			2)	pm_v "	===>>> No ORIGIN for $iport, and +IGNOREME is present"
+			2)	pm_v "	===>>> No ORIGIN for $iport, and +IGNOREME is present or the package is locked"
 				echo "	===>>> Skipping"
 				echo '' ;;
 			*)	fail 'Cannot continue' ;;
@@ -3029,8 +3063,12 @@ no_valid_port () {
 	echo "===>>> Try $progname --help" ; echo '' ; safe_exit 1
 }
 
+make_target=$(target_part "$1")
+post_clean_l=""
+
 # Figure out what we are going to be working on
 if [ -z "$REPLACE_ORIGIN" ]; then
+	portdir="${1%:*}"
 	export_flavor $(flavor_part $portdir)
 	[ -n "$portdir" ] && { argv=$portdir ; unset portdir; }
 	argv=${argv:-$1} ; argv=${argv%/} ; argv=`globstrip $argv`
@@ -3048,7 +3086,7 @@ if [ -z "$REPLACE_ORIGIN" ]; then
 			*)	echo '' ; no_valid_port ;;
 			esac
 		done ;;
-	*)	upg_port=$(pkg query %n-%v "$argv") ;;
+	*)	upg_port=$($PKG_CMD query %n-%v "$argv") ;;
 	esac
 
 	if [ -z "$portdir" -a -z "$upg_port" ]; then
@@ -3061,9 +3099,11 @@ if [ -z "$REPLACE_ORIGIN" ]; then
 		unset glob_dirs
 	fi
 	unset argv
+
+	[ "$make_target" != install ] && PM_MAKE_ARGS="-DDISABLE_CONFLICTS $PM_MAKE_ARGS"
 else
 	portdir="${1#$pd/}" ; portdir="${portdir%/}"
-	export_flavor=$(flavor_part $portdir)
+	export_flavor $(flavor_part $portdir)
 	if [ -z "$PM_INDEX_ONLY" ]; then
 		pm_isdir_pd "$portdir" ] || missing=missing
 	else
@@ -3076,12 +3116,12 @@ else
 		echo '' ; no_valid_port
 	fi
 
-	upg_port=`iport_from_origin $portdir` || upg_port=$opd
+	upg_port=`iport_from_origin $portdir` || upg_port=$opd	# <se> $opd is never set???
 	arg2=${2#$pd/} ; arg2=${arg2#$pdb/} ; arg2=${arg2%/}
 
 	case "$arg2" in
 	*/*)	ro_opd=$arg2 ; ro_upg_port=`iport_from_origin $ro_opd` ;;
-	*)	if pm_isdir "$pdb/$arg2" && pkg info -e $arg2; then
+	*)	if pm_isdir "$pdb/$arg2" && $PKG_CMD info -e $arg2; then
 			ro_upg_port=$arg2
 		else
 			find_glob_dirs $arg2 && ro_upg_port=${glob_dirs#$pdb/}
@@ -3097,7 +3137,7 @@ else
 	unset arg2
 
 	if [ -z "$ro_upg_port" ]; then
-		pkg query -a "%do" | grep -q "^$ro_opd$"
+		$PKG_CMD query -a "%do" | grep -q "^$ro_opd$"
 		if [ "$?" -eq 1 ]; then
 			echo ''
 			echo "===>>> The second argument to -o can be a package name,"
@@ -3133,6 +3173,7 @@ if [ -z "$PM_INDEX_ONLY" ] && ! pm_isdir_pd "$portdir"
 	pm_isdir_pd "$moved_npd" || no_valid_port
 
 	[ "$$" -eq "$PM_PARENT_PID" ] && parent_exit
+	[ -z "$upg_port" ] && upg_port="$portdir"
 	exec "$program" $ARGS -o $moved_npd $upg_port
 	# NOT REACHED
 fi
@@ -3142,8 +3183,8 @@ iport_from_pkgname () {
 
 	dir=$(dir_part $1)
 	flavor=$(flavor_part $1)
-	pkgname=$(FLAVOR=$flavor make -C "$pd/$dir" -V PKGNAME) || return 1
-	pkg info -x "^${pkgname%-*}"'-[^-]*' 2>/dev/null
+	pkgname=$(FLAVOR=$flavor pm_make -C "$pd/$dir" -V PKGNAME) &&
+		$PKG_CMD query "%n-%v" "${pkgname%-*}"
 }
 
 if [ -z "$upg_port" -a -z "$REPLACE_ORIGIN" ]; then
@@ -3161,7 +3202,7 @@ if pm_islocked "$upg_port"; then
 		*:${upg_port}:*) ;;	# Let it build
 		*)	if [ -z "$FETCH_ONLY" ]; then
 				echo ''
-				echo "===>>> $upg_port has an +IGNOREME file"
+				echo "===>>> $upg_port has an +IGNOREME file or the package is locked"
 				get_answer_g n y "\t===>>> Update anyway? y/n"
 				case "$?" in
 				1)	;;	# Let it build
@@ -3175,7 +3216,7 @@ if pm_islocked "$upg_port"; then
 				esac
 			else
 				echo ''
-				echo "===>>> $upg_port has an +IGNOREME file, ignoring"
+				echo "===>>> $upg_port has an +IGNOREME file or the package is locked, ignoring"
 				echo ''
 				CUR_DEPS="${CUR_DEPS}${upg_port}:${portdir}:"
 				safe_exit
@@ -3183,7 +3224,7 @@ if pm_islocked "$upg_port"; then
 		esac
 	elif [ -n "$PM_URB_UP" ]; then
 		echo ''
-		echo "===>>> $upg_port has an +IGNOREME file, ignoring"
+		echo "===>>> $upg_port has an +IGNOREME file or the package is locked, ignoring"
 		echo ''
 		safe_exit
 	fi
@@ -3229,11 +3270,14 @@ if [ -n "$PM_FIRST_PASS" -a -z "$FETCH_ONLY" ]; then
 		fi
 	fi
 
-	update_build_l $portdir $upg_port
+	update_build_l "$portdir" "$upg_port" "$make_target"
 elif [ -n "$FETCH_ONLY" -a -n "$PM_PACKAGES" ]; then
 	update_pm_nu $portdir
 fi
 
+# Test for necessary privileges to actually install ports
+[ -n "$NO_ACTION" ] || [ "$($PM_SU_CMD id -u)" = 0 ] || fail "Insufficient privileges to install ports (run as root or set PM_SU_CMD)"
+
 # Do these things first time through
 if [ -z "$PM_INDEX_ONLY" -a -z "$PM_BUILDING" -a -z "$SHOW_WORK" -a -z "$NO_ACTION" ]; then
 	# Do not start this in the background until we are sure we are going to proceed
@@ -3389,7 +3433,7 @@ fetch_package () {
 		export ppd
 	fi
 
-	pm_isdir "$ppd" ] || { pm_sv Creating $ppd; pm_mkdir_s $ppd; }
+	pm_isdir "$ppd" || { pm_sv Creating $ppd; pm_mkdir_s $ppd; }
 
 	if [ -z "$FETCH_ARGS" ]; then
 		FETCH_ARGS=`pm_make -f/usr/share/mk/bsd.port.mk -V FETCH_ARGS 2>/dev/null`
@@ -3533,7 +3577,7 @@ notnewer () {
 		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
+			case `$PKG_CMD 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)" ;;
@@ -3549,7 +3593,7 @@ notnewer () {
 			pm_v "===>>> There is a package available ($latest_pv)"
 		fi
 	elif [ -n "$latest_pv" ]; then
-		case `pkg version -t $new_port $latest_pv` in
+		case `$PKG_CMD 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)"
@@ -3603,7 +3647,7 @@ if [ -z "$use_package" ]; then
 
 	pm_cd_pd $portdir
 	export_flavor $(flavor_part $portdir)
-	[ -z "$DONT_PRE_CLEAN" ] && { pm_make clean NOCLEANDEPENDS=ncd ||
+	[ -z "$DONT_PRE_CLEAN" ] && { pm_make_s clean NOCLEANDEPENDS=ncd ||
 		fail 'make clean failed'; }
 
 	fl_read=`echo ${TMPDIR}/f-${PM_PARENT_PID}-fetchlog-${portdir#*/}.*`
@@ -3632,19 +3676,27 @@ if [ -z "$use_package" ]; then
 
 # Return flavor for named pkg (must be executed in port directory!)
 pkg_flavor () {
-	local pkg="$1" flavor
+	local pkg="$1" flavor starttime
 	pm_make pretty-flavors-package-names | sed -ne 's!^\([A-Za-z0-9_]*\): *'$pkg'$!\1!p';
 }
 	export_flavor=$(pkg_flavor $new_port)
-	if [ -n "$HIDE_BUILD" ] && [ -n "$(pm_make -V LICENSE)" ]; then
+	if [ -n "$HIDE_BUILD" ] && [ -n "$(pm_make -V LICENSE)" ] && [ -z "$(pm_make -V DISABLE_LICENSES)" ]; then
 		pm_make extract ask-license || fail "make extract ask-license failed for $portdir"
 	fi
+	if [ "$make_target" = "extract" -o "$make_target" = "patch" ]; then
+		eval pm_make -DNO_DEPENDS $make_target $port_log_args || fail "make $make_target failed for $portdir"
+		safe_exit
+	fi
+	starttime=$(date "+%s")
 	eval pm_make build $port_log_args || fail "make build failed for $portdir"
+	[ "$make_target" = "build" ] && safe_exit
+	echo "===>>> Building the port required $(( `date "+%s"` - $starttime )) seconds"
 
 	pm_sv Running make stage
 	# Defining NO_DEPENDS ensures that we will control the installation
 	# of the depends, not bsd.port.mk.
 	eval pm_make -DNO_DEPENDS stage $port_log_args || fail "make stage failed for $portdir"
+	[ "$make_target" = "stage" ] && safe_exit
 else
 	[ -z "$local_package" ] && {
 		fetch_package $latest_pv || fail "Fetch for ${latest_pv}.txz failed"; }
@@ -3665,7 +3717,7 @@ if [ -n "$upg_port" -o -n "$ro_upg_port" ] && [ -z "$F
 		    grep -v ^$LOCALBASE_COMPAT > $pm_mktemp_file
 
 		unset temp
-		for file in `pkg query %Fp $UPGRADE_PORT |
+		for file in `$PKG_CMD query %Fp $UPGRADE_PORT |
 		    sort - $pm_mktemp_file | uniq -d`; do
 			temp="${temp}$file "
 		done
@@ -3688,7 +3740,7 @@ if [ -n "$upg_port" -o -n "$ro_upg_port" ] && [ -z "$F
 
 	if [ -n "$REPLACE_ORIGIN" -a -n "$ro_upg_port" ]; then # <se> not always true for port moved to flavored version with no version update???
 		# Delete any existing versions of the old port
-		np_orphan=`pkg query "%a" $ro_upg_port`
+		np_orphan=`$PKG_CMD query "%a" $ro_upg_port`
 		pm_sv "Running pkg delete for $ro_upg_port"
 		pm_pkg_delete_s $ro_upg_port
 	fi
@@ -3710,7 +3762,7 @@ if [ -n "$upg_port" -o -n "$ro_upg_port" ] && [ -z "$F
 
 		# If $ro_upg_port was non-automatic, keep its state
 		if [ "${np_orphan:-1}" -eq 1 ]; then
-			np_orphan=`pkg query "%a" $upg_port`
+			np_orphan=`$PKG_CMD query "%a" $upg_port`
 		fi
 		pm_sv "Running pkg delete for $upg_port"
 		pm_pkg_delete_s $upg_port
@@ -3776,7 +3828,7 @@ else
 	[ -n "$local_package" ] && ppd=${LOCAL_PACKAGEDIR}/All
 
 	echo "===>>> Installing package from: ${ppd}/${latest_pv}.txz"
-	if $PM_SU_CMD pkg add --accept-missing --force ${ppd}/${latest_pv}.txz; then
+	if $PM_SU_CMD $PKG_CMD add --accept-missing --force ${ppd}/${latest_pv}.txz; then
 		if [ -n "$PM_DELETE_PACKAGES" ]; then
 			pm_v "===>>> Deleting ${latest_pv}.txz"
 			pm_unlink_s ${ppd}/${latest_pv}.txz
@@ -3786,6 +3838,10 @@ else
 	fi
 fi
 
+if [ "$was_locked" = 1 ]; then
+	$PKG_CMD lock -qy "$new_port"
+fi
+
 if [ -n "$preserve_dir" ]; then
 	rmdir $preserve_dir 2>/dev/null
 	unset preserve_dir preserve_port_files
@@ -3801,14 +3857,14 @@ echo ''
 temp=`find $LOCALBASE_COMPAT -type d -empty 2>/dev/null`
 if [ -z "$temp" ] && pm_isdir "$LOCALBASE_COMPAT"; then
 	unset files
-	for file in `pkg query %Fp $new_port`; do
+	for file in `$PKG_CMD query %Fp $new_port`; do
 		[ -f "${LOCALBASE_COMPAT}/${file##*/}" ] &&
 			files="${files}${LOCALBASE_COMPAT}/${file##*/} "
 	done
 
 	if [ -n "$files" ]; then
 		pm_sv Removing old shared libraries, and running ldconfig
-		pm_rm_s `make -V FILES:O:u FILES="$files"`
+		pm_rm_s `pm_make -V FILES:O:u FILES="$files"`
 		$PM_SU_CMD /etc/rc.d/ldconfig start > /dev/null
 	fi
 	unset temp file files
@@ -3876,14 +3932,18 @@ if [ -n "$MAKE_PACKAGE" ]; then
 fi
 
 if [ -z "$use_package" -a -z "$DONT_POST_CLEAN" ]; then
-#	export_flavor $(flavor_part $portdir)
+	flavor=$(flavor_part $portdir)
+	for dir in $post_clean_l; do
+		pm_v "===>>> Running 'make clean' for special build dependency $dir"
+		(pm_make_s -C "$pd/$dir" ${flavor:+FLAVOR=$flavor} clean NOCLEANDEPENDS=ncd2 >/dev/null)&
+	done
 	pm_v "===>>> Running 'make clean' in the background"
-	(pm_make_s clean NOCLEANDEPENDS=ncd2 >/dev/null)&
+	(pm_make_s ${flavor:+FLAVOR=$flavor} clean NOCLEANDEPENDS=ncd2 >/dev/null)&
 	pm_v
 fi
 if [ -n "$ro_opd" ]; then
 	echo "===>>> Updating dependency entry for $new_port in each dependent port"
-	$PM_SU_CMD pkg set -yo $ro_opd:$portdir
+	$PM_SU_CMD $PKG_CMD set -yo $ro_opd:$portdir
 fi
 
 
@@ -3903,7 +3963,7 @@ if [ "$$" -ne "$PM_PARENT_PID" -o -n "$PM_URB" ]; then
 fi
 
 INSTALLED_LIST="${INSTALLED_LIST}\t${ilist}\n"
-[ -n "`pkg query %M $new_port`" ] && DISPLAY_LIST="${DISPLAY_LIST}$new_port "
+[ -n "`$PKG_CMD query %M $new_port`" ] && DISPLAY_LIST="${DISPLAY_LIST}$new_port "
 CUR_DEPS="${CUR_DEPS}${new_port}:${portdir}:"
 
 [ -n "$HIDE_BUILD" -a -n "$port_log" ] && pm_unlink $port_log