OSDN Git Service

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