-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink
More file actions
executable file
·69 lines (56 loc) · 1.79 KB
/
link
File metadata and controls
executable file
·69 lines (56 loc) · 1.79 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
#!/bin/sh
# shellcheck shell=sh
CLONE_DIR=$(
cd "$(dirname "$0")" || exit 1
pwd
)
in_path() { command -v "$1" 1>/dev/null; }
DST_DIR="$HOME/bin"
mkdir -p "$DST_DIR"
for PAIR in "bash,#!/usr/bin/env bash" "sh,#!/usr/bin/env sh" "py,#!/usr/bin/env python3"; do
SUFFIX=${PAIR%,*}
SHEBANG_LINE=${PAIR#*,}
SHEBANG_PATH=${PAIR#*\#\!}
SHEBANG_PATH=${SHEBANG_PATH% *}
if ! in_path "$SHEBANG_PATH"; then
echo "Warning: Skipping ALL '*.$SUFFIX' files as '$SHEBANG_PATH' isn't in \$PATH" >&2
continue
fi
for SRC_PATH in "$CLONE_DIR/"*."$SUFFIX"; do
[ ! -f "$SRC_PATH" ] && echo "No *.$SUFFIX files" >&2 && continue
NOSUFFIX_NAME=$(basename "$SRC_PATH")
NOSUFFIX_NAME=${NOSUFFIX_NAME%%."$SUFFIX"}
SCRIPT="$(dirname "$SRC_PATH")/.$(basename "$SRC_PATH")"
if ! [ -x "$SRC_PATH" ]; then
echo "Warning: Skipping non executable '$SRC_PATH'" >&2
continue
fi
if [ "$(head -n 1 "$SRC_PATH")" != "$SHEBANG_LINE" ]; then
echo "Warning: Skipping '$SRC_PATH' as it has a malformed or missing shebang" >&2
continue
fi
DST_PATH="$DST_DIR/$NOSUFFIX_NAME"
if [ -e "$DST_PATH" ]; then
if [ "$(stat -c '%d:%i' "$SRC_PATH")" = "$(stat -c '%d:%i' "$DST_PATH")" ]; then
continue
elif
diff -q "$SRC_PATH" "$DST_PATH" >/dev/null 2>&1
then
echo "Note: Replacing '$DST_PATH' with a link (same content)" >&2
rm -fv "$DST_PATH"
else
echo "Warning: Skipping link as '$SRC_PATH' differs from '$DST_PATH'" >&2
continue
fi
fi
if [ -z "$MSG_SHOWN" ]; then
echo "Linking files from $CLONE_DIR to $DST_DIR..." >&2
MSG_SHOWN='true'
fi
if [ -e "$SCRIPT" ]; then
"$SCRIPT" && ln -v "$SRC_PATH" "$DST_PATH"
else
ln -v "$SRC_PATH" "$DST_PATH"
fi
done
done