This repository has been archived by the owner on Aug 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdls2
executable file
·116 lines (104 loc) · 4.3 KB
/
dls2
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
#!/bin/sh
# DEP: (ls, readlink, mkdir, ln, rmdir)
nl='
'
do_link_check() {
source_path="$1";target_path="$2";top_path="$source_path";
ls -A1 -R "$source_path" | while read -r entry; do
[ "$entry" != "${entry%:}" ] && top_path="${entry%:}" && continue
[ -z "$entry" ] && continue
entry_source="$top_path/$entry"
entry_target="$target_path${entry_source#$source_path}"
# Make sure dir exist or can be created.
[ -d "$entry_source" ] && [ ! -d "$entry_target" ] && [ -e "$entry_target" ] && {
echo "[ERROR] $entry_source => $entry_target"
printf "\t---> %s needed to be a directory! <---\n" "$entry_target"
return 1
}
[ -f "$entry_source" ] && [ -e "$entry_target" ] && {
echo "[ERROR] $entry_source => $entry_target"
printf "\t---> %s file exist! <---\n" "$entry_target"
return 1
}
[ -n "$verbose" ] && echo "[CHECK:OK] $entry_source"
done
}
do_link() {
source_path="$1";target_path="$2";top_path="$source_path";
ls -A1 -R "$source_path" | while read -r entry; do
[ "$entry" != "${entry%:}" ] && top_path="${entry%:}" && continue
[ -z "$entry" ] && continue
entry_source="$top_path/$entry"
entry_target="$target_path${entry_source#$source_path}"
[ -d "$entry_source" ] && mkdir -p "$entry_target"
[ -f "$entry_source" ] && ln -sr "$entry_source" "$entry_target"
[ -n "$verbose" ] && echo "[LINK] $entry_source => $entry_target"
done
}
do_unlink() {
source_path="$1";target_path="$2";top_path="$source_path";
dirs=""
ls -A1 -R "$source_path" | {
while read -r entry; do
[ "$entry" != "${entry%:}" ] && top_path="${entry%:}" && continue
[ -z "$entry" ] && continue
entry_source="$top_path/$entry"
entry_target="$target_path${entry_source#$source_path}"
[ -d "$entry_source" ] && dirs="$entry_target$nl$dirs" && continue
[ -e "$entry_target" ] && [ ! -L "$entry_target" ] && {
echo "[WARN] $entry_target is not symbolic in nature."
continue
}
symbolic_trace="$(readlink -f "$entry_target")"
[ -L "$entry_target" ] && [ "$symbolic_trace" == "$entry_source" ] && {
rm "$entry_target"
[ -n "$verbose" ] && echo "[UNLINK] $entry_target is removed."
}
done
for entry_target in $dirs; do
[ -z "$(ls -A "$entry_target")" ] && rmdir "$entry_target"
[ -n "$verbose" ] && echo "[UNLINK] $entry_target is removed." || continue
done
}
}
TEMP=$(getopt -o 'ht:SDR' --long 'help,target:,link,delete,re-link' -n 'new-dls' -- "$@") || echo "$useage"
eval set -- "${TEMP}"
unset TEMP
TARGET_DIR="${HOME}"
while true; do
case "$1" in
'-h'|'--help')
echo "$useage" && exit 0
shift; continue;;
'-t'|'--target')
TARGET_DIR="$2"
shift 2; continue;;
'-S'|'--link')
action="link"
shift; break;;
'-D'|'--delete')
action="delete"
shift; break;;
'-R'|'--re-link')
action="re-link"
shift; break;;
'--')
shift; break;;
*)
echo 'Internal error!' >&2
echo "$useage" && exit 1;;
esac
done
export verbose=1
for source_path in "$@"; do
[ "$source_path" = '--' ] && continue
source_path="$(readlink -f "$source_path")"
if [ "$action" = "link" ]; then
do_link_check "$source_path" "$TARGET_DIR" && do_link "$source_path" "$TARGET_DIR"
elif [ "$action" = "delete" ]; then
do_unlink "$source_path" "$TARGET_DIR"
elif [ "$action" = "re-link" ]; then
do_unlink "$source_path" "$TARGET_DIR"
do_link_check "$source_path" "$TARGET_DIR" && do_link "$source_path" "$TARGET_DIR"
fi
done