templating, rate-limiting, etc.

This commit is contained in:
2026-04-02 22:44:23 +03:00
parent a168b4cbea
commit 9d74765484
5 changed files with 253 additions and 23 deletions

View File

@@ -2,10 +2,16 @@
set -e
BIN_SRC="./whisper"
BIN_DST="/usr/bin/whisper"
CONF_DIR="/etc/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
@@ -20,10 +26,45 @@ usage() {
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\n' "$CONF_DIR"
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
@@ -34,20 +75,35 @@ do_install() {
[ -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"
chown root:root "$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"
printf ' - Keeping existing %s\n' "$CONF_DST"
fi
chown root:root "$CONF_DST"
chmod 644 "$CONF_DST"
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"
}
@@ -55,7 +111,9 @@ do_install() {
do_uninstall() {
printf 'This will remove:\n'
printf ' - %s\n' "$BIN_DST"
printf ' - %s/\n\n' "$CONF_DIR"
[ -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
@@ -65,14 +123,38 @@ do_uninstall() {
esac
rm -f "$BIN_DST"
rm -rf "$CONF_DIR"
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 ---
[ "$(id -u)" -eq 0 ] || die "Must be run as root"
ACTION="install"
while [ $# -gt 0 ]; do
case "$1" in