#!/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/libfs.sh

. ${LIBDIR}/chroot-mount/libcommand.sh
. ${LIBDIR}/chroot-mount/libconf.sh
. ${LIBDIR}/chroot-mount/libdatabase_maintain.sh
. ${LIBDIR}/chroot-mount/libmain.sh
. ${LIBDIR}/chroot-mount/libmessage.sh
. ${LIBDIR}/chroot-mount/liboptions.sh
. ${LIBDIR}/chroot-mount/libtemp.sh
. ${LIBDIR}/chroot-mount/libusage.sh

[ -n "${SYSTEMBASE}" ] || SYSTEMBASE=`realpath "${MYPREFIX}"/../..`
DBDIR=${SYSTEMBASE}/var/tmp/${PROGRAM}-chroot.db

main_set_version
misc_init_vardefs
temp_trap_init
main_load_conf

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

main_parse_options_arguments "$@"
opt_invalidate_mount_privilege=no

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

# Termination messages during construction of the temporary database
temp_setup_terminate_process

# Opening title
message_credit
message_echo

# Check of the mounting/unmounting privilege of the environment
main_check_of_mounting_unmounting_privilege

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

command_all_exec_without_pkgtools "$@"
