#!/bin/bash
#  script - script support library

set -e

lib_load 'core/logging'
lib_load 'core/user'
lib_load 'core/tool'


######
# Settings

# $script_groups[] - Mapping from script names to script groups
declare -gA script_groups
script_groups_init() {
	local a
	for a in "${package_apps[@]}"; do
		local s=$(basename "$a")
		local g=$(dirname "$a")
		script_groups[$s]=$g
	done
}
script_groups_init

script=( $(basename "$0") )
script_name=${script[0]}
script_group=${script_groups[$script_name]}
script_version=$package_version

script_confdir="$confdir/$script_name"
if is_superuser; then
	script_logdir="$logdir/$script_name"
	script_datadir="$datadir/$script_name"
else
	script_logdir="$user_logdir/$script_name"
	script_datadir="$user_datadir/$script_name"
fi
script_tempdir="$user_tempdir/$script_name"

app_log_hook=script_log


######
# Configuration

core_script_config_init() {
	lib_setting_arrays script
	lib_setting_vars script_name script_group
	lib_setting_vars -ro script_version

	lib_setting_vars -ro script_confdir script_logdir
	lib_setting_vars -ro script_datadir script_tempdir

	lib_setting_vars app_log_hook

	lib_setting_assocs script_groups
}


######
# Functions

script_symbol() { tool_symbol "$script_name"; }

script_tempfile() { tempfile -d "$script_tempdir"; }

script_setting_vars() { settings_add_vars "$(script_symbol)" "$@"; }
script_setting_funcs() { settings_add_funcs "$(script_symbol)" "$@"; }
script_setting_arrays() { settings_add_arrays "$(script_symbol)" "$@"; }
script_setting_assocs() { settings_add_assocs "$(script_symbol)" "$@"; }

script_setting_list_vars() { settings_list "$(script_symbol)" "vars"; }
script_setting_list_funcs() { settings_list "$(script_symbol)" "funcs"; }
script_setting_list_arrays() { settings_list "$(script_symbol)" "arrays"; }
script_setting_list_assocs() { settings_list "$(script_symbol)" "assocs"; }

script_cmd_dispatch() {
	local script=( "$script_name" )
	app_cmd_dispatch "$@"
}

script_cmd_exists() {
	local script=( "$script_name" )
	cmd_exists "$@"
}

script_cmd_exec() {
	local script=( "$script_name" )
	cmd_exec "$@"
}


######
# Script Logging

script_log_filename() {
	echo "$script_logdir/${1:-main}.log"
}

script_log() {
	local f
	f=$(script_log_filename "$@")
	log_pipe "$f"
}

View the Developer Guide Index

View the Reference Manual Index


Generated on Fri Jul 28 14:35:03 PDT 2017 by mcsh d14 v0.23.0.