#!/bin/bash
#   app - application support library

set -e


######
# Application API

app_cleanup_tempdirs() { rm -rf "$cmd_tempdir"; }

app_start() {
	info "Running '$script_name${*:+ }$*' ..."
	cleanup_init

	app_timer_start

	config_init
	# override lib_load to perform late initialization
	func_chain config lib_load

	config_load

	config_check

	settings_check
	settings_final

	# override lib_load to perform late init/check
	func_chain app lib_load

	cleanup_add app_cleanup_tempdirs
	run_mkdir "$user_tempdir" "$cmd_tempdir"

	script_cmd_exec init

	! $dumpenv || set >"$script_name.env"
}

app_exit() {
	if [ "$1" ]; then
		error "$@"
		[ "$exit_code" ] || exit_code=1
	else
		info "... done"
	fi

	exit $exit_code
}


######
# Application Timer

app_timer_start() {
	timer_start app_timer_runtime
	cleanup_add app_timer_cleanup
}

app_timer_cleanup() {
	timer_stop app_timer_runtime
	local elapsed
	elapsed=$(timer_elapsed app_timer_runtime)
	info "Running time: $elapsed"
}


######
# Application Commands

app_version() {
	local name version desc
	if [ $script_name = $package_name ]; then
		name=$package_name
		version=$package_version
	else
		name="$package_name $script_name"
		version=$script_version
	fi
	desc=$(local -a script=( $script_name ) && cmd_exec desc)
	echo "$name v$version ($package_build_date)${desc:+  }$desc"
}

app_license() {
	app_version
	lib_load 'core/license'

	case "$1" in
	(''|short|blurb)		license_blurb ;;
	(warranty)			license_warranty ;;
	(full)				license_full ;;
	(*)
		warn "$1: unknown license command"
		;;
	esac
}

app_help() {
	app_version
	cmd_help
}

app_check() {
	if cmd_exists check; then
		app_echo "$script: check started"
		cmd_dispatch check "$@"
		app_echo "$script: check finished"
	else
		# this is NOT an error, just an embarassment
		warn "'check' is not implemented"
	fi
}

app_check_lib() {
	lib_load 'sys/check'
	if [ "$@" ]; then
		for_each lib_load "$@"
		for_each lib_check "$@"
	else
		lib_check_all
	fi
}

# app_dispatch - Top-level dispatch handler.  Scripts can override this
# to add their own dynamic top-level commands.
app_cmd_dispatch() {
	if cmd_exists dispatch; then
		cmd_exec dispatch "$@"
	else
		cmd_dispatch "$@"
	fi
}

app_usage() {
	cat <<USAGE

Tool Commands:
	version				Prints tool version
	config ...			Tool configuration commands
	check [...]			Runs tool self-test/demo
	check-lib <lib>+		Runs library self-test/demo
	shell [<file> [<arg>+]]		Runs tool command shell

Tool Management Commands:
	client ...			Client commands
	server ...			Server commands
	backup ...			Backup commands
	restore ...			Restore commands
USAGE
}

app_main() {
	debug "processing command line arguments..."
	local cmd=$1
	[ "$cmd" ] || error_usage "command required"
	shift

	case "$cmd" in
	version|--version)
		app_version
		;;
	license)
		app_license "$@"
		;;
	check)
		app_check "$@"
		;;
	check-lib)
		app_check_lib "$@"
		;;
	shell)
		lib_load 'sys/shell'
		shell_run "$@"
		;;
	backup|restore)
		lib_load 'sys/backup'
		backup_dispatch "$cmd" "$@"
		;;
	config)
		lib_cmd_dispatch "$cmd" "$@"
		;;
	server)
		lib_load 'sys/server'
		lib_cmd_dispatch "$cmd" "$@"
		;;
	client)
		lib_load 'sys/client'
		lib_cmd_dispatch "$cmd" "$@"
		;;
	*)
		app_cmd_dispatch "$cmd" "$@"
		;;
	esac
}

app_run() {
	app_start "$@"
	app_main "$@"
	app_exit
}


######
# Late Library Load Function Chains

# config_lib_load - chain function to initialize libraries loaded "late"
config_lib_load() {
	local name=$1
	! is_lib_loaded "$name" || return 0
	config_lib_load_next "$name"
	config_init_lib "$name"
}

# app_lib_load - chain function to initialize libraries loaded "late"
app_lib_load() {
	local name=$1
	! is_lib_loaded "$name" || return 0
	app_lib_load_next "$name"
	config_check_lib "$name"
	! $settings_dump || settings_dump_lib "$name"
	settings_check_lib "$name"
	settings_final_lib "$name"
}

View the Script Reference Index


Generated on Tue Apr 25 21:20:00 PDT 2017 by mcsh i7 v0.18.0.