OSDN Git Service

Version: 3.2.1
[portsreinstall/current.git] / lib / libreinstall.sh
1 #!/bin/sh -e
2 # ==============================================================================
3 # portsreinstall library script
4 # - Reinstallation processes -
5 # Copyright (C) 2013-2015 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 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=       # Port origin of the current port
15 REINSTALL_ORIGIN_REGEXP=        # Port origin of the current port where control characters of extended regular expression are escaped
16 REINSTALL_DBNODE_DIR=   # Path of the "requires" section of the temporary database for the current port
17 REINSTALL_IS_CURRENTPKG_LATEST= # Currently installed package name of the current port
18 REINSTALL_DBSUFFIX=     # Database suffix for the evaluation method of dependencies
19
20 # ============= Skip reinstallation by showing messages =============
21 reinstall_skip ()
22 {
23         local message
24         message=$1
25         message_echo "($message)"
26         message_target_relations "$REINSTALL_ORIGIN"
27         message_echo
28         fileedit_rm_a_line "$REINSTALL_ORIGIN" "${DBDIR}/stage.loop_list/reinst_todo.remain"
29 }
30
31 # ============= Check whether a package is forbidden due to conflicts =============
32 reinstall_quick_chk_forbidden_conflicts ()
33 {
34         local pkg pkg_regexp_esc
35         pkg=$1
36         pkg_regexp_esc=`str_escape_regexp "$pkg"`
37         grep -qE "^$pkg_regexp_esc:" "${DBDIR}/forbidden_conflicts"
38 }
39
40 # ============= Check whether a package is forbidden due to conflicts =============
41 reinstall_chk_forbidden_conflicts ()
42 {
43         local pkg tmp_forbidden pkg_regexp_esc
44         pkg=$1
45         tmp_forbidden=${TMPDIR}/reinstall_chk_forbidden_conflicts:forbidden
46         pkg_regexp_esc=`str_escape_regexp "$pkg"`
47         grep -E "^$pkg_regexp_esc:" "${DBDIR}/forbidden_conflicts" > $tmp_forbidden 2> /dev/null || return
48         pkg_info_e `cut -d : -f 2 "$tmp_forbidden"`
49 }
50
51 # ============= Restore a package if it is temporarily deinstalled =============
52 reinstall_restore_if_temporarily_deinstalled ()
53 {
54         local backup_pkg pkg
55         [ -n "$REINSTALL_CURRENTPKG" ] && return
56         backup_pkg=`cat "$REINSTALL_DBNODE_DIR/backedup_pkgfile" 2> /dev/null` || return 0
57         [ -z "$backup_pkg" -o ! -e "$backup_pkg" ] && return
58         pkg=`pkgsys_pkgarc_to_pkgname "$backup_pkg"`
59         if reinstall_chk_forbidden_conflicts "$pkg"
60         then
61                 message_echo "INFO: This port is temporarily deinstalled but avoided because it conflicts with installed packages."
62                 return
63         fi
64         message_echo "INFO: This port is temporarily deinstalled, so restoring from the backup."
65         pkg_add_fF "$backup_pkg" && return
66         message_echo "WARNING: Failed to restore the backup, but continuing anyway." >&2
67 }
68
69 # ============= Skip reinstallation by showing messages if an origin is in a list =============
70 reinstall_skip_if_in_a_list ()
71 {
72         local message list mode origin_regexp_esc
73         message=$1
74         list=$2
75         mode=$3
76         origin_regexp_esc=`str_escape_regexp "$REINSTALL_ORIGIN"`
77         grep -q -E "^$origin_regexp_esc$" "${DBDIR}/$list" 2> /dev/null || return
78         [ "x$mode" = xrestore ] && reinstall_restore_if_temporarily_deinstalled
79         reinstall_skip "$message" || :
80 }
81
82 # ============= Get the make arguments =============
83 reinstall_setup_make_args ()
84 {
85         local mode
86         mode=$1
87         {
88                 for key in LOCALBASE LINUXBASE PORT_DBDIR PORTSDIR DISTDIR PACKAGES PKGREPOSITORY
89                 do
90                         eval echo $key=\$$key
91                 done
92                 [ $opt_avoid_vulner = yes ] || echo 'DISABLE_VULNERABILITIES=yes'
93                 case $mode in
94                 anymode);;
95                 ''|distinct)
96                         [ $opt_batch_ports_only = yes ] && echo 'BATCH=yes'
97                         [ $opt_interactive_ports_only = yes ] && echo 'INTERACTIVE=yes'
98                         ;;
99                 esac
100                 if [ $opt_apply_default_config = yes ]
101                 then
102                         pkgsys_is_dialog4ports_used && echo 'NO_DIALOG=yes'
103                 fi
104                 cat "${DBDIR}/requires/$REINSTALL_ORIGIN/MARG.conf" 2> /dev/null || :
105         } | tr '\n' ' '
106 }
107
108 # ============= Get the make environment variables =============
109 reinstall_setup_make_envs ()
110 {
111         cat "${DBDIR}/requires/$REINSTALL_ORIGIN/MENV.conf" 2> /dev/null | tr '\n' ' '
112 }
113
114 # ============= Execute make command without restricting for BATCH or INTERACTIVE ports =============
115 reinstall_make_anymode ()
116 {
117         local MAKE_ARGS MAKE_ENVS
118         MAKE_ARGS=`reinstall_setup_make_args anymode`
119         MAKE_ENVS=`reinstall_setup_make_envs`
120         env $MAKE_ENVS make -C "${PORTSDIR}/$REINSTALL_ORIGIN" "$@" $MAKE_ARGS
121 }
122
123 # ============= Execute make command =============
124 reinstall_make ()
125 {
126         local MAKE_ARGS MAKE_ENVS
127         MAKE_ARGS=`reinstall_setup_make_args`
128         MAKE_ENVS=`reinstall_setup_make_envs`
129         env $MAKE_ENVS make -C "${PORTSDIR}/$REINSTALL_ORIGIN" "$@" $MAKE_ARGS
130 }
131
132 # ============= Error process during reinstallation =============
133 reinstall_errproc ()
134 {
135         local position msg
136         position=$1
137         msg=$2
138         database_build_update_pkgname "$REINSTALL_ORIGIN" > /dev/null
139         message_echo "ERROR: In $position." >&2
140         message_echo "The port/package in concern is $REINSTALL_ORIGPKGTAG." >&2
141         [ -n "$msg" ] && message_echo "($msg)" >&2
142         message_echo >&2
143         [ -d "${DBDIR}/notes/$REINSTALL_ORIGIN" ] || mkdir -p "${DBDIR}/notes/$REINSTALL_ORIGIN"
144         echo "$position" > ${DBDIR}/notes/$REINSTALL_ORIGIN/note_failtre
145         database_record_failure "$REINSTALL_ORIGIN" noclean
146         fileedit_rm_a_line "$REINSTALL_ORIGIN" "${DBDIR}/stage.loop_list/reinst_todo.remain"
147         message_report_failure_for_a_port "$REINSTALL_ORIGIN"
148 }
149
150 # ============= Check the latest stage for a port =============
151 reinstall_chk_stage ()
152 {
153         local stagetag
154         stagetag=$1
155         [ -e "${DBDIR}/status.ports/$REINSTALL_ORIGIN/$stagetag" ]
156 }
157
158 # ============= Check completion of a stage for a port =============
159 reinstall_chk_stage_complete ()
160 {
161         local stagetag
162         stagetag=$1
163         [ -e "${DBDIR}/status.ports/$REINSTALL_ORIGIN/complete/$stagetag" ]
164 }
165
166 # ============= Register the latest stage for a port =============
167 reinstall_register_stage ()
168 {
169         local stagetag parentdir
170         stagetag=$1
171         parentdir=${DBDIR}/status.ports/$REINSTALL_ORIGIN
172         [ -d "$parentdir" ] || mkdir -p "$parentdir"
173         touch "$parentdir/$stagetag"
174 }
175
176 # ============= Register completion of a stage for a port =============
177 reinstall_register_stage_complete ()
178 {
179         local stagetag parentdir
180         stagetag=$1
181         parentdir=${DBDIR}/status.ports/$REINSTALL_ORIGIN/complete
182         [ -d "$parentdir" ] || mkdir -p "$parentdir"
183         touch "$parentdir/$stagetag"
184 }
185
186 # ============= Deregister the latest stage for a port =============
187 reinstall_deregister_stage ()
188 {
189         local stagetag
190         stagetag=$1
191         rm -f "${DBDIR}/status.ports/$REINSTALL_ORIGIN/$stagetag"
192 }
193
194 # ============= Deregister completion of a stage for a port =============
195 reinstall_deregister_stage_complete ()
196 {
197         local stagetag
198         stagetag=$1
199         rm -f "${DBDIR}/status.ports/$REINSTALL_ORIGIN/complete/$stagetag"
200 }
201
202 # ============= Back up and delete conflicts =============
203 # Package names of conflicts are given via stdin. 
204 reinstall_backup_and_delete_conflicts ()
205 {
206         local pkg origin origin_regexp_esc backup_pkgdir backup_pkg
207         message_echo "INFO: Deinstalling conflicting packages for $REINSTALL_ORIGPKGTAG."
208         while read pkg
209         do
210                 origin=`pkg_info_qo "$pkg"`
211                 message_echo "INFO: Backing up and deleting a conflict, $origin ($pkg)."
212                 origin_regexp_esc=`str_escape_regexp "$origin"`
213                 if [ -d "${DBDIR}/requires/$origin" ]
214                 then
215                         backup_pkgdir=${DBDIR}/backup_packages
216                 else
217                         backup_pkgdir=${PKGREPOSITORY}
218                 fi
219                 [ -d "$backup_pkgdir" ] || mkdir -p "$backup_pkgdir"
220                 if backup_pkg=`pkgsys_get_backup_pkg "$origin"`
221                 then
222                         message_echo "INFO: backup package already exists as $backup_pkg"
223                 elif ! backup_pkg=`pkgsys_create_backup_pkg "$pkg" "$backup_pkgdir"`
224                 then
225                         message_echo "WARNING: Failed to create the backup package, the conflict is kept installed." >&2
226                         continue
227                 fi
228                 grep -v -E "^${origin_regexp_esc}[[:space:]]" "${DBDIR}/deleted_conflicts" \
229                         > ${DBDIR}/deleted_conflicts.tmp 2> /dev/null || :
230                 mv "${DBDIR}/deleted_conflicts.tmp" "${DBDIR}/deleted_conflicts"
231                 printf '%s\t%s\n' "$origin" "$pkg" >> ${DBDIR}/deleted_conflicts
232                 pkg_delete_f "$pkg" || \
233                 {
234                         message_echo "WARNING: Failed to deinstall $pkg by $PKGSYS_CMD_PKG_DELETE." >&2
235                 }
236         done
237         cat "${DBDIR}/deleted_conflicts" 2> /dev/null | sort -u > ${DBDIR}/deleted_conflicts.tmp
238         mv "${DBDIR}/deleted_conflicts.tmp" "${DBDIR}/deleted_conflicts"
239 }
240
241 # ============= Back up and delete remaining actual install conflicts =============
242 reinstall_backup_and_delete_remaining_install_conflicts ()
243 {
244         local stagedir tmp_conflicts
245         tmp_conflicts=${TMPDIR}/reinstall_backup_and_delete_remaining_install_conflicts
246         message_echo "(Checking installation conflict...)"
247         stagedir=`database_query_get_makevar_val "$REINSTALL_ORIGIN" STAGEDIR`
248         {
249                 ( set -e
250                         cd "$stagedir"
251                         find . -not -type d
252                 ) | sed 's|^\.||' | while read filepath
253                 do
254                         [ ! -e "$filepath" ] && continue
255                         pkg=`pkg_which "$filepath" || :`
256                         [ -z "$pkg" ] && continue
257                         origin=`pkg_info_qo "$pkg" || :`
258                         [ x"$origin" = x"$REINSTALL_ORIGIN" ] || echo "$pkg"
259                 done
260                 pkgsys_get_conflicting_installed_pkgs install "$REINSTALL_ORIGIN"
261         } | sort -u > $tmp_conflicts
262         while read pkg_conflict
263         do
264                 fileedit_add_a_line_if_new "$pkg_conflict:$REINSTALL_NEWPKGNAME" \
265                         "${DBDIR}/forbidden_conflicts"
266         done < $tmp_conflicts
267         reinstall_backup_and_delete_conflicts < $tmp_conflicts
268 }
269
270 # ============= Restoration of backed up conflicts =============
271 reinstall_restore_conflicts ()
272 {
273         local origin_current tmpsrc
274         origin_current=$1
275         [ $opt_dry_run = yes ] && return
276         [ -e "${DBDIR}/deleted_conflicts" ] || return 0
277         tmpsrc=${TMPDIR}/reinstall_restore_conflicts::deleted_conflicts
278         cp "${DBDIR}/deleted_conflicts" "$tmpsrc"
279         while read origin pkg
280         do
281                 pkg_regexp_esc=`str_escape_regexp "$pkg"`
282                 origin_orig=`echo "$origin" \
283                         | sed -E -f "${DBDIR}/REVERSE_REPLACE.complete_sed_pattern"`
284                 [ "x$origin_orig" = "x$origin" ] && origin_orig=
285                 origin_replace=`echo "$origin" \
286                         | sed -E -f "${DBDIR}/REPLACE.complete_sed_pattern"`
287                 [ "x$origin_replace" = "x$origin" ] && origin_replace=
288                 if [ -n "$origin_current" -a "x$origin" = "x$origin_current" ] || \
289                         pkg_info_e "$pkg" || pkg_info_eO "$origin" || \
290                         { [ -n "$origin_orig" ] && pkg_info_eO "$origin_orig"; } || \
291                         { [ -n "$origin_replace" ] && pkg_info_eO "$origin_replace"; }
292                 then
293                         is_to_dereg_from_list=yes
294                 else
295                         is_to_dereg_from_list=no
296                 fi
297                 if pkgsys_chk_conflict_by_a_pkg install "$REINSTALL_ORIGIN" "$pkg"
298                 then
299                         fileedit_add_a_line_if_new "$pkg:$REINSTALL_NEWPKGNAME" "${DBDIR}/forbidden_conflicts"
300                         is_skipped=yes
301                 elif reinstall_chk_forbidden_conflicts "$pkg"
302                 then
303                         message_echo "INFO: Restoration of a conflict, $origin ($pkg), is avoided because it conflicts with installed packages."
304                         is_skipped=yes
305                 else
306                         is_skipped=no
307                 fi
308                 if [ $is_to_dereg_from_list = yes ]
309                 then
310                         grep -v -E "[[:space:]]$pkg_regexp_esc$" "${DBDIR}/deleted_conflicts" \
311                                 > ${DBDIR}/deleted_conflicts.tmp || :
312                         mv "${DBDIR}/deleted_conflicts.tmp" "${DBDIR}/deleted_conflicts"
313                         continue
314                 fi
315                 [ $is_skipped = yes ] && continue
316                 origin_regexp_esc=`str_escape_regexp "$origin"`
317                 origin_replace_regexp_esc=`str_escape_regexp "$origin_replace"`
318                 if grep -q -E -e "^$origin_regexp_esc$" -e "^$origin_replace_regexp_esc$" "${DBDIR}/taboo.all.list" 2> /dev/null
319                 then
320                         message_echo "INFO: Restoration of a conflict, $origin ($pkg), is avoided because it is taboo."
321                         continue
322                 fi
323                 message_echo "INFO: Restoring a conflict, $origin ($pkg)."
324                 if ! backup_pkg=`pkgsys_get_backup_pkg "$origin"`
325                 then
326                         message_echo "WARNING: No backup exists, gave up." >&2
327                         continue
328                 fi
329                 if pkg_add_fF "$backup_pkg"
330                 then
331                         grep -v -E "[[:space:]]$pkg_regexp_esc$" "${DBDIR}/deleted_conflicts" \
332                                 > ${DBDIR}/deleted_conflicts.tmp || :
333                         mv "${DBDIR}/deleted_conflicts.tmp" "${DBDIR}/deleted_conflicts"
334                 else
335                         message_echo "WARNING: Failed to restore. Note that your system may experience troubles by this error." >&2
336                 fi
337         done < $tmpsrc
338 }
339
340 # ============= Check whether the all requirements are installed =============
341 reinstall_are_requirements_ready ()
342 {
343         [ -e "$REINSTALL_DBNODE_DIR/requirements.all.direct" ] || return 0
344         while read origin
345         do
346                 pkg_info_eO "$origin" || return 1
347         done < $REINSTALL_DBNODE_DIR/requirements.all.direct
348 }
349
350 # ============= Get the all requirements ready for a port by restarting them if deinstalled =============
351 reinstall_chk_and_restore_requirements ()
352 {
353         local tmp_restore tmp_isfailed
354         tmp_restore=${TMPDIR}/reinstall_setup_requirements:restore
355         tmp_isfailed=${TMPDIR}/reinstall_setup_requirements:isfailed
356         cp /dev/null "$tmp_restore"
357         rm -f "$tmp_isfailed"
358         cat "$REINSTALL_DBNODE_DIR/requirements.all.direct" 2> /dev/null \
359                 | while read origin
360         do
361                 pkg_info_eO "$origin" && continue
362                 origin_regexp=`str_escape_regexp "$origin"`
363                 if grep -q -E "^$origin_regexp$" "${DBDIR}/taboo.all.list" 2> /dev/null
364                 then
365                         message_echo "INFO: Restoration of a requirement [$origin] is avoided because it is set taboo."
366                         touch "$tmp_isfailed"
367                         break
368                 fi
369                 origin_orig=`echo "$origin" \
370                         | sed -E -f "${DBDIR}/REVERSE_REPLACE.complete_sed_pattern"`
371                 if [ "x$origin_orig" != "x$origin" ]
372                 then
373                         pkg_info_eO "$origin_orig" && continue
374                         origin_orig_regexp=`str_escape_regexp "$origin_orig"`
375                         if grep -q -E "^$origin_orig_regexp$" "${DBDIR}/taboo.all.list" 2> /dev/null
376                         then
377                                 message_echo "INFO: Restoration of a requirement [$origin_orig] is avoided because it is set taboo."
378                                 touch "$tmp_isfailed"
379                                 break
380                         fi
381                 fi
382                 if ! pkgarc=`pkgsys_get_backup_pkg "$origin"` && \
383                         ! pkgarc=`pkgsys_get_backup_pkg "$origin_orig"`
384                 then
385                         if grep -q -E "^$origin_regexp$" "${DBDIR}/failed.list" 2> /dev/null
386                         then
387                                 touch "$tmp_isfailed"
388                                 break
389                         fi
390                         continue
391                 fi
392                 printf '%s\t%s\n' "$origin_orig" "$pkgarc" >> $tmp_restore
393         done
394         [ -e "$tmp_isfailed" ] && return 1
395         while read origin pkgarc
396         do
397                 pkg=`pkgsys_pkgarc_to_pkgname "$pkgarc"`
398                 pkg_regexp_esc=`str_escape_regexp "$pkg"`
399                 if reinstall_chk_forbidden_conflicts "$pkg"
400                 then
401                         message_echo "INFO: Restoration of a requirement [$origin ($pkg)] is avoided because it conflicts with installed packages."
402                         continue
403                 fi
404                 message_echo "INFO: Restoring a backed-up requirement [$origin ($pkg)]."
405                 if pkg_add_fF "$pkgarc"
406                 then
407                         grep -v -E "[[:space:]]$pkg_regexp_esc$" "${DBDIR}/deleted_conflicts" \
408                                 > ${DBDIR}/deleted_conflicts.tmp || :
409                         mv "${DBDIR}/deleted_conflicts.tmp" "${DBDIR}/deleted_conflicts"
410                 else
411                         message_echo "WARNING: Failed to restore by the backed-up package." >&2
412                 fi
413                 
414         done < $tmp_restore
415         :
416 }
417
418 # ============= Back-up of the currently installed package =============
419 reinstall_pkg_backup ()
420 {
421         local backup_pkg pkg
422         reinstall_chk_stage_complete PKG_BACKUP && return
423         pkg=`echo "$REINSTALL_CURRENTPKG" | tr ' ' '\n' | grep -v '^$' | tail -n 1`
424         if [ -n "$pkg" ]
425         then
426                 message_echo "-- (Creating temporary backup package for $REINSTALL_ORIGPKGTAG)"
427                 if backup_pkg=`pkgsys_create_backup_pkg "$pkg" "${DBDIR}/backup_packages"`
428                 then
429                         echo "$pkg" > $REINSTALL_DBNODE_DIR/backedup_version
430                         echo "$backup_pkg" > $REINSTALL_DBNODE_DIR/backedup_pkgfile
431                 else
432                         message_echo "WARNING: Failed to create the backup package, but ignored by hoping success." >&2
433                         return 1
434                 fi
435         fi
436         reinstall_register_stage_complete PKG_BACKUP
437 }
438
439 # ============= Deinstallation of the currently installed package =============
440 reinstall_deinstall ()
441 {
442         [ -n "$REINSTALL_CURRENTPKG" ] || return 0
443         reinstall_chk_stage_complete DEINSTALL && return
444         if pkgsys_is_necessary_pkgtool "$REINSTALL_ORIGIN"
445         then
446                 pkg_delete_f "$REINSTALL_CURRENTPKG" || \
447                 {
448                         message_echo "WARNING: Failed to deinstall $REINSTALL_CURRENTPKG by $PKGSYS_CMD_PKG_DELETE." >&2
449                 }
450         else
451                 echo "$REINSTALL_CURRENTPKG" | tr ' ' '\n' | grep -v '^$' | while read pkg
452                 do
453                         pkg_delete_f "$pkg" || \
454                         {
455                                 message_echo "WARNING: Failed to deinstall $pkg by $PKGSYS_CMD_PKG_DELETE." >&2
456                         }
457                 done
458         fi
459         message_echo "-- (Trying to deinstall by ports to make sure. This usually ends up with warnings.)"
460         reinstall_make deinstall || \
461         {
462                 message_echo "WARNING: Failed to deinstall $REINSTALL_CURRENTPKG by make deinstall." >&2
463         }
464         reinstall_register_stage_complete DEINSTALL
465 }
466
467 # ============= Recovery after failure of installation of the new package =============
468 reinstall_failed_install_recover ()
469 {
470         local backedup_version origin_regexp_esc backup_pkg
471         reinstall_chk_stage_complete FAILED_INSTALL.RECOVER && return
472         message_echo "INFO: Trying to deinstall the failed/terminated installation (Ignore failures)."
473         if [ -n "$REINSTALL_CURRENTPKG" ]
474         then
475                 pkg_delete_f "$REINSTALL_CURRENTPKG" || :
476         fi
477         message_echo "INFO: Trying to deinstall by ports to make sure (This usually ends up with warnings)."
478         reinstall_make deinstall || :
479         backedup_version=`cat "$REINSTALL_DBNODE_DIR/backedup_version" 2> /dev/null || :`
480         origin_regexp_esc=`str_escape_regexp "$REINSTALL_ORIGIN"`
481         if grep -q -E "^$origin_regexp_esc$" "${DBDIR}/taboo.all.list" 2> /dev/null
482         then
483                 message_echo "INFO: Restoration of the backup of $backedup_version is avoided because it is taboo."
484         elif [ -n "$backedup_version" ]
485         then
486                 if reinstall_chk_forbidden_conflicts "$backedup_version"
487                 then
488                         message_echo "INFO: Restoration of the backup of $backedup_version, is avoided because it conflicts with installed packages."
489                 else
490                         message_echo "INFO: Restoring the backup of $backedup_version."
491                         backup_pkg=`cat "$REINSTALL_DBNODE_DIR/backedup_pkgfile" 2> /dev/null || :`
492                         if [ -z "$backup_pkg" -o ! -e "$backup_pkg" ]
493                         then
494                                 message_echo "WARNING: No backup exists, gave up." >&2
495                         elif ! pkg_add_fF "$backup_pkg"
496                         then
497                                 message_echo "WARNING: Failed to restore $backedup_version. Note that your system may experience troubles by this error." >&2
498                         fi
499                 fi
500         fi
501         reinstall_register_stage_complete FAILED_INSTALL.RECOVER
502 }
503
504 # ============= Report an installation success to the all dependents =============
505 reinstall_tell_update_to_depandents ()
506 {
507         local tag level dbsuffix
508         pkgsys_is_pkgtool "$REINSTALL_ORIGIN" && return
509         reinstall_chk_stage_complete TELL_UPDATE_TO_DEPANDENTSL && return
510         for tag in all run build none
511         do
512                 for level in full direct
513                 do
514                         dbsuffix=${tag}.${level}
515                         cat "$REINSTALL_DBNODE_DIR/dependents.$dbsuffix" "$REINSTALL_DBNODE_DIR/dependents.$dbsuffix.orig" 2> /dev/null \
516                                 | sort -u \
517                                 | while read origin_dependent
518                         do
519                                 [ -d "${DBDIR}/requires/$origin_dependent" ] || continue
520                                 touch "${DBDIR}/requires/$origin_dependent/need_reinstall_due_to_upgraded_requirements.$dbsuffix"
521                                 fileedit_rm_a_line "$origin_dependent" \
522                                         "${DBDIR}/success.$dbsuffix.list"
523                                 fileedit_rm_a_line "$origin_dependent" \
524                                         "${DBDIR}/todo_after_requirements_succeed.$dbsuffix.list"
525                         done
526                 done
527         done
528         reinstall_register_stage_complete TELL_UPDATE_TO_DEPANDENTS
529 }
530
531 # ============= Closing operations after an installation success =============
532 reinstall_closing_operations_after_successful_install ()
533 {
534         local tag level initial_orig 
535         reinstall_chk_stage_complete CLOSING_OPERATIONS_AFTER_SUCCESSFUL_INSTALL && return
536         database_build_update_pkgname "$REINSTALL_ORIGIN" > /dev/null
537         database_build_update_pkgtag "$REINSTALL_ORIGIN"
538         for tag in all run build none
539         do
540                 for level in full direct
541                 do
542                         rm -f "$REINSTALL_DBNODE_DIR/need_reinstall_due_to_upgraded_requirements.${tag}.${level}"
543                         [ -e "$REINSTALL_DBNODE_DIR/failed_requirements.${tag}.${level}" ] || continue
544                         cp "$REINSTALL_DBNODE_DIR/failed_requirements.${tag}.${level}" \
545                                 "$REINSTALL_DBNODE_DIR/failed_requirements.${tag}.${level}.previous"
546                 done
547         done
548         pkgsys_delete_backup_pkg "$REINSTALL_ORIGIN"
549         initial_orig=`cat "$REINSTALL_DBNODE_DIR/initial_orig" 2> /dev/null` || :
550         [ -n "$initial_orig" ] && pkgsys_delete_backup_pkg "$initial_orig"
551         rm -f "$REINSTALL_DBNODE_DIR/backedup_pkgfile"
552         reinstall_deregister_stage in_postinstall
553         database_record_success "$REINSTALL_ORIGIN"
554         temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
555         message_echo "===>  Done successfully"
556         reinstall_register_stage_complete CLOSING_OPERATIONS_AFTER_SUCCESSFUL_INSTALL
557 }
558
559 # ============= Reinstallation for an origin =============
560 reinstall_exec ()
561 {
562         ( set -e
563         REINSTALL_ORIGIN=$1
564         REINSTALL_ORIGIN_REGEXP=`str_escape_regexp "$REINSTALL_ORIGIN"`
565         REINSTALL_DBNODE_DIR=${DBDIR}/requires/$REINSTALL_ORIGIN
566         REINSTALL_CURRENTPKG=`database_build_update_pkgname "$REINSTALL_ORIGIN" | tr '\n' ' ' | sed 's/ *$//'`
567         REINSTALL_IS_CURRENTPKG_LATEST=no
568         database_build_is_currentpkg_latest "$REINSTALL_ORIGIN" && REINSTALL_IS_CURRENTPKG_LATEST=yes
569         database_build_update_pkgtag "$REINSTALL_ORIGIN"
570         REINSTALL_PKGTAG=`cat "$REINSTALL_DBNODE_DIR/pkgtag"`
571         REINSTALL_ORIGPKGTAG="$REINSTALL_ORIGIN ($REINSTALL_PKGTAG)"
572         REINSTALL_DBSUFFIX=`options_get_dependency_type`.`options_get_dependency_level`
573         REINSTALL_NEWPKGNAME=`database_build_get_new_pkgname "$REINSTALL_ORIGIN"`
574         message_stage_title "$PROGRAM_STEP_COUNTER $REINSTALL_ORIGPKGTAG"
575         database_query_get_target_attributes currentorigin "$REINSTALL_ORIGIN"
576         if [ -z "${currentorigin_is_all}" -a -z "${currentorigin_is_relevant}" ]
577         then
578                 reinstall_skip 'Skipped because being irrelevant'
579                 return
580         fi
581         reinstall_skip_if_in_a_list 'Skipped because being a leaf port' leaf_ports_to_delete && return
582         if expr "$REINSTALL_CURRENTPKG" : '.* .*' > /dev/null
583         then
584                 message_echo "WARNING: Multiple packages are registered for this port. Reinstallation is needed to fix it." >&2
585         elif grep -q -E "^$REINSTALL_ORIGIN_REGEXP$" "${DBDIR}/damaged_package" 2> /dev/null
586         then
587                 message_echo "WARNING: Installed files have invalid checksums for this port. Reinstallation is needed to fix it." >&2
588         else
589                 if [ -e "${DBDIR}/target_all" ]
590                 then
591                         if [ $REINSTALL_IS_CURRENTPKG_LATEST = yes ]
592                         then
593                                 reinstall_restore_if_temporarily_deinstalled
594                                 reinstall_skip 'Skipped because being already latest' "$REINSTALL_ORIGIN"
595                                 return
596                         fi
597                 elif [ ! -e "$REINSTALL_DBNODE_DIR/conf_updated" ]
598                 then
599                         if [ -e "$nodedir/installed_by_pkg" ] && database_query_is_default_conf "$REINSTALL_ORIGIN" quiet
600                         then
601                                 reinstall_restore_if_temporarily_deinstalled
602                                 reinstall_skip 'Skipped because already upgraded with a prebuilt package'
603                                 return
604                         fi
605                         reinstall_skip_if_in_a_list 'Skipped because the reinstallation has been already completed' \
606                                 "success.${REINSTALL_DBSUFFIX}.list" restore && return
607                         if [ $opt_skip_unchanged = yes ]
608                         then
609                                 if [ ! -e "$REINSTALL_DBNODE_DIR/necessary_upgrade.${REINSTALL_DBSUFFIX}" ]
610                                 then
611                                         reinstall_restore_if_temporarily_deinstalled
612                                         if [ -e "$REINSTALL_DBNODE_DIR/installed_version" ]
613                                         then
614                                                 reinstall_skip 'Skipped because being already latest as well as the all requirements'
615                                         else
616                                                 reinstall_skip 'Skipped because being an only-build-time dependency of already latest packages'
617                                         fi
618                                         return
619                                 fi
620                         fi
621                         if [ \( $opt_skip_unchanged = no -a -e "$REINSTALL_DBNODE_DIR/succeeded_once" \) \
622                                 -o \( $opt_skip_unchanged = yes -a $REINSTALL_IS_CURRENTPKG_LATEST = yes \) ]
623                         then
624                                 if [ ! -e "$REINSTALL_DBNODE_DIR/need_reinstall_due_to_upgraded_requirements.${REINSTALL_DBSUFFIX}" ]
625                                 then
626                                         if ! database_query_is_necessary_upgrade "$REINSTALL_ORIGIN"
627                                         then
628                                                 reinstall_restore_if_temporarily_deinstalled
629                                                 fileedit_add_a_line_if_new "$REINSTALL_ORIGIN" \
630                                                         "${DBDIR}/todo_after_requirements_succeed.${REINSTALL_DBSUFFIX}.list"
631                                                 reinstall_skip 'Skipped because being already latest or failed as well as the all requirements'
632                                                 return
633                                         fi
634                                 fi
635                         fi
636                 fi
637                 reinstall_skip_if_in_a_list 'Marked to be manually-done' manually_done.list restore && return
638         fi
639         reinstall_skip_if_in_a_list 'Skipped because being a hold package' conf/HOLD:PORTS.parsed restore && return
640         if database_query_is_a_port_suppressed "$REINSTALL_ORIGIN"
641         then
642                 reinstall_restore_if_temporarily_deinstalled
643                 reinstall_skip 'Skipped because being suppressed'
644                 return
645         fi
646         reinstall_skip_if_in_a_list 'Ignored because being taboo' taboo.all.list && return
647         if ! reinstall_are_requirements_ready && ! reinstall_chk_and_restore_requirements
648         then
649                 reinstall_restore_if_temporarily_deinstalled
650                 for tag in all run build none
651                 do
652                         for level in full direct
653                         do
654                                 fileedit_add_a_line_if_new "$REINSTALL_ORIGIN" \
655                                         "${DBDIR}/todo_after_requirements_succeed.${tag}.${level}.list"
656                         done
657                 done
658                 reinstall_skip 'Skipped because the requirements cannot be ready'
659                 return
660         fi
661         if [ -e "$REINSTALL_DBNODE_DIR/installed_version" ]
662         then
663                 insttarget=reinstall
664                 instdesc='a reinstallation'
665         else
666                 insttarget=install
667                 instdesc='an installation'
668         fi
669         temp_set_msg_current_stage "$instdesc process for $REINSTALL_ORIGPKGTAG $PROGRAM_STEP_COUNTER"
670         message_target_relations "$REINSTALL_ORIGIN"
671         message_echo "------- Starting $instdesc process --------"
672         if [ $opt_dry_run = yes ]
673         then
674                 message_dry_run
675                 temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
676                 return
677         fi
678         if [ $opt_inst_by_pkg_if_can = yes ] && database_query_is_default_conf "$REINSTALL_ORIGIN"
679         then
680                 message_echo "INFO: The configuration of this port is default, so use of a prebuilt package is attempted."
681                 if reinstall_chk_stage in_bypkg
682                 then
683                         message_echo "(Restarting the previously terminated (re)installation-by-package process...)"
684                 else
685                         reinstall_register_stage in_bypkg
686                 fi
687                 if ! reinstall_chk_stage_complete INSTALL_BY_PKG
688                 then
689                         if ! reinstall_chk_stage FAILED_INSTALL_BY_PKG
690                         then
691                                 if [ "x$PKGSYS_USE_PKGNG" = xyes ] \
692                                         && pkgsys_is_necessary_pkgtool "$REINSTALL_ORIGIN"
693                                 then
694                                         if reinstall_deinstall && pkg_is_tool_available && pkg_info_e "$REINSTALL_NEWPKGNAME"
695                                         then
696                                                 reinstall_register_stage_complete INSTALL_BY_PKG
697                                         else
698                                                 reinstall_register_stage FAILED_INSTALL_BY_PKG
699                                         fi
700                                 else
701                                         func_pkg_inst_remote_fetch=pkg_inst_remote_fetch
702                                         func_pkg_inst_remote=pkg_inst_remote
703                                         if [ $opt_use_legacy_pkg_for_missing_pkgng = yes ]
704                                         then
705                                                 func_pkg_inst_remote_fetch=pkg_inst_remote_wild_fetch
706                                                 func_pkg_inst_remote=pkg_inst_remote_wild
707                                         fi
708                                         if $func_pkg_inst_remote_fetch "$REINSTALL_NEWPKGNAME"
709                                         then
710                                                 pkgsys_get_conflicting_installed_pkgs install "$REINSTALL_ORIGIN" \
711                                                         | reinstall_backup_and_delete_conflicts
712                                                 reinstall_pkg_backup || :
713                                                 reinstall_register_stage in_add_pkg
714                                                 reinstall_deinstall
715                                                 if $func_pkg_inst_remote "$REINSTALL_NEWPKGNAME"
716                                                 then
717                                                         reinstall_register_stage_complete INSTALL_BY_PKG
718                                                 else
719                                                         reinstall_register_stage FAILED_INSTALL_BY_PKG
720                                                 fi
721                                                 if pkgsys_is_necessary_pkgtool "$REINSTALL_ORIGIN"
722                                                 then
723                                                         pkg_is_tool_available || pkg_rescue_tools
724                                                 fi
725                                         else
726                                                 reinstall_register_stage FAILED_INSTALL_BY_PKG
727                                         fi
728                                 fi
729                         fi
730                         if reinstall_chk_stage FAILED_IN_ADD_PKG && reinstall_chk_stage in_add_pkg
731                         then
732                                 reinstall_failed_install_recover
733                         fi
734                 fi
735                 if reinstall_chk_stage_complete INSTALL_BY_PKG
736                 then
737                         touch "$REINSTALL_DBNODE_DIR/installed_timestamp"
738                         touch "$REINSTALL_DBNODE_DIR/installed_by_pkg"
739                         reinstall_deregister_stage in_bypkg
740                         reinstall_tell_update_to_depandents
741                         reinstall_closing_operations_after_successful_install
742                         message_echo
743                         return
744                 fi
745                 reinstall_deregister_stage in_bypkg
746                 message_echo "WARNING: (Re)installation-by-package is unsuccessful, so retrying by using port." >&2
747         fi
748         if reinstall_chk_stage in_prebuild
749         then
750                 message_echo "(Restarting the previously terminated pre-build process...)"
751         else
752                 reinstall_restore_conflicts
753                 reinstall_register_stage in_prebuild
754         fi
755         if [ -e "$REINSTALL_DBNODE_DIR/BEFOREBUILD.conf" ] && ! reinstall_chk_stage_complete BEFOREBUILD
756         then
757                 message_echo "-- BEFOREBUILD operations (start)"
758                 sh -e "$REINSTALL_DBNODE_DIR/BEFOREBUILD.conf" || \
759                 {
760                         reinstall_restore_if_temporarily_deinstalled
761                         reinstall_errproc 'BEFOREBUILD operations'
762                         temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
763                         return
764                 }
765                 message_echo "-- BEFOREBUILD operations (end)"
766                 reinstall_register_stage_complete BEFOREBUILD
767         fi
768         if ! reinstall_chk_stage_complete CLEAN_BEFORE_BUILD
769         then
770                 reinstall_make_anymode clean || \
771                 {
772                         reinstall_restore_if_temporarily_deinstalled
773                         reinstall_errproc 'clean before build'
774                         temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
775                         return
776                 }
777                 message_echo
778                 reinstall_register_stage_complete CLEAN_BEFORE_BUILD
779         fi
780         if ! reinstall_chk_stage_complete FETCH
781         then
782                 if ! reinstall_chk_stage FAILED_FETCH
783                 then
784                         reinstall_make_anymode checksum || \
785                                 reinstall_register_stage FAILED_FETCH
786                 fi
787                 if reinstall_chk_stage FAILED_FETCH
788                 then
789                         if ! reinstall_chk_stage_complete FAILED_FETCH.RETRIAL_1
790                         then
791                                 message_echo "INFO: Refetching distfiles for $REINSTALL_ORIGPKGTAG."
792                                 {
793                                         reinstall_make_anymode fetch FETCH_ARGS=-Ap &&
794                                                 reinstall_make_anymode checksum
795                                 } || reinstall_register_stage FAILED_REFETCH_1
796                                 reinstall_register_stage_complete FAILED_FETCH.RETRIAL_1
797                         fi
798                         if reinstall_chk_stage FAILED_REFETCH_1
799                         then
800                                 if ! reinstall_chk_stage_complete FAILED_FETCH.RETRIAL_2.DISTCLEAN
801                                 then
802                                         message_echo "INFO: Cleaning distfiles for the second refetch for $REINSTALL_ORIGPKGTAG."
803                                         reinstall_make_anymode distclean || :
804                                         reinstall_register_stage_complete FAILED_FETCH.RETRIAL_2.DISTCLEAN
805                                 fi
806                                 if ! reinstall_chk_stage_complete FAILED_FETCH.RETRIAL_2
807                                 then
808                                         message_echo "INFO: Refetching distfiles as the second retrial for $REINSTALL_ORIGPKGTAG."
809                                         if ! reinstall_make_anymode fetch FETCH_ARGS=-Ap
810                                         then
811                                                 reinstall_restore_conflicts "$REINSTALL_ORIGIN"
812                                                 reinstall_restore_if_temporarily_deinstalled
813                                                 reinstall_errproc 'fetch'
814                                                 temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
815                                                 return
816                                         fi
817                                         reinstall_make_anymode checksum || \
818                                         {
819                                                 reinstall_restore_conflicts "$REINSTALL_ORIGIN"
820                                                 reinstall_restore_if_temporarily_deinstalled
821                                                 reinstall_errproc 'checksum'
822                                                 temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
823                                                 return
824                                         }
825                                         reinstall_register_stage_complete FAILED_FETCH.RETRIAL_2
826                                 fi
827                         fi
828                 fi
829                 reinstall_register_stage_complete FETCH
830         fi
831         reinstall_deregister_stage in_prebuild
832         if [ $opt_batch_ports_only = yes -o $opt_interactive_ports_only = yes ]
833         then
834                 to_skip=no
835                 case `database_query_get_makevar_val "$REINSTALL_ORIGIN" IS_INTERACTIVE` in
836                         yes)    msg_is_interactive='interactive'
837                                 [ $opt_batch_ports_only = yes ] && to_skip=yes
838                                 ;;
839                         '')     msg_is_interactive='not interactive'
840                                 [ $opt_interactive_ports_only = yes ] && to_skip=yes
841                                 ;;
842                 esac
843                 if [ $to_skip = yes ]
844                 then
845                         reinstall_restore_if_temporarily_deinstalled
846                         database_build_update_pkgname "$REINSTALL_ORIGIN" > /dev/null
847                         message_echo "INFO: Further processes for this port are skipped because it is $msg_is_interactive."
848                         message_echo
849                         fileedit_rm_a_line "$REINSTALL_ORIGIN" "${DBDIR}/stage.loop_list/reinst_todo.remain"
850                         temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
851                         return
852                 fi
853         fi
854         if reinstall_chk_stage in_build
855         then
856                 message_echo "(Restarting the previously terminated build process...)"
857                 flag_restarted_build=yes
858         else
859                 reinstall_register_stage in_build
860                 flag_restarted_build=no
861         fi
862         if ! reinstall_chk_stage_complete BUILD
863         then
864                 pkgsys_get_conflicting_installed_pkgs build "$REINSTALL_ORIGIN" \
865                         | reinstall_backup_and_delete_conflicts
866                 if ! reinstall_make build
867                 then
868                         [ $flag_restarted_build = yes ] && message_echo "INFO: This failure may be due to restarting from a terminated build."
869                         is_build_env_modified=no
870                         if pkgsys_get_conflicting_installed_pkgs install "$REINSTALL_ORIGIN" \
871                                 > ${TMPDIR}/reinstall_exec::conflicts_install
872                         then
873                                 message_echo "INFO: Install-only conflicts are deinstalled for retrial because they may have effects on build."
874                                 reinstall_backup_and_delete_conflicts < ${TMPDIR}/reinstall_exec::conflicts_install
875                                 is_build_env_modified=yes
876                         fi
877                         if pkg_info_eO "$REINSTALL_ORIGIN"
878                         then
879                                 message_echo "INFO: The currently installed package for this port is deinstalled for retrial because it may have effects on build."
880                                 reinstall_pkg_backup || :
881                                 reinstall_deinstall
882                                 is_build_env_modified=yes
883                         fi
884                         if [ $flag_restarted_build = no -a $is_build_env_modified = no ]
885                         then
886                                 if pkgsys_is_necessary_pkgtool "$REINSTALL_ORIGIN"
887                                 then
888                                         pkg_is_tool_available || pkg_rescue_tools
889                                 fi
890                                 reinstall_restore_conflicts "$REINSTALL_ORIGIN"
891                                 reinstall_restore_if_temporarily_deinstalled
892                                 reinstall_errproc 'build'
893                                 temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
894                                 return
895                         fi
896                         reinstall_deregister_stage_complete CLEAN_BEFORE_BUILD
897                         message_echo "INFO: Retrying the build process after cleaning for $REINSTALL_ORIGPKGTAG."
898                         reinstall_make clean || \
899                         {
900                                 if pkgsys_is_necessary_pkgtool "$REINSTALL_ORIGIN"
901                                 then
902                                         pkg_is_tool_available || pkg_rescue_tools
903                                 fi
904                                 reinstall_restore_conflicts "$REINSTALL_ORIGIN"
905                                 reinstall_restore_if_temporarily_deinstalled
906                                 reinstall_errproc 'build and clean after build failure'
907                                 temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
908                                 return
909                         }
910                         reinstall_register_stage_complete CLEAN_BEFORE_BUILD
911                         reinstall_make build MAKE_JOBS_UNSAFE=yes || \
912                         {
913                                 if pkgsys_is_necessary_pkgtool "$REINSTALL_ORIGIN"
914                                 then
915                                         pkg_is_tool_available || pkg_rescue_tools
916                                 fi
917                                 reinstall_restore_conflicts "$REINSTALL_ORIGIN"
918                                 reinstall_restore_if_temporarily_deinstalled
919                                 reinstall_errproc 'retrial of build after failure'
920                                 temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
921                                 return
922                         }
923                 fi
924                 if pkgsys_is_necessary_pkgtool "$REINSTALL_ORIGIN"
925                 then
926                         pkg_is_tool_available || pkg_rescue_tools
927                 fi
928                 reinstall_register_stage_complete BUILD
929         fi
930         reinstall_deregister_stage in_build
931         if reinstall_chk_stage in_stage
932         then
933                 message_echo "(Restarting the previously terminated staging process...)"
934         else
935                 reinstall_register_stage in_stage
936         fi
937         if ! reinstall_chk_stage_complete STAGE
938         then
939                 if ! reinstall_make stage
940                 then
941                         reinstall_restore_conflicts "$REINSTALL_ORIGIN"
942                         reinstall_restore_if_temporarily_deinstalled
943                         reinstall_errproc 'STAGE operations'
944                         temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
945                         return
946                 fi
947                 reinstall_register_stage_complete STAGE
948         fi
949         reinstall_deregister_stage in_stage
950         if reinstall_chk_stage in_install
951         then
952                 message_echo "(Restarting the previously terminated installation process...)"
953         else
954                 reinstall_register_stage in_install
955         fi
956         reinstall_pkg_backup || :
957         if [ -e "$REINSTALL_DBNODE_DIR/BEFOREDEINSTALL.conf" ] && ! reinstall_chk_stage_complete BEFOREDEINSTALL
958         then
959                 if [ -n "$REINSTALL_CURRENTPKG" ]
960                 then
961                         message_echo "-- BEFOREDEINSTALL operations (start)"
962                         sh -e "$REINSTALL_DBNODE_DIR/BEFOREDEINSTALL.conf" || \
963                         {
964                                 reinstall_restore_conflicts "$REINSTALL_ORIGIN"
965                                 reinstall_restore_if_temporarily_deinstalled
966                                 reinstall_errproc 'BEFOREDEINSTALL operations'
967                                 temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
968                                 return
969                         }
970                         message_echo "-- BEFOREDEINSTALL operations (end)"
971                 fi
972                 reinstall_register_stage_complete BEFOREDEINSTALL
973         fi
974         if ! reinstall_chk_stage_complete INSTALL
975         then
976                 if pkgsys_is_necessary_pkgtool "$REINSTALL_ORIGIN"
977                 then
978                         reinstall_backup_and_delete_remaining_install_conflicts
979                         reinstall_deinstall
980                 else
981                         reinstall_deinstall
982                         reinstall_backup_and_delete_remaining_install_conflicts
983                 fi
984                 if ! reinstall_chk_stage FAILED_INSTALL
985                 then
986                         if reinstall_make $insttarget || \
987                                 {
988                                         message_echo "INFO: Cleaning up for retrial."
989                                         reinstall_make deinstall \
990                                                 || message_echo "WARNING: Continuing by hoping a success." >&2
991                                         message_echo "INFO: Retrying the installation."
992                                         reinstall_make $insttarget MAKE_JOBS_UNSAFE=yes
993                                 }
994                         then
995                                 touch "$REINSTALL_DBNODE_DIR/installed_timestamp"
996                                 reinstall_register_stage_complete INSTALL
997                         else
998                                 reinstall_register_stage FAILED_INSTALL
999                         fi
1000                 fi
1001                 if reinstall_chk_stage FAILED_INSTALL
1002                 then
1003                         if pkgsys_is_necessary_pkgtool "$REINSTALL_ORIGIN"
1004                         then
1005                                 pkg_is_tool_available || pkg_rescue_tools
1006                         else
1007                                 reinstall_failed_install_recover
1008                         fi
1009                         if [ -e "$REINSTALL_DBNODE_DIR/AFTERINSTALL.conf" ] && \
1010                                 ! reinstall_chk_stage_complete FAILED_INSTALL.AFTERINSTALL
1011                         then
1012                                 if [ -n "$REINSTALL_CURRENTPKG" ]
1013                                 then
1014                                         message_echo "-- AFTERINSTALL operations (start)"
1015                                         sh -e "$REINSTALL_DBNODE_DIR/AFTERINSTALL.conf" || \
1016                                         {
1017                                                 message_echo "WARNING: Failed in AFTERINSTALL operations." >&2
1018                                                 message_echo "---- (The process is continued anyway)"
1019                                         }
1020                                         message_echo "-- AFTERINSTALL operations (end)"
1021                                 fi
1022                                 reinstall_register_stage_complete FAILED_INSTALL.AFTERINSTALL
1023                         fi
1024                         reinstall_restore_conflicts "$REINSTALL_ORIGIN"
1025                         reinstall_restore_if_temporarily_deinstalled
1026                         reinstall_errproc 'install'
1027                         temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
1028                         return
1029                 fi
1030         fi
1031         reinstall_restore_if_temporarily_deinstalled
1032         if ! reinstall_chk_stage_complete UPDATE_CONFLICTS
1033         then
1034                 pkg_info_qL "$REINSTALL_CURRENTPKG" | str_escape_regexp_filter | sed 's/^/^/; s/$/$/' \
1035                         > ${TMPDIR}/previously_installed_files.grep_pattern
1036                 cat "${DBDIR}/backup_pkgarcs.lst" 2> /dev/null \
1037                         | while read origin_bak pkgpath_bak
1038                 do
1039                         pkg_bak=`pkgsys_pkgarc_to_pkgname "$pkgpath_bak"`
1040                         if [ "$origin_bak" = "$REINSTALL_ORIGIN" ]
1041                         then
1042                                 [ "$pkg_bak" = "$REINSTALL_NEWPKGNAME" ] && continue
1043                         elif reinstall_quick_chk_forbidden_conflicts "$pkg_bak"
1044                         then
1045                                 continue
1046                         elif ! pkgsys_chk_match_to_restored_files_by_backup_pkg "$origin_bak" "${TMPDIR}/previously_installed_files.grep_pattern"
1047                         then
1048                                 continue
1049                         fi
1050                         fileedit_add_a_line_if_new "$pkg_bak:$REINSTALL_NEWPKGNAME" \
1051                                 "${DBDIR}/forbidden_conflicts"
1052                 done
1053                 reinstall_register_stage_complete UPDATE_CONFLICTS
1054         fi
1055         if [ -e "$REINSTALL_DBNODE_DIR/AFTERINSTALL.conf" ] && ! reinstall_chk_stage_complete AFTERINSTALL
1056         then
1057                 message_echo "-- AFTERINSTALL operations (start)"
1058                 sh -e "$REINSTALL_DBNODE_DIR/AFTERINSTALL.conf" || \
1059                 {
1060                         reinstall_restore_conflicts "$REINSTALL_ORIGIN"
1061                         reinstall_errproc 'AFTERINSTALL operations'
1062                         temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
1063                         return
1064                 }
1065                 message_echo "-- AFTERINSTALL operations (end)"
1066                 reinstall_register_stage_complete AFTERINSTALL
1067         fi
1068         reinstall_deregister_stage in_install
1069         reinstall_register_stage in_postinstall
1070         reinstall_restore_conflicts "$REINSTALL_ORIGIN"
1071         if ! reinstall_chk_stage_complete CLEAN_AFTER_INSTALL
1072         then
1073                 reinstall_make clean || \
1074                 {
1075                         message_echo "WARNING: Failed to clean $REINSTALL_ORIGPKGTAG." >&2
1076                 }
1077                 reinstall_register_stage_complete CLEAN_AFTER_INSTALL
1078         fi
1079         reinstall_tell_update_to_depandents
1080         reinstall_closing_operations_after_successful_install
1081         message_echo
1082         )
1083 }