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" {
|
6
|
115 if {[regexp {^(.*) / (.*)$} $value a tracks(albumartist) tracks(albumname)] != 1} {
|
|
116 set tracks(albumname) $value;
|
1
|
117 } else {
|
6
|
118 puts "Atrist - $tracks(albumartist)";
|
1
|
119 }
|
6
|
120 puts "Title - $tracks(albumname)";
|
1
|
121 }
|
|
122
|
|
123 "TTITLE([0-9]+)" {
|
|
124 regexp {TTITLE([0-9]+)} $key a t_no;
|
|
125 set t_no [ expr $t_no + 1 ];
|
|
126
|
6
|
127 if {[info exists tracks(title:$t_no)]} {
|
|
128 append tracks(title:$t_no) $value;
|
|
129 } else {
|
|
130 incr tracks(number);
|
|
131
|
|
132 if {![regexp {^(.*) - (.*)$} $value a tracks(artist:$t_no) tracks(title:$t_no)] == 1} {
|
|
133 if {![regexp {^(.*)--(.*)$} $value a tracks(artist:$t_no) tracks(title:$t_no)] == 1} {
|
|
134 if {![regexp {^(.*) / (.*)$} $value a tracks(artist:$t_no) tracks(title:$t_no)] == 1} {
|
|
135 set tracks(title:$t_no) $value;
|
|
136 }
|
|
137 }
|
1
|
138 }
|
|
139 }
|
|
140 }
|
|
141
|
|
142 "EXTT([0-9]+)" {
|
|
143 regexp {EXTT([0-9]+)} $key a t_no;
|
|
144 set t_no [ expr $t_no + 1 ];
|
|
145 set ext($t_no) $value;
|
|
146 }
|
|
147 }
|
|
148 }
|
|
149
|
|
150 # puts "Offset list - $offset_list";
|
|
151 # puts "Disclength - $tracks(disclen)";
|
|
152
|
|
153 if {$tracks(number) < 1 || [llength $offset_list] != $tracks(number)} {
|
|
154 puts stderr "Invalid file, $tracks(number)";
|
|
155 exit 1;
|
|
156 }
|
|
157
|
|
158 set i 1;
|
|
159 foreach t $offset_list {
|
|
160 set tracks(offset:$i) $t;
|
|
161 incr i;
|
|
162 }
|
|
163
|
|
164 for {set i 1} {$i <= $tracks(number)} {incr i} {
|
|
165 if {![info exists tracks(title:$i)]} {
|
6
|
166 # error "track $i has no title!";
|
|
167 set tracks(title:$i) "";
|
1
|
168 }
|
|
169
|
|
170 if {[info exists tracks(artist:$i)]} {
|
|
171 # puts "artist exists for $i";
|
|
172 continue;
|
|
173 }
|
|
174
|
6
|
175 if {$tracks(albumartist) != ""} {
|
1
|
176 set tracks(artist:$i) $tracks(albumartist);
|
|
177 continue;
|
|
178 }
|
|
179
|
|
180 if {[info exists ext($i)] && $ext($i) != ""} {
|
|
181 set tracks(artist:$i) $ext($i);
|
|
182 # puts "setting artist to $ext($i) for $i";
|
|
183 continue;
|
|
184 }
|
|
185
|
|
186 set t_artist($i) "Unknown";
|
|
187 }
|
|
188
|
|
189 # puts "$tracks(number) tracks";
|
|
190
|
|
191 }
|
|
192
|
6
|
193 if {$tracks(albumartist) == ""} {
|
|
194 set tracks(albumartist) "Various Artists";
|
|
195 }
|
|
196
|
1
|
197 print_tracks "Before editing";
|
|
198 set tracks(updated) 0;
|
|
199 edit_tracks;
|
|
200 print_tracks "After editing";
|
|
201
|
|
202 if {$tracks(updated) == 0} {
|
|
203 puts "Nothing saved aborting";
|
|
204 puts "";
|
|
205 exit 0;
|
|
206 }
|
|
207
|
|
208 set out [ open $shellfile "w" ];
|
|
209
|
|
210 if { [ catch {
|
|
211 if {$infile == "-"} {
|
|
212 set fh stdout;
|
|
213 } else {
|
|
214 file rename -force "$infile" "$infile.old"
|
|
215 set fh [ open "$infile" "w" ];
|
|
216 }
|
|
217
|
|
218 puts $fh "# xmcd CD database file
|
|
219 # Copyright (C) 1993-1999 CDDB, Inc.
|
|
220 #
|
|
221 # Track frame offsets:";
|
|
222 for { set i 1 } { $i <= $tracks(number) } { incr i } {
|
|
223 puts $fh "#\t$tracks(offset:$i)"
|
|
224 }
|
|
225 puts $fh "#
|
|
226 # Disc length: $tracks(disclen) seconds
|
|
227 #
|
|
228 # Revision: 1
|
|
229 # Submitted via: Tcl Mangler 0.1 - Copyright (c) 1999 Daniel O'Connor
|
|
230 #
|
|
231 DISCID=$tracks(discid)
|
6
|
232 DTITLE=$tracks(albumartist) - $tracks(albumname)";
|
1
|
233 for { set i 1 } { $i <= $tracks(number) } { incr i } {
|
|
234 puts $fh "TTITLE[expr $i - 1]=$tracks(artist:$i) - $tracks(title:$i)";
|
|
235 }
|
|
236 } msg ] } {
|
|
237 puts "Failed to write $infile, $msg";
|
|
238 }
|
|
239
|
|
240 # Eat illegal characters
|
6
|
241 quoter tracks(albumname);
|
|
242 quoter tracks(albumartist);
|
|
243 set dirname "$tracks(albumartist) - $tracks(albumname)";
|
1
|
244
|
|
245 puts $out "mkdir \"$dirname\"";
|
|
246 for { set t_no 1 } { $t_no <= $tracks(number) } { incr t_no } {
|
|
247 # Eat illegal characters
|
6
|
248 quoter tracks(artist:$t_no);
|
|
249 quoter tracks(title:$t_no);
|
|
250
|
1
|
251 set oldname [ eval format {$oldbasename} {$t_no} ];
|
|
252 set newname [ eval format {$newbasename} {"$tracks(artist:$t_no) - $tracks(title:$t_no)"} ];
|
|
253
|
|
254 puts $out "mp3info -a \"$tracks(artist:$t_no)\" -t \"$tracks(title:$t_no)\" -l \"$tracks(albumname)\" -c \"Track $t_no\" -F 4 \"$oldname\"";
|
|
255 puts $out "mv \"$oldname\" \"$dirname/$newname\"";
|
|
256 }
|
|
257
|
|
258 if {[catch {close $out} msg]} {
|
|
259 puts "Got $msg closing renamer";
|
|
260 }
|
|
261
|
|
262 if {[info exists fh]} {
|
|
263 if {[catch {close $fh} msg]} {
|
|
264 puts "Got $msg closing fh";
|
|
265 }
|
|
266 }
|
|
267 }
|
|
268
|
6
|
269 proc quoter {str_var} {
|
|
270 upvar $str_var str;
|
|
271
|
|
272 regsub -all -- {/} $str {\\} str;
|
|
273 regsub -all -- {\"} $str {\"} str;
|
|
274 # regsub -all -- {\'} $str {\'} str;
|
|
275 }
|
|
276
|
1
|
277 proc print_tracks {outtitle} {
|
|
278 global tracks;
|
|
279
|
|
280 puts "================================================================================";
|
|
281 puts "$outtitle";
|
|
282 puts "================================================================================";
|
|
283 puts "Title: $tracks(albumname)"
|
|
284 if { $tracks(albumartist) != "" } {
|
|
285 puts "Artist: $tracks(albumartist)";
|
|
286 }
|
|
287 for { set t_no 1 } { $t_no <= $tracks(number) } { incr t_no } {
|
|
288 puts [format "Track %d: %s - %s" $t_no $tracks(artist:$t_no) $tracks(title:$t_no)];
|
|
289 }
|
|
290
|
|
291 puts "================================================================================";
|
|
292 }
|
|
293
|
|
294 main;
|