#!/bin/sh -e
# ==================================================================================
# portsreinstall main script
# Copyright (C) 2010-2018 Mamoru Sakaue, MwGhennndo, All Rights Reserved.
# This software is distributed under the 2-Clause BSD License.
# ==================================================================================

# ==================================================
# ================== ENVIRONMENT ===================
# ==================================================

# ============ Set up of environment =============
PROGRAM=portsreinstall
APPNAME=`basename "$0"`

MYPREFIX=`realpath "\`dirname \"$0\"\`"/..`
LIBDIR=${MYPREFIX}/lib/${PROGRAM}
LIBEXECDIR=${MYPREFIX}/libexec/${PROGRAM}
SHAREDIR=${MYPREFIX}/share/${PROGRAM}
ETCDIR=${MYPREFIX}/etc

. ${LIBDIR}/libcommand.sh
. ${LIBDIR}/libcommand_do.sh
. ${LIBDIR}/libcommand_flexconf.sh
. ${LIBDIR}/libcommand_forget.sh
. ${LIBDIR}/libcommand_pkgs.sh
. ${LIBDIR}/libcommand_show.sh
. ${LIBDIR}/libconf.sh
. ${LIBDIR}/libdatabase_build.sh
. ${LIBDIR}/libdatabase_maintain.sh
. ${LIBDIR}/libdatabase_query.sh
. ${LIBDIR}/libdatabase_record.sh
. ${LIBDIR}/libdeinstall.sh
. ${LIBDIR}/libfileedit.sh
. ${LIBDIR}/libmain.sh
. ${LIBDIR}/libmessage.sh
. ${LIBDIR}/libmisc.sh
. ${LIBDIR}/liboptions.sh
. ${LIBDIR}/libpkgsys.sh
. ${LIBDIR}/libprogram.sh
. ${LIBDIR}/libreinstall.sh
. ${LIBDIR}/libstr.sh
. ${LIBDIR}/libtemp.sh
. ${LIBDIR}/libusage.sh

. ${LIBDIR}/main/libcommand.sh
. ${LIBDIR}/main/liboptions.sh
. ${LIBDIR}/main/libtemp.sh
. ${LIBDIR}/main/libusage.sh

DBDIR=/var/tmp/${APPNAME}.db
CONFFILE=${ETCDIR}/${PROGRAM}.conf
PKGTOOLSCONF=${ETCDIR}/pkgtools.conf

main_set_version
misc_init_vardefs
temp_trap_init

# ==================================================
# ========= PARSING OPTIONS AND ARGUMENTS ==========
# ==================================================

main_parse_options_arguments "$@"

# ========================================================
# ================== SET UP OF PARAMETERS ==================
# ========================================================

main_setup_parameters "$@"

# =================================================================================
# ================== OPERATION WITHOUT PACKAGES MANAGEMENT TOOLS ==================
# =================================================================================

main_operation_without_pkg_management_tools "$@"

# ===================================================
# ================== SET UP OF TOOLS ==================
# ===================================================

# ============= Upgrade of tools =============

# Get the flavored port origin for this utility if installed by port
MYPORTORIGIN=`pkgsys_get_my_origin 2> /dev/null` || :

# Collect all installed packages
main_collect_all_installed_packages

# Preliminary inspection of tools which have to be up-to-date
main_preliminary_inspection_of_tools

# Upgrade of pkg(8) if new
main_upgrade_pkg8_if_new

# Upgrade of dialog4ports(1) if new
main_upgrade_dialog4ports1_if_new

# Upgrade of this utility if new
main_self_upgrade


# ==================================================
# ================== PREPARATION ===================
# ==================================================

# ============= Correspondence to configuration changes =============

# Overlay onto the temporary database by reflecting changes in configuration
main_reflect_conf_changes

# ============= Database construction =============

# Execute command operations which must be done before the database construction
command_all_exec_before_build_database "$@"

# Execute command operations of database construction
command_all_exec_build_database "$@"

# ==================================================
# ====================== MAIN ======================
# ==================================================

# Execute command operations which must be done before the main process
command_all_exec_before_main "$@"

# Execute command operations as the main process
command_all_exec_main "$@"
