-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit
More file actions
executable file
·181 lines (158 loc) · 6.13 KB
/
init
File metadata and controls
executable file
·181 lines (158 loc) · 6.13 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env bash
# init - Post-clone setup: create/rename scripts dir, install .aliases, and update ~/.bashrc (optionally runs fixnano).
# ──────────────────────────────────────────────────────
# Author: Don Ferris
# Created: 2025-10-24
# Current Revision: v1.1 - 2025-11-14
# ──────────────────────────────────────────────────────
# Revision History
# ----------------
# v1.1 — 2025-11-14 — Add installation of ntfy (used by mediasync.sh and generally useful for very long-running processes). Also added logic allowing init to be run multiple times without rerunning previously run commands.
# v1.0 — 2025-10-24 — Core functionality
######## ########
set -euo pipefail
info() { printf "\033[1;34m==> %s\033[0m\n" "$*"; }
warn() { printf "\033[1;33m!! %s\033[0m\n" "$*"; }
err() { printf "\033[1;31mERROR: %s\033[0m\n" "$*"; }
# Check if running as root
check_root() {
if [[ $EUID -ne 0 ]]; then
err "This script must be run as root"
exit 1
fi
}
# Install ntfy via Official Repository
install_ntfy() {
local NTFY_DEB_ARCH="amd64" # Change to "arm64" or "armhf" if on ARM
if ! command -v ntfy >/dev/null 2>&1; then
info "Installing ntfy..."
# Add the repository and key (method for newer Debian/Ubuntu)
mkdir -p /etc/apt/keyrings
curl -fsSL -o /etc/apt/keyrings/ntfy.gpg https://archive.ntfy.sh/apt/keyring.gpg
echo "deb [arch=${NTFY_DEB_ARCH} signed-by=/etc/apt/keyrings/ntfy.gpg] https://archive.ntfy.sh/apt stable main" | tee /etc/apt/sources.list.d/ntfy.list >/dev/null
apt update
apt install -y ntfy
info "ntfy installed successfully."
else
info "ntfy is already installed."
fi
}
# Test ntfy installation and provide usage instructions
test_ntfy() {
info "Testing ntfy installation..."
printf "\n\033[1mNTFY SETUP REQUIRED:\033[0m\n"
printf "To receive notifications on your mobile device:\n"
printf "1. Install the ntfy app from your app store\n"
printf "2. Open the app and subscribe to topic: mediasync-test\n"
printf "3. The app will listen for notifications sent to this topic\n"
printf "\nWhen you're ready, press any key to send a test notification...\n"
read -n 1 -r -s
if ntfy publish mediasync-test "MediaSync: Test notification from init script" 2>/dev/null; then
info "Test notification sent to topic: mediasync-test"
info "Check your mobile device for the notification"
else
warn "ntfy test failed"
info "You can manually test later with: ntfy publish your-topic 'message'"
fi
}
# Check for root privileges
check_root
# 1) Rename directory if needed
CUR="$(pwd -P)"
BASENAME="$(basename "$CUR")"
if [ "$BASENAME" = "bash-scripts" ]; then
PARENT="$(dirname "$CUR")"
TARGET="$PARENT/scripts"
if [ -e "$TARGET" ]; then
warn "Target directory $TARGET already exists; skipping rename."
else
info "Renaming '$CUR' -> '$TARGET'"
mv "$CUR" "$TARGET"
cd "$TARGET"
info "Now in $(pwd -P)"
fi
else
info "Current directory name is '$BASENAME' (no rename needed)."
fi
# 2) Ensure $HOME/scripts exists
HOME_SCRIPTS="$HOME/scripts"
if [ ! -d "$HOME_SCRIPTS" ]; then
info "Creating $HOME_SCRIPTS"
mkdir -p "$HOME_SCRIPTS"
else
info "$HOME_SCRIPTS already exists"
fi
# 3) Backup existing ~/.bashrc (if any) before appending
BASHRC="$HOME/.bashrc"
if [ -f "$BASHRC" ]; then
BACKUP="$BASHRC.bak.$(date +%Y%m%d%H%M%S)"
info "Backing up existing $BASHRC -> $BACKUP"
cp -a "$BASHRC" "$BACKUP"
else
info "No existing $BASHRC found; creating one via append operations."
touch "$BASHRC"
fi
# 4) Copy .aliases to $HOME
if [ -f "./.aliases" ]; then
info "Copying .aliases to $HOME/.aliases"
cp -f "./.aliases" "$HOME/.aliases"
else
warn "No .aliases found in repo root; skipping copy."
fi
# 5) Append PATH and source lines to ~/.bashrc if they're not already present
GREPPATHLINE='export PATH="$HOME/scripts:$PATH"'
GREPALIASLINE='source "$HOME/.aliases"'
# Use grep -F to match literal lines
if ! grep -Fq 'export PATH="$HOME/scripts:$PATH"' "$BASHRC"; then
info "Appending PATH addition to $BASHRC"
printf "\n# Added by init script: ensure user's scripts directory is first in PATH\nexport PATH=\"$HOME/scripts:$PATH\"\n" >> "$BASHRC"
else
info "PATH addition already present in $BASHRC"
fi
if ! grep -Fq 'source "$HOME/.aliases"' "$BASHRC"; then
info "Appending source of ~/.aliases to $BASHRC"
printf "\n# Added by init script: source user's aliases if present\nif [ -f \"$HOME/.aliases\" ]; then\n source \"$HOME/.aliases\"\nfi\n" >> "$BASHRC"
else
info "Aliases source already present in $BASHRC"
fi
# 6) Source the updated ~/.bashrc in the current shell
# shellcheck disable=SC1090
if [ -f "$BASHRC" ]; then
info "Sourcing $BASHRC"
# shellcheck disable=SC1090
source "$BASHRC" || true
info "Sourced $BASHRC. Current PATH begins with: $(echo "$PATH" | awk -F: '{print $1}')"
else
warn "$BASHRC not found to source"
fi
# 7) Show loaded aliases (if any)
info "Loaded aliases (if available):"
alias || true
# 8) Install ntfy
install_ntfy
# 9) Test ntfy and provide configuration instructions
test_ntfy
# 10) Prompt to run fixnano
printf "\n\033[1mfixnano\033[0m: Configure nano to enable mouse support and useful key bindings (improves terminal editing).\n"
read -n 1 -r -p "Run fixnano now? [y/N] " ANSWER
printf "\n"
if [[ "$ANSWER" =~ ^[Yy]$ ]]; then
if [ -f "./fixnano.sh" ]; then
info "Running local ./fixnano.sh"
bash ./fixnano.sh
else
info "Local fixnano.sh not found; downloading and running the version from the repository"
RAW_URL="https://raw.githubusercontent.com/don-ferris/bash-scripts/main/fixnano.sh"
TMP="$(mktemp)"
if command -v curl >/dev/null 2>&1; then
curl -fsSL "$RAW_URL" -o "$TMP"
else
wget -qO "$TMP" "$RAW_URL"
fi
bash "$TMP"
rm -f "$TMP"
fi
else
info "Skipping fixnano."
fi
info "Initialization complete. Open a new terminal to ensure PATH and aliases are applied persistently."