-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwepeeter
executable file
·116 lines (106 loc) · 2.76 KB
/
wepeeter
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 -e
#
# Usage: wepeeter [-cdps] REPEATS FILE...
#
# Makes REPEATS-many directories within a subdirectory (named weepeets)
# of the current directory containing symlinks to the specified files.
#
# The main use of this is to give higher weight to preferred files when
# passing a directory to a media player, e.g., your favorite songs get
# played more often.
#
# If REPEATS is 0 then the number of repeats will be chosen
# interactively per-file with the option to preview each file with a
# user-specified program. The -p argument can be used to specify the
# previewer program non-interactively.
#
# The -c argument runs wepeeter on each non-hidden file newer than the
# wepeets directory, then updates the mtime on the wepeets directory if
# successful.
#
# The -p argument allows a previewer to be specified, e.g., -p mpv
#
# The -s argument shows broken symlinks.
#
# The -d argument deletes broken symlinks.
#
unset -v since_change clean_dead previewer show_dead
while getopts 'cdp:s' o; do
case "$o" in
c) since_change=1 ;;
d) clean_dead=1 ;;
p) previewer="$OPTARG" ;;
s) show_dead=1 ;;
#h) help; exit 0 ;;
#*) help; exit 1 ;;
esac
done
shift $((OPTIND-1))
if [ "$clean_dead" ] || [ "$show_dead" ]; then
for l in wepeets/*/*; do
if [ -L "$l" ] && ! [ -f "$(realpath "$l" 2>/dev/null)" ]; then
if [ "$clean_dead" ]; then
rm "$l"
else
printf '%s\n' "$l"
fi
fi
done
exit 0
fi
wepeet() {
repeats="${1:?}"
shift 1
test "${1:?}"
# remove existing symlinks for the specified files
for dir in wepeets/*/; do
case "$dir" in "wepeets/*/") break;; esac
for file; do
rm -f "$dir/$(basename "$file")"
done
# remove wepeet directories if they're empty
rmdir "$dir" 2> /dev/null || :
done
i=1
while [ "$i" -le "$repeats" ]; do
mkdir -p "wepeets/wepeet-${i}"
for file; do
# prefix relative paths with ../../
case "$file" in /*) ;; *) file="../../$file" ;; esac
ln -sf "$file" "wepeets/wepeet-${i}/"
done
i=$((i+1))
done
}
if [ "$since_change" ]; then
if [ ! -d ./wepeets/ ]; then
echo "No wepeets directory found despite -c (run wepeeter normally first)" >&2
exit 1
fi
find . -type f -newer ./wepeets/ ! -path '*/.*' -exec $0 ${previewer:+-p $previewer} 0 {} +
touch ./wepeets/
exit
fi
if [ "${1:?}" -eq 0 ]; then
if [ ! "$previewer" ]; then
echo 'Program for previewing files (leave blank to disable previews): ' >&2
read -r previewer
fi
shift 1
for file; do
unset -v repeats
while [ ! "$repeats" ]; do
if [ "$previewer" ]; then
( set -f; $previewer "$file"; )
fi
echo "Number of repeats for $file (leave blank to preview again): " >&2
read -r repeats
if [ "$repeats" ] && ! [ "$repeats" -ge 0 ]; then
unset -v repeats
fi
done
wepeet "$repeats" "$file"
done
else
wepeet "$@"
fi