OSDN Git Service

773c43ae55b7115c81688ecd85dff92f37953289
[portsreinstall/current.git] / lib / libpkgsys.sh
1 #!/bin/sh -e
2 # ==============================================================================
3 # portsreinstall library script
4 # - Wrappers for hiding version differences in the Ports/Packages system -
5 # Copyright (C) 2013-2014 Mamoru Sakaue, MwGhennndo, All Rights Reserved.
6 # This software is distributed under the 2-Clause BSD License.
7 # ==============================================================================
8
9 # ============= Variables =============
10 PKGSYS_USE_PKGNG=yes    # no: legacy pkg_* tools, yes: the new generation package (pkgng)
11 PKGSYS_CMD_PKG_INFO='pkg info'  # Corresponding command for pkg_info
12 PKGSYS_CMD_PKG_CREATE='pkg create'      # Corresponding command for pkg_create
13 PKGSYS_CMD_PKG_DELETE='pkg delete'      # Corresponding command for pkg_delete
14 PKGSYS_CMD_PKG_ADD='pkg add'    # Corresponding command for pkg_add
15 PKGSYS_AVR_REFETCH_TIMES_PER_SITE=1     # Average number (integer) of retrials for retrieving package or distfiles per mirror site
16 PKGSYS_AVR_REFETCH_TIMES_FOR_CHKSUMERR=2        #  Number (integer) of retrials for check sum error in retrieving a package
17
18 # ============= Check implementation of the ports tree =============
19 pkgsys_chk_ports_tree_implementation ()
20 {
21         local var tmp_work
22         if [ ! -d "${PORTSDIR}" ]
23         then
24                 message_echo "ERROR: Ports directory ${PORTSDIR} is not found." >&2
25                 exit 1
26         fi
27         if [ ! -e "${PORTSDIR}/Makefile" -o ! -e "${PORTSDIR}/Mk/bsd.port.mk" ]
28         then
29                 message_echo "ERROR: Ports tree ${PORTSDIR} is missing, broken or incompatible." >&2
30                 exit 1
31         fi
32 }
33
34 # ============= System defined value for the ports/packages =============
35 pkgsys_sysvar ()
36 {
37         local var tmp_work
38         var=$1
39         tmp_work=${TMPDIR}/pkgsys_sysvar:work
40         rm -rf "$tmp_work"
41         mkdir "$tmp_work"
42         make -C "$tmp_work" -f "${PORTSDIR}/Mk/bsd.port.mk" -V "$var" 2> /dev/null
43 }
44
45 # ============= Get the file name of package check sum file =============
46 pkgsys_pkgchksum_file ()
47 {
48         echo CHECKSUM.MD5
49 }
50
51 # ============= Check whether a port is indispensable for the standard function of the ports/packages system =============
52 pkgsys_is_pkgtool ()
53 {
54         case $1 in
55         ports-mgmt/pkg|ports-mgmt/dialog4ports)
56                 ;;
57         *)      return 1;;
58         esac
59 }
60
61 # ============= Check whether a port is indispensable for package operations =============
62 pkgsys_is_necessary_pkgtool ()
63 {
64         [ x"$WITH_PKGNG" = x'yes' -a "x$1" = x'ports-mgmt/pkg' ]
65 }
66
67 # ============= Check whether the dialog for selecting port options is dialog4ports =============
68 pkgsys_is_dialog4ports_used ()
69 {
70         [ -n "`pkgsys_sysvar DIALOG4PORTS`" ]
71 }
72
73 # ============= Get the number of mirror sites for legacy packages =============
74 pkgsys_num_mirrorsites ()
75 {
76         local siteroots
77         siteroots=$1
78         echo "$siteroots" | tr '|' '\n' | wc -l
79 }
80
81 # ============= Get a URL one of mirror sites =============
82 pkgsys_url_from_mirrors ()
83 {
84         local siteroots subdir nsites id_site site platform version
85         siteroots=$1
86         subdir=$2
87         nsites=`pkgsys_num_mirrorsites "$siteroots"`
88         id_site=`(set +e; random -e $nsites; echo $?)`
89         site=`echo "$siteroots" | tr '|' '\n' | sed -n $((${id_site}+1))p`
90         platform=`uname -p`
91         version=`uname -r | cut -d - -f 1,2 | tr [:upper:] [:lower:]`
92         echo -n "$site"
93         printf "$subdir\n" "$platform" "$version"
94 }
95
96 # ============= Fetch a file from one of mirror sites =============
97 pkgsys_fetch_from_mirrors ()
98 {
99         local siteroots subdir filename dst tmp_work fetch itrial origdir url
100         siteroots=$1
101         subdir=$2
102         filename=$3
103         dst=$4
104         tmp_work=${TMPDIR}/pkgsys_fetch_from_mirrors:work
105         rm -rf "$tmp_work"
106         mkdir "$tmp_work"
107         fetch=`pkgsys_sysvar FETCH_CMD`
108         itrial=$((`pkgsys_num_mirrorsites "$siteroots"`*$PKGSYS_AVR_REFETCH_TIMES_PER_SITE))
109         origdir=`pwd`
110         cd "$tmp_work"
111         while [ $itrial -ge 1 ]
112         do
113                 url=`pkgsys_url_from_mirrors "$siteroots" "$subdir"`$filename
114                 message_echo "INFO: Fetching from $url:"
115                 $fetch "$url"&& break
116                 rm -f "$filename"
117                 itrial=$(($itrial-1))
118         done
119         cd "$origdir"
120         [ -e "$tmp_work/$filename" ] || return
121         mv "$tmp_work/$filename" "$dst"
122 }
123
124 # ============= Get the package check sums file ready =============
125 pkgsys_ready_checksum_file ()
126 {
127         local chksumfile
128         tmp_savedpath=${TMPDIR}/pkgsys_ready_checksum_file:savedpath
129         rm -f "$tmp_savedpath"
130         chksumfile=`pkgsys_pkgchksum_file`
131         if [ ! -e "${DBDIR}/checksum/$chksumfile" ]
132         then
133                 [ -d "${DBDIR}/checksum" ] || mkdir "${DBDIR}/checksum"
134                 if ! pkgsys_fetch_from_mirrors "${PACKAGECHECKSUMROOTS}" "${PACKAGECHECKSUMDIR}" \
135                         "$chksumfile" "${DBDIR}/checksum"
136                 then
137                         message_echo "WARNING: No check sum file is available." >&2
138                         return 1
139                 fi
140         fi
141         echo "${DBDIR}/checksum/$chksumfile" > $tmp_savedpath
142 }
143
144 # ============= Get the location of a check sums file fetched by pkgsys_ready_checksum_file =============
145 pkgsys_ready_checksum_file__fetched_file ()
146 {
147         cat "${TMPDIR}/pkgsys_ready_checksum_file:savedpath"
148 }
149
150 # ============= Fetch a legacy package from one of remote sites =============
151 pkgsys_fetch_legacy_remote ()
152 {
153         local pkg tmp_work tmp_pkgpath pkg_regexp checksumpath validMD5 fetchedMD5 needs_fetch itrial
154         pkg=$1
155         tmp_work=${TMPDIR}/pkgsys_fetch_legacy_remote:work
156         tmp_pkgpath=${TMPDIR}/pkgsys_fetch_legacy_remote:pkgpath
157         rm -rf "$tmp_work"
158         mkdir "$tmp_work"
159         pkg_regexp=`str_escape_regexp "$pkg"`
160         pkgsys_ready_checksum_file || return
161         checksumpath=`pkgsys_ready_checksum_file__fetched_file`
162         validMD5=`grep -m 1 -E -e "^MD5[[:space:]]*\($pkg_regexp\.tbz\)[[:space:]]*=" "$checksumpath" | sed -E "s/^[^=]*=[[:space:]]*(.*)/\1/"`
163         if [ -z "$validMD5" ]
164         then
165                 message_echo "WARNING: No check sum for $pkg.tbz." >&2
166                 return 1
167         fi
168         needs_fetch=yes
169         [ -d "${PKGREPOSITORY}" ] || mkdir -p "${PKGREPOSITORY}"
170         if [ -e "${PKGREPOSITORY}/$pkg.tbz" ]
171         then
172                 if [ -e "${PKGREPOSITORY}/$pkg.md5=$validMD5.tbz" ]
173                 then
174                         fetchedMD5=`md5 "${PKGREPOSITORY}/$pkg.md5=$validMD5.tbz" | sed -E "s/^[^=]*=[[:space:]]*(.*)/\1/"`
175                         [ "x$fetchedMD5" = "x$validMD5" ] || rm "${PKGREPOSITORY}/$pkg.md5=$fetchedMD5.tbz"
176                 fi
177                 if [ -e "${PKGREPOSITORY}/$pkg.md5=$validMD5.tbz" ]
178                 then
179                         ln -f "${PKGREPOSITORY}/$pkg.md5=$fetchedMD5.tbz" "${PKGREPOSITORY}/$pkg.tbz"
180                 else
181                         fetchedMD5=`md5 "${PKGREPOSITORY}/$pkg.tbz" | sed -E "s/^[^=]*=[[:space:]]*(.*)/\1/"`
182                         if [ "x$fetchedMD5" = "x$validMD5" ]
183                         then
184                                 needs_fetch=no
185                         else
186                                 mv "${PKGREPOSITORY}/$pkg.tbz" "${PKGREPOSITORY}/$pkg.md5=$fetchedMD5.tbz"
187                         fi
188                 fi
189         fi
190         if [ $needs_fetch = yes ]
191         then
192                 itrial=$PKGSYS_AVR_REFETCH_TIMES_FOR_CHKSUMERR
193                 while [ $itrial -ge 1 ]
194                 do
195                         if pkgsys_fetch_from_mirrors "${PACKAGEROOTS}" "${PACKAGEDIR}" \
196                                 "$pkg.tbz" "$tmp_work"
197                         then
198                                 fetchedMD5=`md5 "$tmp_work/$pkg.tbz" | sed -E "s/^[^=]*=[[:space:]]*(.*)/\1/"`
199                                 [ "x$fetchedMD5" = "x$validMD5" ] && break
200                                 message_echo "WARNING: Check sum mismatches for $pkg.tbz." >&2
201                         fi
202                         itrial=$(($itrial-1))
203                 done
204                 [ $itrial -ge 1 ] || return
205                 mv "$tmp_work/$pkg.tbz" "${PKGREPOSITORY}"
206         fi
207         echo "${PKGREPOSITORY}/$pkg.tbz" > $tmp_pkgpath
208 }
209
210 # ============= Get the location of a package fetched by pkgsys_fetch_legacy_remote =============
211 pkgsys_fetch_legacy_remote__fetched_pkg ()
212 {
213         cat "${TMPDIR}/pkgsys_fetch_legacy_remote:pkgpath"
214 }
215
216 # ============= Check whether the dependency of a legacy package is the latest =============
217 pkg_is_dependency_of_a_legacypkg_latest ()
218 {
219         local pkgarc tmp_extract tmp_contents tmp_origin tmp_pkg pkg nlines iline origin_req pkg_req pkg_new
220         pkgarc=$1
221         tmp_extract=${TMPDIR}/pkgng:pkg_is_dependency_of_a_legacypkg_latest:extract
222         tmp_contents=${TMPDIR}/pkgng:pkg_is_dependency_of_a_legacypkg_latest:contents
223         tmp_origin=${TMPDIR}/pkgng:pkg_is_dependency_of_a_legacypkg_latest:origin
224         tmp_pkg=${TMPDIR}/pkgng:pkg_is_dependency_of_a_legacypkg_latest:pkg
225         pkg=`pkgsys_pkgarc_to_pkgname "$pkgarc"`
226         [ -e "$pkgarc" ] || return
227         rm -rf "$tmp_extract"
228         mkdir "$tmp_extract"
229         tar xf "$pkgarc" -C "$tmp_extract" +CONTENTS
230         grep -e '^@pkgdep[[:space:]]' -e '^@comment[[:space:]]*DEPORIGIN:' "$tmp_extract/+CONTENTS" \
231                 | sed 's/^@pkgdep[[:space:]]*//;s/^@comment[[:space:]]*DEPORIGIN://' > $tmp_contents
232         nlines=`wc -l < $tmp_contents`
233         iline=1
234         while [ $iline -le $nlines ]
235         do
236                 origin_req=`sed -n ${iline}p "$tmp_contents"`
237                 pkg_req=`sed -n $(($iline+1))p "$tmp_contents"`
238                 iline=$(($iline+2))
239                 pkg_new=`cat "${DBDIR}/requires/$origin_req/new_version" 2> /dev/null` || :
240                 if [ -z "$pkg_new" -o "$pkg_new" != "$pkg_req" ]
241                 then
242                         message_echo "WARNING: Requirements of remote package $pkg are not the latest." >&2
243                         return 1
244                 fi
245         done
246         :
247 }
248
249 # ============= Check whether legacy package tools are available =============
250 pkgsys_is_legacy_tool_available ()
251 {
252         which -s pkg_info
253 }
254
255 # ============= Define wrapper commands for hiding the differences between pkg_* tools and pkgng =============
256 pkgsys_def_pkgtools ()
257 {
258         if [ "${DBDIR}/WITH_PKGNG" -nt /etc/make.conf -o \( -e "${DBDIR}/WITH_PKGNG" -a ! -e /etc/make.conf \) ]
259         then
260                 PKGSYS_USE_PKGNG=`cat "${DBDIR}/WITH_PKGNG"`
261         else
262                 PKGSYS_USE_PKGNG=`pkgsys_sysvar WITH_PKGNG | tr '[:upper:]' '[:lower:]'`
263                 if [ -d "${DBDIR}" ] && misc_is_superuser_privilege
264                 then
265                         echo "$PKGSYS_USE_PKGNG" > ${DBDIR}/WITH_PKGNG.tmp
266                         mv "${DBDIR}/WITH_PKGNG.tmp" "${DBDIR}/WITH_PKGNG"
267                 fi
268         fi
269         if [ "x$PKGSYS_USE_PKGNG" = xyes ]
270         then
271                 export WITH_PKGNG=yes
272                 PKGSYS_CMD_PKG_INFO='pkg info'
273                 PKGSYS_CMD_PKG_CREATE='pkg create'
274                 PKGSYS_CMD_PKG_DELETE='pkg delete'
275                 PKGSYS_CMD_PKG_ADD='pkg add'
276                 pkg_is_tool_available ()
277                 {
278                         if [ -x /usr/sbin/pkg ]
279                         then
280                                 pkg -N 2> /dev/null && return
281                                 env ASSUME_ALWAYS_YES=yes pkg bootstrap -f
282                                 pkg_is_tool_available
283                         else
284                                 which -s pkg && return
285                         fi
286                 }
287                 pkg_info_Ea ()
288                 {
289                         pkg info -qa 2> /dev/null
290                 }
291                 pkg_info_qoa ()
292                 {
293                         pkg info -qoa 2> /dev/null
294                 }
295 #               pkg_info_qox ()
296 #               {
297 #                       pkg info -qox "$@" 2> /dev/null
298 #               }
299                 pkg_info_qoX ()
300                 {
301                         pkg info -qox "$@" 2> /dev/null
302                 }
303                 pkg_info_qO ()
304                 {
305                         pkg info -qO "$@" 2> /dev/null
306                 }
307                 pkg_info_qo ()
308                 {
309                         pkg info -qo "$@" 2> /dev/null
310                 }
311                 pkg_info_qr ()
312                 {
313                         pkg info -qd "$@" 2> /dev/null
314                 }
315                 pkg_info_e ()
316                 {
317                         pkg info -e "$@" 2> /dev/null
318                 }
319                 pkg_info_eO ()
320                 {
321                         pkg info -eO "$1" 2> /dev/null
322                 }
323                 pkg_info_Eg ()
324                 {
325                         pkg info -Eg "$@" 2> /dev/null
326                 }
327                 pkg_info_qR ()
328                 {
329                         pkg info -qr "$@" 2> /dev/null
330                 }
331                 pkg_info_Ex ()
332                 {
333                         pkg info -Ex "$@" 2> /dev/null
334                 }
335                 pkg_info_qL ()
336                 {
337                         pkg info -ql "$@" 2> /dev/null
338                 }
339                 pkg_check_sanity ()
340                 {
341                         local pkg tmp_stdout tmp_stderr
342                         pkg=$1
343                         tmp_stdout=${TMPDIR}/pkgng:pkg_check_sanity:stdout
344                         tmp_stderr=${TMPDIR}/pkgng:pkg_check_sanity:stderr
345                         pkg check -s "$pkg" > $tmp_stdout 2> $tmp_stderr || :
346                         grep '^[^:]*: checksum mismatch for ' "$tmp_stderr" | sed 's/^[^:]*: checksum mismatch for //' || :
347                         if grep -q '^pkg: .*: No such file or directory$' "$tmp_stderr"
348                         then
349                                 pkg info -ql "$pkg" | while read filepath
350                                 do
351                                         [ -e "$filepath" ] || echo "$filepath"
352                                 done
353                         fi
354                         :
355                 }
356                 pkg_which ()
357                 {
358                         local filepath
359                         filepath=$1
360                         pkg which -q "$filepath" 2> /dev/null
361                 }
362                 pkg_info_gen_pkg_origin_table ()
363                 {
364                         pkg query -g '%n-%v\t%o' \* 2> /dev/null > ${DBDIR}/installed_ports:pkg_vs_origin.tbl
365                 }
366                 pkg_create_b ()
367                 {
368                         pkg create "$@"
369                 }
370                 pkg_delete_f ()
371                 {
372                         pkg delete -fqy "$@"
373                         pkg info -e "$@" || return 0    # Countermeasure for a bug found for pkg-1.3.4 (at least not until 1.2.7_4)
374                         pkg delete -fy "$@"
375                 }
376                 pkg_add_tools ()
377                 {
378                         local pkgarc tmp_extract prefix prefix_parent pkg
379                         pkgarc=$1
380                         tmp_extract=${TMPDIR}/pkgng:pkg_add_tools:extract
381                         rm -rf "$tmp_extract"
382                         mkdir "$tmp_extract"
383                         tar xf "$pkgarc" -C "$tmp_extract"
384                         prefix=`grep -m 1 '^prefix: ' "$tmp_extract/+MANIFEST" | sed 's/^prefix: *//'`
385                         prefix_parent=`dirname "$prefix"`
386                         cp -Rp "$tmp_extract/$prefix" "$prefix_parent"/
387                         pkg=`pkgsys_pkgarc_to_pkgname "$pkgarc"`
388                         message_echo "INFO: Contents of $pkg are temporarily installed by simple copy."
389                         if env ASSUME_ALWAYS_YES=YES pkg add "$pkgarc"
390                         then
391                                 message_echo "INFO: $pkg is successfully registered."
392                         else
393                                 message_echo "WARNING: Failed to register $pkg, but the process is continued." >&2
394                         fi
395                 }
396                 pkg_add_f ()
397                 {
398                         local pkgarc pkg pkg_tool pkg_gen
399                         pkg_tool=
400                         pkg_gen=
401                         for pkgarc in "$@"
402                         do
403                                 pkg=`basename "$pkgarc"`
404                                 if expr "$pkg" : '^pkg-[0-9][0-9]*\..*' > /dev/null
405                                 then
406                                         pkg_tool=$pkgarc
407                                 else
408                                         pkg_gen="$pkg_gen $pkgarc"
409                                 fi
410                         done
411                         [ -n "$pkg_tool" ] && pkg_add_tools "$pkg_tool"
412                         [ -n "$pkg_gen" ] && env ASSUME_ALWAYS_YES=YES pkg add $pkg_gen
413                 }
414                 pkg_add_fF ()
415                 {
416                         pkg_add_f "$@"
417                 }
418                 pkg_inst_remote_fetch ()
419                 {
420                         local pkg mode pkgarc
421                         pkg=$1
422                         mode=$2
423                         tmp_extract=${TMPDIR}/pkgng:pkg_inst_remote:extract
424                         pkg fetch -yU "$pkg" || return
425                         pkgarc=`pkgsys_pkgname_to_pkgarc "${PKGNG_PKG_CACHEDIR}/All" "$pkg"` || return
426                         [ "x$mode" = xnodepschk ] && return
427                         rm -rf "$tmp_extract"
428                         mkdir "$tmp_extract"
429                         tar xf "$pkgarc" -C "$tmp_extract" +MANIFEST
430                         sed -E '1,/^deps:/d;/^[^[:space:]]/,$d;s/^[[:space:]]*([^:]+):[[:space:]]*\{origin:[[:space:]]*([^,]+),[[:space:]]*version:[[:space:]]*([^}]+)\}/\2\\\1-\3/' "$tmp_extract/+MANIFEST" \
431                                 | tr '\\' '\t' | while read origin_req pkg_req
432                         do
433                                 pkg_new=`cat "${DBDIR}/requires/$origin_req/new_version" 2> /dev/null` || :
434                                 if [ -z "$pkg_new" -o "$pkg_new" != "$pkg_req" ]
435                                 then
436                                         message_echo "WARNING: Requirements of remote package $pkg are not latest." >&2
437                                         return 1
438                                 fi
439                         done
440                         :
441                 }
442                 pkg_inst_remote ()
443                 {
444                         local pkg mode pkgarc
445                         pkg=$1
446                         mode=$2
447                         pkg_inst_remote_fetch "$pkg" "$mode" || return
448                         pkgarc=`pkgsys_pkgname_to_pkgarc "${PKGNG_PKG_CACHEDIR}/All" "$pkg"` || return
449                         env ASSUME_ALWAYS_YES=YES pkg add "$pkgarc"
450                 }
451                 pkg_inst_remote_wild_fetch ()
452                 {
453                         local pkg mode pkgarc
454                         pkg=$1
455                         mode=$2
456                         if pkg_is_tool_available
457                         then
458                                 pkg_inst_remote "$pkg" "$mode" && return
459                         fi
460                         pkgsys_is_legacy_tool_available || return
461                         message_echo "INFO: Trying to use a legacy package and convert it to pkgng."
462                         pkgsys_fetch_legacy_remote "$pkg" || return
463                         pkgarc=`pkgsys_fetch_legacy_remote__fetched_pkg`
464                         [ "x$mode" = xnodepschk ] && return
465                         pkg_is_dependency_of_a_legacypkg_latest "$pkgarc"
466                 }
467                 pkg_inst_remote_wild ()
468                 {
469                         local pkg mode pkgarc
470                         pkg=$1
471                         mode=$2
472                         pkg_inst_remote_wild_fetch "$pkg" "$mode" || return
473                         pkgarc=`pkgsys_fetch_legacy_remote__fetched_pkg`
474                         pkg_add -ifF "$pkgarc" || return
475                         message_echo "INFO: Trying to convert the installed legacy package to pkgng."
476                         pkg2ng || :
477                         message_echo "INFO: Checking whether the conversion is successful."
478                         pkg info -e "$pkg"
479                 }
480                 pkg_loadconf ()
481                 {
482                         local pkg_conf
483                         # Deafult configuration for pkg(1)
484                         PKGNG_PACKAGESITE='http://pkg.freebsd.org/${ABI}/latest'
485                         PKGNG_SRV_MIRRORS=YES
486                         PKGNG_PKG_DBDIR=/var/db/pkg
487                         PKGNG_PKG_CACHEDIR=/var/cache/pkg
488                         PKGNG_PORTSDIR=/usr/ports
489                         PKGNG_PUBKEY=/etc/ssl/pkg.conf
490                         PKGNG_HANDLE_RC_SCRIPTS=NO
491                         PKGNG_PKG_MULTIREPOS=NO
492                         PKGNG_ASSUME_ALWAYS_YES=NO
493                         PKGNG_SYSLOG=YES
494                         PKGNG_SHLIBS=NO
495                         PKGNG_AUTODEPS=NO
496                         PKGNG_PORTAUDIT_SITE='http=//portaudit.FreeBSD.org/auditfile.tbz'
497                         # Load configuration for pkg(1)
498                         pkg_conf=`pkg query %Fp pkg 2> /dev/null | grep '/etc/pkg\.conf\.sample$' | sed 's/\.sample$//'` || :
499                         [ -n "$pkg_conf" ] || pkg_conf=${MYPREFIX}/etc/pkg.conf
500                         grep -v -e '^[[:space:]]*#' -e '^[[:space:]]*$' "$pkg_conf" 2> /dev/null \
501                                 | grep -e '^[[:space:]]*[A-Z0-9_]*[[:space:]]*:[[:space:]]*.*' \
502                                 | while read srcline
503                         do
504                                 var=`expr "$srcline" : '^[[:space:]]*\([A-Z0-9_]*\)[[:space:]]*:.*'` || :
505                                 val=`expr "$srcline" : '^[[:space:]]*[A-Z0-9_]*[[:space:]]*:[[:space:]]*\(.*\)'` || :
506                                 eval PKGNG_$var=\$val
507                                 misc_get_all_vardefs | grep -E "^PKGNG_$var="
508                         done > ${TMPDIR}/pkgsys_def_pkgtools:pkg.conf.sh
509                         . "${TMPDIR}/pkgsys_def_pkgtools:pkg.conf.sh"
510                 }
511                 pkg_rescue_tools ()
512                 {
513                         local packagepath checksumpath pkgname is_successful
514                         packagepath=`pkgsys_get_backup_pkg ports-mgmt/pkg` && \
515                                 pkg_add_tools "$packagepath" && return
516                         pkg_is_tool_available && return
517                         message_echo "WARNING: WITH_PKGNG is set, but pkgng is still missing. It is installed now." >&2
518                         pkgsys_ready_checksum_file || return
519                         message_echo "INFO: Installing pkgng by legacy package tool."
520                         checksumpath=`pkgsys_ready_checksum_file__fetched_file`
521                         pkgname=`sed 's/^MD5[[:space:]]*(//;s/\.tbz)[[:space:]]*=[^=]*$//' "$checksumpath" \
522                                 | grep -m 1 -E -e "^pkg-[0-9]"` || :
523                         [ -n "$pkgname" ] && pkg_inst_remote_wild "$pkgname" nodepschk && return
524                         message_echo "INFO: Failed by package, so installing pkgng by port."
525                         grep -v '^[[:space:]]*WITH_PKGNG=' /etc/make.conf > ${TMPDIR}/make.conf
526                         echo WITHOUT_PKGNG=yes >> ${TMPDIR}/make.conf
527                         ( set -e
528                                 unset WITH_PKGNG
529                                 unset WITHOUT_PKGNG
530                                 
531                                 message_echo "INFO: Attempting deinstallation of ports-mgmt/pkg to make sure."
532                                 env __MAKE_CONF="${TMPDIR}/make.conf" make -C "${PORTSDIR}/ports-mgmt/pkg" deinstall || :
533                                 message_echo "INFO: Attempting (re)installation by ports-mgmt/pkg."
534                                 env __MAKE_CONF="${TMPDIR}/make.conf" make -C "${PORTSDIR}/ports-mgmt/pkg" reinstall clean
535                         ) && {
536                                 pkg2ng || :
537                                 pkg_is_tool_available
538                         }
539                 }
540                 if ! pkg_rescue_tools
541                 then
542                         message_echo "WARNING: Pkgng is still missing, but continuing for the time being." >&2
543                 fi
544                 pkg_loadconf
545         elif ! pkgsys_is_legacy_tool_available
546         then
547                 message_echo "ERROR: Pkgng is disabled although the legacy packages tools are unavailable. Resolve the problem manually." >&2
548                 exit 1
549         else
550                 unset WITH_PKGNG
551                 PKGSYS_USE_PKGNG=no
552                 PKGSYS_CMD_PKG_INFO='pkg_info'
553                 PKGSYS_CMD_PKG_CREATE='pkg_create'
554                 PKGSYS_CMD_PKG_DELETE='pkg_delete'
555                 PKGSYS_CMD_PKG_ADD='pkg_add'
556                 pkg_is_tool_available ()
557                 {
558                         pkgsys_is_legacy_tool_available
559                 }
560                 pkg_info_Ea ()
561                 {
562                         pkg_info -Ea 2> /dev/null
563                 }
564                 pkg_info_qoa ()
565                 {
566                         pkg_info -qoa 2> /dev/null
567                 }
568 #               pkg_info_qox ()
569 #               {
570 #                       pkg_info -qox "$@" 2> /dev/null
571 #               }
572                 pkg_info_qoX ()
573                 {
574                         pkg_info -qoX "$@" 2> /dev/null
575                 }
576                 pkg_info_qO ()
577                 {
578                         pkg_info -qO "$@" 2> /dev/null
579                 }
580                 pkg_info_qo ()
581                 {
582                         pkg_info -qo "$@" 2> /dev/null
583                 }
584                 pkg_info_qr ()
585                 {
586                         pkg_info -qr "$@" | grep '^@pkgdep ' | sed 's/^@pkgdep[[:space:]]*//' 2> /dev/null
587                 }
588                 pkg_info_e ()
589                 {
590                         pkg_info -e "$@" 2> /dev/null
591                 }
592                 pkg_info_eO ()
593                 {
594                         [ `pkg_info -qO "$1" 2> /dev/null | wc -l` -gt 0 ]
595                 }
596                 pkg_info_Eg ()
597                 {
598                         pkg_info -E "$@" 2> /dev/null
599                 }
600                 pkg_info_qR ()
601                 {
602                         pkg_info -qR "$@" | grep -v '^$' 2> /dev/null
603                 }
604                 pkg_info_Ex ()
605                 {
606                         pkg_info -Ex "$@" 2> /dev/null
607                 }
608                 pkg_info_qL ()
609                 {
610                         pkg_info -qL "$@" 2> /dev/null
611                 }
612                 pkg_check_sanity ()
613                 {
614                         local pkg tmp_stdout tmp_stderr
615                         pkg=$1
616                         tmp_stdout=${TMPDIR}/pkgng:pkg_check_sanity:stdout
617                         tmp_stderr=${TMPDIR}/pkgng:pkg_check_sanity:stderr
618                         pkg_info -qg "$pkg" > $tmp_stdout 2> $tmp_stderr || :
619                         grep ' fails the original MD5 checksum$' "$tmp_stdout" | sed 's/ fails the original MD5 checksum$//' || :
620                         grep "^pkg_info: .* doesn't exist$" "$tmp_stderr" | sed -E "s/^pkg_info: (.*) doesn't exist$/\1/" || :
621                 }
622                 pkg_which ()
623                 {
624                         local filepath
625                         filepath=$1
626                         pkg_info -qW "$filepath" 2> /dev/null
627                 }
628                 pkg_info_gen_pkg_origin_table ()
629                 {
630                         pkg_info -aE 2> /dev/null | while read pkg
631                         do
632                                 origin=`pkg_info -qo "$pkg" 2> /dev/null`
633                                 printf '%s\t%s\n' "$pkg" "$origin"
634                         done > ${DBDIR}/installed_ports:pkg_vs_origin.tbl
635                 }
636                 pkg_create_b ()
637                 {
638                         pkg_create -b "$@"
639                 }
640                 pkg_delete_f ()
641                 {
642                         pkg_delete -f "$@"
643                 }
644                 pkg_add_f ()
645                 {
646                         pkg_add -if "$@"
647                 }
648                 pkg_add_fF ()
649                 {
650                         pkg_add -ifF "$@"
651                 }
652                 pkg_inst_remote_fetch ()
653                 {
654                         local pkg mode pkgarc
655                         pkg=$1
656                         mode=$2
657                         pkgsys_fetch_legacy_remote "$pkg" || return
658                         pkgarc=`pkgsys_fetch_legacy_remote__fetched_pkg`
659                         [ "x$mode" = xnodepschk ] && return
660                         pkg_is_dependency_of_a_legacypkg_latest "$pkgarc"
661                 }
662                 pkg_inst_remote ()
663                 {
664                         local pkg mode pkgarc
665                         pkg=$1
666                         mode=$2
667                         pkg_inst_remote_fetch "$pkg" "$mode" || return
668                         pkgarc=`pkgsys_fetch_legacy_remote__fetched_pkg`
669                         pkg_add "$pkgarc" || return
670                 }
671                 pkg_inst_remote_wild_fetch ()
672                 {
673                         pkg_inst_remote_fetch "$1" "$2"
674                 }
675                 pkg_inst_remote_wild ()
676                 {
677                         pkg_inst_remote "$1" "$2"
678                 }
679                 pkg_loadconf () { :; }
680                 pkg_rescue_tools () { :; }
681         fi
682 }
683
684 # ============= Check existence of initially or currently installed package for an origin =============
685 pkgsys_pkg_info_eO ()
686 {
687         local origin origin_regexp
688         origin=$1
689         origin_regexp=`str_escape_regexp "$origin"`
690         cut -f 2 "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null \
691                 | grep -q -E "^$origin_regexp$" && return
692         pkg_info_eO "$origin"
693 }
694
695 # ============= Get the name of an initially installed package for an origin =============
696 pkgsys_pkg_info_qO_init ()
697 {
698         local origin tmppkg origin_regexp npkgs
699         origin=$1
700         tmppkg=${TMPDIR}/pkgsys_pkg_info_qO_init::pkg
701         origin_regexp=`str_escape_regexp "$origin"`
702         sed -n -E "/[[:space:]]$origin_regexp$/p" "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null \
703                 | cut -f 1 > $tmppkg || :
704         npkgs=`wc -l < $tmppkg`
705         if [ $npkgs -gt 0 ]
706         then
707                 cat "$tmppkg"
708         else
709                 pkg_info_qO "$origin"
710         fi
711 }
712
713 # ============= Get the package name of this utility =============
714 pkgsys_get_my_current_pkg ()
715 {
716         pkg_info_Ex "${APPNAME}-[0-9].*"
717 }
718
719 # ============= Get the origin of this utility =============
720 pkgsys_get_my_origin ()
721 {
722         pkg_info_qo "`pkgsys_get_my_current_pkg`"
723 }
724
725 # ============= Get the origin of an initially installed package by ambiguous matching =============
726 pkgsys_init_pkg_orig_by_ambiguous_matching ()
727 {
728         local pkg origin tmporigin ambsuffix len_pkg pkg_regexp norigins
729         pkg=$1
730         origin=`pkg_info_qo "$pkg" || :`
731         [ -n "$origin" ] && { echo "$origin"; return; }
732         tmporigin=${TMPDIR}/pkgsys_init_pkg_orig_by_ambiguous_matching::origin
733         ambsuffix=
734         norigins=0
735         len_pkg=`echo -n "$pkg" | wc -c`
736         if [ $len_pkg -gt 0 ]
737         then
738                 while :
739                 do
740                         pkg_regexp=`str_escape_regexp "$pkg"`$ambsuffix
741                         grep -E "^${pkg_regexp}[[:space:]]" "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null \
742                                 | cut -f 2 > $tmporigin
743                         norigins=`wc -l < $tmporigin`
744                         [ $norigins -gt 0 ] && break
745                         ambsuffix='[a-zA-Z0-9.,_+-]*'
746                         len_pkg=$(($len_pkg-1))
747                         [ $len_pkg -gt 0 ] || break
748                         pkg=`echo -n "$pkg" | head -c $len_pkg`
749                 done
750         fi
751         [ $norigins -eq 1 ] || return
752         cat "$tmporigin"
753 }
754
755 # ============= A part of message indicating tools for showing concerned issues in UPDATING =============
756 pkgsys_show_pkg_updating_commands ()
757 {
758         if [ "x$PKGSYS_USE_PKGNG" = xyes ]
759         then
760                 if which -s pkg_updating
761                 then
762                         echo 'pkg-updating(8) or pkg_updating(1)'
763                 else
764                         echo 'pkg-updating(8)'
765                 fi
766         elif which -s pkg_updating
767         then
768                 echo 'pkg_updating(1)'
769         fi
770 }
771
772 # ============= Evaluation of ports globs =============
773 pkgsys_eval_ports_glob ()
774 {
775         local pkglist origlist
776         pkglist=${DBDIR}/pkgsys_eval_ports_glob:pkg.lst
777         origlist=${DBDIR}/pkgsys_eval_ports_glob:origin.lst
778         if [ ! -r "$pkglist" ]
779         then
780                 if touch "$pkglist" 2>/dev/null
781                 then
782                         rm "$pkglist"
783                 else
784                         pkglist=${TMPDIR}/pkgsys_eval_ports_glob:pkg.lst
785                 fi
786         fi
787         if [ ! -r "$origlist" ]
788         then
789                 if touch "$origlist" 2>/dev/null
790                 then
791                         rm "$origlist"
792                 else
793                         origlist=${TMPDIR}/pkgsys_eval_ports_glob:origin.lst
794                 fi
795         fi
796         [ -f "$pkglist" ] \
797                 || cut -d \| -f 1 "${PORTS_INDEX_DB}" > $pkglist
798         [ -f "$origlist" ] \
799                 || cut -d \| -f 2 "${PORTS_INDEX_DB}" \
800                 | sed -E "s/^`str_escape_regexp "${PORTSDIR}"`\///" > $origlist
801         while [ $# -gt 0 ]
802         do
803                 glob=$1
804                 shift
805                 expr "x$glob" : '^x-' > /dev/null 2>&1 && continue
806                 glob_regexp=`str_convert_portsglob_to_regexp_pattern "$glob"`
807                 if expr "$glob" : '.*/' > /dev/null 2>&1
808                 then
809                         grep -E "$glob_regexp" "$origlist" 2>&1 || :
810                         {
811                                 pkg_info_qoa
812                                 cut -f 2 "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null
813                         } | grep -E "$glob_regexp" 2>&1 || :
814                 else
815                         if expr "$glob" : '^[a-z][a-zA-Z0-9_.+-]*[a-zA-Z0-9_.+]$' > /dev/null 2>&1 && \
816                                 [ `expr "$glob" : '.*-[0-9]' 2>&1` -eq 0 ]
817                         then
818                                 glob_regexp2=`expr "$glob_regexp" : '\(.*\)\$$' 2>&1`'-[0-9]'
819                         else
820                                 glob_regexp2=$glob_regexp
821                         fi
822                         grep -n -E "$glob_regexp2" "$pkglist" 2>&1 | cut -d : -f 1 \
823                                 | while read index
824                         do
825                                 sed -n ${index}p "$origlist"
826                         done || :
827                         glob_regexp2=`expr "$glob_regexp" : '\(.*\)\$$' 2>&1`'[[:space:]]'
828                         sed -n -E "/$glob_regexp2/p" "${DBDIR}/installed_ports:pkg_vs_origin.tbl" 2> /dev/null \
829                                 | cut -f 2 || :
830                         pkg_info_qoX "$glob_regexp" || :
831                 fi
832         done | sort -u
833 }
834
835 # ============= Create a back-up package archive =============
836 pkgsys_create_backup_pkg ()
837 {
838         local pkgname dstdir origin backup_pkg_old pkgname_ptn backup_pkg dbpath pkgpath origin_regexp
839         pkgname=$1
840         dstdir=$2
841         rm -rf "${TMPDIR}"/package.tmp
842         mkdir "${TMPDIR}"/package.tmp
843         origin=`pkg_info_qo "$pkgname"`
844         if backup_pkg_old=`pkgsys_get_backup_pkg "$origin"` \
845                 [ "$backup_pkg_old" -nt "${DBDIR}/requires/$origin/installed_timestamp" ]
846         then
847                 echo "$backup_pkg_old"
848                 return
849         fi
850         if ( cd "${TMPDIR}"/package.tmp && pkg_create_b "$pkgname" > /dev/null )
851         then
852                 pkgname_ptn=`str_escape_regexp "$pkgname"`
853                 backup_pkg=`ls "${TMPDIR}"/package.tmp | \
854                         grep -m 1 -E "^${pkgname_ptn}\.(txz|tbz|tgz|tar)$"` || :
855         fi
856         if [ -z "$backup_pkg" ]
857         then
858                 message_echo "WARNING: Failed to create backup package for $pkgname." >&2
859                 return 1
860         fi
861         dbpath=${DBDIR}/requires/$origin
862         [ -d "$dbpath" ] || dbpath=${DBDIR}/initial/$origin
863         pkg_info_qL > $dbpath/previously_installed_files
864         [ -d "$dstdir" ] || mkdir -p "$dstdir"
865         mv "${TMPDIR}/package.tmp/$backup_pkg" "$dstdir"
866         pkgpath=$dstdir/$backup_pkg
867         origin_regexp=`str_escape_regexp "$origin"`
868         cat "${DBDIR}/backup_pkgarcs.lst" 2> /dev/null | \
869                 while read origin_cur pkgpath_cur
870                 do
871                         [ "$pkgpath_cur" = "$pkgpath" ] && continue
872                         if [ "$origin_cur" = "$origin" ]
873                         then
874                                 rm -f "$pkgpath_cur"
875                         else
876                                 printf '%s\t%s\n' "$origin_cur" "$pkgpath_cur"
877                         fi
878                 done > ${DBDIR}/backup_pkgarcs.lst.tmp
879         printf '%s\t%s\n' "$origin" "$pkgpath" >> ${DBDIR}/backup_pkgarcs.lst.tmp
880         mv "${DBDIR}/backup_pkgarcs.lst.tmp" "${DBDIR}/backup_pkgarcs.lst"
881         echo "$pkgpath"
882 }
883
884 # ============= Delete a back-up package archive for a port origin =============
885 pkgsys_delete_backup_pkg ()
886 {
887         local origin origin_regexp
888         origin=$1
889         origin_regexp=`str_escape_regexp "$origin"`
890         grep -E "^${origin_regexp}[[:space:]]" "${DBDIR}/backup_pkgarcs.lst" 2> /dev/null \
891                 | cut -f 2 | while read pkgpath_cur
892                 do
893                         rm -f "$pkgpath_cur"
894                 done
895         grep -v -E "^${origin_regexp}[[:space:]]" "${DBDIR}/backup_pkgarcs.lst" \
896                 2> /dev/null > ${DBDIR}/backup_pkgarcs.lst.tmp || :
897         mv "${DBDIR}/backup_pkgarcs.lst.tmp" "${DBDIR}/backup_pkgarcs.lst"
898 }
899
900 # ============= Get an existing package archive path for a port origin =============
901 pkgsys_get_backup_pkg ()
902 {
903         local origin tmpnewest origin_regexp
904         origin=$1
905         tmpnewest=${TMPDIR}/pkgsys_get_backup_pkg::newest
906         origin_regexp=`str_escape_regexp "$origin"`
907         rm -f "$tmpnewest"
908         grep -E "^${origin_regexp}[[:space:]]" "${DBDIR}/backup_pkgarcs.lst" 2> /dev/null \
909                 | cut -f 2 | while read pkgpath
910         do
911                 pkgpath_newest=`cat "$tmpnewest" 2> /dev/null` || :
912                 [ -e "$pkgpath" ] || continue
913                 [ -z "$pkgpath_newest" -o "$pkgpath" -nt "$pkgpath_newest" ] || continue
914                 echo "$pkgpath" > $tmpnewest
915         done
916         cat "$tmpnewest" 2> /dev/null
917 }
918
919 # ============= Get a file list to be restored by the current backup package for a port origin =============
920 pkgsys_get_restored_files_by_backup_pkg ()
921 {
922         local origin dbpath
923         origin=$1
924         dbpath=${DBDIR}/requires/$origin
925         [ -d "$dbpath" ] || dbpath=${DBDIR}/initial/$origin
926         cat "$dbpath/previously_installed_files" 2> /dev/null || :
927 }
928
929 # ============= Get a package name from a package archive file name =============
930 pkgsys_pkgarc_to_pkgname ()
931 {
932         local pkgfile_path
933         pkgfile_path=$1
934         basename "$pkgfile_path" | sed -E 's/\.(txz|tbz|tgz|tar)$//'
935 }
936
937 # ============= Get the file name of an existing package archive for a package name =============
938 pkgsys_pkgname_to_pkgarc ()
939 {
940         local pkgdir pkgname pkgname_ptn pkgnode
941         pkgdir=$1
942         pkgname=$2
943         [ -n "$pkgname" ] || return 1
944         [ -d "$pkgdir" ] || return 1
945         if [ "x$PKGSYS_USE_PKGNG" = xyes ]
946         then
947                 pkgname_ptn=`str_escape_regexp "$pkgname"`
948                 pkgnode=`ls "$pkgdir" 2> /dev/null | grep -m 1 -E "^${pkgname_ptn}\.(txz|tbz|tgz|tar)$"` || :
949         elif [ -e "$pkgdir/$pkgname.tbz" ]
950         then
951                 pkgnode=$pkgname.tbz
952         fi
953         [ -n "$pkgnode" ] || return 1
954         echo "$pkgdir/$pkgnode"
955 }
956
957 # ============= Get port origins matching a glob pattern even if nonexistent =============
958 pkgsys_eval_ports_glob_even_if_nonexistent ()
959 {
960         local glob_pattern
961         glob_pattern=$1
962         {
963                 pkgsys_eval_ports_glob "$glob_pattern" 2> /dev/null || :
964                 echo "$glob_pattern" | grep '^[a-z][a-z]*/[a-zA-Z0-9_.+-][a-zA-Z0-9_.+-]*$' || :
965         } | grep -v -e '^$' | sort -u
966 }
967
968 # ============= Evaluate glob patterns and add/remove non-existing/existing ones of them to/from a file =============
969 pkgsys_register_evaluated_globs ()
970 {
971         local mode listpath dirpath tmp_evaluated
972         mode=$1
973         listpath=$2
974         shift 2
975         dirpath=`dirname "$listpath"`
976         tmp_evaluated=${TMPDIR}/pkgsys_register_evaluated_globs:pkgsys_eval_ports_glob
977         echo "$@" | sed -E 's/[ :]+/\
978 /g' | grep -v '^$' | sort -u | while read glob
979         do
980                 pkgsys_eval_ports_glob "$glob" > $tmp_evaluated
981                 [ `wc -l < $tmp_evaluated` -ge 1 ] || \
982                 {
983                         message_echo "WARNING: No matching ports/package glob [$glob]." >&2
984                         continue
985                 }
986                 cat "$tmp_evaluated"
987         done | while read origin
988         do
989                 [ -d "$dirpath" ] || mkdir -p "$dirpath"
990                 case $mode in
991                 remove) fileedit_rm_a_line "$origin" "$listpath";;
992                 add)    fileedit_add_a_line_if_new "$origin" "$listpath";;
993                 esac
994         done
995 }
996
997 # ============= Evaluate glob patterns for installed packages =============
998 pkgsys_eval_installed_pkgs_globs ()
999 {
1000         local tmp_evaluated
1001         tmp_evaluated=${TMPDIR}/pkgsys_eval_installed_pkgs_globs:origins
1002         rm -f "$tmp_evaluated"
1003         pkgsys_register_evaluated_globs add "$tmp_evaluated" "$@"
1004         [ -e "$tmp_evaluated" ] || return 0
1005         while read origin
1006         do
1007                 pkgsys_pkg_info_eO "$origin" || echo "$origin"
1008         done < $tmp_evaluated
1009 }
1010
1011 # ============= Get glob patterns of conflicting packages of a port =============
1012 pkgsys_get_conflicting_pkgs_patterns ()
1013 {
1014         local mode origin conflicts
1015         mode=$1
1016         origin=$2
1017         conflicts=`database_query_get_makevar_val "$origin" CONFLICTS`
1018         case $mode in
1019         build)
1020                 conflicts=$conflicts' '`database_query_get_makevar_val "$origin" CONFLICTS_BUILD`
1021                 ;;
1022         install)
1023                 conflicts=$conflicts' '`database_query_get_makevar_val "$origin" CONFLICTS_INSTALL`
1024                 ;;
1025         esac
1026         echo "$conflicts" | tr ' ' '\n' | grep -v '^$' || :
1027 }       
1028
1029 # ============= Get conflicting installed packages of a port =============
1030 pkgsys_get_conflicting_installed_pkgs ()
1031 {
1032         local mode origin tmp_conflicts
1033         mode=$1
1034         origin=$2
1035         tmp_conflicts=${TMPDIR}/pkgsys_get_conflicting_installed_pkgs::conflicts
1036         pkg_info_Eg `pkgsys_get_conflicting_pkgs_patterns "$mode" "$origin"` > $tmp_conflicts || :
1037         cat "$tmp_conflicts"
1038         [ `wc -l < $tmp_conflicts` -gt 0 ]
1039 }       
1040
1041 # ============= Check whether a package conflicts with a port =============
1042 pkgsys_chk_conflict_by_a_pkg ()
1043 {
1044         local mode origin pkg tmp_conflicts_ptn
1045         mode=$1
1046         origin=$2
1047         pkg=$3
1048         tmp_conflicts_ptn=${TMPDIR}/pkgsys_chk_conflict_by_a_pkg::conflicts_ptn
1049         pkgsys_get_conflicting_pkgs_patterns "$mode" "$origin" \
1050                 | str_convert_glob_to_regexp_pattern > $tmp_conflicts_ptn
1051         echo "$pkg" | grep -q -E -f "$tmp_conflicts_ptn"
1052 }       
1053
1054 # ============= Check whether installed files are lost or broken for a package =============
1055 pkgsys_sanitychk_pkgcontents ()
1056 {
1057         local pkg var_is_reinstall_encouraged _is_reinstall_encouraged tmp_sanity nlines iline src filename icol filename_esc pkg_owner origin
1058         pkg=$1
1059         var_is_reinstall_encouraged=$2
1060         tmp_sanity=${TMPDIR}/pkgsys_sanitychk_pkgcontents:sanity
1061         pkg_check_sanity "$pkg" > $tmp_sanity || :
1062         eval "$var_is_reinstall_encouraged=no"
1063         [ `wc -l < $tmp_sanity` -eq 0 ] && return
1064         nlines=`wc -l < $tmp_sanity`
1065         iline=1
1066         _is_reinstall_encouraged=no
1067         while [ $iline -le $nlines ]
1068         do
1069                 filename=`sed -n ${iline}p "$tmp_sanity"`
1070                 iline=$(($iline+1))
1071                 if [ ! -e "$filename" ]
1072                 then
1073                         _is_reinstall_encouraged=yes
1074                         break
1075                 fi
1076                 if expr "$filename" : '.*/include/.*' > /dev/null
1077                 then
1078                         _is_reinstall_encouraged=yes
1079                         break
1080                 fi
1081                 filename_esc=`str_escape_regexp "$filename"`
1082                 if file "$filename" | sed -E "s/^$filename_esc:[[:space:]]//" | grep -q '^ELF '
1083                 then
1084                         _is_reinstall_encouraged=yes
1085                         break
1086                 fi
1087                 pkg_owner=`pkg_which "$filename"`
1088                 if [ "$pkg" != "$pkg_owner" ]
1089                 then
1090                         _is_reinstall_encouraged=yes
1091                         break
1092                 fi
1093         done
1094         eval "$var_is_reinstall_encouraged=\$_is_reinstall_encouraged"
1095         origin=`pkg_info_qo "$pkg"`
1096         if [ $opt_batch_mode = no ]
1097         then
1098                 message_echo "[$pkg ($origin)]"
1099                 sed 's/^/  /' "$tmp_sanity"
1100                 message_echo
1101         else
1102                 pkg_replace=`str_escape_replaceval "$pkg"`
1103                 origin_replace=`str_escape_replaceval "$origin"`
1104                 sed "s/^/$pkg_replace\\\\$origin_replace\\\\$_is_reinstall_encouraged\\\\/" "$tmp_sanity" | tr '\\' '\t'
1105         fi
1106         return 1
1107 }