-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoripper.sh
executable file
·156 lines (131 loc) · 3.73 KB
/
autoripper.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/bash
# autoripper wrapper script to check if audio cd is new (as in: yet unripped)
# and run abcde accordingly
# CONFIG is in autoripper.conf
# TODO: use as much as possible from abcde.conf
SELF=$0
CDROM=/dev/$1 # TODO: check if this argument is supplied - if not -> do_eject
DIRNAME=$(dirname $SELF)
. $DIRNAME/autoripper.conf
if [ -f $DIRNAME/autoripper.conf.local ]
then
. $DIRNAME/autoripper.conf.local
fi
# global variables
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:${MYDIR}
# "selected" array of cherrypicked tracks
declare -a selected
# "available" array of all tracks on disk
declare -a available
# stop global variables
# deal with errors
function do_eject {
# eject $CDROM
echo "process ended."
}
# check which audio cd this is and how many tracks it has
function get_cddb_info {
echo "Getting CDDB infos..."
CDDBTEMP=/tmp/.cddb
$ABCDE -a cddb -N -d $CDROM > $CDDBTEMP
ERR=$?
# TODO: for parsing cddb file we can also use acde function "do_cddbparse"
if [ $ERR -eq 0 ]
then
TMPDIR=$( tail -n1 /tmp/.cddb | cut -d ' ' -f 4 )
TMPDIR=${TMPDIR%?}
RETCODE=$( head -n1 $TMPDIR/cddbquery | cut -d ' ' -f 1 )
case $RETCODE in
200|210) # one or more entry
ALBUMARTIST=$( grep DTITLE $TMPDIR/cddbread.1 | cut -d '=' -f 2 )
# build list of tracks for later use (see check_local and select_tracks)
TRACKS=$(expr $(cat $TMPDIR/cddbread.1 | grep TTITLE | tail -n1 | cut -d '=' -f 1 | cut --complement -b 1-6) + 1)
for i in $(seq 0 $(expr $TRACKS - 1)) # check if each number ...
do
available+=( "$( grep TTITLE${i}= $TMPDIR/cddbread.1 | cut -d = -f 2 )" ) # ... add to list of available tracks
done
;;
202) # no entry
do_eject
echo "No CDDB entry for this disc"
rm $CDDBTEMP
exit 0
;;
*) # all other stuff
do_eject
echo "ERR: ${CDDBTOOL} exits with code $RETCODE"
cat $CDDBTEMP
rm $CDDBTEMP
exit 0
;;
esac
else
do_eject
echo "ERR: abcde exits with error $ERR"
fi
rm $CDDBTEMP
}
# compare local files and cddb info
function check_local {
# TODO: for parsing cddb file we can also use acde function "do_cddbparse"
echo "Checking for existing files on disk..."
ARTIST=$( echo $ALBUMARTIST | cut -d '/' -f 1 )
ARTIST=${ARTIST%?}
ALBUM=$( echo $ALBUMARTIST | cut -d '/' -f 2 )
ALBUM=${ALBUM#?}
# is there a directory for this artist/album combination?
# TODO: use abcde.conf / OUTPUTFORMAT
DESTDIR="${DESTINATION}/${ARTIST}/${ALBUM}"
echo $DESTDIR
if [ -d "$DESTDIR" ] # album directory exists
then
# leave it up to select_track which or if any tracks need to be ripped
LOCALCHECK="incomplete"
else # new album
# => return newalbum
LOCALCHECK="newalbum"
fi
}
# select tracks according to check_local()
function select_tracks {
echo "Existing files found - only selecting non-existing ones..."
for i in $(seq 0 $(expr $TRACKS - 1)) # check if each number ...
do
# check for each file vs. track name
# TODO: use abcde.conf / OUTPUTFORMAT
if [ $(find "$DESTDIR" -type f -name "*${available[i]}.${AUDIOFORMAT}" | wc -l) -eq 0 ] # ... is not there and if so ...
then
selected+=( "$(expr $i + 1)" ) # ... add to list of selected tracks
fi
done
echo "Number of tracks selected: ${#selected[@]}"
}
# do the actual ripping
function do_ripping {
echo "Now starting ripping process..."
${MYDIR}/abcde.sh "${ABCDE}" "${MYDIR}" "${CDROM}" "${selected[*]}"
echo "Finished ripping."
}
### MAIN ###
get_cddb_info
check_local
case ${LOCALCHECK} in
"incomplete")
select_tracks
if ( [ ${#selected[@]} -eq 0 ] )
then
do_eject
else
do_ripping
fi
;;
"complete")
do_eject
;;
"newalbum")
do_ripping
;;
*)
do_eject
;;
esac