OSDN Git Service

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