#!/bin/bash # Portable Node.js install script # Author: Dmitri Rubinstein # Version: 1.1 # 2017-02-21 # #Copyright (c) 2013, 2017 # DFKI - German Research Center for Artificial Intelligence # www.dfki.de # #Permission is hereby granted, free of charge, to any person obtaining a copy of #this software and associated documentation files (the "Software"), to deal in #the Software without restriction, including without limitation the rights to #use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies #of the Software, and to permit persons to whom the Software is furnished to do # so, subject to the following conditions: # #The above copyright notice and this permission notice shall be included in all #copies or substantial portions of the Software. # #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE #SOFTWARE. export LC_ALL=C unset CDPATH abspath() { if [ -d "$1" ]; then echo "$(cd "$1"; pwd)" else case "$1" in "" | ".") echo "$PWD";; /*) echo "$1";; *) echo "$PWD/$1";; esac fi } if [ -z "$(type -t dirname)" ]; then # Compute the dirname of FILE. dirname () { case ${1} in */*) echo "${1%/*}${2}" ;; * ) echo "${3}" ;; esac } fi die() { echo >&2 "Error: $@" exit 1 } if [ "$DEBUG" = "true" -o "$DEBUG" = "yes" ] || \ [ "$DEBUG" -eq 1 ] 2>/dev/null; then debug() { echo "Debug: $@"; } else debug() { :; } fi if type -p curl > /dev/null; then download() { echo "Download: $1 to: $2" curl -fLo "$2" "$1" && [ -e "$2" ] } elif type -p wget > /dev/null; then download() { echo "Download: $1 to: $2" wget -O "$2" "$1" && [ -e "$2" ] } else die "No download tool found, please install wget or curl" fi # ask prompt default REPLY= ask() { local prompt=$1 local default=$2 echo "$1" [ -n "$default" ] && echo -n "[$default] " read -e [ -z "$REPLY" ] && REPLY=$default } # setup_ask prompt default setup_ask() { if [ "$AUTO_INSTALL" = "yes" ]; then echo "$1 : $2" REPLY=$2 else ask "$@" fi } # Setup basic variables thisDir=$(abspath "$(dirname "$0")") case "$thisDir" in */bin) baseDir=${thisDir%/*};; *) baseDir=$thisDir;; esac # Check OS runCScript= case "$OSTYPE" in linux-gnu) ;; msys|cygwin) # Windows run install-node.vbs if available [ -e "$thisDir/install-node.vbs" ] || die "Download and run install-node.vbs" runCScript=yes ;; *) die "$OSTYPE OS is not supported." esac # Process command line arguments nodeVersion=6.9.5 if [[ "$HOSTTYPE" == "x86_64" ]]; then nodeArch=x64 else nodeArch=x86 fi forceInstall= while [ $# -gt 0 ]; do case "$1" in --) break;; # end of options -h|--help) cat< /dev/null || die "Could not find tar tool, please install first" nodeExePath=$nodeInstallPath/bin nodeExePathRel=$nodeInstallPathRel/bin nodeExeFile=$nodeExePath/node nodeExeFileRel=$nodeExePathRel/node if [[ ( ! -e "$nodeExeFile" ) || ( "$forceInstall" == "yes" ) ]]; then [[ -d "$nodeInstallPath" ]] && { echo "Deleting directory: $nodeInstallPath" rm -rf "$nodeInstallPath" } echo "Running: cd $nodeBaseDir && tar xzf \"$nodeTarballPath\"" (cd "$nodeBaseDir" || exit 1; tar xzf "$nodeTarballPath") result=$? echo "Result: $result" if [[ "$result" != 0 ]]; then die "Could not install node.js" fi else echo "File $nodeExeFile already exists, use --force to reinstall." fi # Create node launch script funcText=$(declare -f abspath) scriptFile="$baseDir/$nodePrefix" cat > "$scriptFile" < "$scriptFile" < "\$THIS_DIR/share/git-bash-profile.sh" echo "[ -e \\"\\\$NODE_PATH/../lib/node_modules/npm/lib/utils/completion.sh\\" ] && source \\"\\\$NODE_PATH/../lib/node_modules/npm/lib/utils/completion.sh\\"" >> "\$THIS_DIR/share/git-bash-profile.sh" echo "echo Started Node Environment" >> "\$THIS_DIR/share/git-bash-profile.sh" fi exec bash --rcfile "\$THIS_DIR/share/git-bash-profile.sh" EOF chmod +x "$scriptFile" echo "Created bash launch script: $scriptFile"