export PATH=$PATH:/opt/ytools/bin # -- private stuff not intended for use outside of this script _ytools_check_config_present() { if [ -z "$ytools_config_files" ]; then if [ "$1" = true ]; then ytools_log "warning: $0 tries to access configuration witout having a config file" fi return 1; fi return 0; } _ytools_check_config_files_readable() { local new="" for f in $ytools_config_files; do [ -f "$f" -a -r "$f" ] && new="$new $f" done ytools_config_files="$new" } _ytools_probe_config_paths() { local f new exp dir [ ! "$ytools_config_files" ] && ytools_config_files=" /etc/opt/$ytools_project/$ytools_basename.conf $HOME/.$ytools_project/$ytools_basename.conf $HOME/.$ytools_project/$rc $HOME/.$ytools_basename/$ytools_basename.conf $HOME/.$ytools_basename/$rc $HOME/.$rc " _ytools_check_config_files_readable # add includes new="" for f in $ytools_config_files; do exp=`sed ' /^[ ]*include[ ]/ !d s/^[ ]*include[ ]*//g ' $f` if [ "$exp" ]; then if [ "${exp:0:1}" != '/' ]; then dir=`dirname $f` exp=$dir/$exp fi exp=`ls -d $exp 2>/dev/null` new="$new $f $exp" fi done [ "$new" ] && ytools_config_files="$new" _ytools_check_config_files_readable } _ytools_cat_section() { if [ "$2" ]; then sed -n "/\[$2\]/,/[^ ]*\[/ p" $1 | sed '/[^ ]*\[/ d; /^[ ]*include[ ]/ d' return fi sed -n '0,/[^ ]*\[/ p' $1 | sed '/[^ ]*\[/ d; /^[ ]*include[ ]/ d' } _ytools_format_config() { sed '/^$/d; s/$/;/' } _ytools_format_section() { _ytools_cat_section $* | _ytools_format_config } _ytools_source_section() { eval `_ytools_cat_section $* | _ytools_format_config` } _uniq() { sed -n 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P' } # -- exported utilities ytools_log_err() { echo "# $@" >&2 } ytools_log_stdout() { echo "# $@" } ytools_log_stderr() { echo "# $@" >&2 } ytools_log_syslog() { logger -t $ytools_basename "$*" } ytools_log() { local logger for logger in $ytools_loggers; do eval ytools_log_$logger "$@" done } ytools_empty_config() { local o_verbose="" local o_section set -- `getopt 'vs:' $*` while [ "$1" != -- ]; do case $1 in -v) o_verbose=true;; -s) o_section=$2; shift;; *) ytools_log_err unknown option $1 exit 1;; esac shift done shift _ytools_check_config_present $o_verbose || return 1 [ ! -r "$1" ] && return [ "$o_verbose" ] && ytools_log o resetting config "\"$1\"" if [ "$o_section" ]; then [ "$o_verbose" ] && ytools_log o resetting section "[$o_section]" of config "\"$1\"" fi eval `_ytools_format_section $1 $o_section | sed 's/=[^;]*;/=;/g'` } ytools_empty_configs() { local section local c local o_verbose="" local o_section local opts="" set -- `getopt 'vs:' $*` while [ "$1" != -- ]; do case $1 in -v) o_verbose=true opts="$opts -v" ;; -s) o_section="-s $2"; shift;; *) ytools_log_err unknown option $1 exit 1;; esac shift done shift _ytools_check_config_present $o_verbose || return 1 for section in "" `ytools_config_sections`; do for c in $ytools_config_files; do ytools_empty_config -s "$section" section $opts $c if [ -d $c.d ]; then local dirconfs=`find $c.d -maxdepth 1 -type f` local f for f in $dirconfs; do ytools_empty_config -s "$section" $opts $f done fi done done return 0 } ytools_source_config() { local o_verbose="" local o_section set -- `getopt 'vs:' $*` while [ "$1" != -- ]; do case $1 in -v) o_verbose=true ;; -s) o_section=$2; shift;; *) ytools_log_err unknown option $1 exit 1;; esac shift done shift _ytools_check_config_present $o_verbose || return 1 if [ "$o_verbose" = true ]; then if [ "$o_section" ]; then ytools_log o sourcing section "[$o_section]" from config file "\"$1\"" else ytools_log o sourcing config "\"$1\"" fi _ytools_cat_section $1 $o_section fi _ytools_source_section $1 $o_section } ytools_source_configs() { local c local source_opts="" local o_verbose="" set -- `getopt 'vs:' $*` while [ "$1" != -- ]; do case $1 in -v) o_verbose=true source_opts="$source_opts -v" ;; -s) source_opts="$source_opts -s $2" shift;; *) ytools_log_err unknown option $1 exit 1;; esac shift done shift _ytools_check_config_present $o_verbose || return 1 for c in $ytools_config_files; do ytools_source_config $source_opts $c if [ -d $c.d ]; then local dirconfs=`find $c.d -maxdepth 1 -type f` local f for f in $dirconfs; do ytools_source_config $source_opts $f done fi done } ytools_config_sections() { local o_verbose="" local o_section local o_file local o_exact=false OPTIND=1 while getopts ves:f: flag; do case $flag in v) o_verbose=true;; s) o_section="$OPTARG";; f) o_file="$OPTARG";; e) o_exact=true;; *) ytools_log_err "unknown option -$flag" exit 1;; esac done shift $(($OPTIND - 1)) _ytools_check_config_present $o_verbose || return 1 [ "$o_file" ] || o_file="$ytools_config_files" if [ "$o_exact" = false ]; then if [ "$o_section" ]; then sed " /^[ ]*\[$o_section\.\(.*\)\]/ !d s/^[ ]*\[$o_section\.\(.*\)\].*/\1/ " $o_file 2>/dev/null | _uniq else sed " /^[ ]*\[\(.*\)\]/ !d s/^[ ]*\[\(.*\)\].*/\1/ " $o_file 2>/dev/null | _uniq fi else if [ "$o_section" ]; then sed " /^[ ]*\[$o_section\.\([^.]*\)\]/ !d s/^[ ]*\[$o_section\.\([^.]*\)\].*/\1/ " $o_file 2>/dev/null | _uniq else sed " /^[ ]*\[\([^.]*\)\]/ !d s/^[ ]*\[\([^.]*\)\].*/\1/ " $o_file 2>/dev/null | _uniq fi fi return } ytools_cat() { cat | sed 's/^[ ]*|//' } ytools_waitpid() { local pid=$1 local t=5 [ "$2" ] && t=$2 while ((t > 0)); do [ -f /proc/$pid/status ] || { wait $pid return 0 } sleep 1 ((t -= 1)) done return 1 } ytools_terminate() { local pid for pid in $@; do [ -f /proc/$pid/status ] && { echo terminating process $pid kill $pid ytools_waitpid $pid || { echo "failed to normally terminate process $pid, killing it" >&2 kill -9 $pid ytools_waitpid $pid || { echo "failed to kill process $pid" >&2 return 1 } } } done return 0 } # -- exported variables if [ "$ytools_functions_sourced" != "yes" ]; then ytools_functions_sourced="yes" [ -z "$ytools_basename" ] && ytools_basename=`echo "$0" | sed 's/^-*//' | xargs basename | sed 's/\.sh$//'` [ -z "$ytools_project" ] && ytools_project="ytools" [ -z "$ytools_rc" ] && ytools_rc="$ytools_basename"rc _ytools_probe_config_paths [ -z "$ytools_loggers" ] && ytools_loggers="stdout" fi