#!/usr/bin/env bash # 20260912 - Hyperling # "Install" script to get this program runnable from anywhere on the system. ## Variables ## DIR="$(cd "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)" PROJ="$(cd "$DIR"/.. &>/dev/null && pwd)" PROG="$(basename -- "${BASH_SOURCE[0]}")" LOCAL=~/bin GLOBAL="/usr/local" GLOBAL_BIN="$GLOBAL/bin" GLOBAL_SRC="$GLOBAL/src" name_local="prototype.js" name_short="nomad" name_long="nomadic-weather" name_abs="weather-list" ## Functions ## function usage { cat <<- EOF $PROG [-h] [-g] [a: b|l|s] [-r] Install $name_local to the system. Can be local, global, or removed. Parameters) h) Display this message. g) Install globally ($GLOBAL), otherwise local ($LOCAL). a) All names / pseudonyms, rather than choosing from below. b) Basic name, unrelated to being nomadic. ($name_abs) l) Long name, the full name of the project. ($name_long) [Default] s) Short name, the full name of the project. ($name_short) r) Remove / uninstall the project according to the above flags. Examples) - Install to the local user with the default name. $PROG - Install to all users with all names. $PROG -ga - Remove all instances. $PROG -ra && $PROG -gra Happy weather watching! :) ~ Hyperling https://git.hyperling.com/me/nomadic-weather EOF exit $1 } ## Validations ## count=0 while [[ ! -d "$DIR/node_modules" ]]; do if (( count > 0 )); then echo "Failed to download libraries, please intervene." exit 3 fi if [[ -z "`which npm`" ]]; then echo "Libraries are missing, please run 'npm install' first." exit 2 else echo "Downloading libraries with 'npm install'." cd $DIR && npm install fi count=$(( count++ )) done ## Parameters ## while getopts ":gslabrh" arg; do case $arg in g) global="Y" ;; s) short="Y" ;; l) long="Y" ;; b) abstract="Y" ;; a) all="Y" ;; r) remove="Y" ;; h) usage ;; *) echo "ERROR: Parameter $OPTARG was not recognized." && usage 1 ;; esac done name="" if [[ "$all" == "Y" || "$abstract" == "Y" ]]; then name="$name $name_abs" fi if [[ "$all" == "Y" || "$short" == "Y" ]]; then name="$name $name_short" fi if [[ "$all" == "Y" || "$long" == "Y" || -z "$name" ]]; then name="$name $name_long" fi termux_check="/sdcard" if [[ "$global" == "Y" && -e "$termux_check" ]]; then echo "Detected $termux_check, assuming we are on Termux, global flag disabled." global="N" fi ## Processing ## cmd="" dir="$LOCAL" src="$PROJ" if [[ "$global" == "Y" ]]; then cmd="sudo" dir="$GLOBAL_BIN" src="$GLOBAL_SRC/`basename "$PROJ"`" echo "Requesting sudo privileges... " sudo echo "Success!" fi if [[ -z "$remove" ]]; then cmd="$cmd ln -svf $src/cli/$name_local" else cmd="$cmd rm -v" fi ## Main ## # Global Source Code: Install / Removal if [[ "$global" == "Y" ]]; then if [[ "$remove" == "Y" ]]; then sudo rm -rfv "$src" else mkdir -pv "$GLOBAL_SRC" sudo cp -Rv "$PROJ" "$GLOBAL_SRC"/ fi fi # Program: Install / Removal for prog in $name; do if [[ ! -d "$dir" ]]; then mkdir -pv "$dir" fi $cmd "$dir/$prog" done ## Finish ## echo "Done!" exit 0