#!/bin/bash : ${__meat__:=x} if [[ "$__meat__" != 'x' ]]; then return 0 fi __meat__='y' 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/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/.$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" "$@" 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 if [[ "$#" = 0 ]]; then cat else echo "$@" fi | xargs -P $cpu -n $args -- bash -c "$*" "" } function _main { if [[ -z "$shome" ]]; then shome="$(unset CDPATH; cd -P -- "$(dirname -- "${BASH_SOURCE}")/.." && pwd -P)" fi require 'shflags' configure_logging PATH="$shome/bin:$PATH" } _main "$@" set -fue