3
|
1 #!/bin/sh
|
|
2 # tcl magic \
|
9
|
3 exec tclsh8.2 $0 $*
|
3
|
4
|
|
5 #
|
|
6 # This software is copyright Daniel O'Connor (darius@dons.net.au) 1998, 1999
|
|
7 #
|
|
8 # This software is release under the GNU Public License Version 2.
|
|
9 # A copy of this licence must be distributed with this software.
|
|
10 #
|
|
11
|
|
12 proc main {} {
|
|
13 global argv0 argv tracks auto_path;
|
|
14
|
|
15 lappend auto_path ".";
|
|
16
|
|
17 if { [ llength $argv ] < 4 } {
|
|
18 puts stderr "Bad usage";
|
|
19 puts stderr "$argv0 <outfile> <discid> \[ <tracks> ... \] <disclen>";
|
|
20 exit 1;
|
|
21 }
|
|
22 set outfile [ lindex $argv 0 ];
|
|
23 set discid [ lindex $argv 1 ];
|
|
24 set trackoffs [ lrange $argv 2 [ expr [ llength $argv ] - 2 ] ];
|
|
25 set disclen [ lindex $argv end ];
|
|
26
|
|
27 if { $outfile == "-" } {
|
|
28 set wfh stdout;
|
|
29 } else {
|
|
30 set wfh [ open $outfile "w" ];
|
|
31 }
|
|
32
|
9
|
33 # set fh [ socket cddb.cddb.com 8880 ];
|
|
34 if {[catch {set fh [socket freedb.freedb.org 888]} msg]} {
|
|
35 puts stderr "Unable to connect to CDDB - $msg";
|
|
36 puts stderr "Generating dummy file";
|
|
37 generate_dummy $wfh $discid $disclen $trackoffs ;
|
|
38 exit 0;
|
|
39 }
|
3
|
40
|
|
41 # Greeting from server
|
9
|
42 puts [ gets $fh ];
|
|
43 #gets $fh;
|
3
|
44
|
|
45 puts $fh "CDDB HELLO [ exec id -u -n ] [ exec hostname ] TclMangler 0.1";
|
9
|
46 puts "CDDB HELLO [ exec id -u -n ] [ exec hostname ] TclMangler 0.1";
|
3
|
47 flush $fh
|
|
48
|
9
|
49 puts "Said hello"
|
|
50
|
3
|
51 # Hello message
|
9
|
52 # puts [ gets $fh ];
|
3
|
53 gets $fh;
|
|
54
|
|
55 puts $fh "CDDB QUERY $discid [ llength $trackoffs ] $trackoffs $disclen"
|
9
|
56 puts "CDDB QUERY $discid [ llength $trackoffs ] $trackoffs $disclen"
|
3
|
57 flush $fh
|
|
58
|
9
|
59 puts "Queried"
|
3
|
60 set line [ gets $fh ];
|
9
|
61 puts "Got - $line"
|
3
|
62 if { [ regexp {([0-9][0-9][0-9]) (.*)} $line a rtn rest ] } {
|
|
63 switch -- $rtn {
|
|
64 "200" {
|
|
65 if { [ regexp {([a-z]*) ([0-9a-f]*) (.*)} $rest a cat discid name ] } {
|
|
66 puts stderr "Matched CD called $name in category $cat";
|
|
67 } else {
|
|
68 puts stderr "Couldn't parse line with code 200 '$rest'";
|
|
69 exit 1;
|
|
70 }
|
|
71 }
|
|
72
|
|
73 "202" {
|
|
74 puts stderr "No such CD found.. generating dummy file";
|
9
|
75 generate_dummy $wfh $discid $disclen $trackoffs ;
|
3
|
76
|
9
|
77 puts "Wrote dummy";
|
3
|
78 exit;
|
|
79 }
|
|
80
|
|
81 "211" {
|
|
82 set tot 0;
|
|
83 set matches "";
|
|
84
|
|
85 while { 1 } {
|
|
86 set line [ gets $fh ];
|
|
87 if { $line == "." } {
|
|
88 break;
|
|
89 }
|
|
90
|
|
91 if { [ regexp {^([a-z]*) ([0-9a-f]*) (.*)} $line a cat discid name ] } {
|
|
92 puts stderr "[ expr $tot + 1 ]) Matched CD called $name in category $cat";
|
|
93 lappend matches [ list $cat $discid $name ];
|
|
94 incr tot;
|
|
95 } else {
|
|
96 puts stderr "Couldn't parse line after code 210 - '$line'";
|
|
97 exit 1;
|
|
98 }
|
|
99 }
|
|
100
|
|
101 if { $tot == 0 } {
|
|
102 puts stderr "No matches found for 210?";
|
|
103 exit 1;
|
|
104 }
|
|
105
|
|
106 if { $tot == 1 } {
|
9
|
107 puts "Only 1 partial match";
|
3
|
108 set num 0;
|
|
109 } else {
|
|
110 puts stderr "Please enter the number which corresponds to the correct entry";
|
|
111 while { 1 } {
|
|
112 set num [ gets stdin ];
|
|
113 if { ($num >= 1) && ($num <= $tot ) } {
|
|
114 break;
|
|
115 }
|
|
116 puts stderr "Sorry, that number is invalid, please try again";
|
|
117 }
|
|
118
|
|
119 set cat [ lindex [ lindex $matches [ expr $num - 1 ] ] 0 ];
|
|
120 set discid [ lindex [ lindex $matches [ expr $num - 1 ] ] 1 ];
|
|
121 set name [ lindex [ lindex $matches [ expr $num - 1 ] ] 2 ];
|
|
122 }
|
|
123 }
|
|
124
|
|
125 "501" {
|
|
126 puts stderr"Invalid disc ID $discid";
|
|
127 exit 1;
|
|
128 }
|
|
129
|
|
130 default {
|
|
131 puts stderr "Couldn't parse '$line'";
|
|
132 exit 1;
|
|
133 }
|
|
134 }
|
|
135 }
|
|
136
|
|
137 puts $fh "CDDB READ $cat $discid"
|
|
138 flush $fh
|
|
139
|
|
140 gets $fh line;
|
|
141
|
|
142 if { [ regexp {([0-9][0-9][0-9]) (.*)} $line a rtn rest ] } {
|
|
143 if { $rtn != "210" } {
|
|
144 puts stderr "Bad error from CDDB READ command ($line)"
|
|
145 exit 1;
|
|
146 }
|
|
147 } else {
|
|
148 puts stderr "Couldn't parse $line";
|
|
149 exit 1;
|
|
150 }
|
|
151
|
|
152 while { 1 } {
|
|
153 set line [ gets $fh ];
|
|
154 if { $line == "." } {
|
|
155 break;
|
|
156 }
|
|
157
|
|
158 puts $wfh $line;
|
|
159 }
|
|
160
|
|
161 close $fh;
|
|
162 }
|
|
163
|
9
|
164 proc generate_dummy {fh discid disclen trackoffs} {
|
|
165
|
|
166 puts $fh "# xmcd CD database file
|
|
167 # Copyright (C) 1993-1999 CDDB, Inc.
|
|
168 #
|
|
169 # Track frame offsets:";
|
|
170 foreach t $trackoffs {
|
|
171 puts $fh "#\t$t";
|
|
172 }
|
|
173
|
|
174 puts $fh "#
|
|
175 # Disc length: $disclen seconds
|
|
176 #
|
|
177 # Revision: 1
|
|
178 # Submitted via: Tcl Mangler 0.1 - Copyright (c) 1999 Daniel O'Connor
|
|
179 #
|
|
180 DISCID=$discid
|
|
181 DTITLE=";
|
|
182 set i 0;
|
|
183 foreach t $trackoffs {
|
|
184 puts $fh "TTITLE$i=";
|
|
185 incr i
|
|
186 }
|
|
187 }
|
|
188
|
|
189 main;
|