#!/bin/bash if [[ -z "${shome:-}" ]]; then shome="$(unset CDPATH; cd -P -- "$(dirname -- "${BASH_SOURCE}")/.." && pwd -P)" fi function check_help { # taken from shocco if expr -- "$*" : ".*--help" >/dev/null; then display_help exit 0 fi } function display_help { flags_help echo # taken from shocco grep '^#/' <"$shome/$(basename $(dirname -- "$0"))/$(basename -- "$0")" | cut -c4- } # Exits the script with the last error code. Servers as a marker in $0 to # begin ronn documentation until end of file. function __MAN__ { exit "$!" } # Extracts ronn-style Markdown after __MAN__ to stdout. If a line is "MAN", it # is assumed that a here document is used (for syntactical reasons). # # A limitation of detecting "MAN" will truncate the Markdown if "MAN" is a # legimate text. # # __MAN__ << "MAN" # raw ronn-style Markdown # MAN function display_man { awk '/^__MAN__/,/^MAN$/ {print}' <"$0" | tail -n +2 | egrep -v '^MAN$' } function display_synopsis { awk '/^#/ && !/^#!/ { print } /^[^#]/ || /^$/ { exit }' <"$0" | cut -c3- } function which_library { local library="$1"; shift if [[ -r "$shome/vendor/projects/$library/libexec/_$library" ]]; then echo "$shome/vendor/projects/$library/libexec/_$library" elif [[ -r "$shome/libexec/_$library" ]]; then echo "$shome/libexec/_$library" elif [[ -r "$shome/.$library/libexec/_$library" ]]; then echo "$shome/.$library/libexec/_$library" else local nm_library="${library%%/*}" if [[ "$nm_library" != "$library" ]]; then local nm_right="${library##*/}" if [[ -r "$shome/vendor/projects/$nm_library/libexec/_$nm_right" ]]; then echo "$shome/vendor/projects/$nm_library/libexec/_$nm_right" elif [[ -r "$shome/.$nm_library/libexec/_$nm_right" ]]; then echo "$shome/.$nm_library/libexec/_$nm_right" fi fi fi } function require { local nm_library="$1"; shift local pth_lib="$(which_library "$nm_library")" if [[ -r "$pth_lib" ]]; then if [[ "$#" == 0 ]]; then set -- fi source "$pth_lib" "$@" else logger_warn "library $nm_library not found" fi } function parse_command_line { if [[ "$FLAGS_SUB" = "$FLAGS_TRUE" && "$@" > 0 ]]; then export POSIXLY_CORRECT=1 fi if ! FLAGS "$@"; then unset POSIXLY_CORRECT if [[ "$flags_error" = "help requested" ]]; then echo "" display_help exit 0 fi return 4 fi unset POSIXLY_CORRECT return 0 } function configure_logging { # load log4sh (disabling properties file warning) and clear the default # configuration LOG4SH_CONFIGURATION='none' require 'log4sh' log4sh_resetConfiguration # set the global logging level to INFO logger_setLevel INFO # add and configure a FileAppender that outputs to STDERR, and activate the # configuration logger_addAppender stderr appender_setType stderr FileAppender appender_file_setFile stderr STDERR appender_activateOptions stderr } function ryaml { ruby -ryaml -e 'def ps x; unless x.nil?; puts (x.class == String || x.class == Fixnum) ? x : x.to_yaml; end; end; ps ARGV[1..-1].inject(YAML.load(File.read(ARGV[0]))) {|acc, key| acc[acc.class == Array ? key.to_i : key] }' "$@" 2>&- } function random_str { echo "$(date +%s).$$.$RANDOM" } function runmany { local cpu="$1"; shift local args="$1"; shift local cmd="$1"; shift if [[ "$#" = 0 ]]; then cat else echo "$@" fi | xargs -P $cpu -n $args -- bash -c "$cmd" "" } function mark_log { local nm_mark="$1"; shift touch "$tmp_switch/wait-$nm_mark" echo $tmp_switch $nm_mark while [[ -f "$tmp_switch/wait-$nm_mark" ]]; do sleep 1 done } function marked_logger { local nm_switch="" while read -r a b; do if [[ "$a" = "$tmp_switch" ]]; then nm_switch="$b" rm "$tmp_switch/wait-$nm_switch" else if [[ -z "$nm_switch" ]]; then echo "$a $b" else echo "$nm_switch: $a $b" fi fi done } function mark_stdout { if [[ -z "${tmp_switch:-}" ]]; then tmp_switch="$(mktemp -d -t XXXXXXXXX)" fi exec 1> >(marked_logger) } function mark_stderr { if [[ -z "${tmp_switch:-}" ]]; then tmp_switch="$(mktemp -d -t XXXXXXXXX)" fi exec 2> >(marked_logger) } function mark_output { if [[ -z "${tmp_switch:-}" ]]; then tmp_switch="$(mktemp -d -t XXXXXXXXX)" fi exec 1> >(marked_logger) 2>&1 } function _main { if [[ -z "$shome" ]]; then shome="$(unset CDPATH; cd -P -- "$(dirname -- "${BASH_SOURCE}")/.." && pwd -P)" fi : ${__meat__:=x} if [[ "$__meat__" != 'x' ]]; then return 0 fi __meat__='y' require 'shflags' configure_logging } _main "$@" set -fue