setup: install per-user, makefile

This commit is contained in:
Andrew Hurley 2026-09-20 20:55:11 +08:00
parent c207a8a038
commit fe3bab553a
3 changed files with 77 additions and 10 deletions

13
Makefile Normal file
View File

@ -0,0 +1,13 @@
PREFIX ?= /usr/local
.PHONY: install uninstall check
install:
./setup.sh
uninstall:
./setup.sh -u
check:
python3 -m py_compile lxcd.py
bash -n lxcd.completion

View File

@ -14,9 +14,19 @@ and home.
## Install
```sh
make install # copies lxcd.py to /usr/local/bin/lxcd + installs completion
sudo ./setup.sh # copies lxcd.py to /usr/local/bin/lxcd + installs completion
```
`setup.sh` needs root only to write to `/usr/local/bin` and
`/etc/bash_completion.d`; for a user-local install instead:
```sh
BIN_DIR=~/.local/bin COMPLETION_DIR=~/.bash_completion.d ./setup.sh
```
If you use GNU Make you can also run `make install` (same thing, delegates
to `setup.sh`).
Or run in place from this directory:
```sh

View File

@ -1,19 +1,60 @@
#!/bin/bash
set -euo pipefail
BIN_DIR="${BIN_DIR:-/usr/local/bin}"
COMPLETION_DIR="${COMPLETION_DIR:-/etc/bash_completion.d}"
BIN_DIR="${BIN_DIR:-$HOME/.local/bin}"
COMPLETION_DIR="${COMPLETION_DIR:-$HOME/.bash_completion.d}"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
if [ $EUID -ne 0 ]; then
echo "This script must be run with root privileges (sudo)." >&2
echo "Usage: sudo $0" >&2
echo "" >&2
echo "To install to a user-writable location instead:" >&2
echo " BIN_DIR=~/.local/bin COMPLETION_DIR=~/.bash_completion.d $0" >&2
exit 1
MODE="install"
if [ $# -gt 0 ]; then
case "$1" in
-u|--uninstall) MODE="uninstall" ;;
-h|--help)
echo "Usage: $0 [-u|--uninstall]"
echo "Install (default) or uninstall lxcd for the current user."
echo ""
echo "Locations (override with env vars):"
echo " BIN_DIR (default: \$HOME/.local/bin)"
echo " COMPLETION_DIR (default: \$HOME/.bash_completion.d)"
echo ""
echo "Examples:"
echo " $0 # install (per-user)"
echo " $0 -u # uninstall (per-user)"
echo " sudo BIN_DIR=/usr/local/bin COMPLETION_DIR=/etc/bash_completion.d $0"
exit 0
;;
*) echo "Unknown argument: $1 (use -h for help)" >&2; exit 1 ;;
esac
fi
if [ "$MODE" = "uninstall" ]; then
rm -f "${BIN_DIR}/lxcd"
rm -f "${COMPLETION_DIR}/lxcd"
echo "lxcd uninstalled."
# Prompt about the per-user config file (safe to keep; only data, no binaries)
LXCDRC="${LXCDRC:-$HOME/.lxcdrc}"
if [ -f "$LXCDRC" ] || [ -d "$LXCDRC" ]; then
echo ""
read -r -p "Remove your lxcd config file (${LXCDRC})? [y/N] " ans
if [[ "$ans" =~ ^[Yy]$ ]]; then
rm -rf "$LXCDRC"
echo "Removed ${LXCDRC}."
else
echo "Keeping ${LXCDRC}."
fi
fi
exit 0
fi
echo "Installing to user-local locations (no sudo needed):"
echo " bin: $BIN_DIR"
echo " completion: $COMPLETION_DIR"
echo ""
echo "To install system-wide instead, use:"
echo " sudo BIN_DIR=/usr/local/bin COMPLETION_DIR=/etc/bash_completion.d $0"
echo ""
mkdir -p "$BIN_DIR" "$COMPLETION_DIR"
echo "Installing lxcd to ${BIN_DIR}/lxcd..."
@ -28,3 +69,6 @@ fi
echo ""
echo "lxcd installed successfully!"
echo "Usage: lxcd --help"
echo ""
echo "Remember to reload your shell so PATH includes ${BIN_DIR}:"
echo " exec bash -l # login shell — reads ~/.profile (or open a new terminal)"