#!/bin/bash
#  mcsh - Core Runtime Library

set -e

######
# Startup

# $trace - If set to ``true``, enables the built-in shell trace feature
# (``set -x``).
# Default: ``false``
trace="${trace:-false}"
$trace && set -x

# $runtime - The full path to the installed runtime file as used by scripts.
# Example: ``/home/zwelch/src/mcf/mcsh-release/install/share/mcsh/mcsh.sh``
declare -gr runtime="/home/zwelch/src/mcf/mcsh-release/install/share/mcsh/mcsh.sh"

# $runtime_name - Name of runtime library
runtime_name=$(basename "$runtime" .sh)

#=====

######
# Package Information

# $package_name - Full name of this package
# Default: ``Mandolin Creek System Helpers``
package_name="Mandolin Creek System Helpers"

# $package_name - Name of this package
# Default: ``mcsh``
package_name="mcsh"

# $package_version - Version of this package
# Default: ``0.21.0``
package_version="0.21.0"

# $package_build_date - Date when this package was built.
# Default: ``2017-07-04``
package_build_date="2017-07-04"

# $package_build_time - Time of day when this package was built
# Default: ``16:56:52``
package_build_time="16:56:52"

######
# Package Support

# $package_url - Home page for this package
# Default: ``https://systems.mandolincreek.net/projects/mcsh``
package_url="https://systems.mandolincreek.net/projects/mcsh"

# $package_author - Author (or current maintainer) of this package
# Default: ``Zachary T Welch <zach@mandolincreek.net>``
package_author="Zachary T Welch <zach@mandolincreek.net>"

# $package_support_name - Name of support contact
# Default: ``Mandolin Creek Systems``
package_support_name="Mandolin Creek Systems"

# $package_support_email - E-mail for support
# Default: ``systems@mandolincreek.net``
package_support_email="systems@mandolincreek.net"


######
# Package Components

# $package_apps[] - Lists all tools included in this package
# Default: ``dev/d12 dev/d14 dev/i7 dev/p7 dev/mcsh doc/f4 doc/p3 doc/r16 doc/legal/e8 dev/u2 net/a6 net/e5 net/k5 net/l4 net/m7 net/p5 net/t4 net/w4 net/w6 sec/g3g sec/l11 sys/b9 sys/c5 sys/g3 sys/h4 sys/idev7 sys/mok7 sys/s7 sys/v10 sys/v3``
package_apps=( dev/d12 dev/d14 dev/i7 dev/p7 dev/mcsh doc/f4 doc/p3 doc/r16 doc/legal/e8 dev/u2 net/a6 net/e5 net/k5 net/l4 net/m7 net/p5 net/t4 net/w4 net/w6 sec/g3g sec/l11 sys/b9 sys/c5 sys/g3 sys/h4 sys/idev7 sys/mok7 sys/s7 sys/v10 sys/v3 )

# $package_libs[] - Lists all libraries included in this package
# Default: ``core/app core/args core/base core/cleanup core/commands core/config core/exec core/file core/functions core/host core/lib core/license core/list core/logging core/math core/output core/qsort core/script core/settings core/str core/timer core/tool core/user dev/docs/cli dev/docs/color dev/docs/config dev/docs/dep dev/docs/guide dev/docs/html dev/docs/index dev/docs/ref dev/docs/rst dev/gen/bash dev/package/changelog dev/package/check dev/package/cli dev/package/config dev/package/dep dev/package/dist dev/package/edit dev/package/env dev/package/files dev/package/git dev/package/install dev/package/load dev/package/make dev/package/publish dev/package/release dev/package/search dev/package/versions dev/tool/make dev/tool/pygmentize dev/versions doc/html doc/pdf doc/rst doc/text doc/tool/email2pdf doc/tool/enscript doc/tool/ghostscript doc/tool/graphviz doc/tool/latex doc/tool/pandoc doc/tool/pdf2svg doc/tool/wkhtmltopdf doc/legal/exhibit mcui mcui/cli mcui/dialog mcui/input mcui/kdialog net/email net/remote net/tool/apache net/tool/plone net/tool/rsync net/tool/ssh net/tool/trac net/tool/wget net/webdav sec/tool/certbot sec/tool/gpg sys/backup sys/check sys/client sys/cloud sys/packages sys/pid sys/server sys/shell sys/support sys/tool/apt sys/tool/bc sys/tool/chroot sys/tool/cron sys/tool/git sys/tool/mount sys/tool/qemu-img sys/tool/qemu-nbd sys/tool/screen sys/tool/sed sys/tool/supervisor sys/tool/tar sys/tool/vbox sys/tool/virtualenv core/license-parts``
package_libs=( core/app core/args core/base core/cleanup core/commands core/config core/exec core/file core/functions core/host core/lib core/license core/list core/logging core/math core/output core/qsort core/script core/settings core/str core/timer core/tool core/user dev/docs/cli dev/docs/color dev/docs/config dev/docs/dep dev/docs/guide dev/docs/html dev/docs/index dev/docs/ref dev/docs/rst dev/gen/bash dev/package/changelog dev/package/check dev/package/cli dev/package/config dev/package/dep dev/package/dist dev/package/edit dev/package/env dev/package/files dev/package/git dev/package/install dev/package/load dev/package/make dev/package/publish dev/package/release dev/package/search dev/package/versions dev/tool/make dev/tool/pygmentize dev/versions doc/html doc/pdf doc/rst doc/text doc/tool/email2pdf doc/tool/enscript doc/tool/ghostscript doc/tool/graphviz doc/tool/latex doc/tool/pandoc doc/tool/pdf2svg doc/tool/wkhtmltopdf doc/legal/exhibit mcui mcui/cli mcui/dialog mcui/input mcui/kdialog net/email net/remote net/tool/apache net/tool/plone net/tool/rsync net/tool/ssh net/tool/trac net/tool/wget net/webdav sec/tool/certbot sec/tool/gpg sys/backup sys/check sys/client sys/cloud sys/packages sys/pid sys/server sys/shell sys/support sys/tool/apt sys/tool/bc sys/tool/chroot sys/tool/cron sys/tool/git sys/tool/mount sys/tool/qemu-img sys/tool/qemu-nbd sys/tool/screen sys/tool/sed sys/tool/supervisor sys/tool/tar sys/tool/vbox sys/tool/virtualenv core/license-parts )

# $package_extensions[] - List all extensions included with this package
# Default: ``docs site``
package_extensions=( docs site )

#=====

######
# Global Settings

# The variables in this section may be configured in the environment
# or configuration scripts.

# $pretend - If set to ``true``, simulate the requested commands rather
# than executing them.
# Default: ``false``
pretend=${pretend:-false}

# $verbose - If set to ``true``, the script will emit more output by
# producing informational messages.  Such messsages include the full
# command line of every command being run.
# Default: ``$pretend``
verbose=${verbose:-$pretend}

# $debug - If set to ``true``, emits a lot more output; this has the
# side-effect of forcing ``$verbose`` to ``true``.
# Default: ``false``
debug=${debug:-false}
! $debug || verbose=true

# $quiet - If set to ``true``, reduces the amount of output by
# surpressing warning messages.
# Default: ``false``
quiet=${quiet:-false}

# $intense - If set to ``true``, the ``check`` commands may perform
# more extensive.
# Default: false
intense=${intense:-false}

# $dumpenv - If set to ``true``, the active script environment will be
# saved at the end of ``app_start()``.  The resulting snapshot will be
# named '``${script_name}.env``' and created in the current directory.
# Default: false
dumpenv="${dumpenv:-false}"


######
# Directory Settings
#
# The variables in this section may be configured in the environment
# or configuration scripts; however, that may cause things to break.
#
# 		**CHANGE THESE SETTNGS AT YOUR OWN PERIL!**

# $etcdir - Path to global system configuration files.
# Example: ``/home/zwelch/src/mcf/mcsh-release/install/etc``
etcdir=${etcdir:-/home/zwelch/src/mcf/mcsh-release/install/etc}

# $confdir - Path to package system configuration files
# Example: ``$etcdir/$package_name``
confdir="$etcdir/$package_name"

# $tmpdir - Path to global temporary directory
# Example: ``/home/zwelch/src/mcf/mcsh-release/install/tmp``
tmpdir=${tmpdir:-/home/zwelch/src/mcf/mcsh-release/install/tmp}

# $bindir - Path to package tool scripts
# Example: ``/home/zwelch/src/mcf/mcsh-release/install/bin``
bindir=${bindir:-/home/zwelch/src/mcf/mcsh-release/install/bin}

# $libdir - Path to package library scripts
# Example: ``/home/zwelch/src/mcf/mcsh-release/install/share/mcsh``
libdir=${libdir:-/home/zwelch/src/mcf/mcsh-release/install/share/mcsh}

# $datadir - Path to package data directory
# Example: ``/home/zwelch/src/mcf/mcsh-release/install/cache/mcsh``
datadir=${datadir:-/home/zwelch/src/mcf/mcsh-release/install/cache/mcsh}

#=====

######
# Runtime Configuration

mcsh_config_init() {
	lib_setting_vars -ro \
		package_name package_version \
		package_url package_support_name package_support_email \
		etcdir tmpdir libdir datadir confdir

	lib_setting_vars \
		pretend verbose debug trace quiet intense dumpenv

	lib_setting_arrays -ro package_apps package_libs
}

# $script[] - Command namespace stack.
declare -ga script
script=( $(basename "$0") )


######
# Output Stubs
#
# These stub functions will be replaced in the ``core/output`` library,
# but they must be defined in order to load that library.  These provide
# the least amount of support possible without being completely useless.

# debug() - Stub for ``debug()`` in ``core/output``.
debug() { ! $debug || echo "${script[*]}: $*" >&2; }

# info() - Stub for ``info()`` in ``core/output``.
info() { ! $verbose || echo "${script[*]}: INFO: $*" >&2; }

# warn() - Stub for ``warn()`` in ``core/output``.
warn() { $quiet || echo "${script[*]}: WARN: $*" >&2; }

# error() - Stub for ``error()`` in ``core/output``.
error() { echo "${script[*]}: ERROR: $*" >&2; false; }


######
# Core Library Settings

# $lib_debug - If set to ``true``, forces ``$debug`` to ``true`` and
# enables extra debugging information for the library loading process.
# Default: ``false``
lib_debug=${lib_debug:-false}
$debug || debug=$lib_debug

# $lib_loaded[] - List of libraries that have been loaded
declare -ga lib_loaded=( $runtime_name )

# $lib_initied[] - List of libraries that have been loaded
declare -ga lib_inited

# $lib_name[] - Name of libraries being loaded (local only)
declare -ga lib_name=( ${lib_loaded[0]} "${script[@]}" )

######
# Core Library Functions

# lib_info() - Generates an informational message if ``$lib_debug``
# is ``true``.
lib_info() { ! $lib_debug || info "$@"; }

# lib_debug() - Generates a debugging message if ``$lib_debug``
# is ``true``.
lib_debug() { ! $lib_debug || debug "$@"; }

# lib_filename() - Prints the full path to a library.
# $1 - Name of library
lib_filename() { echo "$libdir/$1.sh"; }

# lib_symbol() - Prints the symbol for a library.
# $1 - Name of library
lib_symbol() { echo "$1" | tr '/-' '__'; }

# is_lib_loaded() - Checks to see if a library has been loaded
# $1 - Name of library
# Returns: Success if the named library has been loaded.
is_lib_loaded() {
	local name="$1"
	local lib

	for lib in "${lib_loaded[@]}"; do
		if [ "$lib" = "$name" ]; then
			return
		fi
	done

	false
}

# lib_load_check() - Stub function for checking library prior to loading.
# This stub version will be overridden by the definition in ``core/lib``
# when that library is loaded.
# $1 - Name of library (ignored)
lib_load_check() { lib_debug "$lib_name: loading library"; }

# lib_init() - Calls a library's ``$(lib_symbol)_lib_init`` function (if
# it exists) to initialize a library that is being loaded, then notes the
# library has been initialized.
# $1 - Name of library
lib_init() {
	local name=$1
	local func="$(lib_symbol $name)_lib_init"
	if declare -pf "$func" >/dev/null 2>&1; then
		lib_debug "$name: initializing..."
		$func
	fi
	lib_inited=( "${lib_inited[@]}" $name )
}

# lib_exists() - Checks to see if a library exists.
# $1 - Name of library
# Returns: Success if the named library exists.
lib_exists() { test -f "$(lib_filename "$1")"; }

# lib_load() - Loads a library ($1) and initializes it.
# Runs the library script as part of the calling script, adding
# new variables and functions to the environment.
# If loading was successful, calls ``lib_init()``.
# If library was loaded previously, does nothing.
# $1 - Name of library
lib_load() {
	local name=$1
	if is_lib_loaded "$name"; then
		return
	fi

	local -a _lib_name=( "${lib_name[@]}" )
	local -a lib_name=( "$name" "${_lib_name[@]}" )

	lib_exists "$name" || error "$name: library missing"
	lib_load_check

	#; prevent recursion
	lib_loaded=( "${lib_loaded[@]}" $name )

	local file
	file=$(lib_filename "$name")
	source "$file" || error "$file: library failed to load"

	lib_init "$name"
}

lib_load 'core/base'
lib_load 'core/app'

list_append lib_inited $runtime_name

lib_debug "$(printf "$package_name: runtime loaded (%d/%d libraries loaded)" \
		"${#lib_loaded[*]}" "${#package_libs[*]}")"

View the Developer Guide Index

View the Reference Manual Index


Generated on Tue Jul 4 17:01:09 PDT 2017 by mcsh d14 v0.21.0.