#!/bin/sh -e
# ==================================================================================
# portsreinstall chroot main script
# Copyright (C) 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}/chroot/libcommand.sh
. ${LIBDIR}/chroot/libcommand_do.sh
. ${LIBDIR}/chroot/libdatabase_maintain.sh
. ${LIBDIR}/chroot/libfs.sh
. ${LIBDIR}/chroot/libmain.sh
. ${LIBDIR}/chroot/libmessage.sh
. ${LIBDIR}/chroot/liboptions.sh
. ${LIBDIR}/chroot/libusage.sh

DBDIR_PROG=/var/tmp/${PROGRAM}.db
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 "$@"

fs_safeguard_basedir "$opt_basedir"

# ========================================================
# ================== SET UP OF PARAMETERS ==================
# ========================================================
main_setup_parameters "$@"

misc_inspect_fs_privilege

main_operation_without_pkg_management_tools "$@"

# ==================================================
# ====================== 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 "$@"
