#!/bin/bash eval "set -- $(getopt -o 'hwx:f:' -l help,watch,setup-file:,extract:,module-api:,api-format: -- "$@")" script= script_args=( ) watch_sources= action=setup api_format=markdown while (( $# > 0 )); do opt="$1"; shift case "$opt" in -w|--watch) watch_sources=true;; -x|--extract) action=extract;; -f|--setup-file) case "$1" in git://*|https://*.git) hash=( $(sha256sum <<< "$1") ) script="$HOME/.setup/git-clone/${1##*/}-$hash" if [ ! -e "$script" ]; then mkdir -p "$script" git clone "$1" "$script" fi script+=/Setup;; *) script="$(realpath "$1" || echo "$1")";; esac program="${script##*/}" shift ;; -h|--help) action=show_help;; --module-api) action="show_api $1"; shift;; --api-format) api_format="$1"; shift;; --) :;; *) script_args+=( "$opt" );; esac done shopt -s extglob if [ "${SETUP_INSTALL_DIR+x}" == '' ]; then case "$0" in */*) SETUP_INSTALL_DIR=${0/%\/*([^\/])//..};; *) echo "\$0: $0" >&2 SETUP_INSTALL_DIR="$(which "$0")"; SETUP_INSTALL_DIR="${SETUP_INSTALL_DIR/%\/*//..}";; esac fi case "$action" in show_help) cat <&2 exit 1 fi else cd "${script%/*}" fi source "$SETUP_INSTALL_DIR/lib/setup.shl" "${script_args[@]}" function init-setup() { Setup.use base Setup.load "/$program" -- setup if [ "$action" == extract ]; then Setup.extract "${script_args[@]}" fi } function show-failed() { local red=$'\033[31m' local yellow=$'\033[33m' local def=$'\033[m' local -a errcmd tmpa local IFSBAK="$IFS" for err in "${SETUP_FAILED[@]}"; do errcmd=( "${SETUP_STATE[$err.cmd]}" ) Setup.getArr tmpa "$err.targets" errcmd+=( "${tmpa[@]}" ) Setup.getArr tmpa "$err.full-args" errcmd+=( "${tmpa[@]#*:}" ) printf "${red}Error[%s]: %s${def}\n" "${SETUP_STATE[$err.errno]}" "${errcmd[*]}" IFS=$'\n' printf "${red}|${yellow} %s${def}\n" $(< "$SETUP_TMPDIR/log/$err.log") IFS="$IFSBAK" done >&2 if (( ${#SETUP_FAILED[@]} == 0 )); then :; else return 1; fi } if [ "$watch_sources" ]; then declare -A tracked_files=( ) inotify_pid= mkfifo "$SETUP_TMPDIR/inotify-out" SETUP_ATEXIT_COMMANDS+=( 'if [ -n "$inotify_pid" ]; then kill $inotify_pid; fi' ) function track-new-files() { local arg added=0 for arg in "${SETUP_SOURCE_FILES[@]}"; do if [ "${tracked_files[$arg]:+x}" == '' ]; then echo "Adding $arg to the watch list" tracked_files[$arg]=1 ((added++)) fi done if (( added > 0 )); then if [ -n "$inotify_pid" ]; then kill $inotify_pid; fi inotifywait -q -e close_write,delete_self -m "${!tracked_files[@]}" > "$SETUP_TMPDIR/inotify-out" & inotify_pid="$!" fi } function rebuild-saved-files() { while read root ev file; do case ",$ev," in *,CLOSE_WRITE,*|*,DELETE_SELF,*) echo "$root was updated" teardown "$root" SETUP_FAILED=( ) setup track-new-files show-failed && echo "Compilation finished at $(date)" ;; esac done < "$SETUP_TMPDIR/inotify-out" } fi init-setup if [ "$watch_sources" ]; then track-new-files rebuild-saved-files else show-failed && Setup.isCycleFree fi ;; esac