Files
q-tools/rc
2017-03-05 09:34:45 +02:00

73 lines
2.1 KiB
Plaintext
Executable File

# source me to enable tools
TOOLSPATH=$( readlink -f $( dirname "$BASH_SOURCE" ) )
PATH=$PATH:"$TOOLSPATH"/bin
[[ "$1" = "-f" ]] && PATH="$TOOLSPATH"/bin:$PATH
# remove duplicate path
PATH=$( echo $PATH | awk -F: '{for (i=1;i<=NF;i++) { if ( !x[$i]++ ) printf("%s:",$i); }}' | sed 's,:\+$,,g' )
export PATH
. "$TOOLSPATH"/qcd_function
[[ -f "$TOOLSPATH"/tsv/tsvkit.sh ]] && . "$TOOLSPATH"/tsv/tsvkit.sh &>/dev/null
function _self_update() {
touch "$TOOLSPATH"/.lastupdate
pushd "$TOOLSPATH" > /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 "$TOOLSPATH"/ncsv ]] && {
pushd "$TOOLSPATH"/ncsv > /dev/null
[[ "$1" = "-u" ]] && {
hg pull -u
} || {
timeout 10 hg pull -u > /dev/null 2>&1
}
ln -sfT ../ncsv/ncsv/ncsv.py "$TOOLSPATH"/bin/ncsv
popd > /dev/null
} || true
}
function _check_ncsv() {
[[ -d "$TOOLSPATH"/ncsv ]] || {
hg clone https://bitbucket.org/MoonQ/ncsv "$TOOLSPATH"/ncsv
ln -sfT ../ncsv/ncsv/ncsv.py "$TOOLSPATH"/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 "$TOOLSPATH" ]] || TOOLS_TO_UPDATE=0
[[ -w "$TOOLSPATH"/.hg ]] || TOOLS_TO_UPDATE=0
[[ -t 0 ]] || TOOLS_TO_UPDATE=0
[[ -t 1 ]] || TOOLS_TO_UPDATE=0
[[ -t 2 ]] || TOOLS_TO_UPDATE=0
[[ -f "$TOOLSPATH"/.lastupdate ]] || touch -t 0101010101 "$TOOLSPATH"/.lastupdate 2> /dev/null
[[ -w "$TOOLSPATH"/.lastupdate ]] || TOOLS_TO_UPDATE=0
if [ $TOOLS_TO_UPDATE -eq 1 ]; then
TOOLS_LASTUPDATE=$( "$TOOLSPATH"/files/file-age "$TOOLSPATH"/.lastupdate d )
[[ "$TOOLS_LASTUPDATE" -gt 7 ]] && _self_update
unset TOOLS_LASTUPDATE
fi
unset TOOLS_TO_UPDATE
unset -f _self_update