#!/usr/bin/env bash # # Script-global constants # kTHIS_NAME=$(basename "$0") ##### IMPORTANT: These names must be kept in sync with the master copies in 'n-install'. kINSTALLER_NAME=n-install # The installer script's name. kTHIS_REPO_URL='https://bit.ly/n-install-repo' # This script's source repository. ##### # # BEGIN: Helper functions # # SYNOPSIS # echoColored colorNum [text...] # DESCRIPTION # Prints input in the specified color, which must be an ANSI color code (e.g., 31 for red). # Input is either provided via the TEXT operands, or, in their absence, from stdin. # If input is provided via TXT operands, a trailing \n is added. # NOTE: # - Unlike echo, uses stdin, if no TEXT arguments are specified; you MUST either specify # at least one input operand OR stdin input; in that sense, this function is like a hybrid # between echo and cat. However, *interactive* stdin input makes no sense, and therefore # a newline is simply printed - as with echo without arguments - if stdin is connected to # a terminal and neither operands nor stdin input is provided. # - Coloring is suppressed, if the variable kNO_COLOR exists and is set to 1. # An invoking script may set this in case output is NOT being sent to a terminal. # (E.g., test -t 1 || kNO_COLOR=1) # EXAMPLES # echoColored 31 "I'm red" # cat file | echoColored 32 # file contents is printed in green echoColored() { local pre="\033[${1}m" post='\033[0m' (( kNO_COLOR )) && { pre= post=; } shift # skip the color argument if (( $# )); then printf "${pre}%s${post}\n" "$*" else [[ -t 0 ]] && { printf '\n'; return; } # no interactive stdin input printf "$pre"; cat; printf "$post" fi } # SYNOPSIS # dieSyntax [msg|-] # DESCRIPTION # Prints a red error message to stderr and exits with exit code 2, meant to indicate a # syntax problem (invalid arguments). # A standard message is provided, if no arguments are given. # If the first (and only) argument is '-', input is taken from stdin; otherwise, the # first argument specifies the message to print. # Either way, a preamble with this script's name and the type of message is printed. # NOTES # Uses echoColored(), whose coloring may be suppressed with kNO_COLOR=1. dieSyntax() { local kPREAMBLE="$kTHIS_NAME: ARGUMENT ERROR:" if [[ $1 == '-' ]]; then # from stdin { printf '%s\n' "$kPREAMBLE" sed 's/^/ &/' } | echoColored 31 # red else # from operands echoColored 31 "$kPREAMBLE: ${1:-"Invalid argument(s) specified."} Use -h for help." fi exit 2 } >&2 # SYNOPSIS # die [msg|- [exitCode]] # DESCRIPTION # Prints a red error message to and by default exits with exit code 1, meant to indicate # a runtime problem. # A standard message is provided, if no arguments are given. # If the first (and only) argument is '-', input is taken from stdin; otherwise, the # first argument specifies the message to print. # Either way, a preamble with this script's name and the type of message is printed. # NOTES # Uses echoColored(), whose coloring may be suppressed with kNO_COLOR=1. die() { local kPREAMBLE="$kTHIS_NAME: ERROR:" if [[ $1 == '-' ]]; then # from stdin { printf '%s\n' "$kPREAMBLE" sed 's/^/ &/' } | echoColored 31 # red else # from operands echoColored 31 "$kPREAMBLE ${1:-"ABORTING due to unexpected error."}" fi exit ${2:-1} } >&2 # SYNOPSIS # warn [msg|-] # DESCRIPTION # Prints a yellow warning message to stderr. # If the first (and only) argument is '-', input is taken from stdin; otherwise, the # first argument specifies the message to print. # Either way, a preamble with this script's name and the type of message is printed. # NOTES # Uses echoColored(), whose coloring may be suppressed with kNO_COLOR=1. warn() { local kPREAMBLE="$kTHIS_NAME: WARNING:" [[ $1 == '-' ]] && shift # for consistency with die() and dieSyntax(), accept '-' as an indicator that stdin input should be used. if (( $# == 0 )); then # from stdin { printf '%s\n' "$kPREAMBLE" sed 's/^/ &/' } | echoColored 33 # yellow else # from operands echoColored 33 "$kPREAMBLE $*" fi } >&2 # # END: Helper functions # unset CDPATH # to prevent unpredictable `cd` behavior [[ -t 1 ]] || kNO_COLOR=1 # turn off colored output if stdout is not connected to a terminal # Output version number and exit, if requested. Note that the `ver='...'` statement is automatically updated by `make version VER=` - DO keep the 'v' prefix in the variable _definition_. [ "$1" = '--version' ] && { ver='v0.6.5'; echo "$kTHIS_NAME ${ver#v}"$'\nFor license information and more, visit https://bit.ly/n-install-repo'; exit 0; } # Command-line help. if [ "$1" = '--help' ] || [ "$1" = '-h' ]; then cat </dev/null | grep -Fq "GNU Make" && break [[ $makeExe == 'make' ]] && continue # if 'make' isn't GNU Make, try again with 'gmake'. preReqMsg+="${preReqMsg:+$'\n'}GNU Make not found, which is required for operation."$'\n'"On FreeBSD and PC-BSD, for instance, you can download it with \`sudo pkg install gmake\`." done [[ -z $preReqMsg ]] || die - <<<"$preReqMsg" nDir="$N_PREFIX/n" repo="$nDir/.repo" nExe="$N_PREFIX/bin/n" [[ -d $repo/.git ]] || die "n repository not found in '$nDir'." # Prompt for confirmation. if (( ! skipPrompt )); then while :; do read -p "Check for updates to n (version $("$nExe" --version), installed in '$nDir')? (y/N) " promptInput [[ $promptInput =~ ^[yY]$ ]] && break # OK [[ $promptInput =~ ^[nN]$ || -z $promptInput ]] && { echo "Aborted." 1>&2; exit 3; } echo "Invalid input; please try again (^C to abort)." 1>&2 done fi cd "$repo" || die echo "-- Checking for updates..." # Note: # * We only get the latest commit, because that is all we need to run. # Since we're therefore lacking history, we cannot use *pull*, # and instead must *fetch* and then force checking out what we fetched. # See https://github.com/mklement0/n-install/issues/10 # * We use `git config core.autocrlf input` before `git reset ...` to be safe, to ensure that files # updated are not checked out with CRLF newlines, should core.autocrlf (now, unexpectedly) be true globally. git fetch --depth 1 --quiet && git config core.autocrlf input && git reset --hard --quiet origin/master || die src="$repo/bin/n" target="$nExe" # Exit, if nothing has changed. diff "$src" "$target" >/dev/null && { echo "(Installed version $("$nExe" --version) is up-to-date.)"; exit 0; } # Invoke the Makefile installation task, which simply copies bin/n to ${N_PREFIX}/bin. PREFIX="$N_PREFIX" "$makeExe" install >/dev/null || die echo "-- n successfully updated to version $("$nExe" --version)."