-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·218 lines (199 loc) · 7.93 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·218 lines (199 loc) · 7.93 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env bash
# Refocus Shell - Setup
set -euo pipefail
INSTALL_DIR="$HOME/.local/refocus"
BIN_DIR="$HOME/.local/bin"
SRC_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
_info() { echo " $1"; }
_say_ok() { echo "$1"; }
_warn() { echo " $1"; }
_die() { echo "$1" >&2; exit 1; }
install_deps() {
local missing=()
command -v sqlite3 &>/dev/null || missing+=(sqlite3)
command -v crontab &>/dev/null || missing+=(cron)
if [[ "$(uname -s)" == "Darwin" ]]; then
# macOS has no libnotify — focus-nudge already falls back to logger(1),
# and the notify-send calls in the handlers are `|| true` no-ops.
# gdate is what lets relative times ("2 hours ago") parse; core/time.sh
# works without it, but only for explicit formats.
command -v gdate &>/dev/null || missing+=(coreutils)
else
command -v notify-send &>/dev/null || missing+=(libnotify-bin)
fi
[[ ${#missing[@]} -eq 0 ]] && { _say_ok "Dependencies present."; return; }
_info "Installing: ${missing[*]}"
if command -v apt-get &>/dev/null; then
sudo apt-get install -y "${missing[@]}"
elif command -v pacman &>/dev/null; then
local pkgs=()
for pkg in "${missing[@]}"; do
case $pkg in
libnotify-bin) pkgs+=(libnotify);;
cron) pkgs+=(cronie);;
*) pkgs+=("$pkg");;
esac
done
sudo pacman -S --noconfirm "${pkgs[@]}"
elif command -v dnf &>/dev/null; then
local pkgs=()
for pkg in "${missing[@]}"; do
case $pkg in
libnotify-bin) pkgs+=(libnotify);;
cron) pkgs+=(cronie);;
*) pkgs+=("$pkg");;
esac
done
sudo dnf install -y "${pkgs[@]}"
elif command -v brew &>/dev/null; then
local pkgs=()
for pkg in "${missing[@]}"; do
case $pkg in
sqlite3) pkgs+=(sqlite);;
cron) ;; # macOS ships cron; nothing to install
*) pkgs+=("$pkg");;
esac
done
# Guard the expansion: macOS bash is 3.2, where "${empty[@]}" trips set -u.
[[ ${#pkgs[@]} -gt 0 ]] && brew install "${pkgs[@]}"
else
_warn "Unknown package manager. Install manually: ${missing[*]}"
fi
}
install_files() {
local db_tmp="" env_tmp=""
if [[ -d "$INSTALL_DIR" ]]; then
echo -n "Existing installation found. Code will be updated; data and config preserved. Continue? (yes/N): "
read -r ans || true
[[ "$ans" == "yes" ]] || { echo "Aborted."; exit 0; }
# Stash data before the code wipe
if [[ -f "$INSTALL_DIR/refocus.db" ]]; then
db_tmp=$(mktemp)
cp "$INSTALL_DIR/refocus.db" "$db_tmp"
fi
if [[ -f "$INSTALL_DIR/.env" ]]; then
env_tmp=$(mktemp)
cp "$INSTALL_DIR/.env" "$env_tmp"
fi
fi
# Strip any stale nudge/checkin cron entries before wiping (fixed-string — dots in path are literal)
local tmp; tmp=$(mktemp)
crontab -l 2>/dev/null | grep -vF "$INSTALL_DIR/focus-nudge" | grep -vF "$INSTALL_DIR/focus-checkin" > "$tmp" || true
crontab "$tmp"
rm -f "$tmp"
# Wipe and rebuild
rm -rf "$INSTALL_DIR"
mkdir -p "$INSTALL_DIR/services" "$INSTALL_DIR/lib" "$INSTALL_DIR/core" "$BIN_DIR"
cp "$SRC_DIR/env.sh" "$INSTALL_DIR/"
cp "$SRC_DIR/focus" "$INSTALL_DIR/"
cp "$SRC_DIR/focus-nudge" "$INSTALL_DIR/"
cp "$SRC_DIR/focus-checkin" "$INSTALL_DIR/"
cp "$SRC_DIR/services/"*.sh "$INSTALL_DIR/services/"
cp "$SRC_DIR/lib/"*.sh "$INSTALL_DIR/lib/"
cp "$SRC_DIR/core/"*.sh "$INSTALL_DIR/core/"
[[ -d "$SRC_DIR/docs" ]] && cp -r "$SRC_DIR/docs" "$INSTALL_DIR/docs"
chmod +x "$INSTALL_DIR/focus"
chmod +x "$INSTALL_DIR/focus-nudge"
chmod +x "$INSTALL_DIR/focus-checkin"
chmod +x "$INSTALL_DIR/lib/"*.sh
chmod +x "$INSTALL_DIR/services/"*.sh
# Restore stashed data
[[ -n "$db_tmp" ]] && { mv "$db_tmp" "$INSTALL_DIR/refocus.db"; _say_ok "Database preserved."; }
[[ -n "$env_tmp" ]] && { mv "$env_tmp" "$INSTALL_DIR/.env"; _say_ok "Config (.env) preserved."; }
ln -sf "$INSTALL_DIR/focus" "$BIN_DIR/focus"
_say_ok "Files installed to $INSTALL_DIR"
}
install_shell() {
local rc="$HOME/.bashrc"
local line="source $INSTALL_DIR/services/focus-function.sh"
if grep -qF "$line" "$rc" 2>/dev/null; then
_say_ok "Shell integration already in $rc"
else
{
echo ""
echo "# Refocus Shell"
echo "$line"
} >> "$rc"
_say_ok "Shell integration added to $rc"
_warn "Run: source ~/.bashrc"
fi
}
install_desktop_entry() {
# Registers Refocus with the desktop notification subsystem so nudges are
# logged in notification history. Plasma (and other XDG-compliant daemons)
# match the desktop-entry hint sent by focus-nudge against this file's
# basename: 'refocus'. Without it, the bubble shows but is discarded.
local app_dir="$HOME/.local/share/applications"
local desktop="$app_dir/refocus.desktop"
mkdir -p "$app_dir"
cat > "$desktop" <<'DESKTOP'
[Desktop Entry]
Type=Application
Name=Refocus
Comment=Focus and time tracker
Icon=appointment-soon
NoDisplay=true
NotifyRcName=refocus
DESKTOP
if command -v update-desktop-database &>/dev/null; then
update-desktop-database "$app_dir" 2>/dev/null || true
fi
_say_ok "Desktop entry installed (notifications will appear in history)."
}
init_and_enable() {
export REFOCUS_ROOT="$INSTALL_DIR"
source "$INSTALL_DIR/env.sh"
source "$INSTALL_DIR/services/database.sh"
source "$INSTALL_DIR/services/cron.sh"
db_init # INSERT OR IGNORE — no-op on a preserved DB
set_focus_enabled
cron_install || _warn "Could not arm cron nudge — run 'focus enable' manually."
cron_checkin_install || _warn "Could not arm check-in cron — run 'focus enable' manually."
_say_ok "Database ready and nudging armed."
}
case "${1:-install}" in
install)
echo "Installing Refocus Shell..."
install_deps
install_files
[[ -f "$INSTALL_DIR/refocus.db" ]] && _say_ok "Existing database preserved."
init_and_enable
install_shell
install_desktop_entry
echo ""
echo "Done. Open a new terminal or run: source ~/.bashrc"
;;
uninstall)
echo -n "Remove $INSTALL_DIR and shell integration? (yes/N): "
read -r ans || true
[[ "$ans" == "yes" ]] || { echo "Cancelled."; exit 0; }
if [[ -f "$INSTALL_DIR/services/cron.sh" ]]; then
export REFOCUS_ROOT="$INSTALL_DIR"
source "$INSTALL_DIR/services/cron.sh"
cron_remove 2>/dev/null || true
cron_checkin_remove 2>/dev/null || true
fi
rm -rf "$INSTALL_DIR"
rm -f "$BIN_DIR/focus"
rm -f "$HOME/.local/share/applications/refocus.desktop"
rc="$HOME/.bashrc"
# No `sed -i` (GNU takes a bare -i, BSD demands -i ''), and no `mv`
# over the original either: that would swap in mktemp's default 0600
# mode. Write the result back into the original file instead, so its
# permissions survive untouched.
if [[ -f "$rc" ]]; then
rc_tmp=$(mktemp "${rc}.XXXXXX")
# install_shell prepends a blank separator before its own two
# lines; strip it too, but only when it's immediately followed by
# our marker — an unrelated blank line elsewhere in .bashrc must
# survive untouched.
sed -e '/^$/{N;/\n# Refocus Shell/d}' -e '/# Refocus Shell/d' -e '/focus-function\.sh/d' "$rc" > "$rc_tmp" \
&& cat "$rc_tmp" > "$rc"
rm -f "$rc_tmp"
fi
_say_ok "Uninstalled."
;;
*)
echo "Usage: ./setup.sh [install|uninstall]" >&2; exit 2
;;
esac