104 lines
2.7 KiB
Bash
Executable file
104 lines
2.7 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
# He wrote this in BASH because he hates himself, apparently :(
|
|
USAGE="Usage: ./configure [-g|--global] [-u|--uninstall] [-f|--force]"
|
|
set -ue
|
|
|
|
# === Configure Install === #
|
|
INSTALL_PATH_HOME="$HOME/.local/bin"
|
|
INSTALL_PATH_GLOBAL="/usr/local/bin"
|
|
SCRIPT_PERM=555
|
|
ALIASES_PERM=444
|
|
# ========================= #
|
|
|
|
# NOTE: since this last changed in 2015 its a really good unique identifier...
|
|
# NOTE: (to avoid uninstalling other programs called g)
|
|
G_IDENTIFIER="I'm writing this in BASH because I hate myself, apparently."
|
|
|
|
# Filters filenames based on if the file contains the G_IDENTIFIER
|
|
is_this_g() {
|
|
while read -r LOC; do
|
|
if [[ -d "$LOC" ]]; then
|
|
LOC="$LOC/g"
|
|
fi
|
|
grep -lF "$G_IDENTIFIER" "$LOC" 2>/dev/null
|
|
done <&0
|
|
}
|
|
|
|
# Finds absolute paths to all installed g binaries
|
|
# (or the extremely rare case there are multiple)
|
|
find_installed() {
|
|
(echo -e "$INSTALL_PATH_HOME\n$INSTALL_PATH_GLOBAL" && which -a g 2>/dev/null) \
|
|
| is_this_g
|
|
exit 0
|
|
}
|
|
|
|
# Escapes all / \ & characters in sed patterns
|
|
escape_pattern() {
|
|
<<< "$1" sed -e 's/[\/&]/\\&/g'
|
|
}
|
|
|
|
install_g() {
|
|
local LOC=$1
|
|
local FORCE=$2
|
|
mkdir -p "$LOC"
|
|
|
|
FOUND=$(find_installed)
|
|
if [[ -n "$FOUND" && -z "$FORCE" ]]; then
|
|
echo "Failed: g already exists at:" $FOUND >&2
|
|
echo " 1. Run \`./configure --uninstall\` to remove, OR" >&2
|
|
echo " 2. Use \`--force\` to overwrite" >&2
|
|
exit 2
|
|
fi
|
|
|
|
install -m "$SCRIPT_PERM" bin/g "$LOC/g"
|
|
install -m "$ALIASES_PERM" bin/g_aliases "$LOC/g_aliases"
|
|
# Format config strings for `g_aliases`
|
|
local G_BIN_DIR=$(escape_pattern "$LOC")
|
|
sed -i -e "s/AUTO_G_BIN_DIR/$G_BIN_DIR/g" "$LOC/g_aliases"
|
|
|
|
echo "Installed bin/g -> $LOC/g"
|
|
echo "Installed bin/g_aliases -> $LOC/g_aliases"
|
|
echo -e "\n[+] To use the aliases, add this to your ~/.bashrc or ~/.bash_aliases:"
|
|
echo "### source $LOC/g_aliases"
|
|
}
|
|
|
|
uninstall_g() {
|
|
local FOUND=""
|
|
while read -r LOC; do
|
|
local FOUND="1"
|
|
LOC=$(dirname "$LOC")
|
|
rm "$LOC/g" "$LOC/g_aliases"
|
|
rmdir "$LOC"
|
|
echo "Removed: $LOC/g"
|
|
echo "Removed: $LOC/g_aliases"
|
|
done < <(find_installed | uniq -u)
|
|
|
|
if [[ -z "$FOUND" ]]; then
|
|
echo "Failed: g was not found to be installed" >&2
|
|
exit 2
|
|
fi
|
|
|
|
echo -e "\nAliases are still bound!\nUse \`source ~/.bashrc\` or \`exec \`$SHELL\`"
|
|
}
|
|
|
|
INSTALL_PATH="$INSTALL_PATH_HOME"
|
|
FORCE=
|
|
for OPT in "$@"; do
|
|
case $OPT in
|
|
-g|--global)
|
|
INSTALL_PATH="$INSTALL_PATH_GLOBAL"
|
|
;;
|
|
-u|--uninstall)
|
|
uninstall_g
|
|
exit 0 # non-zero will already propagate from `set -e`
|
|
;;
|
|
-f|--force)
|
|
FORCE="1"
|
|
;;
|
|
*)
|
|
echo -e "Unknown option $OPT\n$USAGE" >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
done
|
|
install_g "$INSTALL_PATH" "$FORCE"
|