summaryrefslogtreecommitdiffstats
path: root/sysutils/portmaster/files/portmaster.sh.in
blob: d0baa17bd3ebedc0206e63fd9c97e2e1e4f75179 (plain)
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
#!/bin/sh

# Local version: 1.63
# $FreeBSD$

# Copyright (c) 2005-2006 Douglas Barton, All rights reserved
# Please see detailed copyright below

# %%LOCALBASE%% and %%X11BASE%% are needed in path for make
PATH=/bin:/usr/bin:/sbin:/usr/sbin:%%LOCALBASE%%/bin:%%X11BASE%%/bin
export PATH
umask 022

usage () {
	VERSION_NUMBER=`grep "[$]FreeBSD:" $0 | cut -d ' ' -f 4`
	echo "portmaster version $VERSION_NUMBER"
	echo ''
	echo 'Usage:'
	echo "${0##*/} [-CGanv uf|i D|d] <full name of port directory in $pdb>"
	echo "${0##*/} [-CGanv uf|i D|d] <glob pattern of directory in $pdb>"
	echo "${0##*/} [-CGanv uf|i D|d] -p <port directory in $pd>"
	echo "${0##*/} [-CGanv uf|i D|d] <full path to $pd/foo/bar>"
	echo "${0##*/} [-CGnv uf|i D|d] -r <name/glob of port directory in $pdb>"
	echo "${0##*/} -h"
	echo "${0##*/} -l"
	echo ''
	echo "-C prevents 'make clean' being run in port directory"
	echo "-G prevents recursive 'make config'"
	echo '-a check all ports, update as necessary'
	echo '-n do not launch child process to update dependencies'
	echo '-v verbose output'
	echo '-f always rebuild ports (overrides -i)'
	echo '-i interactive update mode'
	echo '-D prevents cleaning of distfiles'
	echo '-d always clean distfiles'
	echo "-m <arguments for the 'make' command line>"
	echo '-r rebuild port, and all ports that depend on it'
	echo "-u unattended mode -- accept defaults for all but 'make config'"
	echo '-h display this help file'
	echo '-l list installed ports by category'
	echo ''
	exit 1
}

fail () {
	echo ''
	echo "===>>> $1"
	echo "===>>> Aborting update"
	exit 1
}

safe_exit () {
	echo ''
	rm -f $grep_deps $req_deps
	test -n "$FORCE_PARENT" && rm -f $UPDATES_DONE_LIST
	test -n "$CONFIG_PARENT" && rm -f $CONFIG_SEEN_LIST
	exit 0
}

update_contents () {
	local tempfile

	tempfile=`mktemp -t tempfile-${new_port}`
	sed "s/@pkgdep $1/@pkgdep $2/" $dep_port_contents > $tempfile &&
	    mv $tempfile $pdb/$dep_port/+CONTENTS
	    chmod 644 $pdb/$dep_port/+CONTENTS
}

dep_warn () {
	local num_ports1

	echo ''
	echo "===>>> Warning! Potential unrecorded dependencies on $new_port"
	echo "===>>> From existing +CONTENTS files:"
	grep "pkgdep ${short_port}-" $pdb/*/+CONTENTS |
	    sort -u | sed "s#$pdb/##"
	num_ports1=`grep "pkgdep ${short_port}-" $pdb/*/+CONTENTS |
	    sort -u | wc -l | awk '{print $1}'`
	echo "===>>> $num_ports1 ports"
	echo ''
}

update_reqfile () {
	local num_ports2 sdiff_deps

	dep_warn
	num_ports2=`wc -l $req_deps | awk '{print $1}'`
	echo "===>>> From $pdb/$upg_port/+REQUIRED_BY:"
	cat $req_deps
	echo "===>>> $num_ports2 ports"
	echo ''
	echo "===>>> Use dependencies from existing +CONTENTS files	[c]"
	echo "===>>> Use dependencies from existing +REQUIRED_BY file	[r]"
	echo "===>>> Use sdiff to edit both files into a new file	[s]"
	echo ''
	if [ -z "$UNATTENDED" ]; then
		echo -n "===>>> Update dependency list? [r] "
		read DEPUPDATE

		case "$DEPUPDATE" in
		[cC])	mv $grep_deps $req_deps ;;
		[sS])	sdiff_deps=`mktemp -t sdiff-deps-${short_port}`
			sdiff -o $sdiff_deps --text --suppress-common-lines \
			    --width=`tput columns` $req_deps $grep_deps
			mv $sdiff_deps $req_deps
			;;
		esac
	else
		echo "===>>> Default (use +REQUIRED_BY file) in unattended mode"
	fi
}

update_port () {
	local upd

	case "$1" in
	-p)	upd=$2 ;;
	*)	upd=$1 ;;
	esac

	echo "===>>> Launching child to update $upd"
	echo ''
	if [ -z "$NO_ACTION" ]; then
		($0 $ARGS $@) || fail "Update for $upd failed"
	else
		test -n "$VERBOSE" &&
		    echo "===>>> Build canceled due to -n flag"
	fi

	if [ -n "$UPDATE_ALL" ]; then
		echo "===>>> Returning to update check of installed ports"
	elif [ -n "$UPDATE_REQ_BYS" ]; then
		return 0
	elif [ -n "$CONFIG_ONLY" ]; then
		echo "===>>> Continuing 'make config' dependency check for $portdir"
	else
		echo "===>>> Returning to dependency check for $portdir"
	fi

	return 0
}

check_interactive () {
	local UPD_OR_NOT

	if [ -n "$INTERACTIVE_UPDATE" ]; then
		echo -n "===>>> Update ${1}? [y] "
		read UPD_OR_NOT
		case "$UPD_OR_NOT" in
		[nN]*)	return 1 ;;
		esac
	fi
	return 0
}

check_for_updates () {
	local upd_port port_ver do_update

	upd_port=`grep ' ORIGIN' $pdb/$1/+CONTENTS | cut -f2 -d':'`

	if [ -d "$pd/$upd_port" ]; then
		cd $pd/$upd_port ||
		    fail "Cannot cd to port directory: $pd/$upd_port"
		port_ver=`make $MAKE_ARGS -V PKGNAME`

		test "$1" = "$port_ver" && return 0

		case `pkg_version -t $1 $port_ver` in
		\<)	do_update=yes ;;
		*)	if [ -n "$VERBOSE" ]; then
				echo ''
				echo "===>>> Port version $port_ver does not"
				echo "===>>> seem newer than installed $1"
				echo ''
			fi
			;;
		esac
	else
		# This will fail if it doesn't exist anymore
		# It will return 1 if we know nothing about the port
		find_moved_port $upd_port || return 0

		# If the port has moved, we have to update it
		do_update=yes
	fi

	if [ -n "$do_update" ]; then
		if ! check_interactive $1 ; then
			return 0
		fi

		update_port $1 || return 1
	fi

	return 0
}

find_moved_port () {
	# newportdir is used globally
	local m

	newportdir=`sed -ne "s#^${1}|\([^|]*\)|.*#\1#p" $pd/MOVED`
	if [ ! -n "$newportdir" ]; then
		m=`sed -ne "s#^${1}||.*|\(.*\)#\1#p" $pd/MOVED`
		if [ -n "$m" ]; then
			fail "The $1 port has been deleted: $m"
		else
			echo ''
			echo "===>>> No $pd/$1 exists, and no information"
			echo "===>>> about $1 can be found in $pd/MOVED"
			echo ''
			return 1
		fi
	fi

	# Just in case there was more than one match, use the last one
	newportdir=`echo $newportdir | sed 's/.* //'`
	echo "===>>> The $1 port has moved to $pd/$newportdir"

	return 0
}

ports_by_category () {
	local pkg

	for pkg in $pdb/*; do
		# Handle regular files, such as portupgrade's pkgdb.db
		test -d "$pkg" || continue

		if [ -e "$pkg/+REQUIRED_BY" ]; then
			if grep '^@pkgdep ' $pkg/+CONTENTS >/dev/null; then
				branches="$branches $pkg"
			else
				trunks="$trunks $pkg"
			fi
		else
			if grep '^@pkgdep ' $pkg/+CONTENTS >/dev/null; then
				leaves="$leaves $pkg"
			else
				roots="$roots $pkg"
			fi
		fi
	done
}

dependency_check () {
	local dep_port_list dep_port cur_p upd_args p op old_p

	# Print a message here because sometimes all-depends-list takes
	# a long time to return.
	if [ -n "$CONFIG_ONLY" ]; then
		echo "===>>> Checking $portdir dependencies for recursive 'make config'"
	else
		echo "===>>> Checking if dependencies of $portdir are up to date"
	fi

	dep_port_list=`make $MAKE_ARGS all-depends-list`
	if [ -z "$dep_port_list" ]; then
		echo "===>>> No dependencies for $portdir"
		return 0
	fi

	for dep_port in $dep_port_list; do
		test -n "$VERBOSE" &&
		    echo "===>>> Checking dependency: $dep_port"

		if [ -n "$CONFIG_SEEN_LIST" ]; then
			if [ `grep "${dep_port}$" $CONFIG_SEEN_LIST` ]; then
				continue
			else
				echo "$dep_port" >> $CONFIG_SEEN_LIST
			fi
		fi

		if [ -n "$UPDATES_DONE_LIST" ]; then
			if [ `grep "${dep_port}$" $UPDATES_DONE_LIST` ]; then
				continue
			fi
		fi

		cur_p=`grep -l " ORIGIN:${dep_port#$pd/}$" $pdb/*/+CONTENTS`
		if [ -n "$cur_p" ]; then
			cur_p=${cur_p%/+CONTENTS}
			cur_p=${cur_p##*/}
			upd_args=$cur_p
		else
			upd_args="-p $dep_port"		# Sensible default
			# Check to see if the dependency has moved because
			# if so, we need to update the old port to fix it
			p=${dep_port#$pd/}
			op=`sed -ne "s#\([^|]*\)|$p|.*#\1#p" $pd/MOVED`
			# In case there is more than one match, use the latest
			op=`echo $op | sed 's/.* //'`

			if [ -n "$op" ]; then
				old_p=`grep -l " ORIGIN:${op}$" $pdb/*/+CONTENTS`
				if [ -n "$old_p" ]; then
					old_p=${old_p%/+CONTENTS}
					old_p=${old_p##*/}
					upd_args=$old_p
				fi
			fi
		fi

		if [ -n "$FORCE" ]; then
			echo "===>>> Forcing update for $dep_port"
			update_port $upd_args
			continue
		fi

		# If not forcing the update
		if [ -n "$cur_p" ]; then
			check_for_updates $cur_p
		elif [ -n "$old_p" ]; then
			check_for_updates $old_p
		else
			if ! check_interactive $dep_port ; then
				continue
			fi

			update_port -p $dep_port
		fi
	done
	if [ -n "$CONFIG_ONLY" ]; then
		echo "===>>> Recursive 'make config' check complete for $portdir"
	else
		echo "===>>> Dependency check complete for $portdir"
	fi
}

unset_recursive_config () {
	rm -f $CONFIG_SEEN_LIST
	unset CONFIG_ONLY CONFIG_SEEN_LIST

	# Overload this variable to simplify the code
	NO_RECURSIVE_CONFIG=yes
	export NO_RECURSIVE_CONFIG
}

req_by_error () {
	local DISCARD

	echo "===>>> WARNING! $pdb/$1/+REQUIRED_BY "
	echo "===>>> shows that $2 requires $1, but "
	echo "===>>> $2 does not seem to be installed"
	echo -n "===>>> Press Enter to proceed "
	read DISCARD
}

pd=`make $MAKE_ARGS -f/dev/null -V PORTSDIR 2>/dev/null`
pdb=`make $MAKE_ARGS -f/dev/null -V PKG_DBDIR 2>/dev/null`

# Read a global rc file first
if [ -r /etc/portmaster.rc ]; then
	. /etc/portmaster.rc
fi

# Read a local one next, and allow the command line to override
if [ -r "$HOME/.portmasterrc" ]; then
	. $HOME/.portmasterrc
fi

# Set default values here so that they can be overriden above
: ${pd:=/usr/ports}
: ${pdb:=/var/db/pkg}

# Save switches for potential child processes
while getopts 'CDGadfhilm:np:r:uv' COMMAND_LINE_ARGUMENT ; do
	case "${COMMAND_LINE_ARGUMENT}" in
	C)	DONT_PRE_CLEAN=yes; ARGS="-C $ARGS" ;;
	D)	DONT_SCRUB_DISTFILES=yes; ARGS="-D $ARGS" ;;
	G)	NO_RECURSIVE_CONFIG=yes; ARGS="-G $ARGS" ;;
	a)	UPDATE_ALL=yes ;;
	d)	ALWAYS_SCRUB_DISTFILES=yes; ARGS="-d $ARGS" ;;
	f)	FORCE_PARENT=yes
		FORCE=yes
		UPDATES_DONE_LIST=`mktemp -t updates_done_list`
		export FORCE UPDATES_DONE_LIST
		;;
	h)	usage ; exit 0 ;;
	i)	INTERACTIVE_UPDATE=yes; ARGS="-i $ARGS" ;;
	l)	LIST=yes ;;
	m)	MAKE_ARGS=$OPTARG; ARGS="-m $MAKE_ARGS $ARGS" ;;
	n)	NO_ACTION=yes ;;
	p)	portdir="${OPTARG#$pd/}" ;;
	r)	UPDATE_REQ_BYS=yes; upg_port=$OPTARG ;;
	u)	UNATTENDED=yes; ARGS="-u $ARGS" ;;
	v)	VERBOSE=yes; ARGS="-v $ARGS" ;;
	esac
done

if [ -n "$LIST" ]; then
	ports_by_category

	num_roots=0
	num_trunks=0
	num_branches=0
	num_leaves=0

	echo "===>>> Root ports (No dependencies, not depended on)"
	for port in $roots; do
		echo "===>>> ${port##*/}"
		num_roots=$(( $num_roots + 1 ))
	done
	echo "===>>> $num_roots root ports"
	echo ''
	echo "===>>> Trunk ports (No dependencies, are depended on)"
	for port in $trunks; do
		echo "===>>> ${port##*/}"
		num_trunks=$(( $num_trunks + 1 ))
	done
	echo "===>>> $num_trunks trunk ports"
	echo ''
	echo "===>>> Branch ports (Have dependencies, are depended on)"
	for port in $branches; do
		echo "===>>> ${port##*/}"
		num_branches=$(( $num_branches + 1 ))
	done
	echo "===>>> $num_branches branch ports"
	echo ''
	echo "===>>> Leaf ports (Have dependencies, not depended on)"
	for port in $leaves; do
		echo "===>>> ${port##*/}"
		num_leaves=$(( $num_leaves + 1 ))
	done
	echo "===>>> $num_leaves leaf ports"
	echo ''
	num_ports=$(( $num_roots + $num_trunks + $num_branches + $num_leaves ))
	echo "===>>> $num_ports total installed ports"
	exit 0
fi

test -n "$FORCE" && unset INTERACTIVE_UPDATE
if [ -n "$UNATTENDED" ]; then
	unset INTERACTIVE_UPDATE

	if [ -z "$DONT_SCRUB_DISTFILES" -a -z "$ALWAYS_SCRUB_DISTFILES" ]; then
		ALWAYS_SCRUB_DISTFILES=yes
		ARGS="-d $ARGS"
	fi
fi

if [ -z "$CONFIG_ONLY" -a -z "$NO_RECURSIVE_CONFIG" ]; then
	CONFIG_SEEN_LIST=`mktemp -t config_seen_list`
	CONFIG_ONLY=yes
	export CONFIG_SEEN_LIST CONFIG_ONLY

	CONFIG_PARENT=yes
fi

if [ -n "$UPDATE_ALL" ]; then
	echo "===>>> Starting check of installed ports for available updates"
	ports_by_category

	if [ -n "$CONFIG_SEEN_LIST" ]; then
		echo "===>>> Checking ports for recursive 'make config'"
		for pkg in $roots $trunks $branches $leaves; do
			p=`grep ' ORIGIN' $pkg/+CONTENTS | cut -f2 -d':'`
			if [ `grep "${pd}/${p}$" $CONFIG_SEEN_LIST` ]; then
				continue
			else
				echo "${pd}/${p}" >> $CONFIG_SEEN_LIST
				check_for_updates ${pkg##*/} ||
				    fail 'Update failed'
			fi
		done
		unset_recursive_config
		echo ''
		echo "===>>> Starting build for ports that need updating <<<==="
	fi

	for pkg in $roots $trunks $branches $leaves; do
		if [ ! -d "$pkg" ]; then
			# This port probably got updated as a dependency
			# for something else
			continue
		fi
		if [ -n "$FORCE" ]; then
			p=`grep ' ORIGIN' $pkg/+CONTENTS | cut -f2 -d':'`
			if [ ! `grep "${pd}/${p}$" $UPDATES_DONE_LIST` ]; then
				echo "===>>> Forcing update for ${pkg##*/}"
				update_port ${pkg##*/}
				continue
			else
				test -n "$VERBOSE" &&
				    echo "===>>> Update for $p already done"
			fi
		else
			test -n "$VERBOSE" &&
			    echo "===>>> Checking installed port: ${pkg##*/}"
		fi
		check_for_updates ${pkg##*/} || fail 'Update failed'
	done
	echo "===>>> Update check of installed ports complete"
	safe_exit
fi

# Exercised in the common case of not using -p option
case "$portdir" in
'')	eval arg=\$${OPTIND}
	case "${arg}" in
	'')	test -z "$UPDATE_REQ_BYS" && usage ;;
	${pd}/*)	portdir="${arg#$pd/}" ;;
	/*)	upg_port="${arg##*/}" ;;
	*)	upg_port=$arg ;;
	esac
esac

case "$upg_port" in
'')	test -n "$portdir" || usage
	old_port_dir=`grep -l "@comment ORIGIN:${portdir}$" $pdb/*/+CONTENTS`
	if [ -n "$old_port_dir" ]; then
		old_port_dir="${old_port_dir%/+CONTENTS}"
		upg_port="${old_port_dir#$pdb/}"
	fi
	;;
*)	if [ ! -d "$pdb/$upg_port" ]; then
		glob_dirs=`find $pdb -type d -name ${upg_port}\*`
		case "$glob_dirs" in
		*\*)	fail "$upg_port did not match an installed port" ;;
		*)	for dir in $glob_dirs; do
				echo -n "===>>> Update ${dir#$pdb/}? [n] "
				read GLOB_DIR
				case "$GLOB_DIR" in
				[yY])	upg_port=${dir#$pdb/}
					selected=yes
					break
					;;
				esac
			done
			test -n "$selected" || usage
			;;
		esac
	fi
	echo "===>>> Port to upgrade: $upg_port"
	portdir=`grep ' ORIGIN' $pdb/$upg_port/+CONTENTS | cut -f2 -d':'`
	;;
esac

if [ -d "$pd/$portdir" ]; then
	echo "===>>> Port directory: $pd/$portdir"
else
	find_moved_port $portdir
	portdir=$newportdir
fi

cd $pd/$portdir || usage

echo "===>>> Launching 'make checksum' for $portdir in background"
fetchlog=`mktemp -t fetchlog-${portdir##*/}`
(make $MAKE_ARGS checksum >$fetchlog 2>&1 ; rm $fetchlog)&

# Do this here so that the dependency list is accurate
# But only if we have not done it recursively first
test -z "$NO_RECURSIVE_CONFIG" && make $MAKE_ARGS config

if [ -n "$CONFIG_ONLY" ]; then
	dependency_check

	if [ -z "$CONFIG_PARENT" ]; then
		safe_exit
	else
		if [ -n "$UPDATE_REQ_BYS" -a \
		    -r "$pdb/$upg_port/+REQUIRED_BY" ]; then
			echo "$pd/$portdir" >> $CONFIG_SEEN_LIST

			echo ''
			echo "===>>> Checking ports that depend on $upg_port"
			for req_by in `cat $pdb/$upg_port/+REQUIRED_BY`; do
				test -n "$VERBOSE" &&
				  echo "===>>> $req_by is required by $upg_port"
				if [ ! -d "$pdb/$req_by" ]; then
					req_by_error $upg_port $req_by
					continue
				fi
				if ! check_interactive ${req_by} ; then
					continue
				fi

				update_port $req_by
			done
			echo "===>>> Done checking ports that depend on $upg_port"
		fi

		unset_recursive_config
	fi
	echo ''
	echo "===>>> Starting build for $portdir <<<==="
	echo ''
fi

cd $pd/$portdir
dependency_check

if [ -n "$NO_ACTION" ]; then
	test -n "$VERBOSE" &&
	    echo "===>>> Build canceled due to -n flag"
	safe_exit
fi

case "$DONT_PRE_CLEAN" in
'')	make $MAKE_ARGS clean || fail 'make clean failed' ;;
esac

# In case we went elsewhere in the dependency check
cd $pd/$portdir

while [ -f "$fetchlog" ]; do
	echo ''
	echo "===>>> Waiting on fetch to complete for $portdir <<<==="
	tail -10 $fetchlog | grep -v '^$'
	echo ''
	echo "===>>> Waiting on fetch to complete for $portdir <<<==="
	sleep 3
done

make $MAKE_ARGS || fail "make failed for $portdir"

new_port=`make $MAKE_ARGS -V PKGNAME`
prefix=`make $MAKE_ARGS -V PKGNAMEPREFIX`
portname=`make $MAKE_ARGS -V PORTNAME`
suffix=`make $MAKE_ARGS -V PKGNAMESUFFIX`
short_port="${prefix}${portname}${suffix}"

# Check for dependencies here in case +REQUIRED_BY is not up to date or missing
grep_deps=`mktemp -t grep-deps-${short_port}`
grep -l "pkgdep ${short_port}-" $pdb/*/+CONTENTS | cut -f 5 -d '/' |
    sort -u > $grep_deps

if [ -s "$pdb/$upg_port/+REQUIRED_BY" ]; then
	req_deps=`mktemp -t req-deps-${short_port}`
	sort -u $pdb/$upg_port/+REQUIRED_BY > $req_deps
fi

if [ ! -s "$grep_deps" -a ! -s "$req_deps" ]; then
	if [ -n "$upg_port" ]; then
		echo "===>>> $upg_port is not depended on by any other ports"
	fi
elif [ -s "$grep_deps" -a -s "$req_deps" ]; then
	if ! cmp -s $grep_deps $req_deps; then
		update_reqfile
	fi
elif [ -s "$grep_deps" -a ! -s "$req_deps" ]; then
	dep_warn
	echo -n "===>>> Install these as the new +REQUIRED_BY file? [n] "
	if [ -z "$UNATTENDED" ]; then
		read INSTALLDEPS

		case "$INSTALLDEPS" in
		[yY])	req_deps=`mktemp -t req-deps-${short_port}`
			mv $grep_deps $req_deps
			;;
		esac
	else
		echo "===>>> Default (no) in unattended mode"
	fi
else
	# It should not happen that req_deps exist but grep_deps does not
	fail "$pdb/$upg_port/+REQUIRED_BY indicates a dependency on this port, but no other ports have this dependency recorded"
fi

# Ignore if no old port exists
if [ -n "$upg_port" ]; then
	pkg_delete -f $upg_port || fail 'pkg_delete failed'
fi

make $MAKE_ARGS install clean || fail 'installation of new port failed'

# By now, if this file exists, it should be authoritative
if [ -s "$req_deps" ]; then
	echo "===>>> Updating package dependency entry for each dependent port"
	while read dep_port; do
		dep_port_contents="$pdb/$dep_port/+CONTENTS"
		if grep -q "@pkgdep $upg_port" $dep_port_contents; then
			update_contents $upg_port $new_port
		else
			echo -n "===>>> In ${dep_port}"
			echo " no entry for $upg_port, trying $short_port"
			update_contents "$short_port.*" $new_port
		fi
	done < $req_deps

	mv $req_deps $pdb/$new_port/+REQUIRED_BY
	chmod 644 $pdb/$new_port/+REQUIRED_BY
fi

echo ''
test -z "$upg_port" && upg_port=$portdir
echo "===>>> Upgrade for $upg_port to $new_port succeeded"

test -n "$UPDATES_DONE_LIST" && echo "$pd/$portdir" >> $UPDATES_DONE_LIST

if [ -z "$DONT_SCRUB_DISTFILES" ]; then
	distdir=`make $MAKE_ARGS -V DISTDIR`
	dist_subdir=`make $MAKE_ARGS -V DIST_SUBDIR`
	test -n "$dist_subdir" && distdir="${distdir}/${dist_subdir}"

	distfiles=`make $MAKE_ARGS -V DISTFILES`
	cd $distdir || fail "cd to $distdir failed!"
	for distfile in $distfiles; do
		for file in ${distfile%%[_-]*}*; do
			# This generally means the pattern did not match
			case "$file" in
			*\*)	continue ;;
			esac

			case "$distfiles" in
			*${file}*)
				test -n "$VERBOSE" &&
				    echo "===>>> Keeping new distfile: $file"
				continue	# Do not delete current version
				;;
			*)	if [ -n "$ALWAYS_SCRUB_DISTFILES" ]; then
					echo "===>>> Deleting stale distfile: $file"
					rm -f $file
					continue
				fi

				echo -n "===>>> Delete $file? [n] "
				read DELORNOT
				case "$DELORNOT" in
				[yY])	rm -f $file ;;
				esac
				;;
			esac
		done
	done
fi

if [ -n "$UPDATE_REQ_BYS" -a -r "$pdb/$new_port/+REQUIRED_BY" ]; then
	echo ''
	echo "===>>> Updating ports that depend on $new_port"
	for req_by in `cat $pdb/$new_port/+REQUIRED_BY`; do
		test -n "$VERBOSE" &&
		    echo "===>>> $req_by is required by $new_port"
		if [ ! -d "$pdb/$req_by" ]; then
			req_by_error $new_port $req_by
			continue
		fi
		if ! check_interactive ${req_by} ; then
			continue
		fi

		update_port $req_by
	done
	echo "===>>> Done updating ports that depend on $new_port"
fi

safe_exit

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#  Copyright (c) 2005-2006 Douglas Barton
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
#  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
#  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
#  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
#  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
#  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
#  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#  SUCH DAMAGE.
OpenPOWER on IntegriCloud