OSDN Git Service

Version 3.0.1+toward_3.0.2_20130703190902
[portsreinstall/current.git] / bin / portsreinstall
index bae2580..2623a54 100755 (executable)
@@ -5,18 +5,18 @@
 # This software is distributed under the 2-Clause BSD License.
 # ==================================================================================
 
-# ================================================
-# ================ PREPARATION ===================
-# ================================================
+# ==================================================
+# ================== ENVIRONMENT ===================
+# ==================================================
+
 # ============ Set up of environment =============
 APPNAME=`basename "$0"`
 
-# MYVERSION=3.0.0
+# MYVERSION=3.0.1
 # COMPATIBLE_VERSIONS='^(3\.0\.[0-9]+)$'
 # Template for development versions
-MYVERSION=2.2.2+toward_3.0.0_20130531074611
-COMPATIBLE_VERSIONS='^(2\.2\.2\+toward_3\.0\.0_[0-9]+|3\.0\.[0-9]+)$'
-
+MYVERSION=3.0.1+toward_3.0.2_20130703190902
+COMPATIBLE_VERSIONS='^(3\.0\.[0-9]+\+toward_3\.0\.1_[0-9]+|3\.0\.[0-9]+)$'
 MYPREFIX=`dirname "\`dirname \"$0\"\`" | sed 's|/bin$||'`
 MYPREFIX=${MYPREFIX:-/usr/local}
 LIBDIR=${MYPREFIX}/lib/${APPNAME}
@@ -45,6 +45,11 @@ DBDIR=/var/tmp/${APPNAME}.db
 CONFFILE=${MYPREFIX}/etc/${APPNAME}.conf
 PKGTOOLSCONF=${MYPREFIX}/etc/pkgtools.conf
 
+
+# ==================================================
+# ========= PARSING OPTIONS AND ARGUMENTS ==========
+# ==================================================
+
 # ============= Save arguments for upgraded restart =============
 options_dump_args "$@" > ${TMPDIR}/restart_command.sh
 
@@ -82,7 +87,7 @@ then
        exit
 fi
 
-# ============= Output version number if the case of a help mode or option/argument errors =============
+# ============= Output version number =============
 if [ $opt_show_version = yes ]
 then
        message_version
@@ -99,66 +104,9 @@ database_build_create
 command_parse_args "$@"
 shift "${COMMAND_SHIFT}"
 
-# ====================================================
-# ================== FUNCTIONS =======================
-# ====================================================
-
-# chk_and_warn_ignored_target_options ()
-# {
-#      local option val
-#      option=$1
-#      val=$2
-#      [ -z "$val" -o ! -e "${DBDIR}/COMPLETE_PARSE_OPTION_TARGET_PORTS" ] && return
-#      echo "WARNING: -$option option is specified but ignored because we are restarting the previous run." >&2
-# }
-
-# chk_unavailable_target_options_logical_default_no ()
-# {
-#      local option val
-#      option=$1
-#      val=$2
-#      flagfile=$3
-#      [ "x$val" = xno ] && return
-#      if [ -e "${DBDIR}/$flagfile" ]
-#      then
-#              echo "ERROR: -$option option is specified, but the temporary database is not clean." >&2
-#              exit 1
-#      fi
-#      if [ -z "$opt_target_dependents$opt_target_requirements" ]
-#      then
-#              echo "ERROR: -$option option is specified, but invalid without -t or -T option." >&2
-#              exit 1
-#      fi
-# }
-
-# chk_and_warn_ignored_options ()
-# {
-#      local option var is_current_specified is_previous_specified operation
-#      option=$1
-#      var=$2
-#      while [ $# -gt 0 ]
-#      do
-#              [ -e "${DBDIR}/$1" ] && return
-#              shift
-#      done
-#      is_current_specified=n
-#      grep -E "^`str_escape_regexp \"$var\"`=" "${TMPDIR}/changed_optvals.sh" > /dev/null 2>&1 && is_current_specified=y
-#      is_previous_specified=n
-#      grep -E "^`str_escape_regexp \"$var\"`=" "${DBDIR}/saved_changed_options.sh" > /dev/null 2>&1 && is_previous_specified=y
-#      if [ $is_current_specified = y -a $is_previous_specified = n ]
-#      then
-#              operation=specified
-#      elif [ $is_current_specified = n -a $is_previous_specified = y ]
-#      then
-#              operation=removed
-#      else
-#              return 0
-#      fi
-#      echo "WARNING: -$option option is $operation but ignored by transferring the settings from the previous run." >&2
-# }
 
 # ==================================================
-# ==================== MAIN ========================
+# ================== TOOLS SET UP ==================
 # ==================================================
 
 # ============= Opening title =============
@@ -171,7 +119,7 @@ message_echo
 # ============= Execute command operations which do not need package tools =============
 
 command_exec_without_pkgtools "$@"
-touch "${DBDIR}/in_use"
+misc_is_superuser_privilege && touch "${DBDIR}/in_use"
 
 # ============= Definition of environment dependent functions =============
 
@@ -208,10 +156,11 @@ then
        . "${DBDIR}/saved_options.sh"
        . "${TMPDIR}/renewed_optvals.sh"
 fi
-misc_get_all_vardefs | options_filter saved > ${DBDIR}/saved_options.sh
+misc_is_superuser_privilege && misc_get_all_vardefs | options_filter saved > ${DBDIR}/saved_options.sh
 
 # Show option values
-if [ $opt_batch_mode = no -a \( $COMMAND_MODE = do -o $COMMAND_MODE = redo \) ]
+if [ -e "${DBDIR}/saved_options.sh" -a $opt_batch_mode = no \
+       -a \( $COMMAND_MODE = do -o $COMMAND_MODE = redo \) ]
 then
        message_echo "INFO: List of option values:"
        message_echo "-----------------------------------------"
@@ -456,568 +405,10 @@ then
        exit
 fi
 
-# # # Check options changed from the default
-# # misc_get_all_vardefs | grep -E '^opt_[a-zA-Z0-9_]+=' | sort -u | options_filter saved > ${TMPDIR}/current_optvals.sh
-# # option_nochange=n
-# # diff "${TMPDIR}/default_optvals.sh" "${TMPDIR}/current_optvals.sh" \
-# #    | grep '^>' | sed 's/^> //' > ${TMPDIR}/changed_optvals.sh || \
-# #    option_alldefault=y
-# # 
-# # # Save/load of option values
-# # option_reset=n
-# # option_chk_validity=n
-# # if [ "x$COMMAND_MODE" = xredo -a -e "${DBDIR}/COMPLETE_PREPARATION" ]
-# # then
-# #    option_msg_reruntype=redo
-# # else
-# #    option_msg_reruntype=restarted
-# # fi
-# # if [ -e "${DBDIR}/COMPLETE_SAVE_OPTIONS" ]
-# # then
-# #    if [ $option_alldefault = y ]
-# #    then
-# #            echo "INFO: This is a $option_msg_reruntype run."
-# #            echo "      Option settings for ${OPTIONS_TRANSFERRED} are transferred from the previous run."
-# #    elif diff "${DBDIR}/saved_changed_options.sh" "${TMPDIR}/changed_optvals.sh" > /dev/null
-# #    then
-# #            if [ $opt_reset_targets = yes -a "x$COMMAND_MODE" = xredo ]
-# #            then
-# #                    echo "INFO: This is a redo run with upgraded option settings."
-# #                    echo "      Previous option settings are reset except for ${OPTIONS_TRANSFERRED_NONRENEWABLE}."
-# #                    option_reset=y
-# # echo "FIXME:: filter option values" >&2
-# # exit 255
-# #                    option_chk_validity=y
-# #            else
-# #                    echo "INFO: This is a $option_msg_reruntype run."
-# #                    echo "      The specified options for ${OPTIONS_TRANSFERRED} are ignored and the previous ones are loaded instead."
-# #                    option_chk_validity=y
-# #            fi
-# #    else
-# #            echo "INFO: This is a $option_msg_reruntype run."
-# #            echo "      Because the transferable option settings are unchanged from the previous run,"
-# #            echo "      operations are to be continued as the normal restart."
-# #    fi
-# # else
-# #    option_reset=y
-# # fi
-# # if [ $option_reset = y ]
-# # then
-# #    options_filter saved < ${TMPDIR}/current_optvals.sh > ${DBDIR}/saved_options.sh
-# #    cp "${TMPDIR}/changed_optvals.sh" "${DBDIR}/saved_changed_options.sh"
-# #    touch "${DBDIR}/COMPLETE_SAVE_OPTIONS"
-# # else
-# #    
-# #    t_opt_skip_unchanged=$opt_skip_unchanged
-# #    t_opt_include_buildtime_dependencies=$opt_include_buildtime_dependencies
-# #    t_opt_include_runtime_dependencies=$opt_include_runtime_dependencies
-# #    t_opt_keep_distfiles=$opt_keep_distfiles
-# #    . "${DBDIR}/saved_options.sh"
-# #    chk_and_warn_ignored_logical_options q opt_skip_unchanged
-# #    chk_and_warn_ignored_logical_options b opt_include_buildtime_dependencies
-# #    chk_and_warn_ignored_logical_options B opt_include_runtime_dependencies
-# #    chk_and_warn_ignored_logical_options d opt_keep_distfiles
-# # fi
-# # if [ $opt_reset_targets = yes -a "x$COMMAND_MODE" = xredo ]
-# # then
-# #    chk_and_warn_ignored_options t opt_target_dependents COMPLETE_PARSE_OPTION_TARGET_PORTS
-# #    chk_and_warn_ignored_options T opt_target_requirements COMPLETE_PARSE_OPTION_TARGET_PORTS
-# #    chk_and_warn_ignored_options i opt_add_targets_to_needed COMPLETE_PARSE_OPTION_TARGET_PORTS
-# # fi
-# # chk_and_warn_ignored_options o opt_only_target_scope \
-# #    COMPLETE_TARGETS COMPLETE_RUNTIME_REQUIREMENT_LISTS COMPLETE_BUILDTIME_REQUIREMENT_LISTS \
-# #    COMPLETE_INSPECT_DEPENDENTS COMPLETE_INSPECT_ALL_DEPENDENCIES
-# # for option in p P Q
-# # do
-# #    chk_and_warn_ignored_options $option opt_load_pkgtoolsconf COMPLETE_IMPORT_PKGTOOLS_CONF
-# # done
-# 
-# # Check of conflicting option values
-# # options_get_dependency_type > /dev/null
-# # chk_and_warn_ignored_target_options t "$opt_target_dependents" COMPLETE_PARSE_OPTION_TARGET_PORTS
-# # chk_and_warn_ignored_target_options T "$opt_target_requirements"
-# # chk_unavailable_target_options_logical_default_no o "$opt_only_target_scope" COMPLETE_INSTALLED_PORTS
-# # chk_unavailable_target_options_logical_default_no i "$opt_add_targets_to_needed" COMPLETE_INSTALLED_PORTS
-# # if [ $opt_add_targets_to_needed = yes ]
-# # then
-# #    rm -f "${TMPDIR}/is_err"
-# #    echo "$opt_target_dependents$opt_target_requirements" | sed -E 's/[ :]+/\
-# # /g' | grep -v '^$' | sort -u | while read glob
-# #    do
-# #            rm -f "${TMPDIR}/target_ports"
-# #            pkgsys_register_evaluated_globs "$glob" "${TMPDIR}/target_ports"
-# #            num_matching_target_ports=`wc -l < ${TMPDIR}/target_ports`
-# #            if [ $num_matching_target_ports -eq 0 ]
-# #            then
-# #                    echo "ERROR: No matching port for target glob [$glob] (constraint due to -i option)." >&2
-# #                    echo >&2
-# #                    touch "${TMPDIR}/is_err"
-# #            elif [ $num_matching_target_ports -gt 1 ]
-# #            then
-# #                    echo "ERROR: Multiple ports match target glob [$glob] (constraint due to -i option):" >&2
-# #                    cat "${TMPDIR}/target_ports" >&2
-# #                    echo >&2
-# #                    touch "${TMPDIR}/is_err"
-# #            fi
-# #    done
-# #    [ -e "${TMPDIR}/is_err" ] && exit 1
-# # fi
-# # [ "$opt_load_pkgtoolsconf" = undef -o ! -e "${DBDIR}/COMPLETE_IMPORT_PKGTOOLS_CONF" ] \
-# #    || echo "WARNING: -p, -P or -Q option is specified but ignored by following the previous settings." >&2
-# 
-# # Upgrade ports-mgmt/pkg if it is newer in the ports tree
-# if [ "x${command:-do}" = xdo -a $opt_suppress_pkgtools_upadte = no -a "x$PKGSYS_USE_PKGNG" = xyes ] \
-#      && pkg_is_tool_available
-# then
-#      _pkgsys_upgrade_pkgsystools__get_current_pkg ()
-#      {
-#              pkg_info_qO ports-mgmt/pkg
-#      }
-#      _pkgsys_upgrade_pkgsystools__get_port_origin ()
-#      {
-#              echo ports-mgmt/pkg
-#      }
-#      _pkgsys_upgrade_pkgsystools__restore_olg_pkg ()
-#      {
-#              local prefix prefix_parent
-#              mkdir "$tmpprefix:restore_olg_pkg"
-#              tar xf "$backup_pkg" -C "$tmpprefix:restore_olg_pkg"
-#              prefix=`grep -m 1 '^prefix: ' "$tmpprefix:restore_olg_pkg/+MANIFEST" | sed 's/^prefix: *//'`
-#              prefix_parent=`dirname "$prefix"`
-#              cp -Rp "$tmpprefix:restore_olg_pkg/$prefix" "$prefix_parent"/
-#              message_echo "INFO: The old version is restored by simple copy."
-#              if pkg_add_f "$backup_pkg"
-#              then
-#                      message_echo "INFO: The old version is successfully registered again."
-#              else
-#                      echo "WARNING: Failed to register $toolname, but the process is continued." >&2
-#              fi
-#      }
-#      pkgsys_upgrade_pkgsystools pkgng ports-mgmt/pkg
-# #    pkg_info_qO ports-mgmt/pkg > ${TMPDIR}/pkgng_version || :
-# #    num_pkgng_versions=`cat "${TMPDIR}"/pkgng_version | wc -l`
-# #    if [ $num_pkgng_versions -gt 1 ]
-# #    then
-# #            echo "ERROR: Multiple versions of ports-mgmt/pkg are installed. This is unexpected." >&2
-# #            exit 1
-# #    elif [ $num_pkgng_versions -eq 1 ]
-# #    then
-# #            [ -d "${PORTSDIR}/ports-mgmt/pkg" ] \
-# #                    || { echo "ERROR: ${PORTSDIR}/ports-mgmt/pkg does not exist." >&2; exit 1; }
-# #            pkgng_pkg_current=`cat "${TMPDIR}"/pkgng_version`
-# #            pkgng_pkg_new=`make -C "${PORTSDIR}/ports-mgmt/pkg" package-name`
-# #            if [ -n "$pkgng_pkg_new" ]
-# #            then
-# #                    if [ "$pkgng_pkg_new" != `cat "${TMPDIR}"/pkgng_version` ]
-# #                    then
-# #                            echo "INFO: The ports tree has a new version of ports-mgmt/pkg. It will be upgraded first."
-# #                            echo "Updating ports-mgmt/pkg ($pkgng_pkg_current => $pkgng_pkg_new)..."
-# #                            echo "Backing up the installed package..."
-# #                            backup_pkg=`pkgsys_create_backup_pkg "$pkgng_pkg_current" "${PORTSDIR}"/packages/All` || :
-# #                            if [ -n "$backup_pkg" ]
-# #                            then
-# #                                    echo "Reinstalling ports-mgmt/pkg..."
-# #                                    pkg_delete_f "$pkgng_pkg_current"
-# #                                    if make -C "${PORTSDIR}/ports-mgmt/pkg" reinstall
-# #                                    then
-# #                                            make -C "${PORTSDIR}/ports-mgmt/pkg" clean
-# #                                    else
-# #                                            echo "WARNING: Reinstallation failed. Restoring the old package..." >&2
-# #                                            make -C "${PORTSDIR}/ports-mgmt/pkg" deinstall || :
-# #                                            make -C "${PORTSDIR}/ports-mgmt/pkg" clean || :
-# #                                            mkdir "${TMPDIR}"/pkgng:restore
-# #                                            tar xf "$backup_pkg" -C "${TMPDIR}"/pkgng:restore
-# #                                            localbase_parent=`dirname "${LOCALBASE}"`
-# #                                            cp -Rp "${TMPDIR}/pkgng:restore/${LOCALBASE}" "$localbase_parent"/
-# #                                            echo "INFO: The old version is restored by simple copy."
-# #                                            if pkg_add_f "$backup_pkg"
-# #                                            then
-# #                                                    echo "INFO: The old version is successfully registered again."
-# #                                                    :
-# #                                            else
-# #                                                    echo "WARNING: Failed to register ports-mgmt/pkg." >&2
-# #                                            fi
-# #                                            echo "INFO: Continuing with the old version..."
-# #                                    fi
-# #                            else
-# #                                    echo "WARNING: Upgrade of ports-mgmt/pkg is skipped because of the failure in backup." >&2
-# #                                    echo "INFO: Continuing with the old version..."
-# #                            fi
-# #                    fi
-# #            else
-# #                    echo "WARNING: No port for ports-mgmt/pkg exists in the current ports tree." >&2
-# #            fi
-# #    fi
-# #    touch "${DBDIR}/COMPLETE_UPGRADE_PKGNG"
-# fi
-# 
-# # Upgrade portsreinstall itself if it is newer in the ports tree
-# if [ "x${command:-do}" = xdo -a $opt_suppress_self_upadte = no ]
-# then
-#      _pkgsys_upgrade_pkgsystools__get_current_pkg ()
-#      {
-#              pkgsys_get_my_current_pkg
-#      }
-#      _pkgsys_upgrade_pkgsystools__get_port_origin ()
-#      {
-#              pkgsys_get_my_origin
-#      }
-#      _pkgsys_upgrade_pkgsystools__postinstall ()
-#      {
-#              if [ -d "${DBDIR}" ]
-#              then
-#                      database_build_refresh || :
-#              fi
-#              message_echo "INFO: Restarting with the new version..."
-#              message_echo
-#              touch "$db_complete_flag"
-#              . "${TMPDIR}"/restart_command.sh
-#              echo "ERROR: Failed to launch the new version." >&2
-#              exit 1
-#      }
-#      _pkgsys_upgrade_pkgsystools__restore_olg_pkg ()
-#      {
-#              pkg_add_f "$backup_pkg"
-#      }
-#      
-#      pkgsys_upgrade_pkgsystools self "${APPNAME}"
-# #    pkg_info_Ex "${APPNAME}-[0-9].*" > ${TMPDIR}/self_version || :
-# #    num_self_versions=`cat "${TMPDIR}"/self_version | wc -l`
-# #    if [ $num_self_versions -gt 1 ]
-# #    then
-# #            echo "ERROR: Multiple versions of ${APPNAME} are installed. This is unexpected." >&2
-# #            exit 1
-# #    elif [ $num_self_versions -eq 1 ]
-# #    then
-# #            self_origin=`pkg_info_qo "\`cat \"${TMPDIR}\"/self_version\`"`
-# #            [ -d "${PORTSDIR}/$self_origin" ] \
-# #                    || { echo "ERROR: ${PORTSDIR}/$self_origin does not exist." >&2; exit 1; }
-# #            self_pkg_current=`cat "${TMPDIR}"/self_version`
-# #            self_pkg_new=`make -C "${PORTSDIR}/$self_origin" package-name`
-# #            if [ -n "$self_pkg_new" ]
-# #            then
-# #                    if [ "$self_pkg_new" != `cat "${TMPDIR}"/self_version` ]
-# #                    then
-# #                            echo "INFO: The ports tree has a new version of ${APPNAME}. It will be upgraded first."
-# #                            echo "Updating $self_origin ($self_pkg_current => $self_pkg_new)..."
-# #                            echo "Backing up the installed package..."
-# #                            backup_pkg=`pkgsys_create_backup_pkg "$self_pkg_current" "${PORTSDIR}"/packages/All` || :
-# #                            echo "Reinstalling ${APPNAME}..."
-# #                            pkg_delete_f "$self_pkg_current"
-# #                            if make -C "${PORTSDIR}/$self_origin" reinstall
-# #                            then
-# #                                    make -C "${PORTSDIR}/$self_origin" clean
-# #                                    if [ -d "${DBDIR}" ]
-# #                                    then
-# #                                            database_build_refresh || :
-# #                                    fi
-# #                                    echo "INFO: Restarting with the new version..."
-# #                                    echo
-# #                                    touch "${DBDIR}/COMPLETE_UPGRADE_PORTSREINSTALL"
-# #                                    . "${TMPDIR}"/restart_command.sh
-# #                                    echo "ERROR: Failed to launch the new version." >&2
-# #                                    exit 1
-# #                            else
-# #                                    echo "WARNING: Reinstallation failed. Restoring the old package..." >&2
-# #                                    make -C "${PORTSDIR}/$self_origin" deinstall || :
-# #                                    make -C "${PORTSDIR}/$self_origin" clean || :
-# #                                    [ -n "$backup_pkg" ] && pkg_add_f "$backup_pkg"
-# #                                    echo "INFO: Continuing with the old version..."
-# #                            fi
-# #                    fi
-# #            else
-# #                    echo "WARNING: No port for ${APPNAME} exists in the current ports tree." >&2
-# #            fi
-# #    else
-# #            echo "INFO: This version of ${APPNAME} has been installed not by ports/packages mechanism."
-# #            echo "Thus any upgrades on ${APPNAME} will be ignored."
-# #    fi
-# #    touch "${DBDIR}/COMPLETE_UPGRADE_PORTSREINSTALL"
-# fi
-
-# # ------- Commands -------
-# 
-# # Main operations of commands
-# case ${command:-do} in
-# clean)
-#      misc_chk_privilege
-#      echo "Starting to clean up the temporary database..."
-#      rm -rf "${DBDIR}"
-#      echo "Done"
-#      exit
-#      ;;
-# ok)
-#      misc_chk_privilege
-#      database_query_chk_preparation_completion
-#      temp_warn_obsolete_temp_db
-#      opr=$1
-#      shift
-#      [ -e "${DBDIR}/manually_done.list" ] || touch "${DBDIR}/manually_done.list"
-#      cp "${DBDIR}/manually_done.list" "${TMPDIR}/manually_done.list.old"
-#      case $opr in
-#      add)
-#              pkgsys_register_evaluated_globs "$*" "${DBDIR}/manually_done.list"
-#              echo "`str_linearize_list_and \"$*\"` is/are registered to the list of manually resolved ports"
-#              ;;
-#      del)
-#              pkgsys_register_evaluated_globs "$*" "${DBDIR}/manually_done.list" remove
-#              echo "`str_linearize_list_and \"$*\"` is/are deregistered from the list of manually resolved ports"
-#              ;;
-#      esac
-#      if diff "${TMPDIR}/manually_done.list.old" "${DBDIR}/manually_done.list" > ${TMPDIR}/manually_done.list.diff
-#      then
-#              :
-#      else
-#              grep '^<' "${TMPDIR}/manually_done.list.diff" 2> /dev/null | sed 's/^< //' | while read origin
-#              do
-#                      database_record_failure "$origin"
-#              done
-#              grep '^>' "${TMPDIR}/manually_done.list.diff" 2> /dev/null | sed 's/^> //' | while read origin
-#              do
-#                      database_record_success "$origin"
-#              done
-#      fi
-#      echo "Now the following ports have been manually resolved:"
-#      [ ! -r "${DBDIR}/manually_done.list" ] || cat "${DBDIR}/manually_done.list"
-#      exit
-#      ;;
-# taboo)
-#      misc_chk_privilege
-#      temp_warn_obsolete_temp_db
-#      opr=$1
-#      shift
-#      case $opr in
-#      add)
-#              pkgsys_register_evaluated_globs "$*" "${DBDIR}/taboo.list"
-#              echo "`str_linearize_list_and \"$*\"` is/are registered to the list of ports to be ignored."
-#              ;;
-#      del)
-#              pkgsys_register_evaluated_globs "$*" "${DBDIR}/taboo.list" remove
-#              echo "`str_linearize_list_and \"$*\"` is/are deregistered from the list of ports to be ignored."
-#              ;;
-#      esac
-#      fileedit_combine_lists $"{DBDIR}/conf/TABOO:PORTS.parsed" "${DBDIR}/taboo.list" > ${DBDIR}/taboo.all.list
-#      echo "Now the following ports are registered to be ignored:"
-#      cat "${DBDIR}/taboo.all.list"
-#      exit
-#      ;;
-# save)
-#      misc_chk_privilege
-#      [ -d "${DBDIR}" ] || { echo "ERROR: Database has not been built up yet." >&2; exit 1; }
-#      savedir=$1
-#      [ -n "$savedir" ] || { echo "ERROR: Directory to save the temporary database archive is not specified." >&2; exit 1; }
-#      [ -d "$savedir" ] || { echo "ERROR: Directory [$savedir] is not found." >&2; exit 1; }
-#      srcdir=`dirname "${DBDIR}"`
-#      srcnode=`basename "${DBDIR}"`
-#      savefile=`realpath "$savedir"`/${APPNAME}_`date +%Y%m%d_%H%M%S`.tar.gz
-#      echo "Starting to save the temporary database as [$savefile]..."
-#      tar czf "$savefile" -C "$srcdir" "$srcnode"
-#      echo "Done"
-#      exit
-#      ;;
-# load)
-#      misc_chk_privilege
-#      loadfile=$1
-#      [ -n "$loadfile" ] || { echo "ERROR: Database archive is not specified." >&2; exit 1; }
-#      [ -f "$loadfile" ] || { echo "ERROR: Database archive is not found." >&2; exit 1; }
-#      loadfile=`realpath "$loadfile"`
-#      echo "Starting to load the temporary database..."
-#      rm -rf "${DBDIR}"
-#      srcdir=`dirname "${DBDIR}"`
-#      srcnode=`basename "${DBDIR}"`
-#      [ -d "$srcdir" ] || mkdir -p "$srcdir"
-#      tar xzf "$loadfile" -C "$srcdir"
-#      echo "Done"
-#      exit
-#      ;;
-# glob)
-#      echo "Evaluated port origins are as follows:"
-#      pkgsys_eval_ports_glob "$@"
-#      exit
-#      ;;
-# show)
-#      database_query_chk_preparation_completion
-#      temp_warn_obsolete_temp_db
-#      [ -e "${DBDIR}"/saved_options.sh ] && . "${DBDIR}"/saved_options.sh
-#      _filter_skip_unchanged=
-#      _filter_only_target=
-#      _for_each_matching_port=
-#      pkgnamedb=requires
-#      deptag=`options_get_dependency_type`
-#      dbsuffix=$deptag.full
-#      [ $opt_only_target_scope = yes ] && dbsuffix=$deptag.direct
-#      case ${1:-todo} in
-#      todo)
-#              echo "The following ports are to be reinstalled or newly installed in the current do/redo"
-#              echo "process:"
-#              list=reinst_todo
-#              _filter_skip_unchanged=necessary_upgrade.$dbsuffix
-#              _filter_only_target=y
-#              ;;
-#      done)
-#              echo "The following ports have been successfully reinstalled or newly installed:"
-#              list=success.$dbsuffix.list
-#              _filter_skip_unchanged=necessary_upgrade_completed.$dbsuffix
-#              _filter_only_target=y
-#              ;;
-#      redo)
-#              echo "The following ports themselves have been successfully reinstalled or newly installed,"
-#              echo "but are to be reinstalled again because their dependencies were failed:"
-#              list=success_but_dependencies_failed.$dbsuffix.list
-#              _filter_skip_unchanged=necessary_upgrade.$dbsuffix
-#              _filter_only_target=y
-#              ;;
-#      pending)
-#              echo "The following ports need reinstallation but are to be skipped until their requirements"
-#              echo "succeed:"
-#              list=todo_after_requirements_succeed.$dbsuffix.list
-#              _filter_skip_unchanged=necessary_upgrade.$dbsuffix
-#              _filter_only_target=y
-#              ;;
-#      resolved)
-#              echo "The following ports had problems which have been manually resolved:"
-#              list=manually_done.list
-#              ;;
-#      failure)
-#              echo "The following ports experienced failures and kept to be old:"
-#              database_query_show_list_failure
-#              exit
-#              ;;
-#      taboo)
-#              echo "The following ports are registered as taboo:"
-#              list=taboo.all.list
-#              ;;
-#      deleted)
-#              echo "The following ports are to be or have been deleted:"
-#              list=obsolete_ports.actual.list
-#              pkgnamedb=obsolete
-#              _filter_only_target=y
-#              ;;
-#      options)
-#              echo "The following options settings are saved:"
-#              options_inverse_parse
-#              echo
-#              echo "Option -M will reset the setting to be"
-#              options_inverse_parse non_renewable renewable_in_redo_on_target
-#              echo
-#              echo "Option -N will reset the setting to be"
-#              options_inverse_parse non_renewable renewable_anytime
-#              echo
-#              echo "Combination of options -M and -N will reset the setting to be"
-#              options_inverse_parse non_renewable
-#              echo
-#              exit
-#              ;;
-#      requirements)
-#              grandtitle="Dependencies based on the latest ports tree"
-#              title="The following ports are required by %s:"
-#              list=requirements.$dbsuffix
-#              _for_each_matching_port=y
-#              [ $deptag = none ] && echo "WARNING: This command has no meaning with the current options setting." >&2
-#              ;;
-#      dependents)
-#              grandtitle="Dependencies based on the latest ports tree"
-#              title="The following ports depend on %s:"
-#              list=dependents.$dbsuffix
-#              _for_each_matching_port=y
-#              [ $deptag = none ] && echo "WARNING: This command has no meaning with the current options setting." >&2
-#              ;;
-#      *)
-#              echo "ERROR: Invalid show argument [$1]." >&2
-#              exit 1
-#              ;;
-#      esac
-#      [ -n "${_for_each_matching_port}" -o -r "${DBDIR}/$list" ] || exit 0
-#      [ ! -e "${DBDIR}"/saved_options.sh ] || . "${DBDIR}"/saved_options.sh
-#      if [ -n "${_for_each_matching_port}" ]
-#      then
-#              shift
-#              [ $# -gt 0 ] || { echo "ERROR: the argument as port glob is missing." >&2; exit 1; }
-#              echo "[$grandtitle]"
-#              echo "(Evaluated by `options_get_dependency_msgterm` dependency)"
-#              echo
-#              isfirst=y
-#              for origin_target in `pkgsys_eval_ports_glob "$@"`
-#              do
-#                      [ -e "${DBDIR}/requires/$origin_target/pkgtag" ] || continue
-#                      [ "$isfirst" = y ] || echo
-#                      isfirst=n
-#                      pkg_target=`cat "${DBDIR}/$pkgnamedb/$origin_target/pkgtag"`
-#                      printf "$title\n" "$origin_target ($pkg_target)"
-#                      [ -e "${DBDIR}/requires/$origin_target/$list" ] || continue
-#                      while read origin
-#                      do
-#                              if [ -e "${DBDIR}/$pkgnamedb/$origin/pkgtag" ]
-#                              then
-#                                      echo "$origin" '('`cat "${DBDIR}/$pkgnamedb/$origin/pkgtag"`')'
-#                              else
-#                                      echo "$origin"
-#                              fi
-#                      done < ${DBDIR}/requires/$origin_target/$list
-#              done
-#              [ "$isfirst" = n ] || { echo "ERROR: no matching port for the glob." >&2; exit 1; }
-#      elif [ -n "${_filter_skip_unchanged}" -a $opt_skip_unchanged = yes ]
-#      then
-#              while read origin
-#              do
-#                      [ -e "${DBDIR}/requires/$origin/${_filter_skip_unchanged}" ] || continue
-#                      if [ -n "${_filter_only_target}" ]
-#                      then
-#                              database_query_get_target_attributes currentorigin "$origin"
-#                              [ -n "${currentorigin_is_relevant}" ] || continue
-#                              database_query_is_a_port_suppressed "$origin" && continue
-#                      fi
-#                      if [ -e "${DBDIR}/$pkgnamedb/$origin/pkgtag" ]
-#                      then
-#                              echo "$origin" '('`cat "${DBDIR}/$pkgnamedb/$origin/pkgtag"`')'
-#                      else
-#                              echo "$origin"
-#                      fi
-#              done < ${DBDIR}/$list
-#      else
-#              while read origin
-#              do
-#                      if [ -n "${_filter_only_target}" ]
-#                      then
-#                              database_query_get_target_attributes currentorigin "$origin"
-#                              [ -n "${currentorigin_is_relevant}" ] || continue
-#                              database_query_is_a_port_suppressed "$origin" && continue
-#                      fi
-#                      if [ -e "${DBDIR}/$pkgnamedb/$origin/pkgtag" ]
-#                      then
-#                              echo "$origin" '('`cat "${DBDIR}/$pkgnamedb/$origin/pkgtag"`')'
-#                      else
-#                              echo "$origin"
-#                      fi
-#              done < ${DBDIR}/$list
-#      fi
-#      exit
-#      ;;
-# redo)
-#      misc_chk_privilege
-#      temp_warn_obsolete_temp_db
-#      touch "${DBDIR}/MODE_REDO"
-#      rm -f "${DBDIR}/COMPLETE_CLEANUP_REINST_STATUS" "${DBDIR}/COMPLETE_DEINST_UNUSED_PKGS" \
-#              "${DBDIR}/COMPLETE_PREPARATION"
-#      if [ -n "$opt_target_requirements$opt_target_dependents" ]
-#      then
-#              rm -f "${DBDIR}/COMPLETE_PARSE_OPTION_TARGET_PORTS" "${DBDIR}/COMPLETE_INSPECT_ALL_DEPENDENCIES"
-#              touch "${DBDIR}/REQUIRE_CHK_NEW_TARGET"
-#      fi
-#      echo "[REDO mode]"
-#      ;;
-# prepare)
-#      misc_chk_privilege
-#      temp_warn_obsolete_temp_db
-#      ;;
-# do)
-#      misc_chk_privilege
-#      ;;
-# *)
-#      echo "ERROR: Invalid command [$COMMAND_MODE]." >&2
-#      exit 1
-#      ;;
-# esac
+
+# ==================================================
+# ================== PREPARATION ===================
+# ==================================================
 
 # ============= Correspondence to configuration changes =============
 
@@ -1034,7 +425,7 @@ _program_exec_and_record_completion__operation ()
        if fileedit_manipulate_old_new_lines \
                "${DBDIR}/conf.prev/setenv.sh" "${DBDIR}/conf/setenv.sh" "$tmpfile_old" "$tmpfile_new"
        then
-               if grep -m 1 -e ^LOCALBASE= -e ^LINUXBASE= -e ^PORTSDIR= "$tmpfile_old" "$tmpfile_new" > /dev/null
+               if grep -q -e ^LOCALBASE= -e ^LINUXBASE= -e ^PORTSDIR= "$tmpfile_old" "$tmpfile_new"
                then
                        message_echo "ERROR: Migration of the temporary database is unavailable because LOCALBASE, LINUXBASE or PORTSDIR was changed." >&2
                        message_echo "        ${APPNAME} clean" >&2
@@ -1111,6 +502,7 @@ program_exec_and_record_completion REDO_INIT
 PROGRAM_DEPENDS=''
 _program_exec_and_record_completion__operation ()
 {
+       local tag level dbsuffix
        message_section_title "Determining specified targets"
        cat "${DBDIR}/stage.loop_list/target_itself.specified" \
                "${DBDIR}/stage.loop_list/target_dependents.specified" \
@@ -1122,19 +514,6 @@ _program_exec_and_record_completion__operation ()
        rm -f "${DBDIR}/stage.loop_list/target_itself.specified" \
                "${DBDIR}/stage.loop_list/target_dependents.specified" \
                "${DBDIR}/stage.loop_list/target_requirements.specified"
-#      if [ $opt_add_targets_to_needed = no ]
-#      then
-#              pkgsys_eval_installed_pkgs_globs "$opt_target_dependents" \
-#                      > ${DBDIR}/stage.loop_list/target_dependents.specified
-#              pkgsys_eval_installed_pkgs_globs "$opt_target_requirements" \
-#                      > ${DBDIR}/stage.loop_list/target_requirements.specified
-#      else
-#              touch "${DBDIR}/stage.loop_list/target_dependents.specified"
-#              touch "${DBDIR}/stage.loop_list/target_requirements.specified"
-#              pkgsys_register_evaluated_globs "$opt_target_dependents" "${DBDIR}/stage.loop_list/target_dependents.specified"
-#              pkgsys_register_evaluated_globs "$opt_target_requirements" "${DBDIR}/stage.loop_list/target_requirements.specified"
-#      fi
-# Evaluate glob patterns for targets
        if [ -n "$opt_target_itself$opt_target_dependents$opt_target_requirements" ]
        then
                options_select_new_ports_if_duplicated O \
@@ -1154,12 +533,38 @@ _program_exec_and_record_completion__operation ()
                cat "${DBDIR}/stage.loop_list/target_itself.specified" \
                        "${DBDIR}/stage.loop_list/target_dependents.specified" \
                        "${DBDIR}/stage.loop_list/target_requirements.specified" \
-                       "${DBDIR}/need.list"  2> /dev/null \
+                       | sort -u > ${TMPDIR}/DETERMINE_SPECIFIED_TARGETS.reset
+               cat "${TMPDIR}/DETERMINE_SPECIFIED_TARGETS.reset" "${DBDIR}/need.list" 2> /dev/null \
                        | sort -u > ${DBDIR}/need.list.tmp
                mv "${DBDIR}/need.list.tmp" "${DBDIR}/need.list"
                sort -u "${DBDIR}/need.list" "${DBDIR}/targets_specified_so_far" \
                        > ${DBDIR}/targets_specified_so_far.tmp
                mv "${DBDIR}/targets_specified_so_far.tmp" "${DBDIR}/targets_specified_so_far"
+               for tag in all run build none
+               do
+                       for level in direct full
+                       do
+                               dbsuffix=$tag.$level
+                               {
+                                       cat "${TMPDIR}/DETERMINE_SPECIFIED_TARGETS.reset"
+                                       cat "${DBDIR}/stage.loop_list/target_dependents.specified" | while read origin
+                                       do
+                                               nodedir=${DBDIR}/requires/$origin
+                                               cat "$nodedir/dependents.$dbsuffix" 2> /dev/null
+                                       done
+                                       cat "${DBDIR}/stage.loop_list/target_requirements.specified" | while read origin
+                                       do
+                                               nodedir=${DBDIR}/requires/$origin
+                                               cat "$nodedir/requirements.$dbsuffix" 2> /dev/null
+                                       done
+                               } | sort -u | while read origin
+                               do
+                                       fileedit_rm_a_line "$origin" "${DBDIR}/success.$dbsuffix.list"
+                                       fileedit_rm_a_line "$origin" "${DBDIR}/todo_after_requirements_succeed.$dbsuffix.list"
+                                       rm -f "${DBDIR}/requires/$origin/succeeded_once"
+                               done
+                       done
+               done
        fi
        message_echo
 }
@@ -1168,10 +573,11 @@ program_exec_and_record_completion DETERMINE_SPECIFIED_TARGETS
 # Show specified targets
 if [ -n "$opt_target_itself$opt_target_dependents$opt_target_requirements" -a $opt_batch_mode = no ]
 then
-       echo "INFO: (Re/de-)installation will be carried out only for the targets:"
+       message_echo "INFO: (Re/de-)installation will be carried out only for the targets:"
+       echo
        if [ `wc -l < ${DBDIR}/stage.loop_list/target_itself.specified` -gt 0 ]
        then
-               echo "[Targets only]"
+               message_echo "[Targets only]"
                echo "----------------------------------------"
                cat "${DBDIR}/stage.loop_list/target_itself.specified"
                echo "----------------------------------------"
@@ -1179,7 +585,7 @@ then
        fi
        if [ `wc -l < ${DBDIR}/stage.loop_list/target_dependents.specified` -gt 0 ]
        then
-               echo "[Targets with their `options_get_dependency_msgterm` dependents]"
+               message_echo "[Targets with their `options_get_dependency_msgterm` dependents]"
                echo "----------------------------------------"
                cat "${DBDIR}/stage.loop_list/target_dependents.specified"
                echo "----------------------------------------"
@@ -1187,13 +593,12 @@ then
        fi
        if [ `wc -l < ${DBDIR}/stage.loop_list/target_requirements.specified` -gt 0 ]
        then
-               echo "[Targets with their `options_get_dependency_msgterm` requirements]"
+               message_echo "[Targets with their `options_get_dependency_msgterm` requirements]"
                echo "----------------------------------------"
                cat "${DBDIR}/stage.loop_list/target_requirements.specified"
                echo "----------------------------------------"
                echo
        fi
-       echo
 fi
 
 # Determine all target ports
@@ -1211,46 +616,10 @@ _program_exec_and_record_completion__operation ()
                cat "${DBDIR}/stage.loop_list/target_requirements.specified" || :
                cat "${DBDIR}/targets_specified_so_far" || :
        }  2> /dev/null | sort -u > "${DBDIR}/stage.loop_list/ports_to_inspect"
-#      rm -f "${DBDIR}/REQUIRE_CHK_NEW_TARGET"
        message_echo
 }
 program_exec_and_record_completion DETERMINE_ALL_TARGET_PORTS
 
-# if [ -e "${DBDIR}/REQUIRE_CHK_NEW_TARGET" ]
-# then
-#      message_section_title "Checking newly installing ports"
-#      {
-#              cat "${DBDIR}/stage.loop_list/ports_to_inspect"
-#              cat "${DBDIR}/stage.loop_list/target_dependents.specified" || :
-#              cat "${DBDIR}/stage.loop_list/target_requirements.specified" || :
-#      }  2> /dev/null | sort -u > "${DBDIR}/stage.loop_list/ports_to_inspect.new"
-#      if diff "${DBDIR}/stage.loop_list/ports_to_inspect" "${DBDIR}/stage.loop_list/ports_to_inspect.new" > /dev/null 2>&1
-#      then
-#              echo "WARNING: The temporary database will be refreshed so as to reinstall the all failed ports and their dependents." \
-#                      | fold -s
-#              rm -f "${DBDIR}/COMPLETE_INSPECT_ALL_DEPENDENCIES" \
-#                      "${DBDIR}/COMPLETE_ALL_REQUIREMENTS_LISTS" \
-#                      "${DBDIR}/COMPLETE_DISTFILES_LIST" \
-#                      "${DBDIR}/COMPLETE_CONVERT_REQUIRES_LIST" \
-#                      "${DBDIR}/COMPLETE_RUNTIME_REQUIREMENT_LISTS" \
-#                      "${DBDIR}/COMPLETE_BUILDTIME_REQUIREMENT_LISTS" \
-#                      "${DBDIR}/COMPLETE_INSPECT_DEPENDENTS" \
-#                      "${DBDIR}/COMPLETE_PREPARATION_OF_MATCHING_PATTERNS_OF_DEPENDENCIES" \
-#                      "${DBDIR}/COMPLETE_TARGET_ATTR_INFO" \
-#                      "${DBDIR}/COMPLETE_INSPECT_NECESSITY" \
-#                      "${DBDIR}/COMPLETE_NECESSARY_UPDATES" \
-#                      "${DBDIR}/COMPLETE_COPY_DEPENDENCY_TMPFILES" \
-#                      "${DBDIR}/COMPLETE_CLEANUP_REINST_STATUS" \
-#                      "${DBDIR}/COMPLETE_ORDER_ALL_DEPENDENCIES" \
-#                      "${DBDIR}/COMPLETE_CHECKED_UNSATISFIED_DEPENDENCIES" \
-#                      "${DBDIR}/COMPLETE_REFLECTCONF_2"
-#              rm -rf "${DBDIR}/targets"
-#              mv "${DBDIR}/stage.loop_list/ports_to_inspect.new" "${DBDIR}/stage.loop_list/ports_to_inspect"
-#      fi
-#      rm -f "${DBDIR}/REQUIRE_CHK_NEW_TARGET"
-#      echo
-# fi
-
 # Inspection of all dependencies
 PROGRAM_DEPENDS='PARSE_CONF DETERMINE_ALL_TARGET_PORTS'
 _program_exec_restartable_loop_operation__routine ()
@@ -1267,56 +636,11 @@ _program_exec_and_record_completion__operation ()
                message_echo "INFO: Ports which seem irrelevant to the targets are also inspected in order to get complete information."
        DEPTH_INDEX='--'
        program_exec_restartable_loop_operation ports_to_inspect
-       
-#      if ! diff "${DBDIR}/stage.loop_list/ports_to_inspect.prev" "${DBDIR}/stage.loop_list/ports_to_inspect" > ${TMPDIR}/ports_to_inspect.diff 2> /dev/null
-#      then
-#              if [ -f "${DBDIR}/stage.loop_list/ports_to_inspect.remain" ]
-#              then
-#                      cp "${DBDIR}/stage.loop_list/ports_to_inspect.remain" "${DBDIR}/stage.loop_list/ports_to_inspect.remain.tmp"
-#                      grep '^> ' "${TMPDIR}/ports_to_inspect.diff" | sed 's/^> //' >> ${DBDIR}/stage.loop_list/ports_to_inspect.remain.tmp
-#                      sort -u "${DBDIR}/stage.loop_list/ports_to_inspect.remain.tmp" >  ${DBDIR}/stage.loop_list/ports_to_inspect.remain.tmp2
-#                      mv "${DBDIR}/stage.loop_list/ports_to_inspect.remain.tmp2" "${DBDIR}/stage.loop_list/ports_to_inspect.remain"
-#              fi
-#              cp -p "${DBDIR}/stage.loop_list/ports_to_inspect" "${DBDIR}/stage.loop_list/ports_to_inspect.tmp"
-#              mv "${DBDIR}/stage.loop_list/ports_to_inspect.tmp" "${DBDIR}/stage.loop_list/ports_to_inspect.prev"
-#      fi
-#      if [ -f "${DBDIR}/stage.loop_list/ports_to_inspect.remain" ]
-#      then
-#              message_restarted_process
-#      else
-#              cp -p "${DBDIR}/stage.loop_list/ports_to_inspect" "${DBDIR}/stage.loop_list/ports_to_inspect.remain.tmp"
-#              mv "${DBDIR}/stage.loop_list/ports_to_inspect.remain.tmp" "${DBDIR}/stage.loop_list/ports_to_inspect.remain"
-#      fi
-#      cp "${DBDIR}/stage.loop_list/ports_to_inspect.remain" "${TMPDIR}/ports_to_inspect"
-#      nlines=`wc -l < ${TMPDIR}/ports_to_inspect`
-#      iline=1
-#      while [ $iline -le $nlines ]
-#      do
-#              origin=`sed -n ${iline}p "${TMPDIR}/ports_to_inspect"`
-#              iline=$(($iline+1))
-#              database_build_inspect_dependencies "$origin"
-#              continue
-#      done
-       
        database_build_post_inspect_dependencies
        message_echo
 }
 program_exec_and_record_completion INSPECT_ALL_DEPENDENCIES
 
-# # Preparation for converting dependency-lists to actual ones
-# PROGRAM_DEPENDS='INSPECT_ALL_DEPENDENCIES'
-# _program_exec_and_record_completion__operation ()
-# {
-#      message_section_title "Preparation for converting dependency-lists to actual ones"
-#      ( set -e
-#              cd "${DBDIR}/initial" 2> /dev/null && find . -depth 2 -type d
-#              cd "${DBDIR}/requires" 2> /dev/null && find . -depth 2 -type d
-#      ) | sed 's|^\./||' | sort -u > ${DBDIR}/stage.loop_list/convert_dependency_lists
-#      cp /dev/null "${DBDIR}/update_dependencies"
-#      message_echo
-# }
-# program_exec_and_record_completion ALL_REQUIREMENTS_LISTS
-
 # Convert dependency-lists to actual ones
 PROGRAM_DEPENDS='INSPECT_ALL_DEPENDENCIES'
 _program_exec_restartable_loop_operation__routine ()
@@ -1332,23 +656,17 @@ _program_exec_restartable_loop_operation__routine ()
                                for tag in all run build
                                do
                                        target=${DBDIR}/$dbtag/$origin/${table}.${tag}.${level}
-                                       [ -e "$target.src" ] || continue
-#                                      [ "$target" -nt "$target.src" -a "$target" -nt "${DBDIR}/REPLACE.complete_sed_pattern" ] && continue
-#                                      if [ -e "$target" ]
-#                                      then
-#                                              [ "$target.src" -nt "$target" -o \
-# #                                                    "${DBDIR}/REPLACE.complete_sed_pattern" -nt "$target" ] || continue
-#                                      fi
-#                                      if [ $dbtag = initial ]
-#                                      then
-#                                              ln "$target.src" "$target.tmp"
-#                                      else
-                                       sed -E -f "${DBDIR}/REPLACE.complete_sed_pattern" "$target.src" \
-                                               | grep -v '^$' | sort -u > $target.tmp
-#                                      fi
-                                       [ -e "$target" ] && ! diff "$target.tmp" "$target" > /dev/null \
-                                               && echo "$origin" >> ${DBDIR}/update_dependencies
-                                       mv "$target.tmp" "$target"
+                                       if [ -e "$target.src" ]
+                                       then
+                                               sed -E -f "${DBDIR}/REPLACE.complete_sed_pattern" "$target.src" \
+                                                       | grep -v '^$' | sort -u > $target.tmp
+                                               [ -e "$target" ] && ! diff "$target.tmp" "$target" > /dev/null \
+                                                       && echo "$origin" >> ${DBDIR}/update_dependencies
+                                               mv "$target.tmp" "$target"
+                                       else
+                                               [ -e "$target" ] && echo "$origin" >> ${DBDIR}/update_dependencies
+                                               rm -f "$target"
+                                       fi
                                done
                        done
                done
@@ -1377,7 +695,7 @@ _program_exec_and_record_completion__operation ()
        for inspected_level_tmp in direct node
        do
                cat "${DBDIR}/ports.inspected.${inspected_level_tmp}.list" || :
-       done 2> /dev/null | sort -u > ${DBDIR}/stage.loop_list/strip_dependency_lists_rm_uninspected_ports
+       done 2> /dev/null | sort -u > ${DBDIR}/stage.loop_list/trim_dependency_lists_rm_uninspected_ports
        find "${DBDIR}/requires" -depth 2 -type d > ${DBDIR}/stage.loop_list/inspect_dependent
        [ -e "${DBDIR}/dependents_files" ] && \
                mv "${DBDIR}/dependents_files" "${DBDIR}/dependents_files.prev"
@@ -1386,48 +704,6 @@ _program_exec_and_record_completion__operation ()
 }
 program_exec_and_record_completion CONVERT_REQUIREMENTS_LIST
 
-# if [ ! -e "${DBDIR}/COMPLETE_CONVERT_REQUIREMENTS_LIST" ]
-# then
-#      message_section_title "Conversion of dependency-lists to actual ones"
-#      if [ -f "${DBDIR}/stage.loop_list/convert_dependency_lists.remain" ]
-#      then
-#              message_restarted_process
-#      else
-#              ( set -e
-#                      cd "${DBDIR}/initial" 2> /dev/null && find . -depth 2 -type d
-#                      cd "${DBDIR}/requires" 2> /dev/null && find . -depth 2 -type d
-#              ) | sort -u > ${DBDIR}/stage.loop_list/convert_dependency_lists.remain.tmp
-#              mv "${DBDIR}/stage.loop_list/convert_dependency_lists.remain.tmp" "${DBDIR}/stage.loop_list/convert_dependency_lists.remain"
-#      fi
-#      cp "${DBDIR}/stage.loop_list/convert_dependency_lists.remain" "${TMPDIR}/${DBDIR}/stage.loop_list/convert_dependency_lists"
-#      while read dbpath
-#      do
-#              portname=`basename "$dbpath"`
-#              catpath=`dirname "$dbpath"`
-#              catname=`basename "$catpath"`
-#              origin=$catname/$portname
-#              for table in dependents requirements
-#              do
-#                      for dbtag in requires initial
-#                      do
-#                              for level in direct full
-#                              do
-#                                      for tag in all run build
-#                                      do
-#                                              target=${DBDIR}/$dbtag/$origin/${table}.${tag}.${level}
-#                                              [ -e "$target.src" ] || continue
-#                                              sed -E -f "${DBDIR}/REPLACE.complete_sed_pattern" "$target.src" \
-#                                                      | grep -v '^$' | sort -u > $target
-#                                      done
-#                              done
-#                      done
-#              done
-#              sed -i '' 1d "${DBDIR}/stage.loop_list/convert_dependency_lists.remain"
-#      done < ${TMPDIR}/${DBDIR}/stage.loop_list/convert_dependency_lists
-#      touch "${DBDIR}/COMPLETE_CONVERT_REQUIREMENTS_LIST"
-#      echo
-# fi
-
 # Completion of recursive requirement lists
 for _REQUIREMENT_LISTS_tag in all run build
 do
@@ -1449,34 +725,9 @@ do
                message_echo
        }
        program_exec_and_record_completion REQUIREMENT_LISTS:${_REQUIREMENT_LISTS_tag}
-#              if [ -e "${DBDIR}/COMPLETE_${captag}TIME_REQUIREMENT_LISTS" ]
-#              then
-#                      message_section_title "Completion of ${tag}-time requirement lists"
-#                      if [ -f "${DBDIR}/stage.loop_list/complete_${tag}time_reqlists.remain" ]
-#                      then
-#                              message_restarted_process
-#                      else
-#                              find "${DBDIR}/requires" -depth 3 -type f -name requirements.${tag}.direct \
-#                                      > ${DBDIR}/stage.loop_list/complete_${tag}time_reqlists.remain.tmp
-#                              mv "${DBDIR}/stage.loop_list/complete_${tag}time_reqlists.remain.tmp" "${DBDIR}/stage.loop_list/complete_${tag}time_reqlists.remain"
-#                      fi
-#                      cp "${DBDIR}/stage.loop_list/complete_${tag}time_reqlists.remain" "${TMPDIR}/complete_${tag}time_reqlists"
-#                      while read reqlist
-#                      do
-#                              dbpath=`dirname "$reqlist"`
-#                              portname=`basename "$dbpath"`
-#                              catpath=`dirname "$dbpath"`
-#                              catname=`basename "$catpath"`
-#                              origin=$catname/$portname
-#                              database_build_get_complete_recursive_dependency requirements.${tag} "$origin" > /dev/null
-#                              sed -i '' 1d "${DBDIR}/stage.loop_list/complete_${tag}time_reqlists.remain"
-#                      done < ${TMPDIR}/complete_${tag}time_reqlists
-#                      touch "${DBDIR}/COMPLETE_${captag}TIME_REQUIREMENT_LISTS"
-#                      echo
-#              fi
 done
 
-# Strip dependency lists by removing uninspected ports
+# Trim dependency lists by removing uninspected ports
 if [ $opt_only_target_scope = yes ]
 then
        PROGRAM_DEPENDS='INSPECT_ALL_DEPENDENCIES CONVERT_REQUIREMENTS_LIST'
@@ -1498,15 +749,15 @@ then
        }
        _program_exec_and_record_completion__operation ()
        {
-               message_section_title "Stripping dependency lists by removing uninspected ports"
-               program_exec_restartable_loop_operation strip_dependency_lists_rm_uninspected_ports
+               message_section_title "Trimming dependency lists by removing uninspected ports"
+               program_exec_restartable_loop_operation trim_dependency_lists_rm_uninspected_ports
                message_echo
        }
-       program_exec_and_record_completion STRIP_DEPENDENCY_LISTS_RM_UNINSPECTED_PORTS
+       program_exec_and_record_completion TRIM_DEPENDENCY_LISTS_RM_UNINSPECTED_PORTS
 fi
 
 # Inspection of dependents
-PROGRAM_DEPENDS='INSPECT_ALL_DEPENDENCIES CONVERT_REQUIREMENTS_LIST STRIP_DEPENDENCY_LISTS_RM_UNINSPECTED_PORTS'
+PROGRAM_DEPENDS='INSPECT_ALL_DEPENDENCIES CONVERT_REQUIREMENTS_LIST TRIM_DEPENDENCY_LISTS_RM_UNINSPECTED_PORTS'
 _program_exec_restartable_loop_operation__routine ()
 {
        local dbpath origin tag level srcdb dstdb
@@ -1556,16 +807,6 @@ _program_exec_restartable_loop_operation__routine ()
        cat "$dstdb" "$dstdb.raw" 2> /dev/null | sort -u > $dstdb.tmp
        mv "$dstdb.tmp" "$dstdb"
        rm -f "$dstdb.raw"
-#      for tag in all run build
-#      do
-#              for level in direct full
-#              do
-#                      dstdb=dependents.${tag}.${level}
-#                      [ -e "$dbpath/$dstdb" ] || continue
-#                      sort -u "$dbpath/$dstdb" > $dbpath/$dstdb.tmp
-#                      mv "$dbpath/$dstdb.tmp" "$dbpath/$dstdb"
-#              done
-#      done
 }
 _program_exec_and_record_completion__operation ()
 {
@@ -1575,115 +816,11 @@ _program_exec_and_record_completion__operation ()
        message_echo
 }
 program_exec_and_record_completion MAKE_DEPENDENTS_LISTS_UNIQUE
-# if [ ! -e "${DBDIR}/COMPLETE_INSPECT_DEPENDENTS" ]
-# then
-#      message_section_title "Inspection of dependents"
-#      if [ -f "${DBDIR}/stage.loop_list/inspect_dependent.remain" ]
-#      then
-#              message_restarted_process
-#      else
-#              find "${DBDIR}/requires" -depth 2 -type d > ${DBDIR}/stage.loop_list/inspect_dependent.remain.tmp
-#              mv "${DBDIR}/stage.loop_list/inspect_dependent.remain.tmp" "${DBDIR}/stage.loop_list/inspect_dependent.remain"
-#      fi
-#      dbrequires_valesc=`str_escape_replaceval "${DBDIR}/requires/"`
-#      cp "${DBDIR}/stage.loop_list/inspect_dependent.remain" "${TMPDIR}/inspect_dependent"
-#      while read dbpath
-#      do
-#              portname=`basename "$dbpath"`
-#              catpath=`dirname "$dbpath"`
-#              catname=`basename "$catpath"`
-#              origin=$catname/$portname
-#              for tag in all run build
-#              do
-#                      for level in direct full
-#                      do
-#                              srcdb=requirements.${tag}.${level}
-#                              dstdb=dependents.${tag}.${level}
-#                              [ -e "$dbpath/$srcdb" ] || continue
-#                              if [ $opt_only_target_scope = yes ]
-#                              then
-#                                      grep -E -f "${DBDIR}/inspected_ports.grep_pattern" "$dbpath/$srcdb" \
-#                                              > $dbpath/$srcdb.tmp || :
-#                                      mv "$dbpath/$srcdb.tmp" "$dbpath/$srcdb"
-#                              fi
-#                              sed -E "s/^/$dbrequires_valesc/; s|$|/$dstdb|" "$dbpath/$srcdb" \
-#                                      | fileedit_add_a_line_to_files_if_new "$origin"
-#                      done
-#              done
-#              sed -i '' 1d "${DBDIR}/stage.loop_list/inspect_dependent.remain"
-#      done < ${TMPDIR}/inspect_dependent
-#      touch "${DBDIR}/COMPLETE_INSPECT_DEPENDENTS"
-#      echo
-# fi
-
-# # Preparation of matching patterns of dependencies
-# if [ ! -e "${DBDIR}/COMPLETE_PREPARATION_OF_MATCHING_PATTERNS_OF_DEPENDENCIES" ]
-# then
-#      message_section_title "Preparation of matching patterns of dependencies"
-#      if [ -f "${DBDIR}/prepare_matching_patterns_dependencies.remain" ]
-#      then
-#              message_restarted_process
-#      else
-#              find "${DBDIR}/requires" -depth 2 -type d > ${DBDIR}/prepare_matching_patterns_dependencies.remain
-#      fi
-#      cp "${DBDIR}/prepare_matching_patterns_dependencies.remain" "${DBDIR}/prepare_matching_patterns_dependencies"
-#      while read dbpath
-#      do
-#              portname=`basename "$dbpath"`
-#              catpath=`dirname "$dbpath"`
-#              catname=`basename "$catpath"`
-#              origin=$catname/$portname
-#              for table in dependents requirements
-#              do
-#                      for tag in all run build
-#                      do
-#                              for dbtag in requires initial
-#                              do
-#                                      target=${DBDIR}/$dbtag/$origin/${table}_${tag}
-#                                      [ -e "$target" ] || continue
-#                                      str_escape_regexp_filter < $target \
-#                                              | sed 's/^/^/;s/$/$/' > $target.pattern
-#                              done
-#                      done
-#              done
-#              sed -i '' 1d "${DBDIR}/prepare_matching_patterns_dependencies.remain"
-#      done < ${DBDIR}/prepare_matching_patterns_dependencies
-#      touch "${DBDIR}/COMPLETE_PREPARATION_OF_MATCHING_PATTERNS_OF_DEPENDENCIES"
-#      echo
-# fi
-
-# # Preparation of target attribute information
-# if [ ! -e "${DBDIR}/COMPLETE_TARGET_ATTR_INFO" ]
-# then
-#      message_section_title "Preparation of target attribute information to be deleted"
-#      if [ -f "${DBDIR}/prepare_target_attribute_information.remain" ]
-#      then
-#              message_restarted_process
-#      else
-#              ( set -e
-#                      cd "${DBDIR}/initial" && find . -depth 2 -type d
-#                      cd "${DBDIR}/requires" && find . -depth 2 -type d
-#              ) | sort -u > ${DBDIR}/prepare_target_attribute_information.remain
-#      fi
-#      cp "${DBDIR}/prepare_target_attribute_information.remain" \
-#              "${DBDIR}/prepare_target_attribute_information"
-#      while read origin
-#      do
-#              database_build_target_attributes "$origin"
-#              [ -z "${currentorigin_is_all}" -a -n "${currentorigin_is_relevant}" ] \
-#                      && touch "${DBDIR}/requires/$origin/relevant_to_target"
-#              sed -i '' 1d "${DBDIR}/prepare_target_attribute_information.remain"
-#      done < ${DBDIR}/prepare_target_attribute_information
-#      touch "${DBDIR}/COMPLETE_TARGET_ATTR_INFO"
-#      echo
-# fi
 
 # Preparation of target attribute information
-# if [ ! -e "${DBDIR}/COMPLETE_TARGET_ATTR_INFO" ]
-# then
 for _TARGET_ATTR_INFO_table in requirements dependents itself
 do
-       [ `cat "${DBDIR}/stage.loop_list/target_${_TARGET_ATTR_INFO_table}.specified" 2> /dev/null \
+       [ `cat "${DBDIR}/stage.loop_list/target_${_TARGET_ATTR_INFO_table}.replaced.specified" 2> /dev/null \
                | wc -l` -gt 0 ] || continue
        PROGRAM_DEPENDS='DETERMINE_SPECIFIED_TARGETS CONVERT_REQUIREMENTS_LIST'
        _program_exec_restartable_loop_operation__routine ()
@@ -1719,54 +856,11 @@ do
                local table
                table=${_TARGET_ATTR_INFO_table}
                message_section_title "Preparation of target attribute information for dependency [$table]"
-               program_exec_restartable_loop_operation target_$table.specified
+               program_exec_restartable_loop_operation target_$table.replaced.specified
                message_echo
        }
        program_exec_and_record_completion TARGET_ATTR_INFO:${_TARGET_ATTR_INFO_table}
 done
-#                      if [ ! -e "${DBDIR}/COMPLETE_TARGET_ATTR_INFO_${captable}" ]
-#                      then
-#                              message_section_title "Preparation of target attribute information for $table"
-#                              if [ -f "${DBDIR}/target_info_${table}.remain" ]
-#                              then
-#                                      message_restarted_process
-#                              else
-#                                      cp "${DBDIR}/stage.loop_list/target_${table}.specified" "${DBDIR}/target_info_${table}.remain"
-#                              fi
-#                              cp "${DBDIR}/target_info_${table}.remain" "${DBDIR}/target_info_${table}"
-#                              while read origin
-#                              do
-#                                      for database in requires initial
-#                                      do
-#                                              dbpath=${DBDIR}/$database/$origin
-#                                              dstpath=${DBDIR}/targets/$origin
-#                                              [ -d "$dstpath" ] || mkdir -p "$dstpath"
-#                                              touch "$dstpath/target_itself"
-#                                              echo "$origin" >> ${DBDIR}/all_targets.lst
-#                                              for tag in all run build
-#                                              do
-#                                                      for level in direct full
-#                                                      do
-#                                                              srcdb=${table}.${tag}.${level}
-#                                                              dstdb=target_${database}_${table}.${tag}.${level}
-#                                                              [ -e "$dbpath/$srcdb" ] || continue
-#                                                              cat "$dbpath/$srcdb" >> ${DBDIR}/all_targets.lst
-#                                                              sed -E "s/^/$dbtargets_valesc/; s|$|/$dstdb|" "$dbpath/$srcdb" \
-#                                                                      | fileedit_add_a_line_to_files_if_new "$origin"
-#                                                      done
-#                                              done
-#                                      done
-#                                      sed -i '' 1d "${DBDIR}/target_info_${table}.remain"
-#                              done < ${DBDIR}/target_info_${table}
-#                              touch "${DBDIR}/COMPLETE_TARGET_ATTR_INFO_${captable}"
-#                              echo
-#                      fi
-#      sort -u "${DBDIR}/all_targets.lst" \
-#              | grep -E -f "${DBDIR}/inspected_ports.grep_pattern" \
-#              > ${DBDIR}/all_targets.lst.tmp 2> /dev/null || :
-#      mv "${DBDIR}/all_targets.lst.tmp" "${DBDIR}/all_targets.lst"
-#      touch "${DBDIR}/COMPLETE_TARGET_ATTR_INFO"
-# fi
 
 # Post-process after the preparation of target attribute information
 PROGRAM_DEPENDS='MAKE_DEPENDENTS_LISTS_UNIQUE TARGET_ATTR_INFO:requirements TARGET_ATTR_INFO:dependents'
@@ -1775,11 +869,12 @@ _program_exec_and_record_completion__operation ()
        message_section_title "Post-process after the preparation of target attribute information"
        sort -u "${DBDIR}/all_targets.lst" 2> /dev/null \
                | grep -E -f "${DBDIR}/inspected_ports.grep_pattern" \
+               | sed -E -f "${DBDIR}/REPLACE.complete_sed_pattern" \
                > ${DBDIR}/all_targets.lst.tmp || :
        mv "${DBDIR}/all_targets.lst.tmp" "${DBDIR}/all_targets.lst"
        find "${DBDIR}/targets" -depth 2 -type d > ${DBDIR}/stage.loop_list/build_complement_to_new_dependents_for_targets 2> /dev/null || :
        {
-               cat "${DBDIR}/all_targets.lst" "${DBDIR}/need.list" 2> /dev/null || :
+               cat "${DBDIR}/all_targets.lst" "${DBDIR}/need.with_replaced.list" 2> /dev/null || :
                find "${DBDIR}/requires" -depth 3 -type f -name installed_version \
                        | sed -E 's|.*/([^/]+/[^/]+)/[^/]*$|\1|'
        } | sort -u > ${DBDIR}/stage.loop_list/inspect_necessity
@@ -1824,29 +919,6 @@ _program_exec_and_record_completion__operation ()
        message_echo
 }
 program_exec_and_record_completion PARSE_TARGET_ATTR_INFO
-# if [ ! -e "${DBDIR}/COMPLETE_PARSE_TARGET_ATTR_INFO" ]
-# then
-#      message_section_title "Parsing target attribute information"
-#      if [ -f "${DBDIR}/stage.loop_list/parse_target_attr_info.remain" ]
-#      then
-#              message_restarted_process
-#      else
-#              find "${DBDIR}/targets" -depth 2 -type d > ${DBDIR}/stage.loop_list/parse_target_attr_info.remain.tmp
-#              mv "${DBDIR}/stage.loop_list/parse_target_attr_info.remain.tmp" "${DBDIR}/stage.loop_list/parse_target_attr_info.remain"
-#      fi
-#      cp "${DBDIR}/stage.loop_list/parse_target_attr_info.remain" "${TMPDIR}/parse_target_attr_info"
-#      while read dbpath
-#      do
-#              portname=`basename "$dbpath"`
-#              catpath=`dirname "$dbpath"`
-#              catname=`basename "$catpath"`
-#              origin=$catname/$portname
-#              database_build_target_attributes "$origin"
-#              sed -i '' 1d "${DBDIR}/stage.loop_list/parse_target_attr_info.remain"
-#      done < ${TMPDIR}/parse_target_attr_info
-#      touch "${DBDIR}/COMPLETE_PARSE_TARGET_ATTR_INFO"
-#      echo
-# fi
 
 # Inspection of necessity
 PROGRAM_DEPENDS='TARGET_ATTR_INFO_POSTPROCESS REQUIREMENT_LISTS:run-time REQUIREMENT_LISTS:build-time INSPECT_ALL_DEPENDENCIES'
@@ -1871,32 +943,6 @@ _program_exec_and_record_completion__operation ()
        message_echo
 }
 program_exec_and_record_completion INSPECT_NECESSITY
-# if [ ! -e "${DBDIR}/COMPLETE_INSPECT_NECESSITY" ]
-# then
-#      message_section_title "Inspection of necessity"
-#      if [ -f "${DBDIR}/stage.loop_list/inspect_necessity.remain" ]
-#      then
-#              message_restarted_process
-#      else
-#              {
-#                      cat "${DBDIR}/all_targets.lst"
-#                      find "${DBDIR}/requires" -depth 3 -type f -name installed_version \
-#                              | sed -E 's|.*/([^/]+/[^/]+)/[^/]*$|\1|'
-#              } | sort -u > ${DBDIR}/stage.loop_list/inspect_necessity.remain.tmp
-#              mv "${DBDIR}/stage.loop_list/inspect_necessity.remain.tmp" "${DBDIR}/stage.loop_list/inspect_necessity.remain"
-#      fi
-#      cp "${DBDIR}/stage.loop_list/inspect_necessity.remain" "${TMPDIR}/inspect_necessity"
-#      while read origin
-#      do
-#              for level in direct full
-#              do
-#                      database_build_inspect_necessity_for_only_new_upgrade "$origin" "$level"
-#              done
-#              sed -i '' 1d "${DBDIR}/stage.loop_list/inspect_necessity.remain"
-#      done < ${TMPDIR}/inspect_necessity
-#      touch "${DBDIR}/COMPLETE_INSPECT_NECESSITY"
-#      echo
-# fi
 
 # Inspection of necessary upgrades
 for _NECESSARY_UPDATES_level in direct full
@@ -1918,10 +964,6 @@ do
                        do
                                touch "${DBDIR}/requires/$origin_dependent/necessary_upgrade.$tag.${level}"
                        done < $dbpath/dependents.$tag.${level}
-# # For debug
-#                      sed -E "s/^/$_NECESSARY_UPDATES_dstfile_prefix/;s/$/$_NECESSARY_UPDATES_dstfile_suffix/" \
-#                              < $dbpath/dependents.$tag.${level} \
-#                              | fileedit_add_a_line_to_files_if_new "$origin"
                done
        }
        _program_exec_and_record_completion__operation ()
@@ -1929,47 +971,10 @@ do
                local level
                level=${_NECESSARY_UPDATES_level}
                message_section_title "Inspection of necessary upgrades at the $level level"
-# # For debug
-#              _NECESSARY_UPDATES_dstfile_prefix=`str_escape_replaceval "${DBDIR}/requires/"`
-#              _NECESSARY_UPDATES_dstfile_suffix=`str_escape_replaceval "/necessary_upgrade.$tag.${level}"`
                program_exec_restartable_loop_operation necessary_ports.${level}
                message_echo
        }
        program_exec_and_record_completion NECESSARY_UPDATES:${_NECESSARY_UPDATES_level}
-#      caplevel=`echo "$level" | tr '[:upper:]' '[:lower:]'`
-#      if [ ! -e "${DBDIR}/COMPLETE_NECESSARY_UPDATES_${caplevel}" ]
-#      then
-#              message_section_title "Inspection of necessary upgrades for at the $level level"
-#              if [ -f "${DBDIR}/stage.loop_list/necessary_ports.${level}.remain" ]
-#              then
-#                      message_restarted_process
-#              else
-#                      find "${DBDIR}/requires" -depth 3 -type f -name "necessary_port.${level}" \
-#                              > ${DBDIR}/stage.loop_list/necessary_ports.${level}.remain
-#              fi
-#              cp "${DBDIR}/stage.loop_list/necessary_ports.${level}.remain" "${TMPDIR}/necessary_ports.${level}"
-#              while read markerpath
-#              do
-#                      dbpath=`dirname "$markerpath"`
-#                      if [ -e "$dbpath/new_version" ]
-#                      then
-#                              for tag in all run build none
-#                              do
-#                                      touch "$dbpath/necessary_upgrade.$tag.${level}"
-#                                      if [ -e "$dbpath/dependents.$tag.${level}" ]
-#                                      then
-#                                              while read origin_dependent
-#                                              do
-#                                                      touch "${DBDIR}/requires/$origin_dependent/necessary_upgrade.$tag.${level}"
-#                                              done < $dbpath/dependents.$tag.${level}
-#                                      fi
-#                              done
-#                      fi
-#                      sed -i '' 1d "${DBDIR}/stage.loop_list/necessary_ports.${level}.remain"
-#              done < ${TMPDIR}/necessary_ports.${level}
-#              touch "${DBDIR}/COMPLETE_NECESSARY_UPDATES_${caplevel}"
-#              echo
-#      fi
 done
 
 # Preparation for inspection of new leaf ports
@@ -1984,7 +989,8 @@ then
                        | sort -u > ${TMPDIR}/PREPARE_INSPECT_LEAF_PORTS:nonleaf_ports
                sort -u "${DBDIR}/inspected_ports" > ${TMPDIR}/PREPARE_INSPECT_LEAF_PORTS:inspected_ports
                fileedit_manipulate_new_lines \
-                       "${TMPDIR}/PREPARE_INSPECT_LEAF_PORTS:nonleaf_ports" "${TMPDIR}/PREPARE_INSPECT_LEAF_PORTS:inspected_ports" \
+                       "${TMPDIR}/PREPARE_INSPECT_LEAF_PORTS:nonleaf_ports" \
+                       "${TMPDIR}/PREPARE_INSPECT_LEAF_PORTS:inspected_ports" \
                        | grep -v -E -f "${DBDIR}/conf/HOLD_PORTS.grep_pattern" \
                        > ${DBDIR}/stage.loop_list/leaf_ports_primary_candidates || :
                cp /dev/null "${DBDIR}/grep.leaf_ports.pattern"
@@ -2005,8 +1011,8 @@ then
                pkgsys_is_pkgtool "$origin" && return
                dbpath=${DBDIR}/requires/$origin
                origin_esc=`str_escape_regexp "$origin"`
-               grep -m 1 -E "^$origin_esc$" "${DBDIR}/need.list" > /dev/null 2>&1 && return
-               if ! grep -m 1 -E "^$origin_esc$" "${DBDIR}/noneed.list" > /dev/null 2>&1
+               grep -q -E "^$origin_esc$" "${DBDIR}/need.with_replaced.list" 2> /dev/null && return
+               if ! grep -q -E "^$origin_esc$" "${DBDIR}/noneed.list" 2> /dev/null
                then
                        if [ -e "$dbpath/initial_orig" ]
                        then
@@ -2057,7 +1063,7 @@ then
                                pkgsys_is_pkgtool "$origin" && return
                                dbpath=${DBDIR}/requires/$origin
                                origin_esc=`str_escape_regexp "$origin"`
-                               grep -m 1 -E "^$origin_esc$" "${DBDIR}/need.list" > /dev/null 2>&1 && return
+                               grep -q -E "^$origin_esc$" "${DBDIR}/need.with_replaced.list" 2> /dev/null && return
                                grep -E -v -f "${DBDIR}/grep.leaf_ports.pattern" \
                                        "$dbpath/dependents.all.full" > /dev/null 2>&1 && return
                                cat "$dbpath/requirements.all.full" 2> /dev/null \
@@ -2094,15 +1100,6 @@ then
        program_exec_and_record_completion INSPECT_REQUIREMENTS_OF_LEAF_PORTS
 fi
 
-# # Copying dependencies for preparation
-# if [ ! -e "${DBDIR}/COMPLETE_COPY_DEPENDENCY_TMPFILES" ]
-# then
-#      message_section_title "Preparation for order of dependencies"
-#      find "${DBDIR}/requires" -depth 3 -name requirements.all.full -exec cp -p {} {}.remained \;
-#      touch "${DBDIR}/COMPLETE_COPY_DEPENDENCY_TMPFILES"
-#      echo
-# fi
-
 # Order the ports considering dependencies
 PROGRAM_DEPENDS='CONVERT_REQUIREMENTS_LIST'
 _program_exec_and_record_completion__operation ()
@@ -2124,127 +1121,6 @@ _program_exec_and_record_completion__operation ()
        message_echo
 }
 program_exec_and_record_completion ORDER_ALL_DEPENDENCIES
-# if [ ! -e "${DBDIR}/COMPLETE_ORDER_ALL_DEPENDENCIES" ]
-# then
-#      message_section_title "Ordering dependencies"
-#      [ ! -f "${DBDIR}/reinst_order.list.tmp" ] || \
-#              message_restarted_process
-#      touch "${DBDIR}/reinst_order.list.tmp"
-#      cat > ${TMPDIR}/order_dependencies.awk << eof
-# BEGIN {
-#      it = 0;
-#      i = 0;
-# }
-# {
-#      if (NF == 0)
-#      {
-#              i = 0;
-#      }
-#      else
-#      {
-#              if (i == 0)
-#              {
-#                      target = \$0;
-#                      sub (/\/requirements.all\.full$/, "", target);
-#                      sub (/^\.\//, "", target);
-#                      srcikey[it] = target;
-#                      srckeyi[target] = it;
-#                      it++;
-#              }
-#              else
-#              {
-#                      src[it-1,i-1] = \$0;
-#                      srcimax[it-1] = srcsize[it-1] = i;
-#              }
-#              i++;
-#      }
-# }
-# END {
-#      ntargets = it;
-#      norder = 0;
-#      order_str = "";
-#      icycle = 0;
-#      lf_order_str = "";
-#      while (1)
-#      {
-#              is_operated = 0;
-#              for (it = 0; it < ntargets; it++)
-#              {
-#                      if (!(it in srcikey)) continue;
-#                      if (srcsize[it] > 0) continue;
-#                      is_operated = 1;
-#                      target = srcikey[it];
-# #                    print "[" icycle "]-- " target;
-#                      delete srcikey[it];
-#                      order[norder++] = target;
-#                      order_str = order_str lf_order_str;
-#                      order_str = sprintf ("%s%s", order_str, target);
-#                      lf_order_str = "\n";
-#                      for (jt = 0; jt < ntargets; jt++)
-#                      {
-#                              for (j = 0; j < srcimax[jt]; j++)
-#                              {
-#                                      if ((jt,j) in src && src[jt,j] == target)
-#                                      {
-#                                              delete src[jt,j];
-#                                              srcsize[jt]--;
-#                                              break;
-#                                      }
-#                              }
-#                      }
-#              }
-#              if (is_operated == 0) break;
-#              icycle++;
-#      }
-#      reinst_order_list = sprintf ("%s%s", ENVIRON["DBDIR"], "/reinst_order.list.tmp");
-#      print order_str > reinst_order_list;
-#      unsatisfied = "";
-#      for (it = 0; it < ntargets; it++)
-#      {
-#              if (srcsize[it] == 0) continue;
-#              reqs = "";
-#              sp_reqs = "";
-#              for (i = 0; i < srcimax[it]; i++)
-#              {
-#                      if ((it,i) in src)
-#                      {
-#                              reqs = reqs ", " src[it,i];
-#                              sp_reqs = ", ";
-#                      }
-#              }
-#              unsatisfied = sprintf ("%s%s [%d] (%s)\n", unsatisfied, srcikey[it], srcsize[it], reqs);
-#      }
-#      if (unsatisfied != "")
-#      {
-#              unsatisfied_list = sprintf ("%s%s", ENVIRON["DBDIR"], "/unsatisfied.list");
-#              print unsatisfied > unsatisfied_list;
-#              exit 1;
-#      }
-# }
-# eof
-#      if (cd "${DBDIR}/requires" && \
-#              find . -depth 3 -name requirements.all.full -exec echo {} \; -exec cat {} \; -exec echo \;) | \
-#              env "DBDIR=${DBDIR}" awk -f "${TMPDIR}"/order_dependencies.awk
-#      then
-#              :
-#      else
-#              echo "ERROR: Unsatisfied dependencies are remained:" >&2
-#              cat "${DBDIR}/unsatisfied.list"
-#              echo "*** Aborted by ${APPNAME}"
-#              echo "The ports tree seems broken. You might have caught an incomplete version."
-#              echo "You are encouraged to update the ports tree by portsnap(8)."
-#              echo "Then execute"
-#              echo " ${APPNAME} clean"
-#              echo "before restart."
-#              temp_terminate_process ()
-#              {
-#              }
-#              exit 1
-#      fi
-#      grep -v '^$' "${DBDIR}/reinst_order.list.tmp" > "${DBDIR}/reinst_order.list" || :
-#      touch "${DBDIR}/COMPLETE_ORDER_ALL_DEPENDENCIES"
-#      echo
-# fi
 
 # Selection of removing leaf ports
 PROGRAM_DEPENDS='INSPECT_REQUIREMENTS_OF_LEAF_PORTS'
@@ -2320,13 +1196,6 @@ _program_exec_and_record_completion__operation ()
        message_echo
 }
 program_exec_and_record_completion SETUP_REINST_TODO
-# if [ -e "${DBDIR}/MODE_REDO" -a ! -e "${DBDIR}/COMPLETE_CLEANUP_REINST_STATUS" ]
-# then
-#      message_section_title "Preparation for order of dependencies"
-#      find "${DBDIR}/requires" -depth 3 -type d -name status -exec rm -rf {} \; -exec mkdir {} \;
-#      touch "${DBDIR}/COMPLETE_CLEANUP_REINST_STATUS"
-#      echo
-# fi
 
 # Composition of a list for deinstallation of obsolete and leaf packages
 PROGRAM_DEPENDS='COLLECT_LEAF_PORTS_TO_DELETE COLLECT_OBSOLETE_PORTS_TO_DELETE'
@@ -2398,19 +1267,6 @@ _program_exec_and_record_completion__operation ()
        message_echo
 }
 program_exec_and_record_completion LIST_DEINST_PKGS
-# if [ ! -e "${DBDIR}/COMPLETE_LIST_DEINST_PKGS" ]
-# then
-#      message_section_title "Composing a list for deinstallation of obsolete packages"
-#      while read origin
-#      do
-#              origin_ptn=`str_escape_regexp "${origin}"`
-#              grep -m 1 -E "^${origin_ptn}$" "${DBDIR}/conf/HOLD:PORTS.parsed" > /dev/null 2>&1 || \
-#                      echo "$origin"
-#      done < ${DBDIR}/obsolete_ports > ${DBDIR}/obsolete_ports.actual.list.tmp
-#      mv "${DBDIR}"/obsolete_ports.actual.list.tmp "${DBDIR}"/obsolete_ports.actual.list
-#      touch "${DBDIR}/COMPLETE_LIST_DEINST_PKGS"
-#      echo
-# fi
 
 # Collect entire distfiles list
 if [ $opt_suppress_inspect_entire_distinfo = yes ]
@@ -2445,7 +1301,6 @@ PROGRAM_DEPENDS='REDO_INIT INSPECT_ALL_DEPENDENCIES'
 _program_exec_and_record_completion__operation ()
 {
        message_section_title "Cleaning up of reinstallation status for preparation"
-#      find "${DBDIR}/requires" -depth 3 -type d -name status -exec rm -rf {} \; -exec mkdir {} \;
        rm -rf "${DBDIR}/status.ports"
        message_echo
 }
@@ -2460,7 +1315,10 @@ _program_exec_and_record_completion__operation ()
 }
 program_exec_and_record_completion PREPARATION
 
-# ============= Main operations =============
+
+# ==================================================
+# ====================== MAIN ======================
+# ==================================================
 
 # Execute command operations which must be done before actual (re/de)installation processes
 command_exec_before_actual_re_de_installation "$@"
@@ -2472,48 +1330,9 @@ temp_terminate_process ()
 }
 
 # Reinstallation of remained ports
-# [ ! -e "${DBDIR}/MODE_REDO" ] \
-#      || rm -f "${DBDIR}/COMPLETE_REINSTALLATION" \
-#              "${DBDIR}/COMPLETE_CLEANUP_OBSLETE_DISTFILES" \
-#              "${DBDIR}/COMPLETE_REBUILD_PKGDB"
-# [ `wc -l < ${DBDIR}/reinst_order.list` -gt 0 ] || touch "${DBDIR}/COMPLETE_REINSTALLATION"
 PROGRAM_DEPENDS='PREPARATION'
 _program_exec_restartable_loop_operation__routine ()
 {
-#      local origin currentpkg _MSG_CURRENT_STAGE tag backup_pkg
-# if [ ! -e "${DBDIR}/COMPLETE_REINSTALLATION" ]
-# then
-#      _MSG_CURRENT_STAGE_general="reinstallation"
-#      _MSG_CURRENT_STAGE=${_MSG_CURRENT_STAGE_general}
-#      message_section_title "Reinstallation"
-#      savedir=`pwd`
-#      mkdir "${TMPDIR}/backup"
-#      [ -d "${DBDIR}/backup_packages" ] || mkdir -p "${DBDIR}/backup_packages"
-#      if [ ! -e "${DBDIR}/MODE_REDO" -a -f "${DBDIR}/stage.loop_list/reinst_todo" ]
-#      then
-#              message_restarted_process
-#      else
-#              cp -p "${DBDIR}/reinst_order.list" "${DBDIR}/stage.loop_list/reinst_todo.tmp"
-#              mv "${DBDIR}/stage.loop_list/reinst_todo.tmp" "${DBDIR}/stage.loop_list/reinst_todo"
-#      fi
-#      rm -f "${DBDIR}/MODE_REDO"
-#      cp -p "${DBDIR}/stage.loop_list/reinst_todo" "${TMPDIR}/reinst_todo.tmp"
-#      touch "${DBDIR}/failed.list"
-#      for tag in all run build none
-#      do
-#              touch "${DBDIR}/success_but_dependencies_failed.$tag.list"
-#              touch "${DBDIR}/success.$tag.list"
-#      done
-#      PROGRAM_NUM_STEPS=`wc -l < ${DBDIR}/reinst_order.list`
-#      nlines=`wc -l < ${TMPDIR}/reinst_todo.tmp`
-#      PROGRAM_STEP_PROGRESS=$(($PROGRAM_NUM_STEPS-$nlines))
-#      iline=1
-#      while [ $iline -le $nlines ]
-#      do
-#              _MSG_CURRENT_STAGE=${_MSG_CURRENT_STAGE_general}
-#              origin=`sed -n ${iline}p "${TMPDIR}/reinst_todo.tmp"`
-#              iline=$(($iline+1))
-#              PROGRAM_STEP_PROGRESS=$(($PROGRAM_STEP_PROGRESS+1))
        reinstall_exec "$@"
 }
 _program_exec_and_record_completion__operation ()
@@ -2555,7 +1374,6 @@ _program_exec_restartable_loop_operation__routine ()
 _program_exec_and_record_completion__operation ()
 {
        local _MSG_CURRENT_STAGE_general
-#      [ `wc -l < ${DBDIR}/obsolete_ports.actual.list` -gt 0 ] || return 0
        _MSG_CURRENT_STAGE_general="deinstallation of obsolete/leaf packages"
        temp_set_msg_current_stage "${_MSG_CURRENT_STAGE_general}"
        message_section_title "Deinstallation of unused obsolete/leaf packages"
@@ -2564,87 +1382,6 @@ _program_exec_and_record_completion__operation ()
        message_echo
 }
 program_exec_and_record_completion DEINST_UNUSED_PKGS
-# if [ ! -e "${DBDIR}/COMPLETE_DEINST_UNUSED_PKGS" ]
-# then
-#      _MSG_CURRENT_STAGE_general="deinstallation of obsolete packages"
-#      _MSG_CURRENT_STAGE=${_MSG_CURRENT_STAGE_general}
-#      message_section_title "Deinstallation of obsolete packages"
-#      [ -d "${DBDIR}/backup_obsolete" ] || mkdir -p "${DBDIR}/backup_obsolete"
-#      [ -d "${DBDIR}/status_deinst" ] || mkdir -p "${DBDIR}/status_deinst"
-#      if [ `wc -l < ${DBDIR}/obsolete_ports` -gt 0 ]
-#      then
-#              if [ -f "${DBDIR}/obsolete_ports.remained" ]
-#              then
-#                      message_restarted_process
-#              else
-#                      cp -p "${DBDIR}/obsolete_ports" "${DBDIR}/obsolete_ports.remained.tmp"
-#                      mv "${DBDIR}/obsolete_ports.remained.tmp" "${DBDIR}/obsolete_ports.remained"
-#              fi
-#              cp -p "${DBDIR}/obsolete_ports.remained" "${TMPDIR}/obsolete_ports.tmp"
-#              nlines=`wc -l < ${TMPDIR}/obsolete_ports.tmp`
-#              iline=1
-#              while [ $iline -le $nlines ]
-#              do
-#                      origin=`sed -n ${iline}p "${TMPDIR}/obsolete_ports.tmp"`
-#                      iline=$(($iline+1))
-#                      currentpkg=`pkg_info_qO "$origin" 2> /dev/null`
-#                      [ -n "$currentpkg" ] || continue
-#                      database_query_get_target_attributes currentorigin "$origin"
-#                      if [ -z "${currentorigin_is_relevant}" ]
-#                      then
-#                              echo "-- (Skipping an irrelevant package for obsolete port $origin as $currentpkg)"
-#                              continue
-#                      fi
-#                      origin_ptn=`str_escape_regexp ${origin}`
-#                      if grep -m 1 -E "^${origin_ptn}$" "${DBDIR}/conf/HOLD:PORTS.parsed" > /dev/null 2>&1
-#                      then
-#                              :
-#                      else
-#                              echo "-- (Skipping a hold package for obsolete port $origin as $currentpkg)"
-#                              continue
-#                      fi
-#                      if database_query_is_a_port_suppressed "$origin"
-#                      then
-#                              echo "-- (Skipped because being suppressed)"
-#                              continue
-#                      fi
-#                      _MSG_CURRENT_STAGE=${_MSG_CURRENT_STAGE_general}
-#                      message_stage_title "Starting deinstallation process for obsolete port $origin as $currentpkg"
-#                      if [ -z "${currentorigin_is_all}" -a -n "${currentorigin_is_relevant}" ]
-#                      then
-#                              message_target_relations "$origin"
-#                      fi
-#                      if [ $opt_dry_run = yes ]
-#                      then
-#                              message_dry_run
-#                              continue
-#                      fi
-#                      echo "-- (Creating backup package for $origin as $currentpkg)"
-#                      tag=`echo "$origin" | tr / :`
-#                      if [ ! -e "${DBDIR}/status_deinst/$tag.backup" ]
-#                      then
-#                              backup_pkg=`pkgsys_create_backup_pkg "$currentpkg" "${DBDIR}/backup_obsolete"` || :
-#                              if [ -n "$backup_pkg" ]
-#                              then
-#                                      echo "INFO: The backup is saved at $backup_pkg."
-#                              else
-#                                      echo "*** Continuating forcibly by keeping the installed package"
-#                                      continue
-#                              fi
-#                              touch "${DBDIR}/status_deinst/$tag.backup"
-#                      fi
-#                      echo "-- (Deleting package for $origin as $currentpkg)"
-#                      pkg_delete_f "$currentpkg" \
-#                              || { echo "*** Continuating forcibly by hoping success..."; continue; }
-#                      fileedit_rm_a_line "$origin" "${DBDIR}/obsolete_ports.remained"
-#                      _MSG_CURRENT_STAGE=${_MSG_CURRENT_STAGE_general}
-#                      echo
-#              done
-#      fi
-#      touch "${DBDIR}/COMPLETE_DEINST_UNUSED_PKGS"
-#      _MSG_CURRENT_STAGE=
-#      echo
-# fi
 
 # Clean up obsolete or unused distfiles
 if [ $opt_only_target_scope = no -a $opt_keep_distfiles = no ]
@@ -2660,24 +1397,6 @@ then
                        | sed 's|^\./||' | sort -u > $tmp_distfiles_exists
                fileedit_manipulate_old_lines "$tmp_distfiles_exists" "${DBDIR}/distfiles.entire" \
                        | while read distfile
-#              cat > ${TMPDIR}/CLEANUP_OBSLETE_DISTFILES::rmlist.awk << eof
-# BEGIN {
-#      distfles_available_list = sprintf ("%s%s", ENVIRON["DBDIR"], "/distfiles.entire");
-#      distfles_exists_list = sprintf ("%s%s", ENVIRON["TMPDIR"], "/CLEANUP_OBSLETE_DISTFILES::distfiles_exists");
-# }
-# {
-#      if (getline < distfles_available_list) distfles_available[NR] = $0;
-#      if (getline < distfles_exists_list) distfles_exists[NR] = $0;
-#      i = 0;
-# }
-# eof
-#              num_distfiles=`cat "${DBDIR}/distfiles.entire" | wc -l`
-#              ( set -ex; cd "${DISTDIR}" && find . -type f ) | while read distfile
-#              do
-#                      
-# exit 255
-#              ( set -ex; cd "${DISTDIR}" && find . -type f ) \
-#                      | grep -v -E -f "${DBDIR}/distfiles.grep.pattern" | while read distfile
                do
                        if [ $opt_batch_mode = no ]
                        then
@@ -2702,11 +1421,15 @@ _program_exec_and_record_completion__operation ()
 }
 program_exec_and_record_completion REBUILD_PKGDB
 
+
+# ==================================================
+# ================ ENDING MESSAGES =================
+# ==================================================
+
 # Notice of failures
 exists_unresolved_ports=
 message_summary_dependents_of_failed_reinstallation failure || exists_unresolved_ports=y
 message_summary_dependents_of_failed_reinstallation redo || exists_unresolved_ports=y
-# message_summary_dependents_of_failed_reinstallation pending || exists_unresolved_ports=y
 message_summary_dependents_of_failed_reinstallation conflict || exists_unresolved_ports=y
 [ -n "$exists_unresolved_ports" ] && message_summary_advice_on_manual_solution