172 lines
4.8 KiB
Bash
Executable File
172 lines
4.8 KiB
Bash
Executable File
#!/bin/sh
|
|
set -e
|
|
|
|
BIN_SRC="./whisper"
|
|
BIN_DIR="$HOME/.local/bin"
|
|
BIN_DST="$BIN_DIR/whisper"
|
|
CONF_DIR="$HOME/.config/whisper"
|
|
CONF_DST="$CONF_DIR/.env"
|
|
CONF_SRC="./example.env"
|
|
TMPL_SRC_DIR="./templates"
|
|
TMPL_DST_DIR="$CONF_DIR/templates"
|
|
FISH_BIN="/opt/homebrew/bin/fish"
|
|
FISH_FUNC_DIR="$HOME/.config/fish/functions"
|
|
FISH_FUNC="$FISH_FUNC_DIR/whisper.fish"
|
|
|
|
die() {
|
|
printf '%s\n' "$1" >&2
|
|
exit 1
|
|
}
|
|
|
|
usage() {
|
|
printf 'Usage: install.sh [-i|--install] [-u|--uninstall] [-h|--help]\n'
|
|
printf ' -i, --install Install whisper (default)\n'
|
|
printf ' -u, --uninstall Remove whisper\n'
|
|
printf ' -h, --help Show this help\n'
|
|
exit 0
|
|
}
|
|
|
|
# Append PATH export to an rc file if BIN_DIR isn't already referenced there
|
|
patch_rc() {
|
|
_rc="$1"
|
|
[ -f "$_rc" ] || return 0
|
|
grep -qF "$BIN_DIR" "$_rc" && return 0
|
|
printf '\nexport PATH="%s:$PATH"\n' "$BIN_DIR" >> "$_rc"
|
|
printf ' - Added %s to PATH in %s\n' "$BIN_DIR" "$_rc"
|
|
}
|
|
|
|
ensure_in_path() {
|
|
patch_rc "$HOME/.bashrc"
|
|
patch_rc "$HOME/.bash_profile"
|
|
patch_rc "$HOME/.zshrc"
|
|
patch_rc "$HOME/.zprofile"
|
|
}
|
|
|
|
install_fish_function() {
|
|
mkdir -p "$FISH_FUNC_DIR"
|
|
printf 'function whisper --description "Send messages via transport (telegram)"\n' > "$FISH_FUNC"
|
|
printf ' %s $argv\n' "$BIN_DST" >> "$FISH_FUNC"
|
|
printf 'end\n' >> "$FISH_FUNC"
|
|
printf ' - Created fish function at %s\n' "$FISH_FUNC"
|
|
}
|
|
|
|
remove_fish_function() {
|
|
if [ -f "$FISH_FUNC" ]; then
|
|
rm -f "$FISH_FUNC"
|
|
printf ' - Removed %s\n' "$FISH_FUNC"
|
|
fi
|
|
}
|
|
|
|
do_install() {
|
|
printf 'This will:\n'
|
|
printf ' - Copy whisper to %s\n' "$BIN_DST"
|
|
printf ' - Create %s/ with config\n' "$CONF_DIR"
|
|
printf ' - Patch ~/.bashrc, ~/.bash_profile, ~/.zshrc, ~/.zprofile if needed\n'
|
|
[ -x "$FISH_BIN" ] && printf ' - Create fish function at %s\n' "$FISH_FUNC"
|
|
printf ' - Seed %s/ with example templates\n' "$TMPL_DST_DIR"
|
|
printf '\n'
|
|
|
|
printf 'Proceed? [Y/n] '
|
|
read -r answer
|
|
case "$answer" in
|
|
[nN]) printf 'Aborted.\n'; exit 0 ;;
|
|
esac
|
|
|
|
[ -f "$BIN_SRC" ] || die "Cannot find $BIN_SRC in current directory"
|
|
[ -f "$CONF_SRC" ] || die "Cannot find $CONF_SRC in current directory"
|
|
|
|
mkdir -p "$BIN_DIR"
|
|
cp "$BIN_SRC" "$BIN_DST"
|
|
chmod 755 "$BIN_DST"
|
|
|
|
mkdir -p "$CONF_DIR"
|
|
if [ ! -f "$CONF_DST" ]; then
|
|
cp "$CONF_SRC" "$CONF_DST"
|
|
chmod 600 "$CONF_DST"
|
|
else
|
|
printf ' - Keeping existing %s\n' "$CONF_DST"
|
|
fi
|
|
|
|
mkdir -p "$TMPL_DST_DIR"
|
|
if [ -d "$TMPL_SRC_DIR" ]; then
|
|
for _tmpl in "$TMPL_SRC_DIR"/*; do
|
|
[ -f "$_tmpl" ] || continue
|
|
_basename=$(basename "$_tmpl")
|
|
if [ ! -f "$TMPL_DST_DIR/$_basename" ]; then
|
|
cp "$_tmpl" "$TMPL_DST_DIR/$_basename"
|
|
printf ' - Installed template: %s\n' "$_basename"
|
|
else
|
|
printf ' - Keeping existing template: %s\n' "$_basename"
|
|
fi
|
|
done
|
|
fi
|
|
|
|
ensure_in_path
|
|
|
|
[ -x "$FISH_BIN" ] && install_fish_function
|
|
|
|
printf '\nInstalled. Edit %s and set your values.\n' "$CONF_DST"
|
|
}
|
|
|
|
do_uninstall() {
|
|
printf 'This will remove:\n'
|
|
printf ' - %s\n' "$BIN_DST"
|
|
[ -f "$FISH_FUNC" ] && printf ' - %s\n' "$FISH_FUNC"
|
|
[ -d "$CONF_DIR" ] && printf ' - %s/ (env files confirmed individually)\n' "$CONF_DIR"
|
|
printf '\n'
|
|
|
|
printf 'Proceed? [y/N] '
|
|
read -r answer
|
|
case "$answer" in
|
|
[yY]) ;;
|
|
*) printf 'Aborted.\n'; exit 0 ;;
|
|
esac
|
|
|
|
rm -f "$BIN_DST"
|
|
remove_fish_function
|
|
|
|
if [ -d "$CONF_DIR" ]; then
|
|
for _f in "$CONF_DIR"/.env "$CONF_DIR"/*.env; do
|
|
[ -f "$_f" ] || continue
|
|
printf 'Delete %s? [y/N] ' "$_f"
|
|
read -r _ans
|
|
case "$_ans" in
|
|
[yY]) rm -f "$_f"; printf ' - Removed %s\n' "$_f" ;;
|
|
*) printf ' - Kept %s\n' "$_f" ;;
|
|
esac
|
|
done
|
|
if [ -d "$TMPL_DST_DIR" ]; then
|
|
printf 'Delete templates directory %s/? [y/N] ' "$TMPL_DST_DIR"
|
|
read -r _ans
|
|
case "$_ans" in
|
|
[yY]) rm -rf "$TMPL_DST_DIR"; printf ' - Removed %s/\n' "$TMPL_DST_DIR" ;;
|
|
*) printf ' - Kept %s/\n' "$TMPL_DST_DIR" ;;
|
|
esac
|
|
fi
|
|
# Remove the directory only if it is now empty
|
|
if [ -z "$(ls -A "$CONF_DIR" 2>/dev/null)" ]; then
|
|
rmdir "$CONF_DIR"
|
|
else
|
|
printf ' - Kept %s/ (not empty)\n' "$CONF_DIR"
|
|
fi
|
|
fi
|
|
|
|
printf 'Done.\n'
|
|
}
|
|
|
|
# --- main ---
|
|
ACTION="install"
|
|
while [ $# -gt 0 ]; do
|
|
case "$1" in
|
|
-i|--install) ACTION="install"; shift ;;
|
|
-u|--uninstall) ACTION="uninstall"; shift ;;
|
|
-h|--help) usage ;;
|
|
*) die "Unknown option: $1" ;;
|
|
esac
|
|
done
|
|
|
|
case "$ACTION" in
|
|
install) do_install ;;
|
|
uninstall) do_uninstall ;;
|
|
esac
|