-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall
More file actions
executable file
·130 lines (109 loc) · 3.37 KB
/
install
File metadata and controls
executable file
·130 lines (109 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/bash
set -euo pipefail
usage() {
cat <<EOF
Usage: sudo ./install [OPTIONS]
Installs the backUP tool and optional Bash auto-completion.
Options:
-u <user> User to install auto-completion for (default: \$SUDO_USER)
-d <dir> Destination directory for bash completions (default: ~<user>/.bash_completion.d)
-n Do NOT install bash completion
-h Show this help message
EOF
exit 0
}
# Default values
user="${SUDO_USER:-}"
completion_dir=""
install_completion=true
# Parse options
while getopts ":u:d:nh" opt; do
case "$opt" in
u) user="$OPTARG" ;;
d) completion_dir="$OPTARG" ;;
n) install_completion=false ;;
h) usage ;;
:) echo "Missing argument for -$OPTARG" >&2; exit 1 ;;
\?) echo "Unknown option: -$OPTARG" >&2; usage ;;
esac
done
# Confirm user is root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root (e.g., with sudo)" >&2
exit 1
fi
# Determine user home
if [[ -z "$user" ]]; then
echo "No user provided; attempting to use SUDO_USER..."
[[ -n "${SUDO_USER:-}" ]] || { echo "Error: -u required when SUDO_USER is not set." >&2; exit 1; }
user="$SUDO_USER"
fi
user_home=$(eval echo "~$user")
[[ -d "$user_home" ]] || { echo "Error: Home directory for $user not found." >&2; exit 1; }
if [[ -z "$completion_dir" ]]; then
completion_dir="$user_home/.bash_completion.d"
fi
# Install binary and library files
echo "Installing backUP and support files..."
declare -A FILES=(
[backUP]="/usr/local/bin/backUP"
[args2array]="/usr/local/lib/backUP/args2array"
[logging]="/usr/local/lib/backUP/logging"
)
mkdir -p /usr/local/lib/backUP /usr/local/etc/backUP
for src in "${!FILES[@]}"; do
dst="${FILES[$src]}"
if [[ -e "$dst" ]]; then
if cmp -s "$src" "$dst"; then
echo "✓ $dst is up to date, skipping"
continue
else
read -p "⚠ $dst differs from source. Overwrite? [y/N] " ans
[[ "$ans" =~ ^[Yy]$ ]] || { echo "Skipping $dst"; continue; }
fi
else
echo "→ Installing $dst"
fi
install -o root -g root -m 755 "$src" "$dst"
done
for cfg in /usr/local/etc/backUP/backUP.conf /usr/local/etc/backUP/backup.txt; do
[[ -e "$cfg" ]] || touch "$cfg"
chmod 660 "$cfg"
chown root:root "$cfg"
done
# Install bash completion if enabled
if $install_completion; then
echo "Setting up Bash completion..."
mkdir -p "$completion_dir"
chown "$user":"$user" "$completion_dir"
completion_src=".bash_completion.d/backUP"
completion_dst="$completion_dir/backUP"
if [[ -e "$completion_dst" ]]; then
if cmp -s "$completion_src" "$completion_dst"; then
echo "✓ Bash completion is up to date, skipping"
else
read -p "⚠ Bash completion differs. Overwrite $completion_dst? [y/N] " ans
[[ "$ans" =~ ^[Yy]$ ]] && cp "$completion_src" "$completion_dst"
fi
else
cp "$completion_src" "$completion_dst"
echo "→ Installed bash completion to $completion_dst"
fi
chown "$user":"$user" "$completion_dst"
# Add sourcing line to .bashrc if needed
bashrc="$user_home/.bashrc"
source_line="[[ -f \"$completion_dst\" ]] && source \"$completion_dst\""
if ! grep -Fxq "$source_line" "$bashrc"; then
read -p "→ Would you like to add sourcing to $bashrc? [y/N] " ans
if [[ "$ans" =~ ^[Yy]$ ]]; then
echo "$source_line" >> "$bashrc"
echo "✓ Added to $bashrc"
else
echo "Skipping .bashrc modification"
fi
else
echo "✓ .bashrc already sources the completion"
fi
else
echo "Skipping bash completion setup (-n was passed)"
fi