1
|
1 #!/usr/local/bin/tclsh8.2
|
|
2
|
|
3 #
|
|
4 # This software is copyright Daniel O'Connor (darius@dons.net.au) 1998, 1999
|
|
5 #
|
|
6 # This software is release under the GNU Public License Version 2.
|
|
7 # A copy of this licence must be distributed with this software.
|
|
8 #
|
|
9
|
|
10 proc main {} {
|
|
11 global argv0 argv tracks auto_path;
|
|
12
|
|
13 lappend auto_path "[file dirname $argv0]";
|
|
14 lappend auto_path ".";
|
|
15
|
|
16 if { [ catch {loadlibs [ list tk82 ]} msg] } {
|
|
17 log "Failed to load library files - $msg";
|
|
18 exit 1;
|
|
19 }
|
|
20 # puts $auto_path
|
|
21
|
|
22 if { [ llength $argv ] != 4 } {
|
|
23 puts "Bad usage";
|
|
24 puts "$argv0 <shellfile> <oldbasename> <newbasename> <infile>";
|
|
25 exit;
|
|
26 }
|
|
27
|
|
28 set shellfile [ lindex $argv 0 ];
|
|
29 set oldbasename [ lindex $argv 1 ];
|
|
30 set newbasename [ lindex $argv 2 ];
|
|
31 set infile [ lindex $argv 3 ]
|
|
32
|
|
33 if {[file exists $shellfile] && ![file writable $shellfile]} {
|
|
34 puts "$shellfile not writeable, aborting";
|
|
35 exit 1;
|
|
36 }
|
|
37
|
|
38 set tracks(albumname) "";
|
|
39 set tracks(albumartist) "";
|
|
40 set tracks(number) 0;
|
|
41
|
|
42 # If the file doesn't exist we assume the user wants to enter new data
|
|
43 if {![file exists $infile] && $infile != "-"} {
|
|
44 puts "Please enter the number of tracks on the CD..."
|
|
45 while { 1 } {
|
|
46 puts -nonewline "-> ";
|
|
47 flush stdout;
|
|
48 set tracks(number) [ gets stdin ];
|
|
49 if { ($tracks(number) >= 1) && ($tracks(number) <= 100) } {
|
|
50 break;
|
|
51 }
|
|
52 puts "Please enter a number between 1 and 100";
|
|
53 }
|
|
54
|
|
55 set tracks(disclen) 1;
|
|
56 set tracks(discid) 1;
|
|
57
|
|
58 for { set t_no 1 } { $t_no <= $tracks(number) } { incr t_no } {
|
|
59 set tracks(artist:$t_no) "";
|
|
60 set tracks(title:$t_no) "";
|
|
61 set tracks(offset:$t_no) 1;
|
|
62 }
|
|
63
|
|
64 } else {
|
|
65 if { $infile == "-" } {
|
|
66 set fh stdin;
|
|
67 } else {
|
|
68 set fh [ open $infile "r" ];
|
|
69 }
|
|
70
|
|
71 set offset_seek 0;
|
|
72 set offset_list "";
|
|
73
|
|
74 while {![eof $fh]} {
|
|
75 set line [gets $fh];
|
|
76 # puts "Line = $line";
|
|
77 if {$offset_seek > 0} {
|
|
78 if {$line == "\#"} {
|
|
79 # puts "Comment skip"
|
|
80 continue;
|
|
81 }
|
|
82
|
|
83 if {[ regexp {^\#[ ]+([0-9]+)} $line a offset] == 1} {
|
|
84 puts "Adding $offset";
|
|
85 lappend offset_list $offset;
|
|
86 continue;
|
|
87 }
|
|
88
|
|
89 # Change back to default state
|
|
90 puts "Not looking for offsets";
|
|
91 set offset_seek 0;
|
|
92 }
|
|
93
|
|
94 if {[regexp {^\# Track frame offsets:} $line] == 1} {
|
|
95 # Change to looking for offsets state
|
|
96 puts "Looking for offsets";
|
|
97 set offset_seek 1;
|
|
98 }
|
|
99
|
|
100 if {[regexp {^\# Disc length: +([0-9]+)} $line a tracks(disclen)] == 1} {
|
|
101 continue;
|
|
102 }
|
|
103
|
|
104 if {[regexp {([A-Z0-9]*)=(.*)} $line a key value] == 0} {
|
|
105 continue;
|
|
106 }
|
|
107
|
|
108 switch -regexp -- $key {
|
|
109 "DISCID" {
|
|
110 set tracks(discid) $value;
|
|
111 # puts "Got ID as $value";
|
|
112 }
|
|
113
|
|
114 "DTITLE" {
|
8
|
115 puts "Searching title - $value";
|
|
116 if {![regexp {^(.*) / (.*)$} $value a tracks(albumartist) tracks(albumname)]} {
|
|
117 if {![regexp {^(.*) - (.*)$} $value a tracks(albumartist) tracks(albumname)]} {
|
|
118 set tracks(albumname) $value;
|
|
119 }
|
1
|
120 }
|
8
|
121 puts "Artist - $tracks(albumartist)";
|
6
|
122 puts "Title - $tracks(albumname)";
|
1
|
123 }
|
|
124
|
|
125 "TTITLE([0-9]+)" {
|
|
126 regexp {TTITLE([0-9]+)} $key a t_no;
|
|
127 set t_no [ expr $t_no + 1 ];
|
|
128
|
6
|
129 if {[info exists tracks(title:$t_no)]} {
|
|
130 append tracks(title:$t_no) $value;
|
|
131 } else {
|
|
132 incr tracks(number);
|
|
133
|
|
134 if {![regexp {^(.*) - (.*)$} $value a tracks(artist:$t_no) tracks(title:$t_no)] == 1} {
|
|
135 if {![regexp {^(.*)--(.*)$} $value a tracks(artist:$t_no) tracks(title:$t_no)] == 1} {
|
|
136 if {![regexp {^(.*) / (.*)$} $value a tracks(artist:$t_no) tracks(title:$t_no)] == 1} {
|
|
137 set tracks(title:$t_no) $value;
|
|
138 }
|
|
139 }
|
1
|
140 }
|
|
141 }
|
|
142 }
|
|
143
|
|
144 "EXTT([0-9]+)" {
|
|
145 regexp {EXTT([0-9]+)} $key a t_no;
|
|
146 set t_no [ expr $t_no + 1 ];
|
|
147 set ext($t_no) $value;
|
|
148 }
|
|
149 }
|
|
150 }
|
|
151
|
|
152 # puts "Offset list - $offset_list";
|
|
153 # puts "Disclength - $tracks(disclen)";
|
|
154
|
|
155 if {$tracks(number) < 1 || [llength $offset_list] != $tracks(number)} {
|
|
156 puts stderr "Invalid file, $tracks(number)";
|
|
157 exit 1;
|
|
158 }
|
|
159
|
|
160 set i 1;
|
|
161 foreach t $offset_list {
|
|
162 set tracks(offset:$i) $t;
|
|
163 incr i;
|
|
164 }
|
|
165
|
|
166 for {set i 1} {$i <= $tracks(number)} {incr i} {
|
|
167 if {![info exists tracks(title:$i)]} {
|
6
|
168 # error "track $i has no title!";
|
|
169 set tracks(title:$i) "";
|
1
|
170 }
|
|
171
|
|
172 if {[info exists tracks(artist:$i)]} {
|
|
173 # puts "artist exists for $i";
|
|
174 continue;
|
|
175 }
|
|
176
|
6
|
177 if {$tracks(albumartist) != ""} {
|
1
|
178 set tracks(artist:$i) $tracks(albumartist);
|
|
179 continue;
|
|
180 }
|
|
181
|
|
182 if {[info exists ext($i)] && $ext($i) != ""} {
|
|
183 set tracks(artist:$i) $ext($i);
|
|
184 # puts "setting artist to $ext($i) for $i";
|
|
185 continue;
|
|
186 }
|
|
187
|
8
|
188 set tracks(artist:$i) "";
|
1
|
189 }
|
|
190
|
|
191 # puts "$tracks(number) tracks";
|
|
192
|
|
193 }
|
|
194
|
6
|
195 if {$tracks(albumartist) == ""} {
|
|
196 set tracks(albumartist) "Various Artists";
|
|
197 }
|
|
198
|
1
|
199 print_tracks "Before editing";
|
|
200 set tracks(updated) 0;
|
|
201 edit_tracks;
|
|
202 print_tracks "After editing";
|
|
203
|
|
204 if {$tracks(updated) == 0} {
|
|
205 puts "Nothing saved aborting";
|
|
206 puts "";
|
|
207 exit 0;
|
|
208 }
|
|
209
|
|
210 set out [ open $shellfile "w" ];
|
|
211
|
|
212 if { [ catch {
|
|
213 if {$infile == "-"} {
|
|
214 set fh stdout;
|
|
215 } else {
|
|
216 file rename -force "$infile" "$infile.old"
|
|
217 set fh [ open "$infile" "w" ];
|
|
218 }
|
|
219
|
|
220 puts $fh "# xmcd CD database file
|
|
221 # Copyright (C) 1993-1999 CDDB, Inc.
|
|
222 #
|
|
223 # Track frame offsets:";
|
|
224 for { set i 1 } { $i <= $tracks(number) } { incr i } {
|
|
225 puts $fh "#\t$tracks(offset:$i)"
|
|
226 }
|
|
227 puts $fh "#
|
|
228 # Disc length: $tracks(disclen) seconds
|
|
229 #
|
|
230 # Revision: 1
|
|
231 # Submitted via: Tcl Mangler 0.1 - Copyright (c) 1999 Daniel O'Connor
|
|
232 #
|
|
233 DISCID=$tracks(discid)
|
6
|
234 DTITLE=$tracks(albumartist) - $tracks(albumname)";
|
1
|
235 for { set i 1 } { $i <= $tracks(number) } { incr i } {
|
|
236 puts $fh "TTITLE[expr $i - 1]=$tracks(artist:$i) - $tracks(title:$i)";
|
|
237 }
|
|
238 } msg ] } {
|
|
239 puts "Failed to write $infile, $msg";
|
|
240 }
|
|
241
|
|
242 # Eat illegal characters
|
6
|
243 quoter tracks(albumname);
|
|
244 quoter tracks(albumartist);
|
|
245 set dirname "$tracks(albumartist) - $tracks(albumname)";
|
1
|
246
|
|
247 puts $out "mkdir \"$dirname\"";
|
|
248 for { set t_no 1 } { $t_no <= $tracks(number) } { incr t_no } {
|
|
249 # Eat illegal characters
|
6
|
250 quoter tracks(artist:$t_no);
|
|
251 quoter tracks(title:$t_no);
|
|
252
|
1
|
253 set oldname [ eval format {$oldbasename} {$t_no} ];
|
8
|
254 set newname [ eval format {$newbasename} {"[format %02d $t_no] $tracks(artist:$t_no) - $tracks(title:$t_no)"} ];
|
1
|
255
|
|
256 puts $out "mp3info -a \"$tracks(artist:$t_no)\" -t \"$tracks(title:$t_no)\" -l \"$tracks(albumname)\" -c \"Track $t_no\" -F 4 \"$oldname\"";
|
|
257 puts $out "mv \"$oldname\" \"$dirname/$newname\"";
|
|
258 }
|
|
259
|
8
|
260 if {$infile != "-"} {
|
|
261 puts $out "mv \"$infile\" \"$dirname\"";
|
|
262 }
|
1
|
263 if {[catch {close $out} msg]} {
|
|
264 puts "Got $msg closing renamer";
|
|
265 }
|
|
266
|
|
267 if {[info exists fh]} {
|
|
268 if {[catch {close $fh} msg]} {
|
|
269 puts "Got $msg closing fh";
|
|
270 }
|
|
271 }
|
|
272 }
|
|
273
|
6
|
274 proc quoter {str_var} {
|
|
275 upvar $str_var str;
|
|
276
|
|
277 regsub -all -- {/} $str {\\} str;
|
|
278 regsub -all -- {\"} $str {\"} str;
|
|
279 # regsub -all -- {\'} $str {\'} str;
|
|
280 }
|
|
281
|
1
|
282 proc print_tracks {outtitle} {
|
|
283 global tracks;
|
|
284
|
|
285 puts "================================================================================";
|
|
286 puts "$outtitle";
|
|
287 puts "================================================================================";
|
|
288 puts "Title: $tracks(albumname)"
|
|
289 if { $tracks(albumartist) != "" } {
|
|
290 puts "Artist: $tracks(albumartist)";
|
|
291 }
|
|
292 for { set t_no 1 } { $t_no <= $tracks(number) } { incr t_no } {
|
|
293 puts [format "Track %d: %s - %s" $t_no $tracks(artist:$t_no) $tracks(title:$t_no)];
|
|
294 }
|
|
295
|
|
296 puts "================================================================================";
|
|
297 }
|
|
298
|
|
299 main;
|