Krolyxon
Commit 3a91ab1

rmptuiinstall.sh

86 lines · 2.1 KB · Bourne Again Shell (bash)

1#!/bin/sh
2
3set -eu
4
5BIN_DIR=/usr/local/bin
6ACTION=install
7
8usage() {
9 cat <<'EOF'
10Build and install rmptui from this checkout, or uninstall it.
11
12Usage:
13 ./install.sh [--bin-dir DIR]
14 ./install.sh --uninstall [--bin-dir DIR]
15
16Options:
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
20EOF
21}
22
23fail() {
24 printf 'install.sh: %s\n' "$1" >&2
25 exit 1
26}
27
28while [ "$#" -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
49done
50
51run_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
61SCRIPT_DIR=$(CDPATH= cd "$(dirname "$0")" && pwd)
62DESTINATION=$BIN_DIR/rmptui
63
64if [ "$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
72fi
73
74if ! 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."
76fi
77
78printf 'Building rmptui from %s\n' "$SCRIPT_DIR"
79cargo build --release --locked --manifest-path "$SCRIPT_DIR/Cargo.toml"
80
81EXECUTABLE=$SCRIPT_DIR/target/release/rmptui
82[ -x "$EXECUTABLE" ] || fail "build succeeded but executable was not found at $EXECUTABLE"
83
84run_privileged install -d "$BIN_DIR"
85run_privileged install -m 0755 "$EXECUTABLE" "$DESTINATION"
86printf 'Installed rmptui to %s\n' "$DESTINATION"