# source me to enable tools case $OSTYPE in darwin*) QTOOLSPATH=$( realpath $( dirname "$BASH_SOURCE" ) ) ;; *) QTOOLSPATH=$( readlink -f $( dirname "$BASH_SOURCE" ) ) ;; esac PATH=$PATH:"$QTOOLSPATH"/bin [[ "$1" = "-f" ]] && PATH="$QTOOLSPATH"/bin:$PATH . "$QTOOLSPATH"/qcd_function path_remove_duplicates [[ -f "$QTOOLSPATH"/tsv/tsvkit.sh ]] && . "$QTOOLSPATH"/tsv/tsvkit.sh &>/dev/null function _q-tools-reload { # reload self [[ -z "$QTOOLSPATH" ]] && return source "$QTOOLSPATH"/rc } function _q-tools-update { # update self [[ -z "$QTOOLSPATH" ]] && return [[ -w "$QTOOLSPATH" ]] || return "$QTOOLSPATH"/rc -u _q-tools-reload } function _q-tools-update-binary { # update self [[ -z "$QTOOLSPATH" ]] && return [[ -w "$QTOOLSPATH" ]] || return [[ -f "$QTOOLSPATH"/.hg_archival.txt ]] || return local foo echo "Updating with binary deletes the existing folder! sure? y/N" read foo [[ "$foo" = y ]] || return rm -rf "$QTOOLSPATH" mkdir -p "$QTOOLSPATH" curl https://bitbucket.org/MoonQ/tools/get/tip.tar.gz | \ tar xz --strip 1 -C "$QTOOLSPATH" _q-tools-reload } function _self_update() { touch "$QTOOLSPATH"/.lastupdate pushd "$QTOOLSPATH" > /dev/null [[ "$1" = "-u" ]] && { hg pull -u https://bitbucket.org/MoonQ/tools } || { timeout 10 hg pull -u https://bitbucket.org/MoonQ/tools > /dev/null 2>&1 } popd > /dev/null # ncsv update [[ -d "$QTOOLSPATH"/ncsv ]] && { pushd "$QTOOLSPATH"/ncsv > /dev/null [[ "$1" = "-u" ]] && { hg pull -u } || { timeout 10 hg pull -u > /dev/null 2>&1 } ln -sfT ../ncsv/ncsv/ncsv.py "$QTOOLSPATH"/bin/ncsv popd > /dev/null } || true } function _check_ncsv() { [[ -d "$QTOOLSPATH"/ncsv ]] || { hg clone https://bitbucket.org/MoonQ/ncsv "$QTOOLSPATH"/ncsv ln -sfT ../ncsv/ncsv/ncsv.py "$QTOOLSPATH"/bin/ncsv } } # install ncsv if necessary which ncsv > /dev/null 2>&1 || _check_ncsv > /dev/null 2>&1 unset -f _check_ncsv [[ "$1" = "-u" ]] && { _self_update -u unset -f _self_update } # Update self weekly, # if folder writable, if repository, if an interactive terminal, if not updated the last week TOOLS_TO_UPDATE=1 [[ -w "$QTOOLSPATH" ]] || TOOLS_TO_UPDATE=0 [[ -w "$QTOOLSPATH"/.hg ]] || TOOLS_TO_UPDATE=0 [[ -t 0 ]] || TOOLS_TO_UPDATE=0 [[ -t 1 ]] || TOOLS_TO_UPDATE=0 [[ -t 2 ]] || TOOLS_TO_UPDATE=0 [[ -f "$QTOOLSPATH"/.lastupdate ]] || touch -t 0101010101 "$QTOOLSPATH"/.lastupdate 2> /dev/null [[ -w "$QTOOLSPATH"/.lastupdate ]] || TOOLS_TO_UPDATE=0 if [ $TOOLS_TO_UPDATE -eq 1 ]; then TOOLS_LASTUPDATE=$( "$QTOOLSPATH"/files/file-age "$QTOOLSPATH"/.lastupdate d ) [[ "$TOOLS_LASTUPDATE" -gt 7 ]] && _self_update unset TOOLS_LASTUPDATE fi unset TOOLS_TO_UPDATE unset -f _self_update