Skip to content

Instantly share code, notes, and snippets.

@DennisLfromGA
Last active September 11, 2021 20:50
Show Gist options
  • Save DennisLfromGA/1ae8c81ff3eff0e129c0e52313490b87 to your computer and use it in GitHub Desktop.
Save DennisLfromGA/1ae8c81ff3eff0e129c0e52313490b87 to your computer and use it in GitHub Desktop.
A script to install 'brio-br' and associated links to '/usr/local/bin' as root (default)
#!/bin/bash
# 'install.brio-br' - A script to install 'brio-br' and associated links to '/usr/local/bin' as root (default)
##
#########################
### Declare variables ###
#########################
##
APPL='install.brio-br'
APPLBR='brio-br'
BIN=${BIN:-/usr/local/bin}
BBRURL='https://raw.githubusercontent.com/DennisLfromGA/brunch-things/main/brio-br'
CWD="$(pwd)"
IBBRURL='https://raw.githubusercontent.com/DennisLfromGA/brunch-things/main/install.brio-br'
INSTRUCTIONS="
IMPORTANT: To initially download, install & run this script enter:
cd /tmp &&\\
curl -LO ${IBBRURL} &&\\
sudo install -Dt /usr/local/bin -m 755 ${APPL} && ${BIN}/${APPL}
Once installed the script will autoupdate if needed when it's run.
"
PERM=${PERM:-755}
OPTIONS="\
Options:
-h Displays help message for options (this blurb)
-i Displays instructions for initial download, install and running this script
-l Lists installed files & links under ${BIN}
-u Displays usage for backup and restore
-f Force update of brio-br & install.brio-br
-c Check if installed versions of '${APPL}' & '${APPLBR}' are up-to-date
-v Displays the current version of '${APPL}'
-V Displays version number plus version history
"
SUDO=${SUDO:-sudo}
UPDATE="${UPDATE:-n}"
USAGE="
'${APPL}': A script to install '${APPLBR}' and associated links to '/usr/local/bin' as root (default)
${INSTRUCTIONS}
Will link: 'brio-b' & 'brio-backup' to '${APPLBR}' which forces those scripts to run the 'backup' option.
Will link: 'brio-r' & 'brio-restore' to '${APPLBR}' which forces those scripts to run the 'restore' option.
Usage: '${APPL}' [-h|-i|-l|-u|-f|-c|-v|-V] (see Help/Options)
Note: To replace the install PATH (default: /usr/local/bin) prepend '${APPL}' with BIN=your/path
To replace the file permissions (default 755) prepend '${APPL}' with PERM=nnn
To execute '${APPL}' as user '${USER}' instead of 'root' (default) prepend '${APPL}' with SUDO=' '
(I.E.) BIN=~/bin PERM=775 SUDO=' ' ${APPL}
"
# VERSION format: "n.n.$(date +%Y%m%d%H%M)"
VERSION='3.1.0.202109111642'
VERHIST="\
${APPL}-3.1.0.202109111642 : Added option '-f' to force update of brio-br & install.brio-br
${APPL}-3.0.0.202109071554 : Added autoupdate feature for install.brio-br
${APPL}-2.0.2.202109061732 : Changed URLS from gists to a repo for install.brio-br & brio-br
${APPL}-2.0.1.202109061542 : Added option to force an update of brio-br
${APPL}-2.0.0.202109061536 : Added update check of both install.brio-br & brio-br
${APPL}-1.6.1.202109061244 : Made mostly minor cosmetic changes to display
${APPL}-1.6.0.202109061236 : Added 3rd digit to VERSION for minor tweaks
${APPL}-1.5.202109051624 : Added 'list files' to options
${APPL}-1.5.202109051624 + Misc. tweaks to functions, clarified file listing location, etc.
${APPL}-1.4.202109051455 : Minor tweak to 'valid responses' for instructions - duh!
${APPL}-1.3.202109051436 : Tweaked to include instructions option throughout
${APPL}-1.2.202109051325 : Added instructions to download, install & execute this script
${APPL}-1.1.202109051151 : Added raw link to download 'brio-br' and command-line options
${APPL}-1.0.202109041942 : Initial script to install 'brio-br' and associated links"
##
#######################
### Setup functions ###
#######################
##
## exit with exit code $1, spitting out message $@ to stderr
echo_e='/bin/echo -e'
error() {
local ecode="${1}"
shift
${echo_e} "${*}" 1>&2
exit "${ecode}"
}
##
check_for_opts() {
## Get command line parameters
local OPTIND
while getopts hoilufcvV OPT; do
case ${OPT} in
h|o) error 0 "${OPTIONS}";;
i) error 0 "${INSTRUCTIONS}";;
l) cd ${BIN} 1>/dev/null
echo; echo "File listing under ${BIN}:"
ls -li ${APPLBR} brio-b brio-backup brio-r brio-restore ${APPL}
exit 0;;
u) error 0 "${USAGE}";;
c) check_versions; exit;;
f) UPDATE=y; echo "Updates requested ...";;
v) echo "${VERHIST}" | grep ${VERSION}; exit 0 ;;
V) error 0 "VERSION:${VERSION}\n${VERHIST}" ;;
\?) error 1 "${USAGE}\n${OPTIONS}";;
esac
done
shift "$((OPTIND-1))"
if [ "${#}" -gt 0 ]; then
echo "${APPL}: invalid option -- ${1}"
error 1 "${USAGE}\n${OPTIONS}"
fi
}
##
check_versions() {
cd /tmp 1>/dev/null
if curl -sLO ${BBRURL} && diff -qw ${APPLBR} /usr/local/bin; then
echo "Your installed version of '${APPLBR}' is up-to-date and good to go ..."
else
echo "Your installed version of '${APPLBR}' is missing or NOT up-to-date, update requested ..."
UPDATE=b
fi
if curl -sLO ${IBBRURL} && diff -qw ${APPL} /usr/local/bin; then
echo "Your installed version of '${APPL}' is up-to-date and good to go ..."
else
echo "Your installed version of '${APPL}' is missing or NOT up-to-date, an update will be done now ..."
UPDATE=i
fi
echo
}
##
ask_nicely() {
local REPLY=''
if [ "${UPDATE}" = "b" -o "${UPDATE}" = "y" ]; then
echo "Install '${APPLBR}' and associated links to '${BIN}' as root (default) [Ysilhq] ?"
read -s -n 1 -p "Enter 'y' to install, 'i' for installation instructions, 'l' to list, 'c' check versions, 'h' for help, or 'q' to quit ? "
echo
case ${REPLY:-Y} in
[yY])
echo; echo "Installing '${APPLBR}' and links to ${BIN} ..."
;;
[iI])
error 0 "${INSTRUCTIONS}"
;;
[lL])
cd ${BIN} 1>/dev/null
echo; echo "File listing under ${BIN}:"
ls -li ${APPLBR} brio-b brio-backup brio-r brio-restore ${APPL}
echo; ask_nicely
;;
[hH]|[oO])
echo "${OPTIONS}"
ask_nicely
;;
[uU])
echo "${USAGE}"
ask_nicely
;;
[cC])
check_versions
ask_nicely
;;
[qQ]|[aA])
echo; error 0 "Aborting/Quitting ..."
;;
v)
echo; echo "${VERHIST}" | grep ${VERSION}
echo; ask_nicely
;;
V)
echo; echo "VERSION:${VERSION}"
echo "${VERHIST}"
echo; ask_nicely
;;
*)
echo "Sorry, valid responses are:"
echo; echo " 'y' to install, 'i' for installation instructions, 'l' to list, 'h' for help, or 'q' to quit."
echo; ask_nicely
;;
esac
else
error 0 "No updates needed or requested, exiting ..."
fi
}
##
install_brio-br() {
cd /tmp 1>/dev/null
echo "Downloading '${APPLBR}' ..."
curl -sLO --progress-bar ${BBRURL} || \
error 2 "Cannot download '${APPLBR}'"
echo "Removing installed '${APPLBR}', if any ..."
${SUDO} rm -f ${BIN}/${APPLBR} 2>/dev/null || \
error 2 "Cannot remove existing '${APPLBR}'"
echo "Installing an updated '${APPLBR}' ..."
${SUDO} install -Dt ${BIN} -m 755 /tmp/${APPLBR} || \
error 3 "Cannot install '${APPLBR}'"
## Using hard links to avoid 'nosymfollow'
echo "Linking apps to '${APPLBR}' ..."
${SUDO} ln -fT ${BIN}/${APPLBR} ${BIN}/brio-b || \
error 4 "Cannot link '${APPLBR}' as 'brio-b'"
${SUDO} ln -fT ${BIN}/${APPLBR} ${BIN}/brio-backup || \
error 4 "Cannot link '${APPLBR}' as 'brio-backup'"
${SUDO} ln -fT ${BIN}/${APPLBR} ${BIN}/brio-r || \
error 4 "Cannot link '${APPLBR}' as 'brio-r'"
${SUDO} ln -fT ${BIN}/${APPLBR} ${BIN}/brio-restore || \
error 4 "Cannot link '${APPLBR}' as 'brio-restore'"
}
##
install_installer() {
if [ "${UPDATE}" = "i" -o "${UPDATE}" = "y" ]; then
cd /tmp 1>/dev/null
echo "Downloading '${APPL}' ..."
curl -sLO --progress-bar ${IBBRURL} || \
error 2 "Cannot download '${APPL}'"
echo "Removing installed '${APPL}', if any ..."
${SUDO} rm -f ${BIN}/${APPL} 2>/dev/null ||
error 2 "Cannot remove existing '${APPL}'"
echo "Installing an updated '${APPL}' ..."
sudo install -Dt /usr/local/bin -m 755 ${APPL} || \
error 3 "Cannot install '${APPL}'"
echo "'${APPL}' updated and installed, now launching it ..."
echo; exec ${BIN}/${APPL} || \
error 4 "Cannot execute '${APPL}'"
exit
fi
}
##
###########################
### Main section ###
###########################
##
if [ $(id -u) -eq 0 ]; then
echo
echo "ERROR: ${APPL} must NOT be run as root!"
error 1 "${USAGE}"
fi
check_for_opts "${@}"
check_versions
ask_nicely
install_brio-br
install_installer
cd ${CWD} 1>/dev/null
error 0 "'${APPL}' successful, exiting."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment