-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuglytagscript.sh
executable file
·137 lines (123 loc) · 3.32 KB
/
uglytagscript.sh
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
#!/bin/bash
METAFLAC="metaflac"
SEDS=( )
RAWTAGS=( )
FILES=( )
GUESSTITLE=0
GUESSARTIST=0
GUESSALBUM=0
DEBUG=0
AUTONUM=0
USAGE="Just do it right!"
while getopts "ade:nr:lt" flag
do
case $flag in
a)
GUESSARTIST=1;;
d)
DEBUG=1;;
e)
SEDS=( "${SEDS[@]}" "$OPTARG" );;
n)
AUTONUM=1;;
r)
RAWTAGS=( "${RAWTAGS[@]}" "$OPTARG" );;
l)
GUESSALBUM=1;;
t)
GUESSTITLE=1;;
esac
done
FILES=( "${@:$OPTIND}" )
for f in "${FILES[@]}"
do
READLINK=`readlink -f "$f"`
DIRNAME=`dirname "$READLINK"`
BASENAME=`basename "$READLINK"`
BASEDIRNAME=`basename "$DIRNAME"`
# Process all tags with sed, note that the tag names are included in the
# calls to sed, so using the arguments "-e 's/ARTIST/bleh'" will ruin the
# tags
if [ "${#SEDS}" -gt 0 ]
then
TAGEXPORT=`$METAFLAC --export-tags-to=- "$f"`
for s in "${SEDS[@]}"
do
TAGEXPORT=`echo "$TAGEXPORT" | sed -e "$s"`
done
`$METAFLAC --remove-all-tags "$f"`
`echo "$TAGEXPORT" $METAFLAC --import-tags-from=- "$f"`
fi
# Add tags supplied in raw form to the arguments, if a tag with the same
# name is already present it will be removed
for r in "${RAWTAGS[@]}"
do
`$METAFLAC --remove-tag=$(echo "$r" | sed 's/=.*\$//') "$f"`
`$METAFLAC --set-tag="$r" "$f"`
done
# Add an ARTIST tag based on the directory name of the file.
if [ $GUESSARTIST -gt 0 ]
then
ARTISTGUESS=`echo $BASEDIRNAME | sed \
-e 's/_/ /g' \
-e 's/[ \t]*-.*$//' \
-e 's/\(^\| \)./\U&/g'` #capitalize
`$METAFLAC --remove-tag="ARTIST" "$f"`
`$METAFLAC --set-tag="ARTIST=$ARTISTGUESS" "$f"`
fi
# Add an ALBUM tag based on the directory name of the file.
if [ $GUESSALBUM -gt 0 ]
then
ALBUMGUESS=`echo $BASEDIRNAME | sed \
-e 's/_/ /g' \
-e 's/^.*-[ \t]*//' \
-e 's/\(^\| \)./\U&/g'` #capitalize
`$METAFLAC --remove-tag="ALBUM" "$f"`
`$METAFLAC --set-tag="ALBUM=$ALBUMGUESS" "$f"`
fi
# Add a TITLE tag based on the name of the file.
if [ $GUESSTITLE -gt 0 ]
then
TITLEGUESS=`echo $BASENAME | sed \
-e 's/_/ /g' \
-e 's/^[^a-zA-Z]*//' \
-e 's/\..*$//' \
-e 's/\(^\| \)./\U&/g'` #capitalize
`$METAFLAC --remove-tag="TITLE" "$f"`
`$METAFLAC --set-tag="TITLE=$TITLEGUESS" "$f"`
fi
done
# Automatically set TRACKNUMBER based on all files given sorted lexically
if [ $AUTONUM -gt 0 ]
then
for f1 in "${FILES[@]}"
do
INDEX=1
for f2 in "${FILES[@]}"
do
if [ "$f1" \> "$f2" ]
then
INDEX=$(( $INDEX + 1 ))
fi
done
`$METAFLAC --remove-tag="TOTALTRACKS" "$f1"`
`$METAFLAC --set-tag="TOTALTRACKS=${#FILES[@]}" "$f1"`
`$METAFLAC --remove-tag="TRACKNUMBER" "$f1"`
`$METAFLAC --set-tag="TRACKNUMBER=$INDEX" "$f1"`
done
fi
if [ $DEBUG -gt 0 ]
then
for s in "${SEDS[@]}"
do
echo $s is a sed
done
for f in "${FILES[@]}"
do
echo $f is a file
done
for r in "${RAWTAGS[@]}"
do
echo $r is a raw tag
done
fi