-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·132 lines (105 loc) · 3.56 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·132 lines (105 loc) · 3.56 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
#!/usr/bin/env bash
: <<'EOF'
Installs git_auto_pull.sh and registers it to run every 30 minutes:
- macOS: launchd agent (~/Library/LaunchAgents)
- Linux: systemd --user timer (~/.config/systemd/user)
Safe to re-run: copies are overwritten and the scheduler is
reloaded/re-enabled rather than duplicated.
Pass --uninstall to remove the symlink and scheduler registration.
EOF
set -euo pipefail
BIN_DIR="$HOME/.local/bin"
SCRIPT_NAME="git_auto_pull.sh"
LABEL="com.pythoninthegrass.git-auto-pull"
RAW_BASE="https://raw.githubusercontent.com/pythoninthegrass/git_auto_pull/main"
SCRIPT_SOURCE="${BASH_SOURCE[0]:-}"
FROM_LOCAL_CHECKOUT=1
if [ -n "$SCRIPT_SOURCE" ] && [ -f "$SCRIPT_SOURCE" ]; then
SCRIPT_DIR="$(cd "$(dirname "$SCRIPT_SOURCE")" && pwd)"
else
# Running via `curl | bash`: no local file, so fetch the files
# install.sh expects to sit alongside into a temp dir.
FROM_LOCAL_CHECKOUT=0
SCRIPT_DIR="$(mktemp -d)"
trap 'rm -rf "$SCRIPT_DIR"' EXIT
for f in "$SCRIPT_NAME" "${LABEL}.plist" git-auto-pull.service git-auto-pull.timer; do
curl -fsSL "${RAW_BASE}/${f}" -o "${SCRIPT_DIR}/${f}"
done
fi
install_script() {
mkdir -p "$BIN_DIR"
chmod +x "${SCRIPT_DIR}/${SCRIPT_NAME}"
if [ "$FROM_LOCAL_CHECKOUT" -eq 1 ]; then
# Symlink to the checkout so `git pull` there keeps the installed script current.
ln -sf "${SCRIPT_DIR}/${SCRIPT_NAME}" "${BIN_DIR}/${SCRIPT_NAME}"
else
# SCRIPT_DIR is a temp dir that gets removed on exit; copy instead of symlinking.
# rm first: cp -f won't write through a dangling symlink left by a prior run.
rm -f "${BIN_DIR}/${SCRIPT_NAME}"
cp -f "${SCRIPT_DIR}/${SCRIPT_NAME}" "${BIN_DIR}/${SCRIPT_NAME}"
fi
}
uninstall() {
case "$1" in
Darwin)
agents_dir="${HOME}/Library/LaunchAgents"
plist_dest="${agents_dir}/${LABEL}.plist"
uid="$(id -u)"
launchctl bootout "gui/${uid}" "$plist_dest" >/dev/null 2>&1 || true
rm -f "$plist_dest"
echo "Removed launchd agent: $plist_dest"
;;
Linux)
systemd_dir="${HOME}/.config/systemd/user"
systemctl --user disable --now git-auto-pull.timer >/dev/null 2>&1 || true
rm -f "${systemd_dir}/git-auto-pull.service" "${systemd_dir}/git-auto-pull.timer"
systemctl --user daemon-reload
echo "Removed systemd user timer: ${systemd_dir}/git-auto-pull.timer"
;;
*)
echo "Unsupported OS: $1" >&2
exit 1
;;
esac
rm -f "${BIN_DIR}/${SCRIPT_NAME}"
echo "Removed script symlink: ${BIN_DIR}/${SCRIPT_NAME}"
}
install() {
case "$1" in
Darwin)
agents_dir="${HOME}/Library/LaunchAgents"
plist_dest="${agents_dir}/${LABEL}.plist"
uid="$(id -u)"
mkdir -p "$agents_dir"
# Template ships with /Users/lance hardcoded; retarget to this $HOME.
sed "s|/Users/lance|${HOME}|g" "${SCRIPT_DIR}/${LABEL}.plist" >"$plist_dest"
launchctl bootout "gui/${uid}" "$plist_dest" >/dev/null 2>&1 || true
launchctl bootstrap "gui/${uid}" "$plist_dest"
launchctl enable "gui/${uid}/${LABEL}"
echo "Installed launchd agent: $plist_dest"
;;
Linux)
systemd_dir="${HOME}/.config/systemd/user"
mkdir -p "$systemd_dir"
cp -f "${SCRIPT_DIR}/git-auto-pull.service" "${systemd_dir}/git-auto-pull.service"
cp -f "${SCRIPT_DIR}/git-auto-pull.timer" "${systemd_dir}/git-auto-pull.timer"
systemctl --user daemon-reload
systemctl --user enable --now git-auto-pull.timer
echo "Installed systemd user timer: ${systemd_dir}/git-auto-pull.timer"
;;
*)
echo "Unsupported OS: $1" >&2
exit 1
;;
esac
}
main() {
if [ "${1:-}" = "--uninstall" ]; then
uninstall "$(uname -s)"
return
fi
install_script
echo "Installed script: ${BIN_DIR}/${SCRIPT_NAME}"
install "$(uname -s)"
}
main "$@"