-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·188 lines (150 loc) · 4.32 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·188 lines (150 loc) · 4.32 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
#!/usr/bin/env bash
set -euo pipefail
# ============================================================
# Walkum Global Scripts Installer
#
# Finds every .js, .py, and .sh file under src/
# and makes it globally executable through bin/.
#
# Example:
#
# src/av/record_audio.js
# ↓
# bin/record_audio
#
# ============================================================
REPO_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
SRC_DIR="$REPO_DIR/src"
BIN_DIR="$REPO_DIR/bin"
PATH_LINE='export PATH="$HOME/github/scripts/bin:$PATH"'
echo "Walkum Global Scripts"
echo "====================="
echo
echo "Repository: $REPO_DIR"
echo "Source: $SRC_DIR"
echo "Bin: $BIN_DIR"
echo
# ------------------------------------------------------------
# Verify source directory
# ------------------------------------------------------------
if [[ ! -d "$SRC_DIR" ]]; then
echo "ERROR: Source directory does not exist:"
echo " $SRC_DIR"
exit 1
fi
mkdir -p "$BIN_DIR"
# ------------------------------------------------------------
# Add bin to PATH
# ------------------------------------------------------------
if ! grep -Fqx "$PATH_LINE" "$HOME/.bashrc" 2>/dev/null; then
{
echo
echo "# Walkum global scripts"
echo "$PATH_LINE"
} >> "$HOME/.bashrc"
echo "Added ~/github/scripts/bin to ~/.bashrc"
fi
# Make available immediately.
case ":$PATH:" in
*":$BIN_DIR:"*)
;;
*)
export PATH="$BIN_DIR:$PATH"
;;
esac
# ------------------------------------------------------------
# Remove broken symlinks
# ------------------------------------------------------------
find "$BIN_DIR" \
-maxdepth 1 \
-type l \
! -exec test -e {} \; \
-delete
installed=0
# ------------------------------------------------------------
# Find ONLY .js, .py, and .sh files
# ------------------------------------------------------------
while IFS= read -r -d '' script; do
filename="$(basename "$script")"
command="${filename%.*}"
destination="$BIN_DIR/$command"
# --------------------------------------------------------
# Automatically make executable
# --------------------------------------------------------
chmod +x "$script"
echo "EXEC $script"
# --------------------------------------------------------
# Validate command name
# --------------------------------------------------------
if [[ ! "$command" =~ ^[a-zA-Z0-9._-]+$ ]]; then
echo "SKIP Invalid command name: $command"
echo
continue
fi
# --------------------------------------------------------
# Don't overwrite a real file
# --------------------------------------------------------
if [[ -e "$destination" && ! -L "$destination" ]]; then
echo
echo "ERROR:"
echo " $destination already exists and is not a symlink."
echo
exit 1
fi
# --------------------------------------------------------
# Existing symlink
# --------------------------------------------------------
if [[ -L "$destination" ]]; then
existing="$(readlink -f "$destination" || true)"
current="$(readlink -f "$script")"
if [[ "$existing" == "$current" ]]; then
echo "OK $command"
echo
((installed+=1))
continue
fi
echo
echo "ERROR: Command collision:"
echo
echo " Command: $command"
echo
echo "Existing:"
echo " $existing"
echo
echo "New:"
echo " $current"
echo
exit 1
fi
# --------------------------------------------------------
# Create symlink
# --------------------------------------------------------
ln -s "$script" "$destination"
echo "LINK $command"
echo " -> $script"
echo
((installed+=1))
done < <(
find "$SRC_DIR" \
-type f \
\( \
-name '*.js' \
-o -name '*.py' \
-o -name '*.sh' \
\) \
-print0
)
# ------------------------------------------------------------
# Summary
# ------------------------------------------------------------
echo "====================="
echo "Installation complete"
echo "====================="
echo
echo "Installed: $installed"
echo
echo "Run:"
echo
echo " source ~/.bashrc"
echo
echo "Done."