OSDN Git Service

Fixed a bug that some commands were broken when options are specified.
[portsreinstall/current.git] / lib / libreinstall.sh
1 #!/bin/sh -e
2 # ==============================================================================
3 # portsreinstall library script
4 # - Reinstallation processes -
5 # Copyright (C) 2013-2018 Mamoru Sakaue, MwGhennndo, All Rights Reserved.
6 # This software is distributed under the 2-Clause BSD License.
7 # ==============================================================================
8
9 # ============= Variables =============
10 # The following variables are available only within reinstall_exec and functions invoked by it.
11 REINSTALL_PKGTAG=       # Tag indicating version changes by reinstallation of the current port
12 REINSTALL_ORIGPKGTAG=   # Tag indicating the flavored port origin and version changes by reinstallation of the current port
13 REINSTALL_CURRENTPKG=   # Currently installed package name of the current port
14 REINSTALL_ORIGIN=       # Flavored port origin of the current port
15 REINSTALL_DBNODE_DIR=   # Path of the "requires" section of the temporary database for the current port
16 REINSTALL_IS_CURRENTPKG_LATEST= # Currently installed package name of the current port
17 REINSTALL_DBSUFFIX=     # Database suffix for the evaluation method of dependencies
18
19 # ============= Skip reinstallation by showing messages =============
20 reinstall_skip ()
21 {
22         local message
23         message=$1
24         message_echo "($message)"
25         message_target_relations "$REINSTALL_ORIGIN"
26         message_echo
27         fileedit_rm_a_line "$REINSTALL_ORIGIN" "${DBDIR}/stage.loop_list/reinst_todo.remain"
28 }
29
30 # ============= Check whether a package is forbidden due to conflict =============
31 reinstall_quick_chk_forbidden_conflicts ()
32 {
33         local pkg pkg_regexp_esc
34         pkg=$1
35         pkg_regexp_esc=`str_escape_regexp "$pkg"`
36         grep -qE "^$pkg_regexp_esc:" "${DBDIR}/forbidden_conflicts" 2> /dev/null
37 }
38
39 # ============= Check whether a package is forbidden due to conflict =============
40 reinstall_chk_forbidden_conflicts ()
41 {
42         local pkg tmp_forbidden pkg_regexp_esc
43         pkg=$1
44         tmp_forbidden=${TMPDIR}/reinstall_chk_forbidden_conflicts:forbidden
45         pkg_regexp_esc=`str_escape_regexp "$pkg"`
46         grep -E "^$pkg_regexp_esc:" "${DBDIR}/forbidden_conflicts" > $tmp_forbidden 2> /dev/null || return
47         pkg_info_e `cut -d : -f 3 "$tmp_forbidden"`
48 }
49
50 # ============= Restore a package if it is temporarily deinstalled =============
51 reinstall_restore_if_temporarily_deinstalled ()
52 {
53         local tmp_delete
54         [ $opt_fetch_only = no -a $opt_dry_run = no ] || return 0
55         [ -n "$REINSTALL_CURRENTPKG" ] && return
56         tmp_list=${TMPDIR}/reinstall_restore_if_temporarily_deinstalled
57         rm -rf "$tmp_list".*
58         [ -e "$REINSTALL_FROMNODE_DIR/backedup_pkgfile" ] || return 0
59         while read backup_pkg
60         do
61                 [ -z "$backup_pkg" -o ! -e "$backup_pkg" ] && continue
62                 echo "$backup_pkg" >> $tmp_list.backedup
63                 pkg=`pkgsys_pkgarc_to_pkgname "$backup_pkg"`
64                 if reinstall_chk_forbidden_conflicts "$pkg"
65                 then
66                         echo "$backup_pkg" >> $tmp_list.avoid
67                 else
68                         echo "$backup_pkg" >> $tmp_list.restore
69                 fi
70         done < $REINSTALL_FROMNODE_DIR/backedup_pkgfile
71         [ -e "$tmp_list.backedup" ] || return 0
72         message_echo "INFO: This port is temporarily deinstalled, so restoration will be attempted."
73         if [ -e "$tmp_list.avoid" ]
74         then
75                 message_echo "INFO: The following backup(s) are avoided because of conflict with installed packages."
76                 message_cat < $tmp_list.avoid
77         fi
78         if [ -e "$tmp_list.restore" ]
79         then
80                 message_echo "INFO: The following backup(s) will be restored."
81                 message_cat < $tmp_list.restore
82                 while read backup_pkg
83                 do
84                         pkg_add_fF "$backup_pkg" || echo "$backup_pkg" >> $tmp_list.failed
85                 done < $tmp_list.restore
86                 if [ -e "$tmp_list.failed" ]
87                 then
88                         message_echo "WARNING: Failed to restore the following backup, but continuing anyway." >&2
89                         message_cat < $tmp_list.failed
90                 fi
91         fi
92         :
93 }
94
95 # ============= Skip reinstallation by showing messages if a flavored origin is in a list =============
96 reinstall_skip_if_in_a_list ()
97 {
98         local message list mode
99         message=$1
100         list=$2
101         mode=$3
102         grep -q -Fx "$REINSTALL_ORIGIN" "${DBDIR}/$list" 2> /dev/null || return
103         [ "x$mode" = xrestore ] && reinstall_restore_if_temporarily_deinstalled
104         reinstall_skip "$message" || :
105 }
106
107 # ============= Get the make arguments =============
108 reinstall_setup_make_args ()
109 {
110         local mode
111         mode=$1
112         {
113                 for key in LOCALBASE LINUXBASE PORT_DBDIR PORTSDIR DISTDIR PACKAGES PKGREPOSITORY
114                 do
115                         eval echo $key=\$$key
116                 done
117                 [ $opt_avoid_vulner = yes ] || echo 'DISABLE_VULNERABILITIES=yes'
118                 case $mode in
119                 anymode )
120                         ;;
121                 '' | distinct )
122                         [ $opt_batch_ports_only = yes ] && echo 'BATCH=yes'
123                         [ $opt_interactive_ports_only = yes ] && echo 'INTERACTIVE=yes'
124                         ;;
125                 esac
126                 if [ $opt_apply_default_config = yes ]
127                 then
128                         pkgsys_is_dialog4ports_used && echo 'NO_DIALOG=yes'
129                 fi
130                 cat "${DBDIR}/requires/$REINSTALL_ORIGIN/MARG.conf" 2> /dev/null || :
131                 flavor=`pkgsys_get_flavor_from_origin "$REINSTALL_ORIGIN"`
132                 [ -z "$flavor" ] || echo "FLAVOR=$flavor"
133         } | tr '\n' ' '
134 }
135
136 # ============= Get the make environment variables =============
137 reinstall_setup_make_envs ()
138 {
139         cat "${DBDIR}/requires/$REINSTALL_ORIGIN/MENV.conf" 2> /dev/null | tr '\n' ' '
140 }
141
142 # ============= Common comand to execute make command =============
143 _reinstall_make_common ()
144 {
145         local mode port_path MAKE_ARGS MAKE_ENVS
146         mode=$1
147         shift
148         MAKE_ARGS=`reinstall_setup_make_args $mode`
149         MAKE_ENVS=`reinstall_setup_make_envs`
150         port_path=`pkgsys_get_portpath_from_origin "$REINSTALL_ORIGIN"`
151         fs_fix_unionfs_image_if_hidden "$port_path"
152         env $MAKE_ENVS make -C "$port_path" "$@" $MAKE_ARGS
153 }
154
155 # ============= Execute make command without restricting for BATCH or INTERACTIVE ports =============
156 reinstall_make_anymode ()
157 {
158         _reinstall_make_common anymode "$@"
159 }
160
161 # ============= Execute make command =============
162 reinstall_make ()
163 {
164         _reinstall_make_common '' "$@"
165 }
166
167 # ============= Error process during reinstallation =============
168 reinstall_errproc ()
169 {
170         local position msg port_path
171         position=$1
172         msg=$2
173         database_build_update_pkgname "$REINSTALL_ORIGIN" > /dev/null
174         message_echo "ERROR: In $position." >&2
175         message_echo "The port/package in concern is $REINSTALL_ORIGPKGTAG." >&2
176         [ -n "$msg" ] && message_echo "($msg)" >&2
177         message_echo >&2
178         port_path=`pkgsys_get_portpath_from_origin "$REINSTALL_ORIGIN"`
179         { fs_fix_unionfs_image_if_hidden "$port_path" \
180                 && pkgsys_chk_ports_tree_implementation; } || exit 1
181         mkdir -p "${DBDIR}/notes/$REINSTALL_ORIGIN"
182         echo "$position" > ${DBDIR}/notes/$REINSTALL_ORIGIN/note_failtre
183         database_record_failure "$REINSTALL_ORIGIN" noclean
184         fileedit_rm_a_line "$REINSTALL_ORIGIN" "${DBDIR}/stage.loop_list/reinst_todo.remain"
185         message_report_failure_for_a_port "$REINSTALL_ORIGIN"
186 }
187
188 # ============= Check the latest stage for a port =============
189 reinstall_chk_stage ()
190 {
191         local stagetag
192         stagetag=$1
193         [ -e "${DBDIR}/status.ports/$REINSTALL_ORIGIN/$stagetag" ]
194 }
195
196 # ============= Check completion of a stage for a port =============
197 reinstall_chk_stage_complete ()
198 {
199         local stagetag
200         stagetag=$1
201         [ -e "${DBDIR}/status.ports/$REINSTALL_ORIGIN/complete/$stagetag" ]
202 }
203
204 # ============= Register the latest stage for a port =============
205 reinstall_register_stage ()
206 {
207         local stagetag parentdir
208         stagetag=$1
209         parentdir=${DBDIR}/status.ports/$REINSTALL_ORIGIN
210         mkdir -p "$parentdir"
211         touch "$parentdir/$stagetag"
212 }
213
214 # ============= Register completion of a stage for a port =============
215 reinstall_register_stage_complete ()
216 {
217         local stagetag parentdir
218         stagetag=$1
219         parentdir=${DBDIR}/status.ports/$REINSTALL_ORIGIN/complete
220         mkdir -p "$parentdir"
221         touch "$parentdir/$stagetag"
222 }
223
224 # ============= Deregister the latest stage for a port =============
225 reinstall_deregister_stage ()
226 {
227         local stagetag
228         stagetag=$1
229         rm -f "${DBDIR}/status.ports/$REINSTALL_ORIGIN/$stagetag"
230 }
231
232 # ============= Deregister completion of a stage for a port =============
233 reinstall_deregister_stage_complete ()
234 {
235         local stagetag
236         stagetag=$1
237         rm -f "${DBDIR}/status.ports/$REINSTALL_ORIGIN/complete/$stagetag"
238 }
239
240 # ============= Back up and delete conflict =============
241 # Package names of conflict are given via stdin. 
242 reinstall_backup_and_delete_conflicts ()
243 {
244         local pkg origin origin_regexp_esc backup_pkgdir backup_pkg
245         message_echo "INFO: Deinstalling conflicting packages for $REINSTALL_ORIGPKGTAG."
246         while read pkg
247         do
248                 origin=`pkg_info_flavored_origin "$pkg"`
249                 message_echo "INFO: Backing up and deleting a conflict, $origin ($pkg)."
250                 origin_regexp_esc=`str_escape_regexp "$origin"`
251                 if [ -d "${DBDIR}/requires/$origin" ]
252                 then
253                         backup_pkgdir=${DBDIR}/backup_packages
254                 else
255                         backup_pkgdir=${PKGREPOSITORY}
256                 fi
257                 mkdir -p "$backup_pkgdir"
258                 if backup_pkg=`pkgsys_get_backup_pkg "$origin"`
259                 then
260                         message_echo "INFO: backup package already exists as $backup_pkg"
261                 elif ! backup_pkg=`pkgsys_create_backup_pkg "$pkg" "$backup_pkgdir"`
262                 then
263                         message_echo "WARNING: Failed to create the backup package, the conflict is kept installed." >&2
264                         continue
265                 fi
266                 grep -v -E "^${origin_regexp_esc}[[:space:]]" "${DBDIR}/deleted_conflicts" \
267                         > ${DBDIR}/deleted_conflicts.tmp 2> /dev/null || :
268                 printf '%s\t%s\n' "$origin" "$pkg" >> ${DBDIR}/deleted_conflicts.tmp
269                 mv "${DBDIR}/deleted_conflicts.tmp" "${DBDIR}/deleted_conflicts"
270                 pkg_delete_f "$pkg" || \
271                 {
272                         message_echo "WARNING: Failed to deinstall $pkg by $PKGSYS_CMD_PKG_DELETE." >&2
273                 }
274         done
275         cat "${DBDIR}/deleted_conflicts" 2> /dev/null | sort -u > ${DBDIR}/deleted_conflicts.tmp
276         mv "${DBDIR}/deleted_conflicts.tmp" "${DBDIR}/deleted_conflicts"
277 }
278
279 # ============= Back up and delete remaining actual install conflict (for installation by package) =============
280 reinstall_backup_and_delete_remaining_install_conflicts__by_pkg ()
281 {
282         local pkg tmp_conflicts
283         pkg=$1
284         tmp_conflicts=${TMPDIR}/reinstall_backup_and_delete_remaining_install_conflicts__by_pkg
285         message_echo "(Checking installation conflict...)"
286         pkgsys_get_conflicting_installed_pkgs install "$REINSTALL_ORIGIN" > $tmp_conflicts.pkgs || :
287         while read pkg_conflict
288         do
289                 fileedit_add_a_line_if_new "$pkg_conflict:$REINSTALL_ORIGIN:$pkg" \
290                         "${DBDIR}/forbidden_conflicts"
291         done < $tmp_conflicts.pkgs
292         reinstall_backup_and_delete_conflicts < $tmp_conflicts.pkgs
293 }
294
295 # ============= Back up and delete remaining actual install conflict (for installation by port) =============
296 reinstall_backup_and_delete_remaining_install_conflicts ()
297 {
298         local stagedir tmp_conflicts db_conflict
299         tmp_conflicts=${TMPDIR}/reinstall_backup_and_delete_remaining_install_conflicts
300         db_conflict=$REINSTALL_DBNODE_DIR/possible_additional_conflist.csv
301         message_echo "(Checking installation conflict...)"
302         rm -rf "$db_conflict.tmp" "$tmp_conflicts".*
303         stagedir=`database_query_get_makevar_val "$REINSTALL_ORIGIN" STAGEDIR`
304         pkgsys_get_conflicting_installed_pkgs install "$REINSTALL_ORIGIN" > $tmp_conflicts.pkgs || :
305         ( set -e
306                 cd "$stagedir"
307                 find . -not -type d
308         ) | sed 's|^\.||' | while read filepath
309         do
310                 [ ! -e "$filepath" ] && continue
311                 pkg=`pkg_which "$filepath" || :`
312                 [ -z "$pkg" ] && continue
313                 grep -qFx "$pkg" "$tmp_conflicts.pkgs" && continue
314                 origin=`pkg_info_flavored_origin "$pkg"`
315                 [ x"$origin" = x"$REINSTALL_ORIGIN" ] && continue
316                 printf '%s\t%s\n' "$pkg" "$filepath" >> $db_conflict.tmp
317         done
318         if [ -e "$db_conflict.tmp" ]
319         then
320                 while read pkg filepath
321                 do
322                         message_echo "INFO: Possible additional conflict with $pkg: $filepath"
323                 done < $db_conflict.tmp
324                 message_echo "INFO: The possible additional conflict packages will be escaped just in case."
325                 mv "$db_conflict.tmp" "$db_conflict"
326                 cut -f 1 "$db_conflict" >> $tmp_conflicts.pkgs
327         else
328                 rm -f "$db_conflict.tmp" "$db_conflict"
329         fi
330         while read pkg_conflict
331         do
332                 fileedit_add_a_line_if_new "$pkg_conflict:$REINSTALL_ORIGIN:$REINSTALL_NEWPKGNAME" \
333                         "${DBDIR}/forbidden_conflicts"
334         done < $tmp_conflicts.pkgs
335         reinstall_backup_and_delete_conflicts < $tmp_conflicts.pkgs
336 }
337
338 # ============= Remove needless possible additional conflict =============
339 # Use after installation of the target port and before restoration of its possible additional conflict.
340 reinstall_remove_needless_possible_conflict ()
341 {
342         local db_conflict db_forbidden suffix tmp_db
343         db_conflict=$REINSTALL_DBNODE_DIR/possible_additional_conflist.csv
344         db_forbidden=${DBDIR}/forbidden_conflicts
345         tmp_db=${TMPDIR}/reinstall_remove_needless_possible_conflict:db
346         [ -e "$db_conflict" ] || return 0
347         if [ ! -e "$db_forbidden" ]
348         then
349                 rm -fr "$db_conflict"
350                 return
351         fi
352         rm -rf "$db_conflict".*
353         touch $db_conflict.tmp
354         while read pkg filepath
355         do
356                 if which -s "$filepath"
357                 then
358                         echo printf '%s\t%s\n' "$pkg" "$filepath" >> $db_conflict.tmp
359                         message_echo "WARNING: Unregistered additional conflict with $pkg: $filepath" >&2
360                 else
361                         message_echo "INFO: The above notice of the possible additional conflict was needless: $pkg: $filepath"
362                 fi
363         done < $db_conflict
364         cut -f 1 "$db_conflict" | sort -u > $tmp_db.conflict_pkg.old
365         cut -f 1 "$db_conflict.tmp" | sort -u > $tmp_db.conflict_pkg.new
366         suffix=`echo ":$REINSTALL_ORIGIN:$REINSTALL_NEWPKGNAME" | str_escape_replaceval_filter`
367         grep -vFx -f "$tmp_db.conflict_pkg.new" "$tmp_db.conflict_pkg.old" | \
368                 sed -E "s/$/$suffix/" > $tmp_db.conflict_pkg.needless.filter
369         grep -vFx -f "$tmp_db.conflict_pkg.needless.filter" "$db_forbidden" > $db_forbidden.tmp || :
370         mv "$db_forbidden.tmp" "$db_forbidden"
371         mv "$db_conflict.tmp" "$db_conflict"
372         [ `wc -l < $db_conflict` -gt 0 ] || rm "$db_conflict"
373 }
374
375 # ============= Restoration of backed up conflict =============
376 reinstall_restore_conflicts ()
377 {
378         local origin_current tmpsrc
379         origin_current=$1
380         [ $opt_fetch_only = no -a $opt_dry_run = no ] || return 0
381         [ -e "${DBDIR}/deleted_conflicts" ] || return 0
382         tmpsrc=${TMPDIR}/reinstall_restore_conflicts::deleted_conflicts
383         cp "${DBDIR}/deleted_conflicts" "$tmpsrc"
384         while read origin pkg
385         do
386                 pkg_regexp_esc=`str_escape_regexp "$pkg"`
387                 origins_init=`database_query_initial_orgins "$origin" | grep -vFx "$origin" || :`
388                 origin_replace=`echo "$origin" \
389                         | sed -E -f "${DBDIR}/REPLACE.complete_sed_pattern"`
390                 [ "x$origin_replace" = "x$origin" ] && origin_replace=
391                 is_to_dereg_from_list=no
392                 if [ -n "$origin_current" -a "x$origin" = "x$origin_current" ] || \
393                         pkg_info_e "$pkg" || pkgsys_exists_from_orig "$origin"
394                 then
395                         is_to_dereg_from_list=yes
396                 else
397                         for origin_orig in $origins_init $origin_replace
398                         do
399                                 if [ -n "$origin_orig" ] && pkgsys_exists_from_orig "$origin_orig"
400                                 then
401                                         is_to_dereg_from_list=yes
402                                         break
403                                 fi
404                         done
405                 fi
406                 if pkgsys_chk_conflict_by_a_pkg install "$REINSTALL_ORIGIN" "$pkg"
407                 then
408                         fileedit_add_a_line_if_new "$pkg:$REINSTALL_ORIGIN:$REINSTALL_NEWPKGNAME" "${DBDIR}/forbidden_conflicts"
409                         is_skipped=yes
410                 elif reinstall_chk_forbidden_conflicts "$pkg"
411                 then
412                         is_skipped=yes
413                 else
414                         is_skipped=no
415                 fi
416                 if [ $is_skipped = yes ]
417                 then
418                         message_echo "INFO: Restoration of a conflict, $origin ($pkg), is avoided because it conflicts with installed packages."
419                 fi
420                 if [ $is_to_dereg_from_list = yes ]
421                 then
422                         pkg_current=
423                         origin_current=
424                         for origin_trial in $origin $origins_init $origin_replace
425                         do
426                                 pkg_trial=`pkgsys_get_installed_pkg_from_origin "$origin_trial" || :`
427                                 [ -z "$pkg_trial" ] && continue
428                                 pkg_current="$pkg_current, $pkg_trial"
429                                 origin_current="$origin_current, $origin_trial"
430                         done
431                         pkg_current=`expr "$pkg_current" : ', \(.*\)'` || pkg_current=none
432                         origin_current=`expr "$origin_current" : ', \(.*\)'` || origin_current=none
433                         pkgname_msg=$pkg
434                         origin_msg=$origin
435                         [ "x$pkg_current" = "x$pkg" ] || pkgname_msg="$pkg => $pkg_current"
436                         [ "x$origin_current" = "x$origin" ] || origin_msg="$origin => $origin_current"
437                         if [ $is_skipped = yes ]
438                         then
439                                 message_echo "WARNING: Conflicting package is installed: $origin_msg ($pkgname_msg)"
440                         else
441                                 message_echo "INFO: $origin_msg ($pkgname_msg) is already restored."
442                                 grep -v -E "[[:space:]]$pkg_regexp_esc$" "${DBDIR}/deleted_conflicts" \
443                                         > ${DBDIR}/deleted_conflicts.tmp || :
444                                 mv "${DBDIR}/deleted_conflicts.tmp" "${DBDIR}/deleted_conflicts"
445                         fi
446                         continue
447                 fi
448                 [ $is_skipped = yes ] && continue
449                 if grep -q -Fx -e "$origin" -e "$origin_replace" "${DBDIR}/taboo.all.list" 2> /dev/null
450                 then
451                         message_echo "INFO: Restoration of a conflict, $origin ($pkg), is avoided because it is taboo."
452                         continue
453                 fi
454                 message_echo "INFO: Restoring a former conflict, $origin ($pkg)."
455                 if ! backup_pkg=`pkgsys_get_backup_pkg "$origin"`
456                 then
457                         message_echo "WARNING: No backup exists, gave up." >&2
458                         continue
459                 fi
460                 if pkg_add_fF "$backup_pkg"
461                 then
462                         grep -v -E "[[:space:]]$pkg_regexp_esc$" "${DBDIR}/deleted_conflicts" \
463                                 > ${DBDIR}/deleted_conflicts.tmp || :
464                         mv "${DBDIR}/deleted_conflicts.tmp" "${DBDIR}/deleted_conflicts"
465                 else
466                         message_echo "WARNING: Failed to restore. Note that your system may experience troubles by this error." >&2
467                 fi
468         done < $tmpsrc
469 }
470
471 # ============= Check whether the all non-looped requirements are installed =============
472 reinstall_are_requirements_ready ()
473 {
474         [ -e "$REINSTALL_DBNODE_DIR/requirements.all.direct" ] || return 0
475         while read origin
476         do
477                 pkgsys_exists_from_orig "$origin" || return 1
478         done < $REINSTALL_DBNODE_DIR/requirements.all.direct
479 }
480
481 # ============= Get the all non-looped requirements ready for a port by restarting them if deinstalled =============
482 reinstall_chk_and_restore_requirements ()
483 {
484         local tmp_restore tmp_isfailed
485         tmp_restore=${TMPDIR}/reinstall_setup_requirements:restore
486         tmp_isfailed=${TMPDIR}/reinstall_setup_requirements:isfailed
487         cp /dev/null "$tmp_restore"
488         rm -f "$tmp_isfailed"
489         cat "$REINSTALL_DBNODE_DIR/requirements.all.direct" 2> /dev/null \
490                 | while read origin
491         do
492                 pkgsys_exists_from_orig "$origin" && continue
493                 if grep -q -Fx "$origin" "${DBDIR}/taboo.all.list" 2> /dev/null
494                 then
495                         message_echo "INFO: Restoration of a requirement [$origin] is avoided because it is set taboo."
496                         touch "$tmp_isfailed"
497                         break
498                 fi
499                 origins_init=`database_query_initial_orgins "$origin" | grep -vFx "$origin" || :`
500                 origin_replace=`echo "$origin" \
501                         | sed -E -f "${DBDIR}/REPLACE.complete_sed_pattern"`
502                 [ "x$origin_replace" = "x$origin" ] && origin_replace=
503                 is_installed=no
504                 for origin_orig in $origins_init $origin_replace
505                 do
506                         [ "x$origin_orig" = "x$origin" ] && continue
507                         if pkgsys_exists_from_orig "$origin_orig"
508                         then
509                                 is_installed=yes
510                                 break
511                         fi
512                         if grep -q -Fx "$origin_orig" "${DBDIR}/taboo.all.list" 2> /dev/null
513                         then
514                                 message_echo "INFO: Restoration of a requirement [$origin_orig] is avoided because it is set taboo."
515                                 touch "$tmp_isfailed"
516                                 is_installed=taboo
517                                 break
518                         fi
519                 done
520                 [ $is_installed = yes ] && continue
521                 [ $is_installed = taboo ] && break
522                 for origin_orig in $origin $origins_init $origin_replace
523                 do
524                         pkgarc=`pkgsys_get_backup_pkg "$origin_orig"` && break
525                 done
526                 if [ -z "$pkgarc" ]
527                 then
528                         if grep -q -Fx "$origin" "${DBDIR}/failed.list" 2> /dev/null
529                         then
530                                 touch "$tmp_isfailed"
531                                 break
532                         fi
533                         continue
534                 fi
535                 printf '%s\t%s\n' "$origin_orig" "$pkgarc" >> $tmp_restore
536         done
537         [ -e "$tmp_isfailed" ] && return 1
538         while read origin pkgarc
539         do
540                 pkg=`pkgsys_pkgarc_to_pkgname "$pkgarc"`
541                 pkg_regexp_esc=`str_escape_regexp "$pkg"`
542                 if reinstall_chk_forbidden_conflicts "$pkg"
543                 then
544                         message_echo "INFO: Restoration of a requirement [$origin ($pkg)] is avoided because it conflicts with installed packages."
545                         continue
546                 fi
547                 message_echo "INFO: Restoring a backed-up requirement [$origin ($pkg)]."
548                 if pkg_add_fF "$pkgarc"
549                 then
550                         grep -v -E "[[:space:]]$pkg_regexp_esc$" "${DBDIR}/deleted_conflicts" \
551                                 > ${DBDIR}/deleted_conflicts.tmp 2> /dev/null || :
552                         mv "${DBDIR}/deleted_conflicts.tmp" "${DBDIR}/deleted_conflicts"
553                 else
554                         message_echo "WARNING: Failed to restore by the backed-up package." >&2
555                 fi
556                 
557         done < $tmp_restore
558         :
559 }
560
561 # ============= Back-up of the currently installed package =============
562 reinstall_pkg_backup ()
563 {
564         local backup_pkg pkg
565         reinstall_chk_stage_complete PKG_BACKUP && return
566         pkg=`echo "$REINSTALL_CURRENTPKG" | tr ' ' '\n' | grep -v '^$' | tail -n 1`
567         if [ -n "$pkg" ]
568         then
569                 message_echo "-- (Creating temporary backup package for $REINSTALL_ORIGPKGTAG)"
570                 if backup_pkg=`pkgsys_create_backup_pkg "$pkg" "${DBDIR}/backup_packages"`
571                 then
572                         fileedit_add_a_line_if_new "$pkg" "$REINSTALL_FROMNODE_DIR/backedup_version"
573                         fileedit_add_a_line_if_new "$backup_pkg" "$REINSTALL_FROMNODE_DIR/backedup_pkgfile"
574                 else
575                         message_echo "WARNING: Failed to create the backup package, but ignored by hoping success." >&2
576                         return 1
577                 fi
578         fi
579         reinstall_register_stage_complete PKG_BACKUP
580 }
581
582 # ============= Deinstallation of the currently installed package =============
583 reinstall_deinstall ()
584 {
585         local tmp_installedpkg installed_pkgs
586         tmp_installedpkg=${TMPDIR}/reinstall_deinstall:installedpkg
587         pkgsys_get_installed_pkg_from_origin "$REINSTALL_ORIGIN" > $tmp_installedpkg
588         [ `wc -l < $tmp_installedpkg` -gt 0 ] || return 0
589         installed_pkgs=`tr '\n' ' ' < $tmp_installedpkg | sed 's/ *$//'`
590         if pkgsys_is_necessary_pkgtool "$REINSTALL_ORIGIN"
591         then
592                 message_echo "INFO: Deinstalling $installed_pkgs by $PKGSYS_CMD_PKG_DELETE."
593                 pkg_delete_f $installed_pkgs || \
594                 {
595                         message_echo "WARNING: Failed to deinstall." >&2
596                 }
597         else
598                 while read pkg
599                 do
600                         message_echo "INFO: Deinstalling $pkg by $PKGSYS_CMD_PKG_DELETE." >&2
601                         pkg_delete_f "$pkg" || \
602                         {
603                                 message_echo "WARNING: Failed to deinstall." >&2
604                         }
605                 done < $tmp_installedpkg
606         fi
607         message_echo "-- (Trying to deinstall by ports to make sure. This may cause negligible warnings.)"
608         reinstall_make deinstall || \
609         {
610                 message_echo "WARNING: Failed to deinstall $REINSTALL_CURRENTPKG by make deinstall." >&2
611         }
612 }
613
614 # ============= Deinstallation of installed packages for old ports of the current one =============
615 reinstall_deinstall_old_ports ()
616 {
617         [ -e "$REINSTALL_FROMNODE_DIR/old_origs" ] || return 0
618         while read origin_old
619         do
620                 pkgsys_get_installed_pkg_from_origin "$origin_old"
621         done < $REINSTALL_FROMNODE_DIR/old_origs | reinstall_backup_and_delete_conflicts
622 }
623
624 # ============= Recovery after failure of installation of the new package =============
625 reinstall_failed_install_recover ()
626 {
627         local backedup_version backup_pkg
628         reinstall_chk_stage_complete FAILED_INSTALL.RECOVER && return
629         message_echo "INFO: Trying to deinstall the failed/terminated installation (Ignore failures)."
630         if [ -n "$REINSTALL_CURRENTPKG" ]
631         then
632                 pkg_delete_f "$REINSTALL_CURRENTPKG" || :
633         fi
634         message_echo "INFO: Trying to deinstall by ports to make sure (This may cause negligible warnings)."
635         reinstall_make deinstall || :
636         backedup_version=`cat "$REINSTALL_FROMNODE_DIR/backedup_version" 2> /dev/null || :`
637         if grep -q -Fx "$REINSTALL_ORIGIN" "${DBDIR}/taboo.all.list" 2> /dev/null
638         then
639                 message_echo "INFO: Restoration of the backup of $backedup_version is avoided because it is taboo."
640         elif [ -n "$backedup_version" ]
641         then
642                 if reinstall_chk_forbidden_conflicts "$backedup_version"
643                 then
644                         message_echo "INFO: Restoration of the backup of $backedup_version, is avoided because it conflicts with installed packages."
645                 else
646                         message_echo "INFO: Restoring the backup of $backedup_version."
647                         if [ -e "$REINSTALL_FROMNODE_DIR/backedup_pkgfile" ]
648                         then
649                                 while read backup_pkg
650                                 do
651                                         if [ ! -e "$backup_pkg" ]
652                                         then
653                                                 message_echo "WARNING: The backup file $backup_pkg doesn't exist, gave up." >&2
654                                         elif ! pkg_add_fF "$backup_pkg"
655                                         then
656                                                 message_echo "WARNING: Failed to restore $backedup_version. Note that your system may experience troubles by this error." >&2
657                                         fi
658                                 done < $REINSTALL_FROMNODE_DIR/backedup_pkgfile
659                         else
660                                 message_echo "WARNING: No backup was saved, gave up." >&2
661                         fi
662                 fi
663         fi
664         reinstall_register_stage_complete FAILED_INSTALL.RECOVER
665 }
666
667 # ============= Report an installation success to the all dependents =============
668 reinstall_tell_update_to_depandents ()
669 {
670         local tag level dbsuffix
671         pkgsys_is_pkgtool "$REINSTALL_ORIGIN" && return
672         reinstall_chk_stage_complete TELL_UPDATE_TO_DEPANDENTSL && return
673         for tag in all run build none
674         do
675                 for level in full direct
676                 do
677                         dbsuffix=${tag}.${level}
678                         {
679                                 cat "$REINSTALL_DBNODE_DIR/dependents.$dbsuffix" "$REINSTALL_DBNODE_DIR/dependents.$dbsuffix.orig"
680                                 [ -e "$REINSTALL_DBNODE_DIR/succeeded_once" ] || cat "$REINSTALL_DBNODE_DIR/ignored_dependents.$tag" "$REINSTALL_DBNODE_DIR/ignored_dependents.$tag.orig"
681                         } 2> /dev/null \
682                                 | sort -u \
683                                 | while read origin_dependent
684                         do
685                                 [ -d "${DBDIR}/requires/$origin_dependent" ] || continue
686                                 touch "${DBDIR}/requires/$origin_dependent/need_reinstall_due_to_upgraded_requirements.$dbsuffix"
687                                 fileedit_rm_a_line "$origin_dependent" \
688                                         "${DBDIR}/success.$dbsuffix.list"
689                                 fileedit_rm_a_line "$origin_dependent" \
690                                         "${DBDIR}/todo_after_requirements_succeed.$dbsuffix.list"
691                         done
692                 done
693         done
694         reinstall_register_stage_complete TELL_UPDATE_TO_DEPANDENTS
695 }
696
697 # ============= Closing operations after an installation success =============
698 reinstall_closing_operations_after_successful_install ()
699 {
700         local tag level
701         reinstall_chk_stage_complete CLOSING_OPERATIONS_AFTER_SUCCESSFUL_INSTALL && return
702         database_build_update_pkgname "$REINSTALL_ORIGIN" > /dev/null
703         database_build_update_pkgtag "$REINSTALL_ORIGIN"
704         for tag in all run build none
705         do
706                 for level in full direct
707                 do
708                         rm -f "$REINSTALL_DBNODE_DIR/need_reinstall_due_to_upgraded_requirements.${tag}.${level}"
709                         [ -e "$REINSTALL_DBNODE_DIR/failed_requirements.${tag}.${level}" ] || continue
710                         cp "$REINSTALL_DBNODE_DIR/failed_requirements.${tag}.${level}" \
711                                 "$REINSTALL_DBNODE_DIR/failed_requirements.${tag}.${level}.previous"
712                 done
713         done
714         {
715                 echo "$REINSTALL_ORIGIN"
716                 database_query_initial_orgins "$REINSTALL_ORIGIN"
717         } | sort -u | while read initial_orig
718         do
719                 initial_orig_regexp=`str_escape_regexp "$initial_orig"`
720                 grep -E "^${initial_orig_regexp}[[:space:]]" "${DBDIR}/deleted_conflicts" 2> /dev/null \
721                         | cut -f 2 | while read initial_pkg
722                 do
723                         pkg_regexp=`str_escape_regexp "$initial_pkg"`
724                         grep -v -E "^${pkg_regexp}:" "${DBDIR}/forbidden_conflicts" \
725                                 > ${DBDIR}/forbidden_conflicts.tmp 2> /dev/null || :
726                         mv "${DBDIR}/forbidden_conflicts.tmp" "${DBDIR}/forbidden_conflicts"
727                 done
728                 grep -v -E "^${initial_orig_regexp}[[:space:]]" "${DBDIR}/deleted_conflicts" \
729                         > ${DBDIR}/deleted_conflicts.tmp 2> /dev/null || :
730                 mv "${DBDIR}/deleted_conflicts.tmp" "${DBDIR}/deleted_conflicts"
731                 pkgsys_delete_backup_pkg "$initial_orig"
732         done
733         rm -f "$REINSTALL_FROMNODE_DIR/backedup_pkgfile"
734         reinstall_deregister_stage in_postinstall
735         database_record_success "$REINSTALL_ORIGIN"
736         temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
737         message_echo "===>  Done successfully"
738         reinstall_register_stage_complete CLOSING_OPERATIONS_AFTER_SUCCESSFUL_INSTALL
739 }
740
741 # ============= Fetch missing distfiles of a port without sanity check =============
742 reinstall_fetch_missing_distfiles ()
743 {
744         local port_path tmp_fetch
745         port_path=`pkgsys_get_portpath_from_origin "$REINSTALL_ORIGIN"` || return
746         tmp_fetch=${TMPDIR}/reinstall_fetch_missing_distfiles:fetch.sh
747         tmp_missing_fetch=${TMPDIR}/reinstall_fetch_missing_distfiles:missing_fetch.sh
748         rm -rf "$tmp_fetch.pre"
749         reinstall_make_anymode fetch-list 2> /dev/null > $tmp_fetch.src || return
750         grep '^SHA256[[:space:]]' "$port_path/distinfo" | \
751                 sed -E 's/^SHA256[[:space:]]+\(([^)]*)\).*/\1/' | \
752                 while read relative_distfile_path
753         do
754                 if ! { testhash=`( cd "${DISTDIR}" && sha256 "$relative_distfile_path" ) 2> /dev/null` && \
755                         grep -qxF "$testhash" "$port_path/distinfo"; }
756                 then
757                         relative_distfile_path_ptn=`str_escape_regexp "|| echo \"$relative_distfile_path\" not fetched; }"`
758                         if grep -E "$relative_distfile_path_ptn$" "$tmp_fetch.src"
759                         then
760                                 subdir_distfile=`dirname "$relative_distfile_path"`
761                                 [ "x$subdir_distfile" = x. ] || echo "mkdir -p \"${DISTDIR}/$subdir_distfile\"" >> $tmp_fetch.pre
762                         fi
763                 fi
764         done | grep -v '^[[:space:]]*$' > $tmp_fetch.main || :
765         [ -e "$tmp_fetch.pre" ] && sort -u "$tmp_fetch.pre"
766         cat "$tmp_fetch.main"
767 }
768
769 # ============= Execute a command by recording the standard output and error output into a file in case of error =============
770 reinstall_execcmd_tee_errlog ()
771 {
772         local func tmp_err
773         func=$1
774         tmp_err=${TMPDIR}/reinstall_execcmd_tee_errlog::error
775         rm -f "$tmp_err"
776         { {
777                 $func
778         } 2>&1 || touch "$tmp_err"; } | tee "$REINSTALL_DBNODE_DIR/error.log"
779         [ -e "$tmp_err" ] && return 1
780         rm -f "$REINSTALL_DBNODE_DIR/error.log"
781         return
782 }
783
784 # ============= Execute a command to output to stdout by recording the error output into a file in case of error =============
785 reinstall_execcmd_getstdout_errlog ()
786 {
787         local func
788         func=$1
789         if $func 2> $REINSTALL_DBNODE_DIR/error.log
790         then
791                 rm -f "$REINSTALL_DBNODE_DIR/error.log"
792                 return
793         fi
794         cat "$REINSTALL_DBNODE_DIR/error.log" >&2
795         return 1
796 }
797
798 # ============= Check whether any required package is missing or too old for build by ports =============
799 reinstall_chk_missing_requirement ()
800 {
801         local tmp_filter tmp_miising
802         tmp_filter=${TMPDIR}/reinstall_chk_missing_requirement::requirements.all_but_test.unflavored
803         tmp_miising=${TMPDIR}/reinstall_chk_missing_requirement::missing_requirements
804         message_echo "Checking whether any required package is missing or too old..."
805         sed 's/@.*//' "$REINSTALL_DBNODE_DIR/requirements.all.full" > $tmp_filter || :
806         if reinstall_make missing 2> /dev/null | grep -Fx -f "$tmp_filter" > $tmp_miising
807         then
808                 message_echo "Found missing/too old requirements:"
809                 message_cat < $tmp_miising
810                 {
811                         echo "Found missing/too old requirements:"
812                         cat "$tmp_miising"
813                 } > "$REINSTALL_DBNODE_DIR/error.log"
814                 reinstall_restore_conflicts "$REINSTALL_ORIGIN"
815                 reinstall_restore_if_temporarily_deinstalled
816                 reinstall_errproc 'check of missing/too old packages'
817                 temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
818                 return 1
819         fi
820 }
821
822 # ============= Reinstallation for a flavored origin =============
823 reinstall_exec ()
824 {
825         ( set -e
826         REINSTALL_ORIGIN=$1
827         REINSTALL_DBNODE_DIR=${DBDIR}/requires/$REINSTALL_ORIGIN
828         REINSTALL_FROMNODE_DIR=${DBDIR}/moved_from/$REINSTALL_ORIGIN
829         REINSTALL_CURRENTPKG=`database_build_update_pkgname "$REINSTALL_ORIGIN" | tr '\n' ' ' | sed 's/ *$//'`
830         REINSTALL_IS_CURRENTPKG_LATEST=no
831         database_build_is_currentpkg_latest "$REINSTALL_ORIGIN" && REINSTALL_IS_CURRENTPKG_LATEST=yes
832         database_build_update_pkgtag "$REINSTALL_ORIGIN"
833         REINSTALL_PKGTAG=`cat "$REINSTALL_FROMNODE_DIR/pkgtag"`
834         REINSTALL_ORIGPKGTAG="$REINSTALL_ORIGIN ($REINSTALL_PKGTAG)"
835         REINSTALL_DBSUFFIX=`options_get_dependency_type`.`options_get_dependency_level`
836         REINSTALL_NEWPKGNAME=`database_build_get_new_pkgname "$REINSTALL_ORIGIN"`
837         REINSTALL_IS_FROZEN=no
838         grep -qFx "$REINSTALL_ORIGIN" "${DBDIR}/freeze.all.list" 2> /dev/null && REINSTALL_IS_FROZEN=yes
839         message_stage_title "$PROGRAM_STEP_COUNTER $REINSTALL_ORIGPKGTAG"
840         database_query_get_target_attributes currentorigin "$REINSTALL_ORIGIN"
841         if [ -z "${currentorigin_is_all}" -a -z "${currentorigin_is_relevant}" ]
842         then
843                 reinstall_skip 'Skipped because being irrelevant'
844                 return
845         fi
846         reinstall_skip_if_in_a_list 'Skipped because being a leaf port' leaf_ports_to_delete && return
847         rm -f "$REINSTALL_DBNODE_DIR/this_is_skipped_build_requirement"
848         if expr "$REINSTALL_CURRENTPKG" : '.* .*' > /dev/null
849         then
850                 message_echo "WARNING: Multiple packages are registered for this port. Reinstallation is needed to fix it." >&2
851         elif grep -q -Fx "$REINSTALL_ORIGIN" "${DBDIR}/damaged_package" 2> /dev/null
852         then
853                 message_echo "WARNING: Installed files have invalid checksums for this port. Reinstallation is needed to fix it." >&2
854         else
855                 if [ -e "${DBDIR}/target_all" ]
856                 then
857                         if [ $REINSTALL_IS_CURRENTPKG_LATEST = yes ]
858                         then
859                                 reinstall_restore_if_temporarily_deinstalled
860                                 reinstall_skip 'Skipped because being already latest' "$REINSTALL_ORIGIN"
861                                 return
862                         fi
863                 elif [ ! -e "$REINSTALL_DBNODE_DIR/conf_updated" ]
864                 then
865                         if [ -e "$nodedir/installed_by_pkg" ] && database_query_is_default_conf "$REINSTALL_ORIGIN" quiet
866                         then
867                                 reinstall_restore_if_temporarily_deinstalled
868                                 reinstall_skip 'Skipped because already upgraded with a prebuilt package'
869                                 return
870                         fi
871                         reinstall_skip_if_in_a_list 'Skipped because the reinstallation has been already completed' \
872                                 "success.${REINSTALL_DBSUFFIX}.list" restore && return
873                         if [ $opt_skip_unchanged = yes ]
874                         then
875                                 if [ ! -e "$REINSTALL_DBNODE_DIR/necessary_upgrade.${REINSTALL_DBSUFFIX}" ]
876                                 then
877                                         reinstall_restore_if_temporarily_deinstalled
878                                         if [ -e "$REINSTALL_FROMNODE_DIR/installed_version" ]
879                                         then
880                                                 reinstall_skip 'Skipped because being already latest as well as the all requirements'
881                                         else
882                                                 touch "$REINSTALL_DBNODE_DIR/this_is_skipped_build_requirement"
883                                                 reinstall_skip 'Skipped because being an only-build-time dependency of already latest packages'
884                                         fi
885                                         return
886                                 fi
887                         fi
888                         if [ \( $opt_skip_unchanged = no -a -e "$REINSTALL_DBNODE_DIR/succeeded_once" \) \
889                                 -o \( $opt_skip_unchanged = yes -a $REINSTALL_IS_CURRENTPKG_LATEST = yes \) ]
890                         then
891                                 if [ ! -e "$REINSTALL_DBNODE_DIR/need_reinstall_due_to_upgraded_requirements.${REINSTALL_DBSUFFIX}" ]
892                                 then
893                                         if ! database_query_is_necessary_upgrade "$REINSTALL_ORIGIN"
894                                         then
895                                                 reinstall_restore_if_temporarily_deinstalled
896                                                 fileedit_add_a_line_if_new "$REINSTALL_ORIGIN" \
897                                                         "${DBDIR}/todo_after_requirements_succeed.${REINSTALL_DBSUFFIX}.list"
898                                                 reinstall_skip 'Skipped because being already latest or failed as well as the all requirements'
899                                                 return
900                                         fi
901                                 fi
902                         fi
903                 fi
904                 reinstall_skip_if_in_a_list 'Marked to be manually-done' manually_done.list restore && return
905         fi
906         reinstall_skip_if_in_a_list 'Skipped because being a hold package' conf/HOLD:PORTS.parsed restore && return
907         if database_query_is_a_port_suppressed "$REINSTALL_ORIGIN"
908         then
909                 reinstall_restore_if_temporarily_deinstalled
910                 reinstall_skip 'Skipped because being suppressed'
911                 return
912         fi
913         reinstall_skip_if_in_a_list 'Ignored because being taboo' taboo.all.list && return
914         if [ $opt_fetch_only = no ] && \
915                 ! reinstall_are_requirements_ready && \
916                 ! reinstall_chk_and_restore_requirements
917         then
918                 reinstall_restore_if_temporarily_deinstalled
919                 for tag in all run build none
920                 do
921                         for level in full direct
922                         do
923                                 fileedit_add_a_line_if_new "$REINSTALL_ORIGIN" \
924                                         "${DBDIR}/todo_after_requirements_succeed.${tag}.${level}.list"
925                         done
926                 done
927                 reinstall_skip 'Skipped because the requirements cannot be ready'
928                 return
929         fi
930         if [ -e "$REINSTALL_FROMNODE_DIR/installed_version" ]
931         then
932                 insttarget=reinstall
933                 instdesc='a reinstallation'
934         else
935                 insttarget=install
936                 instdesc='an installation'
937         fi
938         temp_set_msg_current_stage "$instdesc process for $REINSTALL_ORIGPKGTAG $PROGRAM_STEP_COUNTER"
939         message_target_relations "$REINSTALL_ORIGIN"
940         message_echo "------- Starting $instdesc process --------"
941         if ! reinstall_chk_stage_complete CHECK_SANITY
942         then
943                 message_echo "Sanity Check..."
944                 _reinstall_exec__tmpcmd () { reinstall_make_anymode check-sanity; }
945                 if ! check_sanity_msg=`reinstall_execcmd_getstdout_errlog _reinstall_exec__tmpcmd`
946                 then
947                         echo "$check_sanity_msg"
948                         reinstall_restore_if_temporarily_deinstalled
949                         reinstall_errproc 'check sanity'
950                         temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
951                         if [ $opt_fetch_only = yes ]
952                         then
953                                 if [ $opt_inst_by_pkg_if_can = yes ]
954                                 then
955                                         if ! reinstall_chk_stage_complete FAILOVER_FETCH_PKG && \
956                                                 database_query_is_default_conf "$REINSTALL_ORIGIN"
957                                         then
958                                                 message_echo "INFO: The configuration of this port is default, so use of a prebuilt package is attempted."
959                                                 func_pkg_inst_remote_verify_fetch=pkg_inst_remote_verify_fetch
960                                                 func_pkg_inst_verify_pkg=pkg_inst_verify_pkg
961                                                 if [ $opt_use_legacy_pkg_for_missing_pkgng = yes ]
962                                                 then
963                                                         func_pkg_inst_remote_verify_fetch=pkg_inst_remote_wild_verify_fetch
964                                                         func_pkg_inst_verify_pkg=pkg_inst_wild_verify_pkg
965                                                 fi
966                                                 if ! $func_pkg_inst_verify_pkg "$REINSTALL_NEWPKGNAME"
967                                                 then
968                                                         message_echo "Trying to fetch the package because of the fetch only mode..."
969                                                         if $func_pkg_inst_remote_verify_fetch "$REINSTALL_NEWPKGNAME"
970                                                         then
971                                                                 reinstall_register_stage_complete FAILOVER_FETCH_PKG
972                                                         else
973                                                                 message_echo "WARNING: Failed to fetch package for $REINSTALL_NEWPKGNAME"
974                                                         fi
975                                                 fi
976                                         fi
977                                 fi
978                                 if ! reinstall_chk_stage_complete FAILOVER_FETCH
979                                 then
980                                         tmp_fetch_missing=${TMPDIR}/reinstall_exec:fetch.sh
981                                         reinstall_fetch_missing_distfiles > $tmp_fetch_missing
982                                         if [ `wc -l < $tmp_fetch_missing` -gt 0 ]
983                                         then
984                                                 message_echo "Trying to fetch the distfiles because of the fetch only mode..."
985                                                 tmp_fetch_dir=${TMPDIR}/reinstall_exec:fetch_dir
986                                                 rm -rf "$tmp_fetch_dir"
987                                                 mkdir -p "$tmp_fetch_dir"
988                                                 if ( cd "$tmp_fetch_dir" && sh "$tmp_fetch_missing" )
989                                                 then
990                                                         reinstall_register_stage_complete FAILOVER_FETCH
991                                                 else
992                                                         message_echo "WARNING: Failed to fetch distfiles"
993                                                 fi
994                                         fi
995                                 fi
996                                 message_fetch_only
997                         fi
998                         return
999                 fi
1000                 message_echo
1001                 reinstall_register_stage_complete CHECK_SANITY
1002         fi
1003         if [ $opt_dry_run = yes ]
1004         then
1005                 message_dry_run
1006                 temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
1007                 return
1008         fi
1009         if [ $REINSTALL_IS_FROZEN = yes ] || [ $opt_inst_by_pkg_if_can = yes ] && database_query_is_default_conf "$REINSTALL_ORIGIN"
1010         then
1011                 if [ $REINSTALL_IS_FROZEN = yes ]
1012                 then
1013                         pkg=`pkg_get_remote_repository_version "$REINSTALL_ORIGIN" || :`
1014                 else
1015                         pkg=$REINSTALL_NEWPKGNAME
1016                 fi
1017                 message_echo "INFO: The configuration of this port is default, so use of a prebuilt package is attempted."
1018                 if reinstall_chk_stage in_bypkg
1019                 then
1020                         message_echo "(Restarting the previously terminated (re)installation-by-package process...)"
1021                 else
1022                         reinstall_register_stage in_bypkg
1023                 fi
1024                 if ! reinstall_chk_stage_complete INSTALL_BY_PKG
1025                 then
1026                         if ! reinstall_chk_stage FAILED_INSTALL_BY_PKG
1027                         then
1028                                 if [ $opt_fetch_only = no -a "x$PKGSYS_USE_PKGNG" = xyes ] \
1029                                         && pkgsys_is_necessary_pkgtool "$REINSTALL_ORIGIN"
1030                                 then
1031                                         if reinstall_deinstall && pkg_is_tool_available && pkg_info_e "$pkg"
1032                                         then
1033                                                 reinstall_register_stage_complete INSTALL_BY_PKG
1034                                         else
1035                                                 reinstall_register_stage FAILED_INSTALL_BY_PKG
1036                                         fi
1037                                 else
1038                                         func_pkg_inst_remote_verify_fetch=pkg_inst_remote_verify_fetch
1039                                         func_pkg_inst_remote=pkg_inst_remote
1040                                         if [ $opt_use_legacy_pkg_for_missing_pkgng = yes ]
1041                                         then
1042                                                 func_pkg_inst_remote_verify_fetch=pkg_inst_remote_wild_verify_fetch
1043                                                 func_pkg_inst_remote=pkg_inst_remote_wild
1044                                         fi
1045                                         if $func_pkg_inst_remote_verify_fetch "$pkg"
1046                                         then
1047                                                 if [ $opt_fetch_only = yes ]
1048                                                 then
1049                                                         message_fetch_only
1050                                                 else
1051                                                         reinstall_backup_and_delete_remaining_install_conflicts__by_pkg "$pkg"
1052                                                         reinstall_pkg_backup || :
1053                                                         reinstall_register_stage in_add_pkg
1054                                                         reinstall_deinstall_old_ports
1055                                                         reinstall_deinstall
1056                                                         if $func_pkg_inst_remote "$pkg"
1057                                                         then
1058                                                                 if database_query_dependency_matching "$REINSTALL_ORIGIN"
1059                                                                 then
1060                                                                         reinstall_register_stage_complete INSTALL_BY_PKG
1061                                                                 else
1062                                                                         message_echo "INFO: The requirements of the package mismatch the configuration."
1063                                                                         message_echo "INFO: The installed package will be deleted and installation by port will be attempted instead."
1064                                                                         pkg_delete_f "$pkg" || :
1065                                                                         reinstall_register_stage FAILED_INSTALL_BY_PKG
1066                                                                 fi
1067                                                         else
1068                                                                 reinstall_register_stage FAILED_INSTALL_BY_PKG
1069                                                         fi
1070                                                         if pkgsys_is_necessary_pkgtool "$REINSTALL_ORIGIN"
1071                                                         then
1072                                                                 pkg_is_tool_available || pkg_rescue_tools
1073                                                         fi
1074                                                 fi
1075                                         else
1076                                                 reinstall_register_stage FAILED_INSTALL_BY_PKG
1077                                         fi
1078                                 fi
1079                         fi
1080                         if [ $opt_fetch_only = no ]
1081                         then
1082                                 if reinstall_chk_stage FAILED_INSTALL_BY_PKG && \
1083                                         reinstall_chk_stage in_add_pkg
1084                                 then
1085                                         reinstall_failed_install_recover
1086                                 fi
1087                         fi
1088                 fi
1089                 if [ $opt_fetch_only = no ] && reinstall_chk_stage_complete INSTALL_BY_PKG
1090                 then
1091                         touch "$REINSTALL_DBNODE_DIR/installed_timestamp"
1092                         touch "$REINSTALL_DBNODE_DIR/installed_by_pkg"
1093                         reinstall_deregister_stage in_bypkg
1094                         reinstall_tell_update_to_depandents
1095                         reinstall_closing_operations_after_successful_install
1096                         message_echo
1097                         return
1098                 fi
1099                 reinstall_deregister_stage in_bypkg
1100                 if [ $opt_fetch_only = yes ]
1101                 then
1102                         message_echo "INFO: Continue to fetch distfiles in case of installation by port."
1103                 else
1104                         message_echo "WARNING: (Re)installation-by-package is unsuccessful, so retrying by using port." >&2
1105                 fi
1106         fi
1107         if [ $REINSTALL_IS_FROZEN = yes ]
1108         then
1109                 message_echo "(Port to freeze)"
1110                 _reinstall_exec__tmpcmd () { pkg_get_remote_repository_version "$REINSTALL_ORIGIN"; }
1111                 if ! pkg=`reinstall_execcmd_getstdout_errlog _reinstall_exec__tmpcmd` || [ -z "$pkg" ]
1112                 then
1113                         reinstall_restore_conflicts "$REINSTALL_ORIGIN"
1114                         reinstall_restore_if_temporarily_deinstalled
1115                         reinstall_errproc 'freezing'
1116                         temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
1117                         reinstall_skip "ERROR: Failed to get the repository version."
1118                         return
1119                 fi
1120                 pkg_current=`pkgsys_get_installed_pkg_from_origin "$REINSTALL_ORIGIN"`
1121                 if [ "x$pkg" = "x$pkg_current" ]
1122                 then
1123                         message_echo "INFO: The latest repository version $pkg installed, deemed success."
1124                         message_echo "WARNING: This action may cause problems due to the version/option mismatch." >&2
1125                         reinstall_tell_update_to_depandents
1126                         reinstall_closing_operations_after_successful_install
1127                         message_echo
1128                         return
1129                 fi
1130                 message_echo "INFO: The latest repository version $pkg will be installed."
1131                 reinstall_pkg_backup || :
1132                 reinstall_deinstall_old_ports
1133                 reinstall_deinstall
1134                 reinstall_backup_and_delete_remaining_install_conflicts__by_pkg "$pkg"
1135                 _reinstall_exec__tmpcmd () { pkg_inst_remote "$pkg"; }
1136                 if reinstall_execcmd_tee_errlog _reinstall_exec__tmpcmd
1137                 then
1138                         message_echo "INFO: Deemed success."
1139                         message_echo "WARNING: This action may cause problems due to the version/option mismatch." >&2
1140                         reinstall_tell_update_to_depandents
1141                         reinstall_closing_operations_after_successful_install
1142                         message_echo
1143                         return
1144                 fi
1145                 message_echo "ERROR: Failed install the version in the repository for a port to freeze. Dependents are locked." >&2
1146                 reinstall_restore_conflicts "$REINSTALL_ORIGIN"
1147                 reinstall_restore_if_temporarily_deinstalled
1148                 reinstall_errproc 'freezing'
1149                 temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
1150                 reinstall_skip 'Skipped because being a port to freeze.'
1151                 return
1152         fi
1153         if grep -qFx "$REINSTALL_DBNODE_DIR/requirements.all.full" "${DBDIR}/freeze.all.list" 2> /dev/null
1154         then
1155                 reinstall_restore_conflicts
1156                 {
1157                         pkgsys_get_conflicting_installed_pkgs build "$REINSTALL_ORIGIN" || :
1158                         pkgsys_get_conflicting_installed_pkgs install "$REINSTALL_ORIGIN" || :
1159                 } | reinstall_backup_and_delete_conflicts
1160                 if ! database_query_are_requirements_not_locked "$REINSTALL_ORIGIN"
1161                 then
1162                         reinstall_skip 'Skipped because of missing requirements to freeze.'
1163                         return
1164                 fi
1165         fi
1166         if [ $opt_fetch_only = no ]
1167         then
1168                 if reinstall_chk_stage in_prebuild
1169                 then
1170                         message_echo "(Restarting the previously terminated pre-build process...)"
1171                 else
1172                         reinstall_restore_conflicts
1173                         reinstall_register_stage in_prebuild
1174                 fi
1175         fi
1176         if [ $opt_fetch_only = no  -a -e "$REINSTALL_DBNODE_DIR/BEFOREBUILD.conf" ] && \
1177                 ! reinstall_chk_stage_complete BEFOREBUILD
1178         then
1179                 message_echo "-- BEFOREBUILD operations (start)"
1180                 _reinstall_exec__tmpcmd () { sh -e "$REINSTALL_DBNODE_DIR/BEFOREBUILD.conf"; }
1181                 if ! reinstall_execcmd_tee_errlog _reinstall_exec__tmpcmd
1182                 then
1183                         reinstall_restore_if_temporarily_deinstalled
1184                         reinstall_errproc 'BEFOREBUILD operations'
1185                         temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
1186                         return
1187                 fi
1188                 message_echo "-- BEFOREBUILD operations (end)"
1189                 reinstall_register_stage_complete BEFOREBUILD
1190         fi
1191         if [ $opt_fetch_only = no ] && ! reinstall_chk_stage_complete CLEAN_BEFORE_BUILD
1192         then
1193                 _reinstall_exec__tmpcmd () { reinstall_make_anymode clean NOCLEANDEPENDS=yes; }
1194                 if ! reinstall_execcmd_tee_errlog _reinstall_exec__tmpcmd
1195                 then
1196                         reinstall_restore_if_temporarily_deinstalled
1197                         reinstall_errproc 'clean before build'
1198                         temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
1199                         return
1200                 fi
1201                 message_echo
1202                 reinstall_register_stage_complete CLEAN_BEFORE_BUILD
1203         fi
1204         if ! reinstall_chk_stage_complete FETCH
1205         then
1206                 if ! reinstall_chk_stage FAILED_FETCH
1207                 then
1208                         reinstall_make_anymode checksum || \
1209                                 reinstall_register_stage FAILED_FETCH
1210                 fi
1211                 if reinstall_chk_stage FAILED_FETCH
1212                 then
1213                         if ! reinstall_chk_stage_complete FAILED_FETCH.RETRIAL_1
1214                         then
1215                                 message_echo "INFO: Refetching distfiles for $REINSTALL_ORIGPKGTAG."
1216                                 {
1217                                         reinstall_make_anymode fetch FETCH_ARGS=-Ap &&
1218                                                 reinstall_make_anymode checksum
1219                                 } || reinstall_register_stage FAILED_REFETCH_1
1220                                 reinstall_register_stage_complete FAILED_FETCH.RETRIAL_1
1221                         fi
1222                         if reinstall_chk_stage FAILED_REFETCH_1
1223                         then
1224                                 if ! reinstall_chk_stage_complete FAILED_FETCH.RETRIAL_2.DISTCLEAN
1225                                 then
1226                                         message_echo "INFO: Cleaning distfiles for the second refetch for $REINSTALL_ORIGPKGTAG."
1227                                         reinstall_make_anymode distclean || :
1228                                         reinstall_register_stage_complete FAILED_FETCH.RETRIAL_2.DISTCLEAN
1229                                 fi
1230                                 if ! reinstall_chk_stage_complete FAILED_FETCH.RETRIAL_2
1231                                 then
1232                                         message_echo "INFO: Refetching distfiles as the second retrial for $REINSTALL_ORIGPKGTAG."
1233                                         _reinstall_exec__tmpcmd () { reinstall_make_anymode fetch FETCH_ARGS=-Ap; }
1234                                         if ! reinstall_execcmd_tee_errlog _reinstall_exec__tmpcmd
1235                                         then
1236                                                 reinstall_restore_conflicts "$REINSTALL_ORIGIN"
1237                                                 reinstall_restore_if_temporarily_deinstalled
1238                                                 reinstall_errproc 'fetch'
1239                                                 temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
1240                                                 return
1241                                         fi
1242                                         _reinstall_exec__tmpcmd () { reinstall_make_anymode checksum; }
1243                                         if ! reinstall_execcmd_tee_errlog _reinstall_exec__tmpcmd
1244                                         then
1245                                                 reinstall_restore_conflicts "$REINSTALL_ORIGIN"
1246                                                 reinstall_restore_if_temporarily_deinstalled
1247                                                 reinstall_errproc 'checksum'
1248                                                 temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
1249                                                 return
1250                                         fi
1251                                         reinstall_register_stage_complete FAILED_FETCH.RETRIAL_2
1252                                 fi
1253                         fi
1254                 fi
1255                 reinstall_register_stage_complete FETCH
1256         fi
1257         if [ $opt_fetch_only = yes ]
1258         then
1259                 message_fetch_only
1260                 return
1261         fi
1262         reinstall_deregister_stage in_prebuild
1263         if [ $opt_batch_ports_only = yes -o $opt_interactive_ports_only = yes ]
1264         then
1265                 to_skip=no
1266                 case `database_query_get_makevar_val "$REINSTALL_ORIGIN" IS_INTERACTIVE` in
1267                         yes )   msg_is_interactive='interactive'
1268                                 [ $opt_batch_ports_only = yes ] && to_skip=yes
1269                                 ;;
1270                         '' )    msg_is_interactive='not interactive'
1271                                 [ $opt_interactive_ports_only = yes ] && to_skip=yes
1272                                 ;;
1273                 esac
1274                 if [ $to_skip = yes ]
1275                 then
1276                         reinstall_restore_if_temporarily_deinstalled
1277                         database_build_update_pkgname "$REINSTALL_ORIGIN" > /dev/null
1278                         message_echo "INFO: Further processes for this port are skipped because it is $msg_is_interactive."
1279                         message_echo
1280                         fileedit_rm_a_line "$REINSTALL_ORIGIN" "${DBDIR}/stage.loop_list/reinst_todo.remain"
1281                         temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
1282                         return
1283                 fi
1284         fi
1285         if reinstall_chk_stage in_build
1286         then
1287                 message_echo "(Restarting the previously terminated build process...)"
1288                 flag_restarted_build=yes
1289         else
1290                 reinstall_register_stage in_build
1291                 flag_restarted_build=no
1292         fi
1293         if reinstall_chk_stage in_retrial_build
1294         then
1295                 message_echo "(Restarting the previously terminated retrial build process...)"
1296                 build_args='MAKE_JOBS_UNSAFE=yes'
1297         else
1298                 build_args=
1299         fi
1300         if ! reinstall_chk_stage_complete BUILD
1301         then
1302                 pkgsys_get_conflicting_installed_pkgs build "$REINSTALL_ORIGIN" \
1303                         | reinstall_backup_and_delete_conflicts
1304                 reinstall_chk_missing_requirement || return 0
1305                 if ! reinstall_make build $build_args
1306                 then
1307                         reinstall_register_stage in_retrial_build
1308                         [ $flag_restarted_build = yes ] && message_echo "INFO: This failure may be due to restarting from a terminated build."
1309                         is_build_env_modified=no
1310                         if pkgsys_get_conflicting_installed_pkgs install "$REINSTALL_ORIGIN" \
1311                                 > ${TMPDIR}/reinstall_exec::conflicts_install
1312                         then
1313                                 message_echo "INFO: Install-only conflicts are deinstalled for retrial because they may have effects on build."
1314                                 reinstall_backup_and_delete_conflicts < ${TMPDIR}/reinstall_exec::conflicts_install
1315                                 is_build_env_modified=yes
1316                         fi
1317                         if pkgsys_exists_from_orig "$REINSTALL_ORIGIN"
1318                         then
1319                                 message_echo "INFO: The currently installed package for this port is deinstalled for retrial because it may have effects on build."
1320                                 reinstall_pkg_backup || :
1321                                 reinstall_deinstall_old_ports
1322                                 reinstall_deinstall
1323                                 is_build_env_modified=yes
1324                         fi
1325                         reinstall_deregister_stage_complete CLEAN_BEFORE_BUILD
1326                         message_echo "INFO: Retrying the build process after cleaning for $REINSTALL_ORIGPKGTAG."
1327                         _reinstall_exec__tmpcmd () { reinstall_make clean; }
1328                         if ! reinstall_execcmd_tee_errlog _reinstall_exec__tmpcmd
1329                         then
1330                                 if pkgsys_is_necessary_pkgtool "$REINSTALL_ORIGIN"
1331                                 then
1332                                         pkg_is_tool_available || pkg_rescue_tools
1333                                 fi
1334                                 reinstall_restore_conflicts "$REINSTALL_ORIGIN"
1335                                 reinstall_restore_if_temporarily_deinstalled
1336                                 reinstall_errproc 'build and clean after build failure'
1337                                 temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
1338                                 return
1339                         fi
1340                         reinstall_register_stage_complete CLEAN_BEFORE_BUILD
1341                         _reinstall_exec__tmpcmd () { reinstall_make build MAKE_JOBS_UNSAFE=yes; }
1342                         if ! reinstall_execcmd_tee_errlog _reinstall_exec__tmpcmd
1343                         then
1344                                 if pkgsys_is_necessary_pkgtool "$REINSTALL_ORIGIN"
1345                                 then
1346                                         pkg_is_tool_available || pkg_rescue_tools
1347                                 fi
1348                                 reinstall_restore_conflicts "$REINSTALL_ORIGIN"
1349                                 reinstall_restore_if_temporarily_deinstalled
1350                                 reinstall_errproc 'retrial of build after failure'
1351                                 temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
1352                                 return
1353                         fi
1354                         reinstall_deregister_stage in_retrial_build
1355                 fi
1356                 if pkgsys_is_necessary_pkgtool "$REINSTALL_ORIGIN"
1357                 then
1358                         pkg_is_tool_available || pkg_rescue_tools
1359                 fi
1360                 reinstall_register_stage_complete BUILD
1361         fi
1362         reinstall_deregister_stage in_retrial_build
1363         reinstall_deregister_stage in_build
1364         if reinstall_chk_stage in_stage
1365         then
1366                 message_echo "(Restarting the previously terminated staging process...)"
1367         else
1368                 reinstall_register_stage in_stage
1369         fi
1370         if ! reinstall_chk_stage_complete STAGE
1371         then
1372                 _reinstall_exec__tmpcmd () { reinstall_make stage; }
1373                 if ! reinstall_execcmd_tee_errlog _reinstall_exec__tmpcmd
1374                 then
1375                         reinstall_restore_conflicts "$REINSTALL_ORIGIN"
1376                         reinstall_restore_if_temporarily_deinstalled
1377                         reinstall_errproc 'STAGE operations'
1378                         temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
1379                         return
1380                 fi
1381                 reinstall_register_stage_complete STAGE
1382         fi
1383         reinstall_deregister_stage in_stage
1384         if reinstall_chk_stage in_install
1385         then
1386                 message_echo "(Restarting the previously terminated installation process...)"
1387         else
1388                 reinstall_register_stage in_install
1389         fi
1390         reinstall_pkg_backup || :
1391         if [ -e "$REINSTALL_DBNODE_DIR/BEFOREDEINSTALL.conf" ] && ! reinstall_chk_stage_complete BEFOREDEINSTALL
1392         then
1393                 message_echo "-- BEFOREDEINSTALL operations (start)"
1394                 _reinstall_exec__tmpcmd () { sh -e "$REINSTALL_DBNODE_DIR/BEFOREDEINSTALL.conf"; }
1395                 if ! reinstall_execcmd_tee_errlog _reinstall_exec__tmpcmd
1396                 then
1397                         reinstall_restore_conflicts "$REINSTALL_ORIGIN"
1398                         reinstall_restore_if_temporarily_deinstalled
1399                         reinstall_errproc 'BEFOREDEINSTALL operations'
1400                         temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
1401                         return
1402                 fi
1403                 message_echo "-- BEFOREDEINSTALL operations (end)"
1404                 reinstall_register_stage_complete BEFOREDEINSTALL
1405         fi
1406         if ! reinstall_chk_stage_complete INSTALL
1407         then
1408                 if pkgsys_is_necessary_pkgtool "$REINSTALL_ORIGIN"
1409                 then
1410                         reinstall_backup_and_delete_remaining_install_conflicts
1411                         reinstall_deinstall_old_ports
1412                         reinstall_deinstall
1413                 else
1414                         reinstall_deinstall_old_ports
1415                         reinstall_deinstall
1416                         reinstall_backup_and_delete_remaining_install_conflicts
1417                 fi
1418                 if ! reinstall_chk_stage FAILED_INSTALL
1419                 then
1420                         if reinstall_make $insttarget || \
1421                                 {
1422                                         message_echo "INFO: Cleaning up for retrial."
1423                                         reinstall_make deinstall \
1424                                                 || message_echo "WARNING: Continuing by hoping a success." >&2
1425                                         message_echo "INFO: Retrying the installation."
1426                                         _reinstall_exec__tmpcmd () { reinstall_make $insttarget MAKE_JOBS_UNSAFE=yes; }
1427                                         reinstall_execcmd_tee_errlog _reinstall_exec__tmpcmd
1428                                 }
1429                         then
1430                                 touch "$REINSTALL_DBNODE_DIR/installed_timestamp"
1431                                 reinstall_register_stage_complete INSTALL
1432                         else
1433                                 reinstall_register_stage FAILED_INSTALL
1434                         fi
1435                 fi
1436                 if reinstall_chk_stage FAILED_INSTALL
1437                 then
1438                         if pkgsys_is_necessary_pkgtool "$REINSTALL_ORIGIN"
1439                         then
1440                                 pkg_is_tool_available || pkg_rescue_tools
1441                         else
1442                                 reinstall_failed_install_recover
1443                         fi
1444                         if [ -e "$REINSTALL_DBNODE_DIR/AFTERINSTALL.conf" ] && \
1445                                 ! reinstall_chk_stage_complete FAILED_INSTALL.AFTERINSTALL
1446                         then
1447                                 message_echo "-- AFTERINSTALL operations (start)"
1448                                 if ! sh -e "$REINSTALL_DBNODE_DIR/AFTERINSTALL.conf"
1449                                 then
1450                                         message_echo "WARNING: Failed in AFTERINSTALL operations." >&2
1451                                         message_echo "---- (The process is continued anyway)"
1452                                 fi
1453                                 message_echo "-- AFTERINSTALL operations (end)"
1454                                 reinstall_register_stage_complete FAILED_INSTALL.AFTERINSTALL
1455                         fi
1456                         reinstall_restore_conflicts "$REINSTALL_ORIGIN"
1457                         reinstall_restore_if_temporarily_deinstalled
1458                         reinstall_errproc 'install'
1459                         temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
1460                         return
1461                 fi
1462         fi
1463         reinstall_remove_needless_possible_conflict
1464         reinstall_restore_if_temporarily_deinstalled
1465         if ! reinstall_chk_stage_complete UPDATE_CONFLICTS
1466         then
1467                 pkg_info_qL "$REINSTALL_NEWPKGNAME" > ${TMPDIR}/reinstall_exec:currently_installed_files
1468                 cat "${DBDIR}/backup_pkgarcs.lst" 2> /dev/null \
1469                         | while read origin_bak pkgpath_bak
1470                 do
1471                         pkg_bak=`pkgsys_pkgarc_to_pkgname "$pkgpath_bak"`
1472                         if [ "$origin_bak" = "$REINSTALL_ORIGIN" ]
1473                         then
1474                                 [ "$pkg_bak" = "$REINSTALL_NEWPKGNAME" ] && continue
1475                         elif reinstall_quick_chk_forbidden_conflicts "$pkg_bak"
1476                         then
1477                                 continue
1478                         elif ! pkgsys_chk_match_to_restored_files_by_backup_pkg \
1479                                 "$origin_bak" "${TMPDIR}/reinstall_exec:currently_installed_files"
1480                         then
1481                                 continue
1482                         fi
1483                         fileedit_add_a_line_if_new "$pkg_bak:$REINSTALL_ORIGIN:$REINSTALL_NEWPKGNAME" \
1484                                 "${DBDIR}/forbidden_conflicts"
1485                 done
1486                 reinstall_register_stage_complete UPDATE_CONFLICTS
1487         fi
1488         if [ -e "$REINSTALL_DBNODE_DIR/AFTERINSTALL.conf" ] && ! reinstall_chk_stage_complete AFTERINSTALL
1489         then
1490                 message_echo "-- AFTERINSTALL operations (start)"
1491                 _reinstall_exec__tmpcmd () { sh -e "$REINSTALL_DBNODE_DIR/AFTERINSTALL.conf"; }
1492                 if ! reinstall_execcmd_tee_errlog _reinstall_exec__tmpcmd
1493                 then
1494                         reinstall_restore_conflicts "$REINSTALL_ORIGIN"
1495                         reinstall_errproc 'AFTERINSTALL operations'
1496                         temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
1497                         return
1498                 fi
1499                 message_echo "-- AFTERINSTALL operations (end)"
1500                 reinstall_register_stage_complete AFTERINSTALL
1501         fi
1502         reinstall_deregister_stage in_install
1503         reinstall_register_stage in_postinstall
1504         reinstall_restore_conflicts "$REINSTALL_ORIGIN"
1505         if ! reinstall_chk_stage_complete CLEAN_AFTER_INSTALL
1506         then
1507                 if ! reinstall_make clean
1508                 then
1509                         message_echo "WARNING: Failed to clean $REINSTALL_ORIGPKGTAG." >&2
1510                 fi
1511                 reinstall_register_stage_complete CLEAN_AFTER_INSTALL
1512         fi
1513         reinstall_tell_update_to_depandents
1514         reinstall_closing_operations_after_successful_install
1515         message_echo
1516         )
1517 }