# Functions required to allow the script template and alert functions to be used _acquireScriptLock_() { # DESC: # Acquire script lock to prevent running the same script a second time before the # first instance exits # ARGS: # $1 (optional) - Scope of script execution lock (system or user) # OUTS: # exports $SCRIPT_LOCK - Path to the directory indicating we have the script lock # Exits script if lock cannot be acquired # NOTE: # If the lock was acquired it's automatically released in _safeExit_() local _lockDir if [[ ${1:-} == 'system' ]]; then _lockDir="${TMPDIR:-/tmp/}$(basename "$0").lock" else _lockDir="${TMPDIR:-/tmp/}$(basename "$0").$UID.lock" fi if command mkdir "${LOCK_DIR}" 2>/dev/null; then readonly SCRIPT_LOCK="${_lockDir}" debug "Acquired script lock: ${tan}${SCRIPT_LOCK}${purple}" else if [ "$(declare -f "_safeExit_")" ]; then error "Unable to acquire script lock: ${yellow}${LOCK_DIR}${red}" fatal "If you trust the script isn't running, delete the lock dir" else printf "%s\n" "ERROR: Could not acquire script lock. If you trust the script isn't running, delete: ${LOCK_DIR}" exit 1 fi fi } _makeTempDir_() { # DESC: # Creates a temp directory to house temporary files # ARGS: # $1 (Optional) - First characters/word of directory name # OUTS: # Sets $TMP_DIR variable to the path of the temp directory # USAGE: # _makeTempDir_ "$(basename "$0")" [ -d "${TMP_DIR:-}" ] && return 0 if [ -n "${1:-}" ]; then TMP_DIR="${TMPDIR:-/tmp/}${1}.${RANDOM}.${RANDOM}.$$" else TMP_DIR="${TMPDIR:-/tmp/}$(basename "$0").${RANDOM}.${RANDOM}.${RANDOM}.$$" fi (umask 077 && mkdir "${TMP_DIR}") || { fatal "Could not create temporary directory! Exiting." } debug "\$TMP_DIR=${TMP_DIR}" } _safeExit_() { # DESC: # Cleanup and exit from a script # ARGS: # $1 (optional) - Exit code (defaults to 0) # OUTS: # None if [[ -d ${SCRIPT_LOCK:-} ]]; then if command rm -rf "${SCRIPT_LOCK}"; then debug "Removing script lock" else warning "Script lock could not be removed. Try manually deleting ${yellow}'${LOCK_DIR}'" fi fi if [[ -n ${TMP_DIR:-} && -d ${TMP_DIR:-} ]]; then if [[ ${1:-} == 1 && -n "$(ls "${TMP_DIR}")" ]]; then command rm -r "${TMP_DIR}" else command rm -r "${TMP_DIR}" debug "Removing temp directory" fi fi trap - INT TERM EXIT exit ${1:-0} } _setPATH_() { # DESC: # Add directories to $PATH so script can find executables # ARGS: # $@ - One or more paths # OUTS: Adds items to $PATH # USAGE: # _setPATH_ "/usr/local/bin" "${HOME}/bin" "$(npm bin)" [[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}" local _newPath for _newPath in "$@"; do if [ -d "${_newPath}" ]; then if ! echo "${PATH}" | grep -Eq "(^|:)${_newPath}($|:)"; then if PATH="${_newPath}:${PATH}"; then debug "Added '${_newPath}' to PATH" else return 1 fi else debug "_setPATH_: '${_newPath}' already exists in PATH" fi else debug "_setPATH_: can not find: ${_newPath}" return 1 fi done return 0 }