A little utility script to run the correaltion and publish the results

# The collection of sh calls that gather and publish correaltion data
# If an arguement is supplied - it is assumed to be the PLOTDIR on the external webserver
CORREL_THIS_DIR="$( cd "$( dirname "$BASH_SOURCE" )" && pwd )"
EXTERNAL_PUBLISH_LOCATION="/home/dynamo/a/tgrogers/website/gpgpu-sim-plots/"
EXTERNAL_SERVER="tgrogers@dynamo.ecn.purdue.edu"

$CORREL_THIS_DIR/../job_launching/get_stats.py -R -K -k -B `cat $CORREL_THIS_DIR/../job_launching/apps/all-apps.list` -C TITANX_P102-2B,TITANX_P102-L1ON-2B,P100_HBM-2B > stats-$$.csv
$CORREL_THIS_DIR/../../run_hw/get_hw_data.sh && rm -rf $CORREL_THIS_DIR/correl-html
$CORREL_THIS_DIR/plot-correlation.py -c stats-$$.csv -H $CORREL_THIS_DIR/../../run_hw/

if [ $# -eq 2 ] ; then
    PLOTDIR=$1
    BUILDNUM=$2
    ssh $EXTERNAL_SERVER mkdir -p $EXTERNAL_PUBLISH_LOCATION/$PLOTDIR/$BUILDNUM
    scp  $CORREL_THIS_DIR/correl-html/* $EXTERNAL_SERVER:$EXTERNAL_PUBLISH_LOCATION/$PLOTDIR/$BUILDNUM
    ssh $EXTERNAL_SERVER "cd $EXTERNAL_PUBLISH_LOCATION/$PLOTDIR && ln -s -f $BUILDNUM latest"
fi

error: pathspec 'util/plotting/correlate_and_publish.sh' did not match any file(s) known to git


Add base image

Add base image

#!/bin/bash
log() {
    echo "$@" >&2
}

if [[ ! -x /sync.sh ]]; then
    log '/sync.sh not found'
    exit 1
fi

. /sync.sh

error: pathspec 'base/entry.sh' did not match any file(s) known to git


INIT tree command with -i flag for ignoring files to display

#!/usr/bin/env bash

## like tree function but has ignore capabilities using '-i' flag
## funcitons uses grep -v

while getopts i: option
do
    case "${option}" in
	i) IGNORE=${OPTARG};;
    esac
done

if [[ $IGNORE == "" ]]; then
    IGNORE="^$"
else
    IGNORE=`echo $IGNORE | sed 's/^/^.\//; s/\/$//'`
    echo "ignoring="$IGNORE
fi

find . | grep -v $IGNORE | awk -v pwd=$PWD 'BEGIN {print pwd} \
                                          !/^\.$/ {for (i=1; i<NF; i++) { \
                                                       printf("%4s", "|") \
                                                       } \
                                                   print "-- "$NF \
                                                  }' FS='/'

error: pathspec 'bash/tree2.sh' did not match any file(s) known to git


Modified the nightly build script with new build steps using make

remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/NerdWalletOSS/Q.git/'


creating script to run specs

creating script to run specs

error: pathspec 'examples/test.sh' did not match any file(s) known to git


adds proper projects support

adds proper projects support

remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/psd401/psd-osx-bootstrap.git/'


script to build yardoc documentation

script to build yardoc documentation

#!/bin/bash

# --incremental \
# --use-cache .yardoc_cache \
yardoc \
  bin/rsence \
  lib/rsence.rb \
  lib/**/*.rb \
  plugins/**/*.rb \
  --no-private \
  --exclude lib/util/ruby19_fixes.rb \
  --exclude lib/http/rackup.rb \
  - conf/default_*.yaml INSTALL.rdoc LICENSE.txt VERSION \
    docs/*.rdoc

error: pathspec 'bin/yardoc.sh' did not match any file(s) known to git


Create compile_android_aosp_build-buildtype.sh

#!/bin/bash
export USE_CCACHE=1
export CCACHE_DIR=ccache
prebuilts/misc/linux-x86/ccache/ccache -M 50G
#Measure execution time
start_time=`date +%s`
#Building the system
source build/envsetup.sh
lunch $1
make -j16 VERBOSE=1 | tee build.log
end_time=`date +%s`
echo execution time was `expr $end_time - $start_time` s.

error: pathspec 'code/compile_android_aosp_build-buildtype.sh' did not match any file(s) known to git


Create backup_script.sh

Create backup_script.sh

#!/bin/bash
# copy only fastq files, sample sheets and config files
SRC=/cygdrive/D/Illumina/MiSeqOutput
SRC2=/cygdrive/d/Illumina/MiSeqSampleSheets
SRC3=/cygdrive/d/Illumina/MiSeqRunParameters
DST=virologysrv04.uzh.ch:/data/MiSeq

LIST=$(ls $SRC); for i in $LIST; do

# get sample sheet name from runParamter.xml file and save runParameter.xml file with the sample sheet name in SRC3
SAMPLESHEET=$(cat ${SRC}/${i}/runParameters.xml | grep -A 1 ReagentKitRFIDTag | grep SerialNumber | sed 's/^.*<SerialNumber>//' | sed 's/<\/SerialNumber>//')
cp ${SRC}/${i}/runParameters.xml ${SRC3}/${SAMPLESHEET}.xml

done

echo 'syncing fastq files'
echo '' >> backup_fq.log
echo '--------------------' >> backup_fq.log
date >> backup_fq.log
rsync -av --stats --chmod=ug+rwx -p --include="*/" --include="*/Data/Intensities/BaseCalls/*" --include="*.config.xml" --exclude="*" -m $SRC $DST &>> backup_fq.log
echo 'synced'

echo 'syncing sample sheets'
rsync -av --stats --chmod=ug+rwx -p $SRC2 $DST &>> backup_fq.log
echo 'backed up' >> backup_fq.log
date >> backup_fq.log
echo '--------------------' >> backup_fq.log
echo '' >> backup_fq.log

echo 'syncing run parameters'
rsync -av --stats --chmod=ug+rwx -p $SRC3 $DST &>> backup_fq.log
echo 'backed up' >> backup_fq.log
date >> backup_fq.log
echo '--------------------' >> backup_fq.log
echo '' >> backup_fq.log

error: pathspec 'backup_script.sh' did not match any file(s) known to git


git push --set-upstream helper

git push --set-upstream helper

remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/unicell/dotfiles.git/'


[gnutls] unconditionally disable assembler in gmp (#833)

Signed-off-by: Nikos Mavrogiannopoulos <df6e1a462b5068e7cd37b5ebdba5d9f6b4da01fc@redhat.com>

#!/bin/bash -eu
# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################

export DEPS_PATH=$SRC/deps
export PKG_CONFIG_PATH=$DEPS_PATH/lib/pkgconfig
export CPPFLAGS="-I$DEPS_PATH/include"
export LDFLAGS="-L$DEPS_PATH/lib"
export GNULIB_SRCDIR=$SRC/gnulib

cd $SRC/libunistring
./autogen.sh
ASAN_OPTIONS=detect_leaks=0 \
  ./configure --enable-static --disable-shared --prefix=$DEPS_PATH
make -j$(nproc)
make install

cd $SRC/libidn2
./bootstrap
ASAN_OPTIONS=detect_leaks=0 \
  ./configure --enable-static --disable-shared --disable-doc --disable-gcc-warnings --prefix=$DEPS_PATH
make -j$(nproc)
make install

# always disable assembly in GMP to avoid issues due to SIGILL
#   https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=3119
#   https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=3159
GMP_CONFIGURE_FLAGS="--disable-assembly --disable-fat"

cd $SRC/gmp
bash .bootstrap
ASAN_OPTIONS=detect_leaks=0 \
  ./configure --disable-shared --prefix=$DEPS_PATH $GMP_CONFIGURE_FLAGS
make -j$(nproc)
make install

cd $SRC/libtasn1
CFGFLAGS="--disable-gcc-warnings --disable-gtk-doc --disable-gtk-doc-pdf --disable-doc \
  --disable-shared --enable-static --prefix=$DEPS_PATH" \
  make bootstrap
make -j$(nproc)
make install

NETTLE_CONFIGURE_FLAGS=""
if [[ $CFLAGS = *sanitize=memory* ]]; then
  NETTLE_CONFIGURE_FLAGS="--disable-assembler --disable-fat"
fi
cd $SRC/nettle
bash .bootstrap
ASAN_OPTIONS=detect_leaks=0 \
  ./configure --enable-static --disable-shared --disable-documentation --prefix=$DEPS_PATH $NETTLE_CONFIGURE_FLAGS
( make -j$(nproc) || make -j$(nproc) ) && make install
if test $? != 0; then
  echo "Failed to compile nettle"
  exit 1
fi

GNUTLS_CONFIGURE_FLAGS=""
if [[ $CFLAGS = *sanitize=memory* ]]; then
  GNUTLS_CONFIGURE_FLAGS="--disable-hardware-acceleration"
fi
cd $SRC/gnutls
make autoreconf
ASAN_OPTIONS=detect_leaks=0 LIBS="-lunistring" CXXFLAGS="$CXXFLAGS -L$DEPS_PATH/lib" \
  ./configure --enable-fuzzer-target --disable-gcc-warnings --enable-static --disable-shared --disable-doc --disable-tests \
    --disable-tools --disable-cxx --disable-maintainer-mode --disable-libdane --without-p11-kit $GNUTLS_CONFIGURE_FLAGS

# Do not use the syscall interface for randomness in oss-fuzz, it seems
# to confuse memory sanitizer.
sed -i 's|include <sys/syscall.h>|include <sys/syscall.h>\n#undef SYS_getrandom|' lib/nettle/sysrng-linux.c

make -j$(nproc) -C gl
make -j$(nproc) -C lib

cd fuzz
make oss-fuzz
find . -name '*_fuzzer' -exec cp -v '{}' $OUT ';'
find . -name '*_fuzzer.dict' -exec cp -v '{}' $OUT ';'
find . -name '*_fuzzer.options' -exec cp -v '{}' $OUT ';'

for dir in *_fuzzer.in; do
    fuzzer=$(basename $dir .in)
    zip -rj "$OUT/${fuzzer}_seed_corpus.zip" "${dir}/"
done

#!/bin/bash -eu
# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################

export DEPS_PATH=$SRC/deps
export PKG_CONFIG_PATH=$DEPS_PATH/lib/pkgconfig
export CPPFLAGS="-I$DEPS_PATH/include"
export LDFLAGS="-L$DEPS_PATH/lib"
export GNULIB_SRCDIR=$SRC/gnulib

cd $SRC/libunistring
./autogen.sh
ASAN_OPTIONS=detect_leaks=0 \
  ./configure --enable-static --disable-shared --prefix=$DEPS_PATH
make -j$(nproc)
make install

cd $SRC/libidn2
./bootstrap
ASAN_OPTIONS=detect_leaks=0 \
  ./configure --enable-static --disable-shared --disable-doc --disable-gcc-warnings --prefix=$DEPS_PATH
make -j$(nproc)
make install

GMP_CONFIGURE_FLAGS=""
if [[ $CFLAGS = *sanitize=memory* ]]; then
  GMP_CONFIGURE_FLAGS="--disable-assembly --disable-fat"
fi
cd $SRC/gmp
bash .bootstrap
ASAN_OPTIONS=detect_leaks=0 \
  ./configure --disable-shared --prefix=$DEPS_PATH $GMP_CONFIGURE_FLAGS
make -j$(nproc)
make install

cd $SRC/libtasn1
CFGFLAGS="--disable-gcc-warnings --disable-gtk-doc --disable-gtk-doc-pdf --disable-doc \
  --disable-shared --enable-static --prefix=$DEPS_PATH" \
  make bootstrap
make -j$(nproc)
make install

NETTLE_CONFIGURE_FLAGS=""
if [[ $CFLAGS = *sanitize=memory* ]]; then
  NETTLE_CONFIGURE_FLAGS="--disable-assembler --disable-fat"
fi
cd $SRC/nettle
bash .bootstrap
ASAN_OPTIONS=detect_leaks=0 \
  ./configure --enable-static --disable-shared --disable-documentation --prefix=$DEPS_PATH $NETTLE_CONFIGURE_FLAGS
( make -j$(nproc) || make -j$(nproc) ) && make install
if test $? != 0; then
  echo "Failed to compile nettle"
  exit 1
fi

GNUTLS_CONFIGURE_FLAGS=""
if [[ $CFLAGS = *sanitize=memory* ]]; then
  GNUTLS_CONFIGURE_FLAGS="--disable-hardware-acceleration"
fi
cd $SRC/gnutls
make autoreconf
ASAN_OPTIONS=detect_leaks=0 LIBS="-lunistring" CXXFLAGS="$CXXFLAGS -L$DEPS_PATH/lib" \
  ./configure --enable-fuzzer-target --disable-gcc-warnings --enable-static --disable-shared --disable-doc --disable-tests \
    --disable-tools --disable-cxx --disable-maintainer-mode --disable-libdane --without-p11-kit $GNUTLS_CONFIGURE_FLAGS

# Do not use the syscall interface for randomness in oss-fuzz, it seems
# to confuse memory sanitizer.
sed -i 's|include <sys/syscall.h>|include <sys/syscall.h>\n#undef SYS_getrandom|' lib/nettle/sysrng-linux.c

make -j$(nproc) -C gl
make -j$(nproc) -C lib

cd fuzz
make oss-fuzz
find . -name '*_fuzzer' -exec cp -v '{}' $OUT ';'
find . -name '*_fuzzer.dict' -exec cp -v '{}' $OUT ';'
find . -name '*_fuzzer.options' -exec cp -v '{}' $OUT ';'

for dir in *_fuzzer.in; do
    fuzzer=$(basename $dir .in)
    zip -rj "$OUT/${fuzzer}_seed_corpus.zip" "${dir}/"
done


This should actually be run from project root.

#!/bin/sh
git archive --format zip --output "output.zip" grails-2.5.x  -0
zip -d output.zip "*.tgz"

error: pathspec 'package-src.sh' did not match any file(s) known to git


added setuptools recipe

added setuptools recipe

#!/bin/bash

VERSION_setuptools=0.6c11
URL_setuptools=http://pypi.python.org/packages/source/s/setuptools/setuptools-$VERSION_setuptools.tar.gz
DEPS_setuptools=(python)
MD5_setuptools=7df2a529a074f613b509fb44feefe74e
BUILD_setuptools=$BUILD_PATH/setuptools/$(get_directory $URL_setuptools)
RECIPE_setuptools=$RECIPES_PATH/setuptools

function prebuild_setuptools() {
	true
}

function build_setuptools() {

	if [ -d "$BUILD_PATH/python-install/lib/python2.7/site-packages/setuptools" ]; then
		return
	fi

	cd $BUILD_setuptools

	push_arm
	# build setuptools for android
        try $BUILD_PATH/hostpython/Python-2.7.2/hostpython setup.py install -O2 --root=$BUILD_PATH/python-install --install-lib=lib/python2.7/site-packages

	# build setuptools for python-for-android
        try $BUILD_PATH/hostpython/Python-2.7.2/hostpython setup.py install -O2 --root=$BUILD_PATH/hostpython/Python-2.7.2 --install-lib=Lib/site-packages
	pop_arm
}

function postbuild_setuptools() {
	true
}

error: pathspec 'recipes/setuptools/recipe.sh' did not match any file(s) known to git


Test exit code with error

Test exit code with error

remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/Altemista/starter-golang.git/'


Create client_setup.sh

Create client_setup.sh

sudo su
sudo curl -sSL https://raw.githubusercontent.com/genx7up/dotfiles/master/misc/volt.sh -o /usr/local/bin/volt.sh
echo 'function volt() { bash /usr/local/bin/volt.sh $@ && source /usr/local/bin/volt.sh load; }' >> /etc/profile
echo 'if [[ -f /usr/local/bin/volt.sh ]]; then source /usr/local/bin/volt.sh load; fi' >> /etc/profile

error: pathspec 'misc/client_setup.sh' did not match any file(s) known to git


roo: add Javascript install script

roo: add Javascript install script

#!/bin/env bash
echo "Install nodejs and ruby..."
yaourt -S --needed nodejs ruby

echo "Install npm modules..."
npm install -g yo karma bower grunt-cli # yeoman
npm install -g generator-angular generator-webapp

error: pathspec 'root/js_install.sh' did not match any file(s) known to git


Improve formatting for add_project()

Improve formatting for add_project()

remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/jakemayfield/bash-scripts-for-linux-ubuntu-apache-server-management.git/'


Create tini.sh

Create tini.sh

#!/usr/bin/env bash
### every exit != 0 fails the script
set -e

echo "Install Tini"
wget --quiet https://github.com/krallin/tini/releases/download/v0.10.0/tini && \
    echo "1361527f39190a7338a0b434bd8c88ff7233ce7b9a4876f3315c22fce7eca1b0 *tini" | sha256sum -c - && \
    mv tini /usr/local/bin/tini && \
    chmod +x /usr/local/bin/tini

error: pathspec 'DugongCMD/.config/install/tini.sh' did not match any file(s) known to git


chore: add project-all script

chore: add project-all script

#!/bin/bash

git holo project emergence-skeleton \
    --fetch \
    --ref=origin/releases/v3 \
    --commit-to=emergence/skeleton/v3

git holo project emergence-vfs-site \
    --fetch \
    --ref=origin/releases/v3 \
    --commit-to=emergence/vfs-site/v3

error: pathspec 'project-all.sh' did not match any file(s) known to git


Fix EPEL repository enablement

Fix EPEL repository enablement

Ansible package is now included in centos-liberty repo, not in in EPEL
so adding EPEL repo if ansible rpm is not available is not valid anymore.

Instead add EPEL for CentOS OS only.

remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/markllama/openshift-on-openstack.git/'


Add script to load database dump

Add script to load database dump

#!/bin/sh

mongorestore --drop data/dump/

error: pathspec 'restore_db.sh' did not match any file(s) known to git


Added Encrypt.me to list of apps to install for macOS

remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/kirktrue/kpm.git/'


Add app-prefs-sync.sh

Add app-prefs-sync.sh

#!/bin/bash

ln -sf ~/Dropbox/Library/Preferences/com.googlecode.iterm2.plist ~/Library/Preferences/.
# dotfiles
PREFS=(
  com.googlecode.iterm2.plist
)

echo "Create symbolic link:"
for pref in ${PREFS[@]}
do
    if [ ! -L $HOME/Library/Preferences/$pref ]; then
        echo $HOME/Library/Preferences/$pref
        ln -sf $HOME/Dropbox/Library/Preferences/$pref $HOME/Library/Preferences/.
    fi
done

error: pathspec 'app-prefs-sync.sh' did not match any file(s) known to git


updated tachyon stop script

updated tachyon stop script

#!/bin/bash

zookeeper-server-stop ~/pipeline/config/kafka/zookeeper.properties
kafka-server-stop ~/pipeline/config/kafka/server.properties
schema-registry-stop ~/pipeline/config/schema-registry/schema-registry.properties
kafka-rest-stop ~/pipeline/config/kafka-rest/kafka-rest.properties

~/incubator-zeppelin/bin/zeppelin-daemon.sh stop
~/spark-1.4.0-bin-hadoop2.6/sbin/stop-master.sh --webui-port 6060 &
~/spark-1.4.0-bin-hadoop2.6/sbin/stop-slave.sh --webui-port 6061 spark://$HOSTNAME:7077 &
~/spark-1.4.0-bin-hadoop2.6/sbin/stop-thriftserver.sh &
~/tachyon-0.6.4/bin/tachyon-stop.sh
# TODO:  stop spark-notebook?

service neo4j-service stop
service redis stop
service cassandra stop
service elasticsearch stop
service apache2 stop
service ssh stop

#!/bin/bash

zookeeper-server-stop ~/pipeline/config/kafka/zookeeper.properties
kafka-server-stop ~/pipeline/config/kafka/server.properties
schema-registry-stop ~/pipeline/config/schema-registry/schema-registry.properties
kafka-rest-stop ~/pipeline/config/kafka-rest/kafka-rest.properties

~/incubator-zeppelin/bin/zeppelin-daemon.sh stop
~/spark-1.4.0-bin-hadoop2.6/sbin/stop-master.sh --webui-port 6060 &
~/spark-1.4.0-bin-hadoop2.6/sbin/stop-slave.sh --webui-port 6061 spark://$HOSTNAME:7077 &
~/spark-1.4.0-bin-hadoop2.6/sbin/stop-thriftserver.sh &
~/tachyon-1.6.4/bin/tachyon-stop.sh
# TODO:  stop spark-notebook?

service neo4j-service stop
service redis stop
service cassandra stop
service elasticsearch stop
service apache2 stop
service ssh stop


Create compileDebug.bash

Create compileDebug.bash

#!/bin/bash

gcc -DQUICK_LOG=0 -c log.c -o log.o
gcc -DQUICK_LOG=0 -c testLog.c -o testLog.o

gcc -DQUICK_LOG=0 testLog.o log.o -o testLogDebug

error: pathspec 'LogUtility/compileDebug.bash' did not match any file(s) known to git


修改git命名失败后退出的问题

修改git命名失败后退出的问题

#!/bin/bash

set -e

{ # this ensures the entire script is downloaded #

nvm_has() {
  type "$1" > /dev/null 2>&1
}

if [ -z "$NVM_DIR" ]; then
  NVM_DIR="$HOME/.tnvm"
fi

#
# Outputs the location to NVM depending on:
# * The availability of $NVM_SOURCE
# * The method used ("script" or "git" in the script, defaults to "git")
# NVM_SOURCE always takes precedence unless the method is "script-nvm-exec"
#
nvm_source() {
  local NVM_METHOD
  NVM_METHOD="$1"
  local NVM_SOURCE_URL
  NVM_SOURCE_URL="$NVM_SOURCE"
  if [ "_$NVM_METHOD" = "_script-nvm-exec" ]; then
    NVM_SOURCE_URL="https://raw.githubusercontent.com/yjhjstz/tnvm/master/nvm-exec"
  elif [ -z "$NVM_SOURCE_URL" ]; then
    if [ "_$NVM_METHOD" = "_script" ]; then
      NVM_SOURCE_URL="https://raw.githubusercontent.com/yjhjstz/tnvm/master/nvm.sh"
    elif [ "_$NVM_METHOD" = "_git" ] || [ -z "$NVM_METHOD" ]; then
      NVM_SOURCE_URL="https://github.com/yjhjstz/tnvm.git"
    else
      echo >&2 "Unexpected value \"$NVM_METHOD\" for \$NVM_METHOD"
      return 1
    fi
  fi
  echo "$NVM_SOURCE_URL"
}

nvm_download() {
  if nvm_has "curl"; then
    curl -q $*
  elif nvm_has "wget"; then
    # Emulate curl with wget
    ARGS=$(echo "$*" | command sed -e 's/--progress-bar /--progress=bar /' \
                           -e 's/-L //' \
                           -e 's/-I /--server-response /' \
                           -e 's/-s /-q /' \
                           -e 's/-o /-O /' \
                           -e 's/-C - /-c /')
    wget $ARGS
  fi
}

install_nvm_from_git() {
  if [ -d "$NVM_DIR/.git" ]; then
    echo "=> tnvm is already installed in $NVM_DIR, trying to update using git"
    printf "\r=> "
    cd "$NVM_DIR" && (command git fetch 2> /dev/null || {
      echo >&2 "Failed to update tnvm, run 'git fetch' in $NVM_DIR yourself." && exit 1
    })
  else
    # Cloning to $NVM_DIR
    echo "=> Downloading tnvm from git to '$NVM_DIR'"
    printf "\r=> "
    mkdir -p "$NVM_DIR"
    command git clone "$(nvm_source git)" "$NVM_DIR"
  fi
  cd "$NVM_DIR" && command git checkout --quiet master
  return
}

install_nvm_as_script() {
  local NVM_SOURCE_LOCAL
  NVM_SOURCE_LOCAL=$(nvm_source script)
  local NVM_EXEC_SOURCE
  NVM_EXEC_SOURCE=$(nvm_source script-nvm-exec)

  # Downloading to $NVM_DIR
  mkdir -p "$NVM_DIR"
  if [ -d "$NVM_DIR/nvm.sh" ]; then
    echo "=> tnvm is already installed in $NVM_DIR, trying to update the script"
  else
    echo "=> Downloading tnvm as script to '$NVM_DIR'"
  fi
  nvm_download -s "$NVM_SOURCE_LOCAL" -o "$NVM_DIR/nvm.sh" || {
    echo >&2 "Failed to download '$NVM_SOURCE_LOCAL'"
    return 1
  }
  nvm_download -s "$NVM_EXEC_SOURCE" -o "$NVM_DIR/nvm-exec" || {
    echo >&2 "Failed to download '$NVM_EXEC_SOURCE'"
    return 2
  }
  chmod a+x "$NVM_DIR/nvm-exec" || {
    echo >&2 "Failed to mark '$NVM_DIR/nvm-exec' as executable"
    return 3
  }
}

#
# Detect profile file if not specified as environment variable
# (eg: PROFILE=~/.myprofile)
# The echo'ed path is guaranteed to be an existing file
# Otherwise, an empty string is returned
#
nvm_detect_profile() {
  if [ -f "$PROFILE" ]; then
    echo "$PROFILE"
  elif [ -f "$HOME/.bashrc" ]; then
    echo "$HOME/.bashrc"
  elif [ -f "$HOME/.bash_profile" ]; then
    echo "$HOME/.bash_profile"
  elif [ -f "$HOME/.zshrc" ]; then
    echo "$HOME/.zshrc"
  elif [ -f "$HOME/.profile" ]; then
    echo "$HOME/.profile"
  fi
}

#
# Check whether the user has any globally-installed npm modules in their system
# Node, and warn them if so.
#
nvm_check_global_modules() {
  command -v npm >/dev/null 2>&1 || return 0

  local NPM_VERSION
  NPM_VERSION="$(npm --version)"
  NPM_VERSION="${NPM_VERSION:--1}"
  [ "${NPM_VERSION%%[!-0-9]*}" -gt 0 ] || return 0

  local NPM_GLOBAL_MODULES
  NPM_GLOBAL_MODULES="$(
    npm list -g --depth=0 |
    sed '/ npm@/d' |
    sed '/ (empty)$/d'
  )"

  local MODULE_COUNT
  MODULE_COUNT="$(
    printf %s\\n "$NPM_GLOBAL_MODULES" |
    sed -ne '1!p' |                             # Remove the first line
    wc -l | tr -d ' '                           # Count entries
  )"

  if [ $MODULE_COUNT -ne 0 ]; then
    cat <<-'END_MESSAGE'
	=> You currently have modules installed globally with `npm`. These will no
	=> longer be linked to the active version of Node when you install a new node
	=> with `nvm`; and they may (depending on how you construct your `$PATH`)
	=> override the binaries of modules installed with `nvm`:

	END_MESSAGE
    printf %s\\n "$NPM_GLOBAL_MODULES"
    cat <<-'END_MESSAGE'

	=> If you wish to uninstall them at a later point (or re-install them under your
	=> `nvm` Nodes), you can remove them from the system Node as follows:

	     $ nvm use system
	     $ npm uninstall -g a_module

	END_MESSAGE
  fi
}

nvm_do_install() {
  if [ -z "$METHOD" ]; then
    # Autodetect install method
    if nvm_has "git"; then
      install_nvm_from_git
    elif nvm_has "nvm_download"; then
      install_nvm_as_script
    else
      echo >&2 "You need git, curl, or wget to install nvm"
      exit 1
    fi
  elif [ "~$METHOD" = "~git" ]; then
    if ! nvm_has "git"; then
      echo >&2 "You need git to install nvm"
      exit 1
    fi
    install_nvm_from_git
  elif [ "~$METHOD" = "~script" ]; then
    if ! nvm_has "nvm_download"; then
      echo >&2 "You need curl or wget to install nvm"
      exit 1
    fi
    install_nvm_as_script
  fi

  echo

  local NVM_PROFILE
  NVM_PROFILE=$(nvm_detect_profile)

  SOURCE_STR="\nexport NVM_DIR=\"$NVM_DIR\"\n[ -s \"\$NVM_DIR/nvm.sh\" ] && . \"\$NVM_DIR/nvm.sh\"  # This loads nvm"

  if [ -z "$NVM_PROFILE" ] ; then
    echo "=> Profile not found. Tried $NVM_PROFILE (as defined in \$PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile."
    echo "=> Create one of them and run this script again"
    echo "=> Create it (touch $NVM_PROFILE) and run this script again"
    echo "   OR"
    echo "=> Append the following lines to the correct file yourself:"
    printf "$SOURCE_STR"
    echo
  else
    if ! grep -qc 'nvm.sh' "$NVM_PROFILE"; then
      echo "=> Appending source string to $NVM_PROFILE"
      printf "$SOURCE_STR\n" >> "$NVM_PROFILE"
    else
      echo "=> Source string already in $NVM_PROFILE"
    fi
  fi

  nvm_check_global_modules

  echo "=> Close and reopen your terminal to start using tnvm"
  nvm_reset
}

#
# Unsets the various functions defined
# during the execution of the install script
#
nvm_reset() {
  unset -f nvm_reset nvm_has nvm_latest_version \
    nvm_source nvm_download install_nvm_as_script install_nvm_from_git \
    nvm_detect_profile nvm_check_global_modules nvm_do_install
}

nvm_do_install

} # this ensures the entire script is downloaded #

#!/bin/bash

set -e

{ # this ensures the entire script is downloaded #

nvm_has() {
  type "$1" > /dev/null 2>&1
}

if [ -z "$NVM_DIR" ]; then
  NVM_DIR="$HOME/.tnvm"
fi

#
# Outputs the location to NVM depending on:
# * The availability of $NVM_SOURCE
# * The method used ("script" or "git" in the script, defaults to "git")
# NVM_SOURCE always takes precedence unless the method is "script-nvm-exec"
#
nvm_source() {
  local NVM_METHOD
  NVM_METHOD="$1"
  local NVM_SOURCE_URL
  NVM_SOURCE_URL="$NVM_SOURCE"
  if [ "_$NVM_METHOD" = "_script-nvm-exec" ]; then
    NVM_SOURCE_URL="https://raw.githubusercontent.com/yjhjstz/tnvm/master/nvm-exec"
  elif [ -z "$NVM_SOURCE_URL" ]; then
    if [ "_$NVM_METHOD" = "_script" ]; then
      NVM_SOURCE_URL="https://raw.githubusercontent.com/yjhjstz/tnvm/master/nvm.sh"
    elif [ "_$NVM_METHOD" = "_git" ] || [ -z "$NVM_METHOD" ]; then
      NVM_SOURCE_URL="https://github.com/yjhjstz/tnvm.git"
    else
      echo >&2 "Unexpected value \"$NVM_METHOD\" for \$NVM_METHOD"
      return 1
    fi
  fi
  echo "$NVM_SOURCE_URL"
}

nvm_download() {
  if nvm_has "curl"; then
    curl -q $*
  elif nvm_has "wget"; then
    # Emulate curl with wget
    ARGS=$(echo "$*" | command sed -e 's/--progress-bar /--progress=bar /' \
                           -e 's/-L //' \
                           -e 's/-I /--server-response /' \
                           -e 's/-s /-q /' \
                           -e 's/-o /-O /' \
                           -e 's/-C - /-c /')
    wget $ARGS
  fi
}

install_nvm_from_git() {
  if [ -d "$NVM_DIR/.git" ]; then
    echo "=> tnvm is already installed in $NVM_DIR, trying to update using git"
    printf "\r=> "
    cd "$NVM_DIR" && (command git fetch 2> /dev/null || {
      echo >&2 "Failed to update tnvm, run 'git fetch' in $NVM_DIR yourself." && exit 1
    })
  else
    # Cloning to $NVM_DIR
    echo "=> Downloading tnvm from git to '$NVM_DIR'"
    printf "\r=> "
    mkdir -p "$NVM_DIR"
    command git clone "$(nvm_source git)" "$NVM_DIR"
  fi
  cd "$NVM_DIR" && command git checkout --quiet $(nvm_latest_version)
  if [ ! -z "$(cd "$NVM_DIR" && git show-ref refs/heads/master)" ]; then
    if git branch --quiet 2>/dev/null; then
      cd "$NVM_DIR" && command git branch --quiet -D master >/dev/null 2>&1
    else
      echo >&2 "Your version of git is out of date. Please update it!"
      cd "$NVM_DIR" && command git branch -D master >/dev/null 2>&1
    fi
  fi
  return
}

install_nvm_as_script() {
  local NVM_SOURCE_LOCAL
  NVM_SOURCE_LOCAL=$(nvm_source script)
  local NVM_EXEC_SOURCE
  NVM_EXEC_SOURCE=$(nvm_source script-nvm-exec)

  # Downloading to $NVM_DIR
  mkdir -p "$NVM_DIR"
  if [ -d "$NVM_DIR/nvm.sh" ]; then
    echo "=> tnvm is already installed in $NVM_DIR, trying to update the script"
  else
    echo "=> Downloading tnvm as script to '$NVM_DIR'"
  fi
  nvm_download -s "$NVM_SOURCE_LOCAL" -o "$NVM_DIR/nvm.sh" || {
    echo >&2 "Failed to download '$NVM_SOURCE_LOCAL'"
    return 1
  }
  nvm_download -s "$NVM_EXEC_SOURCE" -o "$NVM_DIR/nvm-exec" || {
    echo >&2 "Failed to download '$NVM_EXEC_SOURCE'"
    return 2
  }
  chmod a+x "$NVM_DIR/nvm-exec" || {
    echo >&2 "Failed to mark '$NVM_DIR/nvm-exec' as executable"
    return 3
  }
}

#
# Detect profile file if not specified as environment variable
# (eg: PROFILE=~/.myprofile)
# The echo'ed path is guaranteed to be an existing file
# Otherwise, an empty string is returned
#
nvm_detect_profile() {
  if [ -f "$PROFILE" ]; then
    echo "$PROFILE"
  elif [ -f "$HOME/.bashrc" ]; then
    echo "$HOME/.bashrc"
  elif [ -f "$HOME/.bash_profile" ]; then
    echo "$HOME/.bash_profile"
  elif [ -f "$HOME/.zshrc" ]; then
    echo "$HOME/.zshrc"
  elif [ -f "$HOME/.profile" ]; then
    echo "$HOME/.profile"
  fi
}

#
# Check whether the user has any globally-installed npm modules in their system
# Node, and warn them if so.
#
nvm_check_global_modules() {
  command -v npm >/dev/null 2>&1 || return 0

  local NPM_VERSION
  NPM_VERSION="$(npm --version)"
  NPM_VERSION="${NPM_VERSION:--1}"
  [ "${NPM_VERSION%%[!-0-9]*}" -gt 0 ] || return 0

  local NPM_GLOBAL_MODULES
  NPM_GLOBAL_MODULES="$(
    npm list -g --depth=0 |
    sed '/ npm@/d' |
    sed '/ (empty)$/d'
  )"

  local MODULE_COUNT
  MODULE_COUNT="$(
    printf %s\\n "$NPM_GLOBAL_MODULES" |
    sed -ne '1!p' |                             # Remove the first line
    wc -l | tr -d ' '                           # Count entries
  )"

  if [ $MODULE_COUNT -ne 0 ]; then
    cat <<-'END_MESSAGE'
	=> You currently have modules installed globally with `npm`. These will no
	=> longer be linked to the active version of Node when you install a new node
	=> with `nvm`; and they may (depending on how you construct your `$PATH`)
	=> override the binaries of modules installed with `nvm`:

	END_MESSAGE
    printf %s\\n "$NPM_GLOBAL_MODULES"
    cat <<-'END_MESSAGE'

	=> If you wish to uninstall them at a later point (or re-install them under your
	=> `nvm` Nodes), you can remove them from the system Node as follows:

	     $ nvm use system
	     $ npm uninstall -g a_module

	END_MESSAGE
  fi
}

nvm_do_install() {
  if [ -z "$METHOD" ]; then
    # Autodetect install method
    if nvm_has "git"; then
      install_nvm_from_git
    elif nvm_has "nvm_download"; then
      install_nvm_as_script
    else
      echo >&2 "You need git, curl, or wget to install nvm"
      exit 1
    fi
  elif [ "~$METHOD" = "~git" ]; then
    if ! nvm_has "git"; then
      echo >&2 "You need git to install nvm"
      exit 1
    fi
    install_nvm_from_git
  elif [ "~$METHOD" = "~script" ]; then
    if ! nvm_has "nvm_download"; then
      echo >&2 "You need curl or wget to install nvm"
      exit 1
    fi
    install_nvm_as_script
  fi

  echo

  local NVM_PROFILE
  NVM_PROFILE=$(nvm_detect_profile)

  SOURCE_STR="\nexport NVM_DIR=\"$NVM_DIR\"\n[ -s \"\$NVM_DIR/nvm.sh\" ] && . \"\$NVM_DIR/nvm.sh\"  # This loads nvm"

  if [ -z "$NVM_PROFILE" ] ; then
    echo "=> Profile not found. Tried $NVM_PROFILE (as defined in \$PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile."
    echo "=> Create one of them and run this script again"
    echo "=> Create it (touch $NVM_PROFILE) and run this script again"
    echo "   OR"
    echo "=> Append the following lines to the correct file yourself:"
    printf "$SOURCE_STR"
    echo
  else
    if ! grep -qc 'nvm.sh' "$NVM_PROFILE"; then
      echo "=> Appending source string to $NVM_PROFILE"
      printf "$SOURCE_STR\n" >> "$NVM_PROFILE"
    else
      echo "=> Source string already in $NVM_PROFILE"
    fi
  fi

  nvm_check_global_modules

  echo "=> Close and reopen your terminal to start using tnvm"
  nvm_reset
}

#
# Unsets the various functions defined
# during the execution of the install script
#
nvm_reset() {
  unset -f nvm_reset nvm_has nvm_latest_version \
    nvm_source nvm_download install_nvm_as_script install_nvm_from_git \
    nvm_detect_profile nvm_check_global_modules nvm_do_install
}

nvm_do_install

} # this ensures the entire script is downloaded #