| 1 | #!/bin/sh |
| 2 | |
| 3 | set -eu |
| 4 | |
| 5 | BIN_DIR=/usr/local/bin |
| 6 | ACTION=install |
| 7 | |
| 8 | usage() { |
| 9 | cat <<'EOF' |
| 10 | Build and install rmptui from this checkout, or uninstall it. |
| 11 | |
| 12 | Usage: |
| 13 | ./install.sh [--bin-dir DIR] |
| 14 | ./install.sh --uninstall [--bin-dir DIR] |
| 15 | |
| 16 | Options: |
| 17 | --bin-dir DIR Install/remove rmptui in DIR (default: /usr/local/bin) |
| 18 | --uninstall Remove the installed rmptui executable |
| 19 | -h, --help Show this help |
| 20 | EOF |
| 21 | } |
| 22 | |
| 23 | fail() { |
| 24 | printf 'install.sh: %s\n' "$1" >&2 |
| 25 | exit 1 |
| 26 | } |
| 27 | |
| 28 | while [ "$#" -gt 0 ]; do |
| 29 | case "$1" in |
| 30 | --bin-dir) |
| 31 | [ "$#" -ge 2 ] || fail "--bin-dir requires a directory" |
| 32 | [ -n "$2" ] || fail "--bin-dir cannot be empty" |
| 33 | BIN_DIR=$2 |
| 34 | shift 2 |
| 35 | ;; |
| 36 | --uninstall) |
| 37 | [ "$ACTION" = install ] || fail "--uninstall cannot be specified more than once" |
| 38 | ACTION=uninstall |
| 39 | shift |
| 40 | ;; |
| 41 | -h|--help) |
| 42 | usage |
| 43 | exit 0 |
| 44 | ;; |
| 45 | *) |
| 46 | fail "unknown option: $1 (use --help for usage)" |
| 47 | ;; |
| 48 | esac |
| 49 | done |
| 50 | |
| 51 | run_privileged() { |
| 52 | if [ "$(id -u)" -eq 0 ]; then |
| 53 | "$@" |
| 54 | elif command -v sudo >/dev/null 2>&1; then |
| 55 | sudo "$@" |
| 56 | else |
| 57 | fail "sudo is required to write to $BIN_DIR; install sudo or run this script as root" |
| 58 | fi |
| 59 | } |
| 60 | |
| 61 | SCRIPT_DIR=$(CDPATH= cd "$(dirname "$0")" && pwd) |
| 62 | DESTINATION=$BIN_DIR/rmptui |
| 63 | |
| 64 | if [ "$ACTION" = uninstall ]; then |
| 65 | if [ ! -e "$DESTINATION" ]; then |
| 66 | printf 'rmptui is not installed at %s\n' "$DESTINATION" |
| 67 | exit 0 |
| 68 | fi |
| 69 | run_privileged rm -f "$DESTINATION" |
| 70 | printf 'Removed %s\n' "$DESTINATION" |
| 71 | exit 0 |
| 72 | fi |
| 73 | |
| 74 | if ! command -v cargo >/dev/null 2>&1; then |
| 75 | fail "Cargo was not found. Install Rust and Cargo from https://rustup.rs/ and try again." |
| 76 | fi |
| 77 | |
| 78 | printf 'Building rmptui from %s\n' "$SCRIPT_DIR" |
| 79 | cargo build --release --locked --manifest-path "$SCRIPT_DIR/Cargo.toml" |
| 80 | |
| 81 | EXECUTABLE=$SCRIPT_DIR/target/release/rmptui |
| 82 | [ -x "$EXECUTABLE" ] || fail "build succeeded but executable was not found at $EXECUTABLE" |
| 83 | |
| 84 | run_privileged install -d "$BIN_DIR" |
| 85 | run_privileged install -m 0755 "$EXECUTABLE" "$DESTINATION" |
| 86 | printf 'Installed rmptui to %s\n' "$DESTINATION" |