summaryrefslogtreecommitdiff
path: root/pkgtools/pkgchk/files/pkgchk.sh
blob: d98aa60481e0e991c39ef3af913eb5fcb9df6d21 (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
#!/bin/sh -e
#
# $Id: pkgchk.sh,v 1.19 2001/09/29 08:24:23 abs Exp $
#
# TODO: Handle and as well as or tags (eg: i386+x11)
# TODO: Handle updates with dependencies via binary packages

PATH=/usr/sbin:${PATH}

echo_n()
    {
    echo $ac_n "$*"$ac_c
    }

extract_make_vars()
    {
    MAKEDATA="x:\n";
    for var in $* ; do
	MAKEDATA=$MAKEDATA"\t@echo $var=\${$var}\n"
    done
    eval `printf "$MAKEDATA" | ${MAKE} -f - -f Makefile x`
    }

extract_variables()
    {
    # Establish PKGSRCDIR
    #

    if [ -z "$PKGSRCDIR" ];then
	if [ -f /etc/mk.conf ] ;then
	    eval `printf 'BSD_PKG_MK=1\nx:\n\t@echo PKGSRCDIR=${PKGSRCDIR}\n' | ${MAKE} -f - -f /etc/mk.conf x` 
	fi
	if [ -z "$PKGSRCDIR" ];then
	    PKGSRCDIR=/usr/pkgsrc
	fi
    fi
    if [ ! -d $PKGSRCDIR ];then
	echo "Unable to locate PKGSRCDIR '$PKGSRCDIR'"
	exit 1;
    fi

    # Now we have PKGSRCDIR, use it to determine PACKAGES, and PKGCHK_CONF
    #

    cd $PKGSRCDIR/pkgtools/pkgchk
    extract_make_vars AWK GREP SED PACKAGES PKGCHK_CONF

    if [ -z "$PACKAGES" ];then
	PACKAGES=$PKGSRCDIR/packages
    fi

    if [ -z "$PKGCHK_CONF" ];then
	PKGCHK_CONF=$PKGSRCDIR/pkgchk.conf
    fi
    }

get_build_version()
    {
    extract_make_vars FILESDIR PKGDIR DISTINFO_FILE PATCHDIR
    files=""
    for f in `pwd`/Makefile ${FILESDIR}/* ${PKGDIR}/*; do
	if [ -f $f ];then
	    files="$files $f"
	fi
    done
    if [ -f ${DISTINFO_FILE} ]; then
	for f in `${AWK} 'NF == 4 && $3 == "=" { gsub("[()]", "", $2); print $2 }' < ${DISTINFO_FILE}`; do 
	    if [ -f ${PATCHDIR}/$f ]; then
		files="$files ${PATCHDIR}/$f";
	    fi;
	done
    fi
    if [ -d ${PATCHDIR} ]; then
	for f in ${PATCHDIR}/patch-*; do
	    case $f in
	    *.orig|*.rej|*~) ;;
	    ${PATCHDIR}/patch-local-*)
		files="$files $f" ;;
	    esac
	done
    fi
    ${GREP} '\$NetBSD' $files | ${SED} -e "s|^${real_pkgsrcdir}/||"
    }

pkg_install()
    {
    PKGNAME=$1
    PKGDIR=$2
    INSTALL=$3

    if [ -d /var/db/pkg/$PKGNAME ];then
	echo "$PKGNAME installed in previous stage"
    elif [ -n "$opt_b" -a -f $PACKAGES/All/$PKGNAME.tgz ] ; then
	if [ $INSTALL = U ];then
	    PKG=`echo $PKGNAME | sed 's/-[0-9].*//'`
	    run_cmd "pkg_delete $PKG" 1
	    if [ -n "$FAIL" ]; then
		echo "Can only update packages with dependencies via -s"
		exit 1
	    fi
	fi
	run_cmd "pkg_add $PACKAGES/All/$PKGNAME.tgz"
    elif [ -n "$opt_s" ]; then
	run_cmd "cd $PKGSRCDIR/$PKGDIR && ${MAKE} update"
    fi

    if [ -z "$opt_n" -a ! -d /var/db/pkg/$PKGNAME ];then
	FAIL=1
    fi

    if [ -n "$FAIL" ]; then
	FAIL_DONE=$FAIL_DONE" "$PKGNAME
    elif [ $INSTALL = U ];then
	UPDATE_DONE=$UPDATE_DONE" "$PKGNAME
    else
	INSTALL_DONE=$INSTALL_DONE" "$PKGNAME
    fi
    }

run_cmd()
    {
    FAIL=
    if [ -n "$2" ]; then
	FAILOK=$2
    else
	FAILOK=$opt_k
    fi
    echo $1
    if [ -z "$opt_n" ];then
        sh -c "$1" || FAIL=1
	if [ -n "$FAIL" ] ; then
            echo "** '$1' failed"
            if [ "$FAILOK" != 1 ]; then
                exit 1
            fi
        fi
    fi
    }

args=`getopt BD:U:abchiknrsuv $*`
if [ $? != 0 ]; then
    opt_h=1
fi
set -- $args
while [ $# != 0 ]; do
    case "$1" in
	-B )    opt_B=1 ; opt_i=1 ;;
	-D )	opt_D="$2" ; shift;;
	-U )	opt_U="$2" ; shift;;
	-a )	opt_a=1 ; opt_c=1 ;;
	-b )	opt_b=1 ;;
	-c )	opt_c=1 ;;
	-h )	opt_h=1 ;;
	-i )	opt_i=1 ;;
	-k )	opt_k=1 ;;
	-n )	opt_n=1 ;;
	-r )	opt_r=1 ;;
	-s )	opt_s=1 ;;
	-u )	opt_u=1 ; opt_i=1 ;;
	-v )	opt_v=1 ;;
	-- )	break ;;
    esac
    shift
done

if [ -z "$opt_b" -a -z "$opt_s" ];then
    opt_b=1; opt_s=1;
fi

if [ -z "$opt_a" -a -z "$opt_c" -a -z "$opt_i" ];then
    echo "Must specify at least one of -a, -c, -i, or -u";
    echo
    opt_h=1;
fi

if [ -n "$opt_h" -o $# != 1 ];then
    echo 'Usage: pkg_chk [opts]
	-B      Check the "Build version" of packages (implies -i)
	-D tags Comma separated list of additional pkgchk.conf tags to set
	-U tags Comma separated list of pkgchk.conf tags to unset
	-a      Add all missing packages (implies -c)
	-b      Limit installations to binary packages
	-c      Check installed packages against pkgchk.conf
	-h      This help
	-i	Check versions of installed packages (not using pkgchk.conf)
	-k	Continue with further packages if errors are encountered
	-n	Display actions that would be taken, but do not perform them
	-r	Recursively remove mismatched files (use with care)
	-s      Limit installations to building from source
	-u      Update all mismatched packages (implies -i)
	-v      Verbose

pkg_chk verifies installed packages against pkgsrc.
The most common usage is 'pkg_chk -i' to check all installed packages.
For more advanced usage, including defining a set of desired packages based
on hostname and type, see pkg_chk(8).
'
    exit 1
fi

test -n "$MAKE" || MAKE="@MAKE@"

# grabbed from GNU configure
if (echo "testing\c"; echo 1,2,3) | grep c >/dev/null; then
  # Stardent Vistra SVR4 grep lacks -e, says ghazi@caip.rutgers.edu.
  if (echo -n testing; echo 1,2,3) | sed s/-n/xn/ | grep xn >/dev/null; then
    ac_n= ac_c='
' ac_t='	'
  else
    ac_n=-n ac_c= ac_t=
  fi
else
  ac_n= ac_c='\c' ac_t=
fi

extract_variables

cd $PKGSRCDIR
real_pkgsrcdir=`pwd`

if [ -n "$opt_i" ];then
    PKGDIRLIST=`pkg_info -B \* | ${AWK} '/PKGPATH= /{print $2}'`
fi

if [ -n "$opt_c" ];then

    if [ ! -r $PKGCHK_CONF ];then
	echo "Unable to read PKGCHK_CONF '$PKGCHK_CONF'"
	exit 1;
    fi

    # Determine list of tags
    #
    TAGS="`hostname | sed -e 's,\..*,,'`,`uname -srm | ${AWK} '{print $1"-"$2"-"$3","$1"-"$2","$1"-"$3","$1","$2","$3}'`"
    if [ -f /usr/X11R6/lib/libX11.so -o -f /usr/X11R6/lib/libX11.a ];then
	TAGS="$TAGS,x11"
    fi
    if [ -n "$opt_D" ];then
	TAGS="$TAGS,$opt_D"
    fi
    if [ -n "$opt_v" ];then
	echo "set   TAGS=$TAGS"
	echo "unset TAGS=$opt_U"
    fi

    # Extract list of valid pkgdirs (skip any 'alreadyset' in $PKGDIRLIST)
    #
    PKGDIRLIST="$PKGDIRLIST "`${AWK} -v alreadyset="$PKGDIRLIST" -v setlist=$TAGS -v unsetlist=$opt_U '
    BEGIN {
	split(alreadyset, tmp, " ");
	for (tag in tmp) { alreadyset[tmp[tag]] = 1; }

	split(setlist, tmp, ",");
	for (tag in tmp) { taglist[tmp[tag]] = 1; }

	split(unsetlist, tmp, ",");
	for (tag in tmp) { delete taglist[tmp[tag]] }

	taglist["*"] = "*"
   	}
    {
    sub("#.*", "");
    if (alreadyset[$1])
	{ next; }
    need = 0;
    for (f = 1 ; f<=NF ; ++f)		# For each word on the line
	{
	if (sub("^-", "", $f))	# If it begins with a '-'
	    {
	    if ($f in taglist)	# If match, discard
		{ next; }
	    }
	else
	    {
	    if ($f in taglist)	# If match, note needed
		{ need = 1; }
	    }
	}
    if (NF == 2 || need)
	{ print $1 }
    }
    ' < $PKGCHK_CONF
    `
fi

# Check packages are installed
#
for pkgdir in $PKGDIRLIST ; do

    if [ ! -f $PKGSRCDIR/$pkgdir/Makefile ];then
	echo "WARNING: No $pkgdir/Makefile - package moved or obsolete?"
	continue
    fi
    cd $PKGSRCDIR/$pkgdir
    # Use 'make x' rather than 'make all' to avoid potential licence errors
    extract_make_vars PKGNAME
    if [ -z "$PKGNAME" ]; then
	echo "Unable to extract PKGNAME for $pkgdir"
	exit 1
    fi
    if [ ! -d /var/db/pkg/$PKGNAME ];then
	echo_n "$PKGNAME: "
	pkg=`echo $PKGNAME | sed 's/-[0-9].*//'`
	pkginstalled=`pkg_info -e $pkg || true`
	INSTALL=
	if [ -n "$pkginstalled" ];then
	    echo_n "version mismatch - $pkginstalled"
	    mismatch="$mismatch $pkginstalled"
	    if [ -n "$opt_u" ]; then
		UPDATE_TODO="$UPDATE_TODO $PKGNAME $pkgdir"
	    fi
	else
	    echo_n "missing"
	    if [ -n "$opt_a" ] ; then
		INSTALL_TODO="$INSTALL_TODO $PKGNAME $pkgdir"
	    fi
	fi
	if [ -f $PACKAGES/All/$PKGNAME.tgz ] ;then
	    echo_n " (binary package available)"
	fi
	echo
    else
	if [ -n "$opt_B" ];then
	    current_build_version=`get_build_version`
	    installed_build_version=`cat /var/db/pkg/$PKGNAME/+BUILD_VERSION | sed "s:^${real_pkgsrcdir}/::"`
	    if [ x"$current_build_version" != x"$installed_build_version" ];then
		echo "$PKGNAME: build version information mismatch"
		mismatch="$mismatch $PKGNAME"
		# should we mark this pkg to be updated if -u is given ??
	    elif [ -n "$opt_v" ];then
		echo "$PKGNAME: OK"
	    fi
	elif [ -n "$opt_v" ];then
	    echo "$PKGNAME: OK"
	fi
    fi
done

if [ -n "$opt_r" -a -n "$mismatch" ]; then
    run_cmd "pkg_delete -r $mismatch"
fi
if [ -n "$UPDATE_TODO" ];then
    echo "[ Update... ]"

    # Generate list including packages which depend on updates
    #
    set -- $UPDATE_TODO
    while [ $# != 0 ]; do
	PKGNAME=`echo $1 | sed 's/-[0-9].*//'`
	if [ -f /var/db/pkg/$PKGNAME-[0-9]*/+REQUIRED_BY ];then
	    LIST="$LIST$1|$2|`cat /var/db/pkg/$PKGNAME-[0-9]*/+REQUIRED_BY | xargs echo`\n"
	else
	    LIST="$LIST$1|$2\n"
	fi
	shift ; shift;
    done

    # drop any packages whose 'parents' are also to be updated
    #
    UPDATE_TODO=`printf "$LIST" | ${AWK} -F '|' '
    {
    pkg2dir[$1] = $2
    split($3, deplist, " ")
    for (pkg in deplist)
	{
	dep = deplist[pkg]
	sub("-[0-9].*", "", dep) # Strip version
	covered[dep] = 1
	}
    }
    END {
    for (pkg in pkg2dir)
	{
	chk = pkg
	sub("-[0-9].*", "", chk); # Strip version
	if (!covered[chk])
	    print pkg" "pkg2dir[pkg]
	}
    }
    '`

    set -- $UPDATE_TODO
    while [ $# != 0 ]; do
	pkg_install $1 $2 U
	shift ; shift;
    done
fi

if [ -n "$INSTALL_TODO" ];then
    echo ""
    echo "[ Install... ]"
    set -- $INSTALL_TODO
    while [ $# != 0 ]; do
	pkg_install $1 $2 I
	shift ; shift;
    done
fi

if [ -n "$UPDATE_DONE" ];then
    echo "Updated:$UPDATE_DONE"
fi
if [ -n "$INSTALL_DONE" ];then
    echo "Installed:$INSTALL_DONE"
fi
if [ -n "$FAIL_DONE" ];then
    echo "Failed:$FAIL_DONE"
fi