-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall_nicebash.sh
More file actions
executable file
·141 lines (123 loc) · 4.18 KB
/
install_nicebash.sh
File metadata and controls
executable file
·141 lines (123 loc) · 4.18 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
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env bash
set -euo pipefail
# 1) Elevate to root if needed so we can write /root/.zshrc
if [[ $EUID -ne 0 ]]; then
echo "Re-running as root to install for both users…"
exec sudo bash "$0" "$@"
fi
# 2) Detect original (non-root) user & home
ORIGINAL_USER="${SUDO_USER:-$USER}"
ORIGINAL_HOME="$(eval echo "~$ORIGINAL_USER")"
# 3) Locate script dir (expects .zshrc next to it)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
if [[ ! -f "$SCRIPT_DIR/.zshrc" ]]; then
echo "ERROR: .zshrc not found in $SCRIPT_DIR" >&2
exit 1
fi
echo
echo "=== Installing system dependencies ==="
if command -v apt-get &>/dev/null; then
apt-get update
apt-get install -y \
zsh git curl direnv fzf \
zsh-syntax-highlighting zsh-autosuggestions
elif command -v yum &>/dev/null; then
yum install -y \
zsh git curl direnv fzf
# plugins will be cloned manually below
else
echo "⚠️ Unknown package manager – please install: zsh, git, curl, direnv, fzf," \
"zsh-syntax-highlighting, zsh-autosuggestions yourself." >&2
fi
echo
echo "=== Installing Oh-My-Zsh for $ORIGINAL_USER (if missing) ==="
if [[ ! -d "$ORIGINAL_HOME/.oh-my-zsh" ]]; then
su - "$ORIGINAL_USER" -c \
"export RUNZSH=no && \
sh -c \"\$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)\" --unattended"
else
echo "→ Already have Oh-My-Zsh at $ORIGINAL_HOME/.oh-my-zsh"
fi
echo
echo "=== Installing Oh-My-Zsh for root (if missing) ==="
if [[ ! -d "/root/.oh-my-zsh" ]]; then
RUNZSH=no CHSH=no KEEP_ZSHRC=yes \
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
else
echo "→ Already have Oh-My-Zsh at /root/.oh-my-zsh"
fi
# 4) Prepare custom plugin dirs
USER_OMZ_CUSTOM="$ORIGINAL_HOME/.oh-my-zsh/custom"
ROOT_OMZ_CUSTOM="/root/.oh-my-zsh/custom"
mkdir -p "$USER_OMZ_CUSTOM/plugins" "$ROOT_OMZ_CUSTOM/plugins"
echo
echo "=== Cloning community plugins for $ORIGINAL_USER ==="
for plugin_repo in \
https://github.com/zsh-users/zsh-autosuggestions \
https://github.com/zsh-users/zsh-syntax-highlighting \
https://github.com/Aloxaf/fzf-tab
do
name="$(basename "$plugin_repo")"
dest="$USER_OMZ_CUSTOM/plugins/$name"
if [[ ! -d "$dest" ]]; then
git clone "$plugin_repo" "$dest"
chown -R "$ORIGINAL_USER":"$ORIGINAL_USER" "$dest"
echo "→ Cloned $name"
fi
done
echo
echo "=== Cloning community plugins for root ==="
for plugin_repo in \
https://github.com/zsh-users/zsh-autosuggestions \
https://github.com/zsh-users/zsh-syntax-highlighting \
https://github.com/Aloxaf/fzf-tab
do
name="$(basename "$plugin_repo")"
dest="$ROOT_OMZ_CUSTOM/plugins/$name"
if [[ ! -d "$dest" ]]; then
git clone "$plugin_repo" "$dest"
echo "→ Cloned $name for root"
fi
done
echo
echo "=== .zshrc locations ==="
USER_ZSHRC="$ORIGINAL_HOME/.zshrc"
ROOT_ZSHRC="/root/.zshrc"
echo " • User .zshrc: $USER_ZSHRC"
echo " • Root .zshrc: $ROOT_ZSHRC"
read -p "Proceed to back up & overwrite both files? [Y/n] " yn
yn=${yn:-Y}
if [[ ! "$yn" =~ ^[Yy] ]]; then
echo "Aborting. No changes made."
exit 0
fi
timestamp="$(date +%Y%m%d%H%M%S)"
echo
echo "→ Backing up & installing for $ORIGINAL_USER"
if [[ -f "$USER_ZSHRC" ]]; then
cp "$USER_ZSHRC" "${USER_ZSHRC}.bak.$timestamp"
echo " backed up to ${USER_ZSHRC}.bak.$timestamp"
fi
cp "$SCRIPT_DIR/.zshrc" "$USER_ZSHRC"
chown "$ORIGINAL_USER":"$ORIGINAL_USER" "$USER_ZSHRC"
echo " installed → $USER_ZSHRC"
echo
echo "→ Backing up & installing for root"
if [[ -f "$ROOT_ZSHRC" ]]; then
cp "$ROOT_ZSHRC" "${ROOT_ZSHRC}.bak.$timestamp"
echo " backed up to ${ROOT_ZSHRC}.bak.$timestamp"
fi
cp "$SCRIPT_DIR/.zshrc" "$ROOT_ZSHRC"
cp -r "$USER_OMZ_CUSTOM/.." "/root/.oh-my-zsh"
echo " installed → $ROOT_ZSHRC"
echo
echo "=== Setting Zsh as default shell for $ORIGINAL_USER and root ==="
ZSH_PATH="$(command -v zsh)"
if [[ -x "$ZSH_PATH" ]]; then
chsh -s "$ZSH_PATH" "$ORIGINAL_USER" && echo "→ $ORIGINAL_USER shell set to Zsh"
chsh -s "$ZSH_PATH" root && echo "→ root shell set to Zsh"
else
echo "❌ Could not find Zsh binary to set as default shell."
fi
echo
echo "✔ Installation complete! Open a new shell or run 'exec zsh'."