File: crip

package info (click to toggle)
crip 3.7-3%2Betch2
  • links: PTS
  • area: main
  • in suites: etch
  • size: 200 kB
  • ctags: 10
  • sloc: perl: 2,030; sh: 289; makefile: 35
file content (2152 lines) | stat: -rwxr-xr-x 78,093 bytes parent folder | download | duplicates (3)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
#!/usr/bin/perl
$version = "crip v3.7 (http://bach.dynet.com/crip)";
# Written to facilitate ripping & encoding CDs under Linux/UNIX
# by Charlton Harrison (charlton@dynet.com)
#
# This script is intended to rip, encode and tag Ogg Vorbis and/or FLAC
# digital music files from CD.  If you want to make MP3s instead go back
# to the past and use crip version 1.0.
#

# Defaults Section ------------------------------------------------------------
# Rather than edit these values here,  it is better to edit them in the
# ~/.criprc file,  which is read in later...

# encoders supported are "vorbis" and "flac"
$encodeto = "vorbis";

# Flags for oggenc (Ogg Vorbis encoder) command.  Only used only if
#  $encodeto is set to "vorbis".
# Default is "-q 4" which is a quality setting very roughly equal to
# about 128kbps.  Note that even "-q 3" is better than 128kbps MP3.
# Hint:  If you'd rather put these flags as a command-line option,
#  use:  -o " -q 4"  (with a space as the first character within the
#  quotes so as to not confuse the command-line parser)
$oggencflags = "-q 4";

# $flacflags:  Only used if $encodeto is set to "flac".
# Note: if you include --ogg as a flag and encode to an ogg-formatted
# flac file,  crip will try to use metaflac to write the tags and that
# seems to fail.  I'm not sure what the best way to handle this scenario is.
# If anyone out there wants to make flac files within the .ogg framework,
# please enlighten me as to 1) why,  and 2) what is one supposed to use to
# modify comments in this .ogg file,  vorbiscomment?!
$flacflags = "--best --replay-gain";

# Flags for cdparanoia (CD ripper) command.
# I like to use "-v -z" here.
# If you find the "-v" is too verbose and pointless,  feel free to take it
# out.  That would significantly reduce the size of your terminal output.
# I find that using "-z" is good because I like knowing that when the script
# finishes cdparanoia did not encounter anything that could lead to an
# audible defect.  However sometimes the script might hang indefinitely
# because of the "-z".  If you find that is the case,  you can now ctrl-C
# during the rip and set new options (say, to "-v --never-skip=40") so that
# you can at least go back and listen to see if there were any audible
# defects (most of the time there isn't even if the rip wasn't perfect).
$cdparanoiaflags="-v -z";

# The first comment field in the tag-info will be:
$comment0 = "Produced using " . $version . " on ";
# ...followed by a date/time timestamp.
$comment = "";
# An additional comment line might be specified in the .criprc file.

# Get the title information that goes into the tag either from the
# "filename" or the "cddb" info.
# Default is "filename",  because sometimes the CDDB info is wrong.
# Note that if you set this to "cddb" but there isn't any CDDB info,
# it'll just set it based on the filename anyway.
$settitle = "filename";

# editnames: "off" = Use the legacy command-line to edit filenames;
# "on" = use an editor;  and "both" = use the command-line to edit the
# filenames but also asks afterwards if you want to edit the filenames
# with the editor.   default = "on".
$editnames = "on";

# Your favorite editor for editing filenames and tag files
$editor = "vim";

# Default contact information (put YOUR e-mail address here!)
# Remember to escape the '@' (by putting a '\' before it).
# Example:  $contact = "nobody\@nowhere.com";
$contact = "";

# Normalization is not necessary with replaygain/vorbisgain tags, so
# turn off normalization by default.
# This option can either be "on" or "off".
$normalize = "off";
# Volume threshold - if peak vol > 1/$volthresh don't bother normalizing
# Default is 1.078.  If you set this value real high,  your songs probably
# won't be normalized but you'll still get the "not normalized, peak=X.XXX"
# in your comment (you won't get this in your comment if normalize is off,
# but you'll still have peak and group-peak info in your replaygain (vorbisgain)
# tags,  but these tags are not visible using xmms thru v1.2.7).
# If normalize = "off" then $volthresh is ignored.
$volthresh = 1.078;

# $trimsilence can either be "on" or "off",  depending on whether or not you
#  want to trim silence off the beginning & end of your track/group.
# Note that if both trimsilence and normalize are off,  you won't need sox.
# I personally always set this to "on",  but I'm defaulting it to "off" so
# that the prerequisits aren't so tough.  It doesn't seem to make much
# difference in the final file size,  interestingly.
$trimsilence = "off";

# trimsilargs are the arguments that sox will take to trim the silence
# from your tracks (if trimsilence is on in the first place).
# A very safe setting is "silence 1 0:0:0.01 -96d"
$trimsilargs = "silence 1 0:0:0.01 -96d";
# (i.e. anything louder than -96db for more than one hundredth of a second
#  will be considered the beginning of the sound.  It doesn't work properly
#  on the end of the file because it processes forward.  Logically, you
#  would start from the end of the file and work your way back to the first
#  detectable sounds, exactly like you did at the beginning of the file.
#  So you basically use the "reverse" option and re-run sox.  This is
#  the actual recommended usage of the sox silence feature, which sucks
#  because you then have to make 2 passes.  They'll probably change the
#  usage in the future and then I'll have to modify this script again.
#  Right now this works for 12.17.4 and 12.17.5 (and probably others))

# The type of media that the source comes from (default = "CD")
# this just gets put in a comment
$sourcemedia = "CD";

# Default genre to "classical" unless otherwise specified later.
$genre = "classical";

# Default device to rip from
$cddevice = "/dev/cdrom";

# Turn on/off deletion of wave and tag files.  "on" by default.
$delfiles = "on";

# Classical-style questioning defaults to "off" (because I'm assuming
# most people will be ripping 'pop-style' CDs most of the time),
$askclassical = "off";
# ...unless the genre winds-up being of the following,  then it defaults
#  to "on". (values will be compared in a case-insensitive way)
#@classicalgenres = ("classical","jazz");
@classicalgenres = ("classical");

# Skip the album abbreviation question?
# (turn this "on" if you never want to be asked for or have the album
#  abbreviation.  If you rip a bunch of classical CDs and don't want
#  to have to answer null to the album abbreviation question every
#  time it's asked,  but otherwise for non-classical CDs you want to
#  have the album abbreviation,  set to "classical".)
# "off", "on", or "classical" - default should be "off".
$skipalbumabv = "off";

# Turn this on to skip the ripping process and create empty .wav files.
# By turning this on you will automatically turn on $stopgap so that
# you will have a chance to substitute the empty .wav files with the
# .wav files that you presumably already have.
$skiprip = "off";

# Add a 'stop-gap' between ripping and encoding in order for, say,
# allowing user to edit the .wav files before continuing (some good
# .wav file editors are: sweep, audacity).
# This is done after the digital silence trimming.
$stopgap = "off";

# Eject CD when done with it.  "off" by default.
$ejectcd = "off";

# Funny characters to allow (in default filenames) (other than [A-Z,a-z,0-9])
# Edit this string with care.  Some chars have to be escaped ("\"),  some
#  apparently have to be double-escaped ("//") (such as "(",")","[", and "]")
#  and some don't need to be escaped at all (such as the ",","","", etc.)
$funnychars = "\-\'\",\\.\\[\\]\\(\\)";
# Map euro characters in the default filenames to American characters
# (turn "on" if you are an American imperialist)
$mapeurochars = "off";
# European characters to allow (if $mapeurochars is set to "off")
# (again, this is only for the computer-chosen default filenames,  which
#  are based on CDDB info.  The tag info will automatically allow European
#  characters from CDDB or user sources.)
$eurochars = "ß¼";

# Allow spaces in filenames
# I recommend leaving this off because it is bad form to have spaces in
# filenames regardless of whether you are on UNIX or Windows.  I have included
# the option because there are too many Windoze users out there who flooded
# the internet with sorry-ass MP3s made with bad encoders, no tag information,
# and inappropriate file naming conventions, and who are still too used to
# putting spaces in their filenames.  I figure having this feature will
# encourage them to use this script and make better quality files for
# everyone.  (no offense to the person who suggested me this feature)
# So if you absolutely must have spaces in your filenames,  turn
# this to "on" (default is "off").
$allowspaces = "off";

# This is the e-mail address used to submit new entries to the freedb
#  CDDB database
$cddbsubmitaddr = "freedb-submit\@freedb.org";
# Directory to write the cddb submit entry file (need to have write
#  permissions to this directory).
$cddbsubmitdir = "/tmp";
# The following is the charset for the freedb-submit e-mail:
$charset = "iso-8859-1";

# Now look and see what is in the .criprc file...
open(CONFIG, "<" . $ENV{HOME} . "/.criprc");
while (<CONFIG>) {
	next if /^\#/;
	chomp;
	next unless $_; # skip empty lines
	s/\=/___EQ___/; # replace first equal sign
	($key, $val) = split /___EQ___/;
	$key =~ s/[\t ]//g;   # trim leading & trailing spaces
	$val = join " ", grep { $_ } split /[\t ]/, $val;
	${$key} = $val;
##print "Key: ",$key, "   Val: ", $val, "\n";
}
close CONFIG;

# Set the following variable to "" if you do not want to receive copies of
# the CDDB submissions that you make.
$cddbsubmitaddrCc = $contact;

# End of Defaults Section -----------------------------------------------------

# Allow the user to override the defaults from the command line...
#use Getopt::Std;
use Getopt::Long;
$Getopt::Long::ignorecase = 0;

$USAGE = "$0 [options]

Options:
  -h, --help        Print this help then exit
  -v, --version     Print version of crip then exit
  -e codec          Encode to vorbis or flac (default = $encodeto)
  -s media          Specify the source media (default = $sourcemedia)
  -g genre          Specify the music genre (default = $genre)
  -q [on/off]       Classical-style questioning (default = $askclassical)
  -m [on/off]       Map European to American-only chars (default = $mapeurochars)
  -t [on/off]       Trim leading/trailing silence (default = $trimsilence)
  -n [on/off]       Normalize the audio (default = $normalize)
  -V volthresh      Volume threshold for normalizing (default = $volthresh)
                       (only used when normalizing is enabled)
  -r [on/off]       Remove files after encoding (default = $delfiles)
  -E editor         Editor to use (default = $editor)
  -u [on/off/both]  Use editor to name the files (default = $editnames)
                       (as opposed to the command-line)
  -o \" flags\"       Flags to pass to oggenc (default = '$oggencflags')
  -f \" flags\"       Flags to pass to flac (default = '$flacflags')
  -c \" flags\"       Flags to pass to cdparanoia (default = '$cdparanoiaflags')
  -d device         CDrom device to read from (default = $cddevice)
  -w [on/off]       Skip the ripping (makes empty .wav files) (default = $skiprip)
                       (useful if you already have the .wav files to encode)
  -p [on/off]       Prompt to continue after ripping (default = $stopgap)
                       (useful pause to edit .wav files before encoding)
  -x [on/off]       Eject CD when done ripping (default = $ejectcd)

";

GetOptions("v","version","h","help","s=s","g=s","d=s","e=s","E=s","V=f","u:s","o:s","f:s","c:s","q:s","t:s","n:s","r:s","w:s","p:s","x:s") || die "$USAGE";

if ((defined $opt_h) || (defined $opt_help)) { print "$USAGE"; exit(); }
if ((defined $opt_v) || (defined $opt_version)) { print "$version\n"; exit(); }
$sourcemedia = $opt_s if (defined $opt_s);
$genre = $opt_g if (defined $opt_g);
$askclassicalflag = $opt_q if (defined $opt_q);
$mapeurochars = $opt_m if (defined $opt_m);
$editnames = $opt_u if (defined $opt_u);
$encodeto = $opt_e if (defined $opt_e);
$editor = $opt_E if (defined $opt_E);
$normalize = $opt_n if (defined $opt_n);
$volthresh = $opt_V if (defined $opt_V);
$trimsilence = $opt_t if (defined $opt_t);
$delfiles = $opt_r if (defined $opt_r);
$oggencflags = $opt_o if (defined $opt_o);
$flacflags = $opt_f if (defined $opt_f);
$cdparanoiaflags = $opt_c if (defined $opt_c);
$cddevice = $opt_d if (defined $opt_d);
$skiprip = $opt_w if (defined $opt_w);
$stopgap = $opt_p if (defined $opt_p);
$ejectcd = $opt_x if (defined $opt_x);

if ($normalize eq "") { $normalize = "on"; }
if (($normalize ne "on") && ($normalize ne "off")) {
	die "Unknown normalize (-n) setting ($normalize)!\n$USAGE";
}

if ($trimsilence eq "") { $trimsilence = "on"; }
if (($trimsilence ne "on") && ($trimsilence ne "off")) {
	die "Unknown trim silence (-t) setting ($trimsilence)!\n$USAGE";
}

if ($editnames eq "") { $editnames = "on"; }
if (($editnames ne "on") && ($editnames ne "off") && ($editnames ne "both")) {
	die "Unknown editnames (-f) setting ($editnames)!\n$USAGE";
}

if ($encodeto eq "") {
	print "Did not specify a codec to encode to.  Assuming \"vorbis\".\n";
	$encodeto = "vorbis";
}
if (($encodeto ne "vorbis") && ($encodeto ne "flac")) {
	die "Invalid/unsupported codec (-e) ($encodeto).\n$USAGE";
}

if ($mapeurochars eq "") { $mapeurochars = "on"; }
if (($mapeurochars ne "on") && ($mapeurochars ne "off")) {
	die "Unknown mapeurochars (-m) setting ($mapeurochars)!\n$USAGE";
}

if ($delfiles eq "") { $delfiles = "on"; }
if (($delfiles ne "on") && ($delfiles ne "off")) {
	die "Unknown delete (-r) setting ($delfiles)!\n$USAGE";
}

if ($skiprip eq "") { $skiprip = "on"; }
if (($skiprip ne "on") && ($skiprip ne "off")) {
	die "Unknown skiprip (-w) setting ($skiprip)!\n$USAGE";
}
if ($skiprip eq "on") { $stopgap = "on"; }

if ($stopgap eq "") { $stopgap = "on"; }
if (($stopgap ne "on") && ($stopgap ne "off")) {
	die "Unknown stopgap (-p) setting ($stopgap)!\n$USAGE";
}

if ($ejectcd eq "") { $ejectcd = "on"; }
if (($ejectcd ne "on") && ($ejectcd ne "off")) {
	die "Unknown eject (-x) setting ($ejectcd)!\n$USAGE";
}

if ((defined $opt_V) && ($normalize eq "off")) {
	print "You specified a volume threshold,\n";
	print "but you also have normalization turned off.\n\n";
	die "$USAGE";
}

if (defined $askclassicalflag) {
	if ($askclassicalflag eq "") { $askclassicalflag = "on"; }
	if (($askclassicalflag ne "on") && ($askclassicalflag ne "off")) {
		die "Unknown classical (-q) setting ($askclassicalflag)!\n$USAGE";
	}
}

if (! -e $cddevice) {
	die "No such device: $cddevice!\n$USAGE";
}
# End getopts section


# Print current version of crip
print $version, "\n";

# Print my feedback contact information
print "\n   Please report all bugs, feature requests, or\n";
print "   general difficulties to charlton\@dynet.com.\n\n";

# List options and what they're set to
print "Options currently set to:\n";
print "   contact = $contact\n";
print "   sourcemedia = $sourcemedia\n";
print "   encodeto = $encodeto\n";
print "   cddevice = $cddevice\n";
print "   settitle = $settitle\n";
print "   editnames = $editnames\n";
print "   editor = $editor\n";
print "   askclassical (default) = $askclassical\n";
if (defined $askclassicalflag) {
	print "   askclassicalflag = $askclassicalflag\n";
}
print "   skipalbumabv = $skipalbumabv\n";
print "   mapeurochars = $mapeurochars\n";
print "   normalize = $normalize\n";
if ($normalize eq "on") {
	print "   volthresh = $volthresh\n";
}
print "   trimsilence = $trimsilence\n";
if ($trimsilence eq "on") {
	print "   trimsilargs = $trimsilargs\n";
}
print "   delfiles = $delfiles\n";
print "   skiprip = $skiprip\n";
print "   stopgap = $stopgap\n";
print "   ejectcd = $ejectcd\n";

print "\n";

# Print ripper info
$cdprelease = `cdparanoia --version 2>&1 | grep release`;
chop $cdprelease;

print "Ripper: ", $cdprelease, " ", $cdparanoiaflags, "\n";

if ((defined $opt_c) && ($cdparanoiaflags eq "")) {
	print "\nWARNING: Did not specify any flags for cdparanoia.\n";
	print "         Will run cdparanoia without any flags.\n\n";
}

if ((defined $opt_f) && ($flacflags eq "")) {
	if ($encodeto eq "flac") {
	    print "\nWARNING: Did not specify any flags for flac.\n";
		print "         Will run flac without any flags.\n\n";
	} else {
		print "\nWARNING: You specified the '-f' flag but you\n";
		print "         are not encoding to flac!\n\n";
	}
}


# Set the encoding field
if ($encodeto eq "vorbis") {
	$encoding = `oggenc --version`;  chop $encoding;
#	$encoding = $encoding . " (aoTuV-b3 patched) " . $oggencflags;
	$encoding = $encoding . " " . $oggencflags;
} else {
	$encoding = `flac --version`;  chop $encoding;
	$encoding = $encoding . " " . $flacflags;
}
print "Encoder: ", $encoding, "\n";

if ((defined $opt_o) && ($oggencflags eq "")) {
	if ($encodeto eq "vorbis") {
		print "\nWARNING: Did not specify any flags for oggenc.\n";
		print "         Will encode with no flags!\n";
	} else {
		print "\nWARNING: You specified the '-o' flag but you\n";
		print "         are not encoding to vorbis!\n\n";
	}
}

print "\n";
##exit();   # Used for testing command-line option behavior


# Now check to make sure all required components are available

# Check for the encoder and necessary tagging tools
if ($encodeto eq "vorbis") {
	$out = `which oggenc`;
	if ($out eq "") {
		die "Cannot find `oggenc` for encoding to Ogg Vorbis!\n";
	}
	$out = `which vorbiscomment`;
	if ($out eq "") {
		die "Cannot find `vorbiscomment` for tagging the Ogg Vorbis files!\n";
	}
} else {
	$out = `which flac`;
	if ($out eq "") {
		die "Cannot find `flac`!\n";
	}
	$out = `which metaflac`;
	if ($out eq "") {
		die "Cannot find `metaflac` for tagging the flac files!\n";
	}
}

# Unless normalize is on or not encoding to vorbis, check for vorbisgain
unless (($normalize eq "on") || ($encodeto ne "vorbis")) {
	# If vorbisgain is unavailable then deliver this warning.
	$out = `which vorbisgain`;
	if ($out eq "") {
		print "\nIt does not appear that vorbisgain is available on\n";
		print "this system.  Either get it (recommended) or turn on\n";
		print "normalization instead (not recommended).\n";
		print "Either ctrl-C now or press <enter> to continue: ";
		$inp = <stdin>;
	}
}

if ($editnames eq "on") {
	$out = `which $editor`;
	if ($out eq "") {
		print "\nYou have 'editnames' on,  and you have your editor set to '$editor',\n";
		print " but there is no $editor in your path.  You need to either install\n";
		print " this editor in your path,  change your editor to an available editor\n";
		print " in your path,  or turn 'editnames' off.\n";
		exit();
	}
}

if (($normalize eq "on") || ($trimsilence eq "on")) {
	$out = `which sox`;
	if ($out eq "") {
		print "\nsox needs to be installed.\n\n";
		print "Note: You can eliminate your dependency on sox by turning off\n";
		if (($normalize eq "on") && ($trimsilence eq "on")) {
			print " normalization and silence trimming.\n";
		} elsif ($trimsilence eq "on") {
			print " silence trimming.\n";
		} elsif ($normalize eq "on") {
			print " normalization.\n";
		}
		exit();
	} else {
		$out = `sox -h 2>&1 | grep Version`;  chop $out;
		$out =~ m/Version (.+)/;  $soxver = $1;
		$soxver =~ m/(\d+)\.(.+)/;  $soxver1 = $1;  $soxver2 = $2;
#print $out, "\n", $soxver, " | ", $soxver1, "|", $soxver2, "\n";
#if ($soxver2 < "17.6") { print "here\n"; }
#exit();
		if ($soxver2 < "17.4") {
			print "Your sox needs to be updated.\n";
			print "Silence trimming works differently in versions before sox-12.17.4.\n";
			print "Note:  You can eliminate your dependency on sox by turning off\n";
			if (($normalize eq "on") && ($trimsilence eq "on")) {
				print " normalization and silence trimming.\n";
			} elsif ($trimsilence eq "on") {
				print " silence trimming.\n";
			} elsif ($normalize eq "on") {
				print " normalization.\n";
			}
			exit();
		}
	}
}

# This will bomb with an error if not available:
use FindBin;
use CDDB_get qw( get_cddb get_discids );

# Check for existing *.wav, *.tag, *.ogg, *.flac or filenames.txt files
$out = `ls *.wav *.ogg *.flac *.tag 2>/dev/null`;
if ($out ne "") {
	print "You currently have *.wav, *.ogg, *.flac, or *.tag files in the current\n";
	print "working directory.  These are subject to being overwritten.\n\n";
	print "Press <enter> to proceed (or ctrl-C to abort): ";
	$inp = <STDIN>;
}
if (-e "filenames.txt") {
	print "You currently have a filenames.txt file in the current working\n";
	print "working directory.  This will be deleted!\n\n";
	print "Press <enter> to proceed (or ctrl-C to abort): ";
	$inp = <STDIN>;
	# Go ahead and delete the file now so that if the script is stopped and
	#  restarted this question won't keep bugging the user.
	print "Deleting filenames.txt\n\n";
	`rm filenames.txt`;
}

# End of check components section.


# Main loop
while (1) {

$firstdate = 1;

# cddb query
print "Retrieving Disc Information...\n";
$ids = get_discids($cddevice);
$discid = sprintf "%08x", $ids->[0];
$totaltracks = $ids->[1];
$toc = $ids->[2];
print "Retrieving CDDB Information...\n";
# %cd = get_cddb();
eval { %cd = get_cddb(); };
if ($@) {
	print "error: $!\n\n";
	print "get_cddb() has failed for some reason.\n";
	print "Perhaps the network is down?\n";
}
print "\n";
print "DiscID:   ", $discid, "\n";
unless(defined $cd{title}) {
	print "No CDDB info found.\n";
} else {
	print "cddbID:   ", $cd{id}, "\n";
	print "artist:   ", $cd{artist}, "\n";
	print "title:    ", $cd{title}, "\n";
	print "category: ", $cd{cat}, "\n\n";
	$genre = $cd{cat};

	if ($cd{tno} != $totaltracks) {
		print "!!! WARNING !!!\n";
		print "Reported number of tracks different from actual.\n";
		print "Disc reports ", $totaltracks, ".\n";
		print "CDDB reports ", $cd{tno}, ".\n\n";
	}
}

print "total number of tracks: ", $totaltracks, "\n";
$tr=1;
foreach $trtitle ( @{cd->{track}} ) {
	$trname[$tr] = $trtitle;
	$from=$toc->[$tr-1]{frames};
	$to=$toc->[$tr]{frames} - 1;
	$dur=$to-$from;
	$min=int($dur/75/60);
	$sec=int($dur/75)-$min*60;
	$frm=($dur-$sec*75-$min*75*60)*100/75;
	$out=sprintf "track %2d: %8d - %8d  [%2d:%.2d.%.2d]: $trtitle\n",$tr,$from,$to,$min,$sec,$frm;
	print "$out";
	$tr++;
}
print "\n";


############################################################### CDDB test info
#foreach $tmp (0 .. ($totaltracks - 1)) {
#	print $tmp, " - ", $toc->[$tmp]{frames}, "\n";
#}
#$cddbduration = int($toc->[$totaltracks]{frames} / 75);
#print "Duration: ", $cddbduration, " seconds.\n";
#foreach $idx (@{cd->{raw}}) {
#	if ($idx =~ m/^#\s+Revision:\s*(\d+)/) { $revision = $1; }
#	print $idx;
#}
#print "\nRevision: ", $revision, "\n";
##############################################################################

##exit();


# Request normalization groupings

print "\n\nEnter the track numbers for each group (each group will be\n";
print "replaygained/vorbisgained/normalized,  trimmed,  and tagged\n";
print "as if it were one piece of music).\n\n";
print "Delimit the track numbers on each line by a space.\n";
print "Type \"end\" to go on to the next section.\n\n";
print "Note that only the tracks you specify will be ripped.\n";
print "Also note that if you list a track more than once, it will be\n";
print "unnecessarily ripped more than once (so don't do it).\n";
print "You can also enter \"all\" or \"each\" for grouping all tracks as\n";
print "one group, or each track as a separate group respectively.\n";
print "If input is \"all\" or \"each\" then the entire CD is ripped.\n";

$inp = "";  $listing = "";  $listnum = 0;
while ($inp ne "end") {
	print "\n> ";
	$inp = <STDIN>;
	chop $inp;

	if ($inp eq "all") {
		$i = 1;
		while ($i <= $totaltracks) {
			$list[$listnum][$i-1] = $i;
			$i++;
		}
		$listing = "all";
		$inp = "end";
	} elsif ($inp eq "each") {
		$i = 1;
		while ($i <= $totaltracks) {
			$list[$listnum][0] = $i;
			$listnum++;  $i++;
		}
		$listing = "each";
		$inp = "end";
	} elsif ($inp ne "end") {
		# Parse the list of numbers
		# If $inp is not a list of numbers then print "invalid"
		$valid = 0;  $i = 0;
		while ($inp =~ s/^(\d+)\s*//) {
			$list[$listnum][$i] = $1;
			$valid = 1;  $i++;
		}
		if ($valid) {
			$listnum++;
		} else {
		    print "Invalid Entry.\n\n";
		}
	}
}


# Reiterate the lists...
print "\n\nHere are the lists of tracks you picked:\n\n";
$listnum = 0;
while ($listnum <= $#list) {
	$index = 0;
	while ($index <= $#{$list[$listnum]}) {
		print $list[$listnum][$index], " ";
		$index++;
	}
	$listnum++;
	print "\n";
}
print "\n";

# Analyze picked tracks and display any warning messages if necessary
# 1) check to see if any of the tracks are listed more than once
$warning = 0;  $tmpindex = 0;  $listnum = 0;
while ($listnum <= $#list) {
	$index = 0;
	while ($index <= $#{$list[$listnum]}) {
		$tmptrack = $list[$listnum][$index];
		# Check if $tmptrack matches any from the array before it
		$chkindex = 0;
		while ($chkindex < $tmpindex) {
			if ($tmptrack == $tmptracks[$chkindex]) { $warning = 1; }
			$chkindex++;
		}
		# Add $tmptrack to the array
		$tmptracks[$tmpindex] = $tmptrack;
		$tmpindex++;
		$index++;
	}
	$listnum++;
}
if ($warning) { print "Warning:  Some track(s) in the list appear more than once.\n"; }
# 2) check to see if any of the tracks don't appear
$warning = 0;  $listnum = 0;
foreach $tmptrack (1 .. $totaltracks) {
	# Make sure each $tmptrack appears at least once
	$matched = 0;
	$listnum = 0;
	while ($listnum <= $#list) {
		$index = 0;
		while ($index <= $#{$list[$listnum]}) {
			if ($tmptrack == $list[$listnum][$index]) {
				$matched = 1;
			}
			$index++;
		}
		$listnum++;
	}
	if (!$matched) { $warning = 1;}
}
if ($warning) { print "Warning:  Some track(s) on the CD don't appear in your listed tracks.\n"; }
# 3) check to make sure all lists are in consecutive order
$warning = 0;  $listnum = 0;
while ($listnum <= $#list) {
	# Make sure each of these lists is in consecutive order
	$index = 0;  $lasttrack = 0;
	while ($index <= $#{$list[$listnum]}) {
		if ($lasttrack != 0) {
			if ($list[$listnum][$index] != ($lasttrack + 1)) { $warning = 1; }
		}
		$lasttrack = $list[$listnum][$index];
		$index++;
	}
	$listnum++;
}
if ($warning) { print "Warning:  Some list(s) are not in consecutive order.\n"; }
# 4) check to make sure all the tracks are within the bounds of the CD.
$warning = 0;  $listnum = 0;
while ($listnum <= $#list) {
	$index = 0;
	while ($index <= $#{$list[$listnum]}) {
		if (($list[$listnum][$index] < 1) || ($list[$listnum][$index] > $totaltracks)) {
			$warning = 1;
		}
		$index++;
	}
	$listnum++;
}
if ($warning) {
	print "Warning:  Some track(s) you listed are not on the CD (these won't be ripped).\n";
	print "FYI:  Total number of tracks disc reports: $totaltracks\n";
}
print "\n";



# Query section begins...
print "Please enter the genre for this album.\n";
print "Recommended genres are (from freedb.org):\n";
print "    blues       (self explanatory)\n";
print "    classical   (self explanatory)\n";
print "    country     (self explanatory)\n";
print "    folk        (self explanatory)\n";
print "    jazz        (self explanatory)\n";
print "    newage      (self explanatory)\n";
print "    reggae      (self explanatory)\n";
print "    rock        (including funk, soul, rap, pop, industrial, metal, etc.)\n";
print "    soundtrack  (movies, shows)\n";
print "    data        (ISO9660 and other data CDs)\n";
print "    misc        (others that do not fit in the above categories)\n";
print "Enter the genre for this album (single space = null): [", $genre, "]: ";
$inp = <STDIN>;  chop($inp);
if ($inp ne "") { $genre = $inp; }
if ($inp eq " ") { $genre = ""; }
if ($genre =~ m/new\.age/i) {
	print "\"$genre\" -> \"newage\"\n";
	$genre = "newage";
}

# If the '-q' flag was used,  override default
if (defined $askclassicalflag) {
	# We already know from above that $askclassicalflag must be "on" or "off"
	$askclassical = $askclassicalflag;
} else {
	# Set $askclassical if $genre is one of @classicalgenres
	foreach $classicalgenre (@classicalgenres) {
		if ($genre =~ m/^\s*$classicalgenre\s*$/i) {
			$askclassical = "on";
		}
	}
}

# Set $setlabels default
$setlabels = "cd";
$composer = "";  $complast = "";
if ($askclassical eq "on") {
	print "Since this is $genre, there should be a composer.\n";
	if ($#list >= 1) { # (more than one list)
		print "If the composer is different for each group/list, just set the\n";
		print "value to null and add the composer tag manually with each list.\n";
	}
	# Get $composer
	$composer = $cd{artist};
	print "Enter composer (single space = null): [", $composer, "]: ";
	$inp = <STDIN>;  chop($inp);
	if ($inp ne "") { $composer = $inp; }
	if ($inp eq " ") { $composer = ""; }
	# Now try to set $complast to the last name of the $composer
	if ($composer =~ m/^\s*(\S+)\s*,/) {
		$complast = $1;
	} elsif ($composer =~ m/\s+(\S+)\s*$/) {
		$complast = $1;
	} else {
		$complast = $composer;
	}

	# Since this is Classical-style questioning, default $setlabels to "list".
	# ($setlabels default will be reset if $listing eq "each" or "all")
	$setlabels = "list";
}

# Ask query to what $setlabels will actually be
# (this will determine whether to label once for the whole CD or
#  once for each list)
# But don't even bother if $listing is "each" or "all"
if (($listing ne "each") && ($listing ne "all")) {
	if ($#list >= 1) {
		print "Would you like to set the artist and miscellaneous labeling info for\n";
		print "the whole CD right now, or would you prefer to set it for each list?\n";
		print "(list/cd) [" . $setlabels . "]: ";
		$inp = <STDIN>;  chop($inp);
		if (($inp eq "list") || ($inp eq "LIST")) { $setlabels = "list"; }
		if (($inp eq "cd") || ($inp eq "CD")) { $setlabels = "cd"; }
	} else {   # Don't bother asking this question if the number of lists is just one.
		$setlabels = "cd";
	}
} else {
	# $listing is "each" or "all" so it's probably safe to assume that
	# the user doesn't want to label per list.
	$setlabels = "cd";
	# But if the user selected "each", print a message anyway just to
	# let the user know that if he wants to label each list,  he'll have
	# to start the script over and enter in each track as a separate list.
	if (($listing eq "each") && ($#list >= 1)) {
		print "Note:  Since you entered \"each\", I will assume that you will\n";
		print " want to enter all information just once rather than per list.\n";
	}
}

# Set default $artist now if $setlabels eq "cd"
$artist = $cd{artist};  $conductor = "";  $performer = "";  $ensemble = "";
if ($setlabels eq "cd") {   # Set the $artist now
	if ($askclassical eq "on") {
		print "Enter conductor.  If there isn't one, enter null.\n";
		print " (single space = null): [", $artist, "]: ";
		$inp = <STDIN>;  chop($inp);
		if ($inp eq "") { $conductor = $artist; } else { $conductor = $inp; }
		if ($inp eq " ") { $conductor = ""; }
		if ($conductor ne "") {
			print "You have entered a conductor so this will be your artist's name.\n";
			$artist = $conductor;
		} else {
			print "Since there is no conductor,  there's probably a (main) performer.\n";
			print "Enter this performer (single space = null): [", $artist, "]: ";
			$inp = <STDIN>;  chop($inp);
			if ($inp eq "") { $performer = $artist; } else { $performer = $inp; }
			if ($inp eq " ") { $performer = ""; }
			if ($performer ne "") {
				print "You have entered a performer so this will be your artist's name.\n";
				$artist = $performer;
			} else {
				print "Since there is no main performer or conductor,\n";
				print " the 'artist' is probably an ensemble.\n";
				print "If this is true,  enter the name of the ensemble (single space = null): [", $artist, "]: ";
				$inp = <STDIN>;  chop($inp);
				if ($inp eq "") { $ensemble = $artist; } else { $ensemble = $inp; }
				if ($inp eq " ") { $ensemble = ""; }
				if ($ensemble ne "") {
					print "You have entered an ensemble so this will be your 'artist'.\n";
					$artist = $ensemble;
				} else {
					print "Okay, just enter the artist then.\n";
					print "Enter artist (single space = null): [", $artist, "]: ";
					$inp = <STDIN>;  chop($inp);
					if ($inp ne "") { $artist = $inp; }
					if ($inp eq " ") { $artist = ""; }
				}
			}
		}
	} else {
		print "Enter artist (single space = null): [", $artist, "]: ";
		$inp = <STDIN>;  chop($inp);
		if ($inp ne "") { $artist = $inp; }
		if ($inp eq " ") { $artist = ""; }
	}
}

# Set $album (the album title)
$default = $cd{title};
$default =~ s/\r//g;   # Get rid of this common char that causes problems
print "Enter album name (single space = null): [", $default, "]: ";
$inp = <STDIN>;  chop($inp);
if ($inp eq "") { $album = $default; } else { $album = $inp; }
if ($inp eq " ") { $album = ""; }

# Set $albumabv (the album abbreviation).  This is only used to help
#  set the default track names.
if (($skipalbumabv eq "on") || (($skipalbumabv eq "classical") && ($askclassical eq "on"))) {
	# If classical, you probably don't want the default name to include
	#  an album abbreviation.  If you do, name the files manually.
	$albumabv = "";
} else {
	# Set $albumabv to the first four first-letters of the $album
	$albumabv = $album;  $albumabv =~ s/_/ /g;  $tmp = "";
	if ($albumabv =~ s/([\w$eurochars]+)//) { $tmp = substr($1, 0, 1); }
	if ($albumabv =~ s/([\w$eurochars]+)//) { $tmp = $tmp . substr($1, 0, 1); }
	if ($albumabv =~ s/([\w$eurochars]+)//) { $tmp = $tmp . substr($1, 0, 1); }
	if ($albumabv =~ s/([\w$eurochars]+)//) { $tmp = $tmp . substr($1, 0, 1); }
	$albumabv = $tmp;
	print "Enter album abbreviation (single space = null): [", $albumabv, "]: ";
	$inp = <STDIN>;  chop($inp);
	unless ($inp eq "") { $albumabv = $inp; }
	if ($inp eq " ") { $albumabv = ""; }
}

# Set the $label and $labelno
print "Enter label name (single space = null): []: ";
$inp = <STDIN>;  chop($inp);
if (($inp eq "") || ($inp eq " ")) { $label = ""; } else { $label = $inp; }
# Set $labelno
print "Enter label catalog number (single space = null): []: ";
$inp = <STDIN>;  chop($inp);
if (($inp eq "") || ($inp eq " ")) { $labelno = ""; } else { $labelno = $inp; }

# Set $recdate
# Incidently, the date of the recording is one of those things that I want
# to ask for every list,  even if $setlabels="cd".  But if the $listing is
# "all" or "each" then I'll just ask it once right now.
$recdate = "";
if (($listing eq "all") || ($listing eq "each")) {
	# Set $recdate for ALL the tracks here and now...
	if ($firstdate) {
		print "Enter the date of recording -\n";
		print "\n(formatting notes: no format is specifically required, but it is definitely\n";
		print " preferable to include all FOUR digits of the year,  and since it'll never\n";
		print " always be clear whether it's MM-DD or DD-MM, it is actually preferable\n";
		print " to spell out the month (at least the first 3 letters anyway) and then the day,\n";
		print " but if you really want to use a numeric format, chose the ISO-8601 standard\n";
		print " (YYYY-MM-DD),  however note that it may still be unclear to some which number\n";
		print " is the month and which is the day.  So it is recommended that you use dates\n";
		print " such as \"March 24, 1994\", \"March, 1994\", or \"1994\" (depending on how much\n";
		print " information you have),  and if all you have is the production (P) date,  put\n";
		print " \"(P)1994\".  The actual recording date is usually up to a year or two before\n";
		print " the production date.\n";
		$firstdate = 0;
	}
	print "Enter the date of recording (single space = null): [don't put one]: ";
	$inp = <STDIN>;  chop($inp);
	$recdate = $inp;
	if ($inp eq " ") { $recdate = ""; }
}

# If $setlabels ne "list",  set other tag fields here...
if ($setlabels ne "list") {
	print "\nType the name of a field tag that you'd like to add to\n";
	print " all these music files (or make up your own field tag name).\n";
	print "You can enter the fieldname then enter the data for that fieldname,\n";
	print "or you can enter fieldname=data all on one line.\n";
	# List some possible recommended tags
	print "\nPossible tag recommendations:\n";
	if ($askclassical eq "on") {
		if ($composer eq "") { print "  composer"; }
		if ($conductor eq "") { print "  conductor"; }
		if ($ensemble eq "") { print "  ensemble"; }
		print "  opus\n";
	}
	print "  location  performer  copyright  license\n\n";
	# Continue
	print "Just press <enter> (with nothing on the line) to finish:\n";
	$finished = 0;  $tagindex = 0;
	while (!$finished) {
		$inp = <STDIN>;  chop $inp;
		if (($inp ne "") && ($inp ne " ") && ($inp ne ".")) {
			# If there's an "=" assume the line is "fieldname=data"
			if ($inp =~ m/=/) {
				$tag[$tagindex] = $inp;
			} else {
				# There is no "=".  Assume the line is just the fieldname.
				$tagname = $inp;
				print "Set " . $tagname . "=";
				$inp = <STDIN>;  chop $inp;
				$tagdata = $inp;
				$tag[$tagindex] = $tagname . "=" . $tagdata;
			}
			$tagindex++;
		} else {
			$finished = 1;
		}
	}
}



# Filename Setting Section
print "\nTrack/filename labeling section...\n";

# Set all the default track names
$i = 1;
while ($i <= $totaltracks) {
	if ($i < 10) { $tmpindex = "0" . $i; } else { $tmpindex = $i; }
	$tn2 = $trname[$i];  $tocorigname = $tn2;
	# If there's no useful info in $tn2,  set it to "track_??"
	unless ($tn2 =~ m/\w/) { $tn2 = "track_" . $tmpindex; }
	if ($complast ne "") {
		$tn0 = $complast;
		$tn0 = $tn0 . "_-_";
	} elsif ($artist ne "") {
		$tn0 = $artist;
		$tn0 = $tn0 . "_-_";
	} else {
		$tn0 = "";
	}
	if ($albumabv eq "") {
		$tn1 = "";
	} else {
		$tn1 = $albumabv . $tmpindex . "_";
	}
	unless(defined $cd{title}) {
		if ($album eq "") {
			$album = "CDDB info not available";
		}
	}

	# Set default filename from its components
#	$tmpname = $artist . "_-_" . $albumabv . $tmpindex . "_" . $title;
	$tmpname = $tn0 . $tn1 . $tn2;

	# Process $tmpname
	$tmpname =~ s/\s+/_/g;  # Substitute white-space character blocks with "_"
	# Map special chars to other allowable chars
	$tmpname =~ s/[\/\\~]/-/g;    # Replace any of these chars with a "-"
	$tmpname =~ s/</(/g;  $tmpname =~ s/>/)/g;	# Substitute <> with ()
	$tmpname =~ s/{/[/g;  $tmpname =~ s/}/]/g;  # Substitute {} with []
	$tmpname =~ s/\&/_and_/g;	# Replace & with "and" (double-"_" will be taken out later)
	if ($mapeurochars eq "on") {
		# Map euro-characters to American characters
		$tmpname =~ s/[]/a/g;  $tmpname =~ s/[]/A/g;
		$tmpname =~ s/[]/e/g;  $tmpname =~ s/[]/E/g;
		$tmpname =~ s/[]/i/g;  $tmpname =~ s/[]/I/g;
		$tmpname =~ s/[]/o/g;  $tmpname =~ s/[]/O/g;
		$tmpname =~ s/[]/u/g;  $tmpname =~ s/[]/U/g;
		$tmpname =~ s/[]/n/g;  $tmpname =~ s/[]/N/g;
		$tmpname =~ s/[]/c/g;  $tmpname =~ s/[]/C/g;
		$tmpname =~ s/[]/s/g;  $tmpname =~ s/[]/S/g;
		$tmpname =~ s/[]/y/g;  $tmpname =~ s/[ݾ]/Y/g;
		$tmpname =~ s/[]/z/g;  $tmpname =~ s/[]/Z/g;
		$tmpname =~ s/[]/ae/g;  $tmpname =~ s/[]/AE/g;
		$tmpname =~ s/[]/oe/g;  $tmpname =~ s/[]/OE/g;
		$tmpname =~ s/[]/B/g;
	}
	# Now get rid of the rest (of the special chars)
	$tmpname =~ s/[^\w$funnychars$eurochars]//g;    # Only keep specified $funnychars and $eurochars
	$tmpname =~ s/__/_/g;   # Get rid of any double-blanks caused by the last line
	$tmpname =~ s/(,_|_,)/,/g;  $tmpname =~ s/(\._|_\.)/./g;    # Rid more extraneous "_"s
	if ($allowspaces eq "on") {
		# Replace all suggested "_" with " "
		# Easier doing this once at the end of the process than
		# making many exceptions from the beginning
		$tmpname =~ s/_/ /g;
	}
	# Done.  $tmpname will now be the default (computer-generated) filename

	$tocname[$tmpindex] = $tmpname;
	$tocorigname[$tmpindex] = $tocorigname;
	$i++;
}


# If $album is empty set it to $albumabv
unless ($album =~ m/\S+/) { $album = $albumabv; }

# User interaction begins for filename selection...
if ($editnames eq "on") {
	print "Here are the computer-chosen filenames for the tracks that you've picked:\n";
	# Create the filenames.txt file now
	open(NAMEFILE, ">filenames.txt");
	$listnum = 0;
	while ($listnum <= $#list) {
		$index = 0;
		while ($index <= $#{$list[$listnum]}) {
			print NAMEFILE "$tocname[$list[$listnum][$index]]\n";
			$index++;
		}
		print NAMEFILE "\n";
		$listnum++;
	}
	close(NAMEFILE);
	# Assume the computer-generated defaults aren't broken!
	$broken = 0;  $warning = 0;  $firstpass = 1;
	do {
		# Print out the filenames.txt file to the screen
		print "-------------------------------------------------------------------------------\n";
		open(NAMEFILE, "filenames.txt");
		while ($line = <NAMEFILE>) {
			# Process $line just like the filenames would be before showing them
			chop $line;  $line =~ s/^\s*//;  $line =~ s/\s*$//;
			print $line, "\n";
		}
		close(NAMEFILE);

		# Query
		$default = "no";
		if ($warning) {
			print $warningmsg;
			$default = "yes";
		}
		if ($broken) {
			print $reason;
			$default = "yes";
		}
		if ($firstpass) {
			$default = "yes";
		}
		print "Edit the above filenames? [" . $default . "]: ";
		$inp=<STDIN>;  chop($inp);
		if ($inp eq "") { $inp = $default; }
		$inp =~ s/\s//g;
		if ($inp =~ m/^\s*y/i) {
			system "$editor filenames.txt";
			$firstpass = 0;
		}
		# Process filenames.txt into $oname[][] and $name[][]
		$broken = 0;	# Will set this to 1 if the file is discovered to be broken
		$warning = 0;
		open(NAMEFILE, "filenames.txt");
		$listnum = 0;
		while ($listnum <= $#list) {
			$index = 0;
			while ($index <= $#{$list[$listnum]}) {
				$oname[$listnum][$index] = <NAMEFILE>;
				chop $oname[$listnum][$index];
				# Clear off any whitespace at the beginning or end of string
				$oname[$listnum][$index] =~ s/^\s*//;  $oname[$listnum][$index] =~ s/\s*$//;
				# But there are any spaces left set $broken
				if (($oname[$listnum][$index] =~ m/\s/) && ($allowspaces ne "on")) {
					$broken = 1;  $reason = "No spaces allowed in filenames.\n";
				}
				unless ($oname[$listnum][$index] =~ m/\w/) {
					$broken = 1;  $reason = "Invalid filename detected (no word characters).\n";
				}
				if ($oname[$listnum][$index] eq "") {
					$broken = 1;  $reason = "There must be one (and only one) empty line between each list.\n";
				}
				if ($oname[$listnum][$index] =~ m/\.(ogg|mp3|wav)$/i) {
					# Not broken,  but print a notice and set default to edit again.
					$warning = 1;
					$warningmsg = "Warning:  You are not supposed to end your filenames with an extension here.\n";
				}
				$name[$listnum][$index] = $oname[$listnum][$index];
				# Add escapes to the appropriate characters
				# Replace (, ), ', ` chars with \(, \), \', \`, etc.
				$name[$listnum][$index] =~ s/\(/\\(/g;  $name[$listnum][$index] =~ s/\)/\\)/g;
				$name[$listnum][$index] =~ s/'/\\'/g;  $name[$listnum][$index] =~ s/`/\\`/g;
				$name[$listnum][$index] =~ s/\"/\\\"/g;  $name[$listnum][$index] =~ s/ /\\ /g;
				$index++;
			}
			$emptyline = <NAMEFILE>;  chop $emptyline;  $emptyline =~ s/\s//g;
			if ($emptyline ne "") {
				$broken = 1;  $emptyline = "not_empty";
				$reason = "There cannot be empty lines within a list.\n";
			}
			$listnum++;
		}

		# If the names do not process go thru the loop again
	} until (($inp eq "no") && (!$broken));

} else {
	print "NOTE:  If you make a mistake during this section,  you can\n";
	print " enter a single space to go backwards in the list(s).  You\n";
	print " will then get a chance to redo the previous item(s).\n";
}
$listnum = 0;
while ($listnum <= $#list) {
	if ($editnames eq "on") {
		if (($setlabels eq "list") || (($listing ne "all") && ($listing ne "each"))) {
			print "\n\n-------------------------------------------------------------------------------\n";
			print "Enter information for the list:\n\n";
			# Print out the filenames in this list
			$index = 0;
			while ($index <= $#{$list[$listnum]}) {
				print "$oname[$listnum][$index]\n";
				$index++;
			}
			print "\n";
		}
	} else {
		print "\nNew List\n\n";
		$index = 0;
		while ($index <= $#{$list[$listnum]}) {
			print "Name for track ", $list[$listnum][$index], " - ";
			# $oname[][] will be the filename BEFORE inserting '\'s (for certain chars)
			$oname[$listnum][$index] = $tocname[$list[$listnum][$index]];
			$name[$listnum][$index] = $tocname[$list[$listnum][$index]];
			# Replace (, ), ', ` chars with \(, \), \', \`   *** - Might need to add on this later
			$name[$listnum][$index] =~ s/\(/\\(/g;  $name[$listnum][$index] =~ s/\)/\\)/g;
			$name[$listnum][$index] =~ s/'/\\'/g;  $name[$listnum][$index] =~ s/`/\\`/g;
			print "[", $oname[$listnum][$index], "]: ";
			$inp=<STDIN>;  chop($inp);
			unless (($inp eq "") || ($inp eq " ")) {
				print "Naming track ", $list[$listnum][$index], " to ", $inp, "\n";
				# Set $oname[][] to the name before putting in the '\'s.
				$oname[$listnum][$index] = $inp;
				# Add escapes to the appropriate characters
				# Replace (, ), ', ` chars with \(, \), \', \`, etc.
				$inp =~ s/\(/\\(/g;  $inp =~ s/\)/\\)/g;
				$inp =~ s/'/\\'/g;  $inp =~ s/`/\\`/g;
				$inp =~ s/\"/\\\"/g;  $inp =~ s/ /\\ /g;
				$name[$listnum][$index] = $inp;
			}
			$index++;
			if ($inp eq " ") {
				print "Going back...\n";
				$index--;  $index--;
				if ($index == -1) {
					$listnum--;  $index = 0;
					if ($listnum == -1) {
						$listnum = 0;
					} else {
						$index = $#{$list[$listnum]};
					}
				}
			}
		}
		print "-------------------------------------------------------------------------------\n";
	}

	# If $setlabels eq "list" set $artist[$listnum] now.
	if ($setlabels eq "list") {   # Set the $artist now
		if ($askclassical eq "on") {
			# If this is the first time around,  set $default = $artist
			if (($listnum < 1) && ($conductor eq "")) { $default = $artist; } else { $default = $conductor; }
			print "Enter conductor.  If there isn't one, enter null.\n";
			print " (single space = null): [", $default, "]: ";
			$inp = <STDIN>;  chop($inp); 
			if ($inp eq "") { $conductor = $default; } else { $conductor = $inp; }
			if ($inp eq " ") { $conductor = ""; }
			if ($conductor ne "") {
				print "You have entered a conductor so this will be your artist's name.\n";
				$artist = $conductor;
			} else {
				if (($listnum < 1) && ($performer eq "")) { $default = $artist; } else { $default = $performer; }
				print "Since there is no conductor,  there's probably a (main) performer.\n";
				print "If this is true,  enter this performer (single space = null): [", $default, "]: ";
				$inp = <STDIN>;  chop($inp); 
				if ($inp eq "") { $performer = $default; } else { $performer = $inp; }
				if ($inp eq " ") { $performer = ""; }
				if ($performer ne "") {
					print "You have entered a performer so this will be your artist's name.\n";
					$artist = $performer;
				} else {
					if (($listnum < 1) && ($ensemble eq "")) { $default = $artist; } else { $default = $ensemble; }
					print "Since there is no main performer or conductor,\n";
					print " the 'artist' is probably an ensemble.\n";
					print "If this is true,  enter the name of the ensemble (single space = null): [", $default, "]: ";
					$inp = <STDIN>;  chop($inp);
					if ($inp eq "") { $ensemble = $default; } else { $ensemble = $inp; }
					if ($inp eq " ") { $ensemble = ""; }
					if ($ensemble ne "") {
						print "You have entered an ensemble so this will be your 'artist'.\n";
						$artist = $ensemble;
					} else {
						print "Okay, just enter the artist then.\n";
						print "Enter artist (single space = null): [", $artist, "]: ";
						$inp = <STDIN>;  chop($inp);
						if ($inp ne "") { $artist = $inp; }
						if ($inp eq " ") { $artist = ""; }
					}
				}
			}
		} else {
			print "Enter artist (single space = null): [", $artist, "]: ";
			$inp = <STDIN>;  chop($inp);
			if ($inp ne "") { $artist = $inp; }
			if ($inp eq " ") { $artist = ""; }
		}
	}
	# Set these fields regardless if null or not
	#  because they are all labeled per list.
	$conductor[$listnum] = $conductor;
	$performer[$listnum] = $performer;
	$ensemble[$listnum] = $ensemble;
	$artist[$listnum] = $artist;


	if (($listing ne "all") && ($listing ne "each")) {
		# Set $recdate for the tracks in this list
		if ($firstdate) {
			print "Enter the date of recording -\n";
			print "\n(formatting notes: no format is specifically required, but it is definitely\n";
			print " preferable to include all FOUR digits of the year,  and since it'll never\n";
			print " always be clear whether it's MM-DD or DD-MM, it is actually preferable\n";
			print " to spell out the month (at least the first 3 letters anyway) and then the day,\n";
			print " but if you really want to use a numeric format, chose the ISO-8601 standard\n";
			print " (YYYY-MM-DD),  however note that it may still be unclear to some which number\n";
			print " is the month and which is the day.  So it is recommended that you use dates\n";
			print " such as \"March 24, 1994\", \"March, 1994\", or \"1994\" (depending on how much\n";
			print " information you have),  and if all you have is the production (P) date,  put\n";
			print " \"(P)1994\".  The actual recording date is usually up to a year or two before\n";
			print " the production date.\n";
			$firstdate = 0;
		}
		print "Enter the date of recording (single space = null): [";
		if ($recdate eq "") { print "don't put one"; }
		else { print $recdate; }
		print "]: ";
		$inp = <STDIN>;  chop $inp;
		if ($inp ne "") { $recdate = $inp; }
		if ($inp eq " ") { $recdate = ""; }
	}
	$recdate[$listnum] = $recdate;

	# If $setlabels eq "list",  set other tag fields here...
	if ($setlabels eq "list") {
		print "\nType the name of a field tag that you'd like to add to\n";
		print " all these music files (or make up your own field tag name).\n";
		print "You can enter the fieldname then enter the data for that fieldname,\n";
		print "or you can enter fieldname=data all on one line.\n";
		# List some possible recommended tags
		print "\nPossible tag recommendations:\n";
		if ($askclassical eq "on") {
			if ($composer eq "") { print "  composer"; }
			if ($conductor eq "") { print "  conductor"; }
			if ($ensemble eq "") { print "  ensemble"; }
			print "  opus\n";
		}
		print "  location  performer  copyright  license\n\n";
		# Continue
		print "Just press <enter> (with nothing on the line) to finish:\n";
		$finished = 0;  $tagindex = 0;
		while (!$finished) {
			$inp = <STDIN>;  chop $inp;
			if (($inp ne "") && ($inp ne " ") && ($inp ne ".")) {
				# If there's an "=" assume the line is "fieldname=data"
				if ($inp =~ m/=/) {
					$tag[$listnum][$tagindex] = $inp;
				} else {
					# There is no "=".  Assume the line is just the fieldname.
					$tagname = $inp;
					print "Set " . $tagname . "=";
					$inp = <STDIN>;  chop $inp;
					$tagdata = $inp;
					$tag[$listnum][$tagindex] = $tagname . "=" . $tagdata;
				}
				$tagindex++;
			} else {
				$finished = 1;
			}
		}
	}

	$listnum++;
}


# If editnames is "both",  here is the last chance to edit the filenames
# This is basically a reiteration of the above code.
if ($editnames eq "both") {
	print "\nHere are the filenames you put:\n";
	# Create the filenames.txt file now
	open(NAMEFILE, ">filenames.txt");
	$listnum = 0;
	while ($listnum <= $#list) {
		$index = 0;
		while ($index <= $#{$list[$listnum]}) {
			print NAMEFILE "$oname[$listnum][$index]\n";
			$index++;
		}
		print NAMEFILE "\n";
		$listnum++;
	}
	close(NAMEFILE);
	# Assume the computer-generated defaults aren't broken!
	$broken = 0;  $warning = 0;  $firstpass = 1;
	do {
		# Print out the filenames.txt file to the screen
		print "-------------------------------------------------------------------------------\n";
		open(NAMEFILE, "filenames.txt");
		while ($line = <NAMEFILE>) {
			# Process $line just like the filenames would be before showing them
			chop $line;  $line =~ s/^\s*//;  $line =~ s/\s*$//;
			print $line, "\n";
		}
		close(NAMEFILE);

		# Query
		$default = "no";
		if ($warning) {
			print $warningmsg;
			$default = "yes";
		}
		if ($broken) {
			print $reason;
			$default = "yes";
		}
		if ($firstpass) {
			$default = "yes";
		}
		print "Edit the above filenames? [" . $default . "]: ";
		$inp=<STDIN>;  chop($inp);
		if ($inp eq "") { $inp = $default; }
		$inp =~ s/\s//g;
		if ($inp =~ m/^\s*y/i) {
			system "$editor filenames.txt";
			$firstpass = 0;
		}
		# Process filenames.txt into $oname[][] and $name[][]
		$broken = 0;	# Will set this to 1 if the file is discovered to be broken
		$warning = 0;
		open(NAMEFILE, "filenames.txt");
		$listnum = 0;
		while ($listnum <= $#list) {
			$index = 0;
			while ($index <= $#{$list[$listnum]}) {
				$oname[$listnum][$index] = <NAMEFILE>;
				chop $oname[$listnum][$index];
				# Clear off any whitespace at the beginning or end of string
				$oname[$listnum][$index] =~ s/^\s*//;  $oname[$listnum][$index] =~ s/\s*$//;
				# But there are any spaces left set $broken
				if (($oname[$listnum][$index] =~ m/\s/) && ($allowspaces ne "on")) {
					$broken = 1;  $reason = "No spaces allowed in filenames.\n";
				}
				unless ($oname[$listnum][$index] =~ m/\w/) {
					$broken = 1;  $reason = "Invalid filename detected (no word characters).\n";
				}
				if ($oname[$listnum][$index] eq "") {
					$broken = 1;  $reason = "There must be one (and only one) empty line between each list.\n";
				}
				if ($oname[$listnum][$index] =~ m/\.(ogg|mp3|wav)$/i) {
					# Not broken,  but print a notice and set default to edit again.
					$warning = 1;
					$warningmsg = "Warning:  You are not supposed to end your filenames with an extension here.\n";
				}
				$name[$listnum][$index] = $oname[$listnum][$index];
				# Add escapes to the appropriate characters
				# Replace (, ), ', ` chars with \(, \), \', \`, etc.
				$name[$listnum][$index] =~ s/\(/\\(/g;  $name[$listnum][$index] =~ s/\)/\\)/g;
				$name[$listnum][$index] =~ s/'/\\'/g;  $name[$listnum][$index] =~ s/`/\\`/g;
				$name[$listnum][$index] =~ s/\"/\\\"/g;  $name[$listnum][$index] =~ s/ /\\ /g;
				$index++;
			}
			$emptyline = <NAMEFILE>;  chop $emptyline;  $emptyline =~ s/\s//g;
			if ($emptyline ne "") {
				$broken = 1;  $emptyline = "not_empty";
				$reason = "There cannot be empty lines within a list.\n";
			}
			$listnum++;
		}

		# If the names do not process go thru the loop again
	} until (($inp eq "no") && (!$broken));
}

print "\n\n";


# Note:  $oname[][] (original name) is the same as $name[][] with the
#  exception that $name includes an escape "\" on characters used to
#  specify the filename from the shell.  When specifying the filename
#  in perl (i.e. using print),  use $oname[][].  When specifying the
#  filename in a shell (i.e. using ``),  use $name[][].


# Start the .tag file for each file.
$listnum = 0;
while ($listnum <= $#list) {
	$index = 0;
	while ($index <= $#{$list[$listnum]}) {
		open(TAGFILE, ">$oname[$listnum][$index].tag");

		# Set tracknumber (left-pad with zeros to two digits)
		$tmptrnum = $list[$listnum][$index];
		if (length($tmptrnum) == 1) {
			if ($tmptrnum =~ /\d/) { $tmptrnum = "0" . $tmptrnum; }
			else { $tmptrnum = ""; }
		} elsif (length($tmptrnum) == 2) {
			unless ($tmptrnum =~ /\d\d/) { $tmptrnum = ""; }
		} else { $tmptrnum = ""; }

		# Set the title string ($titlestr)
		# If $settitle is "cddb" and there is CDDB info set the title to that,
		# otherwise set it from the filename.
		if (($settitle eq "cddb") && ($tocorigname[$list[$listnum][$index]] ne "")) {
			# Set from CDDB info
			$titlestr = $tocorigname[$list[$listnum][$index]];
			# Now change underscores back to spaces...
			$titlestr =~ s/_/ /g;
		} else {
			# Set from filename (after first "_-_")
			$titlestr = $oname[$listnum][$index];
			# Now strip off the artist name (everything before the first _-_)
			if ($titlestr =~ m/_-_(.+)/) { $titlestr = $1; }
			# Now check to see if $albumabv is in the title.
			if ($albumabv ne "") {
				if ($titlestr =~ m/^\.?$albumabv/) {
					$titlestr =~ s/^\.?$albumabv\.?\d?\d//;
				}
			}
			# Now change underscores back to spaces...
			$titlestr =~ s/_/ /g;
			# Trim off any spaces on the beginning or end
			$titlestr =~ s/^\s+//;
			$titlestr =~ s/\s+$//;
			# Make array of titles for possible CDDB submit
			$titlestr[$list[$listnum][$index]] = $titlestr;
		}

		# Tag the file
		if ($titlestr ne "") {
			print TAGFILE "title=$titlestr\n";
		}

		# Artist info
		if ($artist[$listnum] ne "") {
			print TAGFILE "artist=$artist[$listnum]\n";
		}
		# Composer should be mandatory for Classical,  but if it's not set
		#  it is still possible that it is being added as a user-specified tag.
		if ($composer ne "") {
			print TAGFILE "composer=$composer\n";
		}
		# Conductor and Performer may or may not be set.  If they have been set CD-wide,
		#  then the list-specific variable is set during the list query section.
		if ($conductor[$listnum] ne "") {
			print TAGFILE "conductor=$conductor[$listnum]\n";
		}
		if ($performer[$listnum] ne "") {
			print TAGFILE "performer=$performer[$listnum]\n";
		}
		if ($ensemble[$listnum] ne "") {
			print TAGFILE "ensemble=$ensemble[$listnum]\n";
		}
		# Date of recording - tends to immediately follow artist info
		if ($recdate[$listnum] ne "") {
			print TAGFILE "date=$recdate[$listnum]\n";
		}

		# User-added tags:  (presumably this is probably also artist-related)
		$tagindex = 0;
		if ($setlabels eq "list") {
			while ($tagindex <= $#{$tag[$listnum]}) {
				print TAGFILE "$tag[$listnum][$tagindex]\n";
				$tagindex++;
			}
		} else {
			while ($tagindex <= $#tag) {
				print TAGFILE "$tag[$tagindex]\n";
				$tagindex++;
			}
		}

		# Album info
		if ($album ne "") {
			print TAGFILE "album=$album\n";
		}
		if ($label ne "") {
			print TAGFILE "label=$label\n";
		}
		if ($labelno ne "") {
			print TAGFILE "labelno=$labelno\n";
		}
		# Track & partnumber info
		print TAGFILE "tracknumber=$tmptrnum\n";
		# The partnumber is just which place it is in its list.
		if ($#{$name[$listnum]} > 1) {
			$partnumber = $index + 1;
			print TAGFILE "partnumber=$partnumber\n";
		}

		if ($genre ne "") {
			print TAGFILE "genre=$genre\n";
		}

		# Additional stuff
		if ($contact ne "") {
			print TAGFILE "contact=$contact\n";
		}
		close(TAGFILE);
		# Done creating the tag file for this particular file

		# Now print the track - filename, and the tag file.
		print $tmptrnum . ": " . $oname[$listnum][$index] . "\n";
		$out=`cat -- $name[$listnum][$index].tag`;  print $out, "\n";

		$index++;
	}
	print "\n";
	$listnum++;
}


# Prompt to see if user wants to edit any of the .tag files
print "\nLook at the above tags and make sure they are all okay.\n";
print "You can edit any of these tag files by entering the track number.\n";
print "You can also enter \"submit\" to submit this data to the freedb\n";
print "CDDB database or just press <enter> by itself to go directly to\n";
print "the ripping process!  (or press ctrl-C to abort everything).\n";

do {
	print "Enter track number, submit, or just press <enter> to Rip! : ";
	$inp = <stdin>;  chop $inp;
	if (($inp eq " ") || ($inp eq ".")) { $inp = ""; }
	if ($inp =~ m/\d+/) {
		$listnum = 0;
		while ($listnum <= $#list) {
			$index = 0;
			while ($index <= $#{$list[$listnum]}) {
				if ($inp == $list[$listnum][$index]) {
					system "$editor $name[$listnum][$index].tag";
				}
				$index++;
			}
			$listnum++;
		}
	}
} until (($inp eq "") || ($inp =~ m/submit/i));


# Submit to the freedb CDDB database if desired.
if ($inp =~ m/submit/i) {
	# First make sure $genre is one of the 11 freedb categories
	$cgy = $genre;
	while (($cgy ne "classical") && ($cgy ne "blues") && ($cgy ne "country") &&
		($cgy ne "data") && ($cgy ne "folk") && ($cgy ne "jazz") &&
		($cgy ne "newage") && ($cgy ne "reggae") && ($cgy ne "rock") &&
		($cgy ne "soundtrack") && ($cgy ne "misc")) {
		print "\nThe genre you've selected (\"$cgy\") is not a category that\n";
		print "freedb accepts.\n";
		print "Please enter one of the following categories for submission:\n";
		print "   classical   blues   country   data   folk   jazz   newage\n";
		print "   reggae   rock   soundtrack   misc\n> ";
		$cgy = <STDIN>;  chop $cgy;
		if ($cgy eq "data") {
			print "\"data\"?!   Are you sure?  [no]: ";
			$inp = <STDIN>;  chop $inp;
			unless ($inp =~ m/y/i) { $cgy = ""; }
		}
	}

	# Write the original raw CDDB info pulled from freedb
	$cddborig = "$cddbsubmitdir/cddb_orig_$discid";
	open(CDDBOR, ">$cddborig");
	foreach $tmp (@{cd->{raw}}) {
		# Now's the chance to pull the revision number
		if ($tmp =~ m/^#\s+Revision:\s*(\d+)/) { $revision = $1 + 1; }
		print CDDBOR $tmp;
	}
	close(CDDBOR);

	# Now create and write submission to file to be e-mailed
	$cddbfile = "$cddbsubmitdir/cddb_submit_$discid";
	open(CDDBFD, ">$cddbfile");
	print CDDBFD "# xmcd CD database file\n#\n# Track frame offsets:\n";
	foreach $tmp (0 .. ($totaltracks - 1)) {
		print CDDBFD "#       ", $toc->[$tmp]{frames}, "\n";
	}
	$cddbduration = int($toc->[$totaltracks]{frames} / 75);
	print CDDBFD "#\n# Disc length: ", $cddbduration, " seconds\n#\n";
	print CDDBFD "# Revision: ", $revision, "\n";
	print CDDBFD "# Submitted via: ", $version, "\n#\n";
	# End of header
	print CDDBFD "DISCID=$discid\n";
	if (($cgy eq "classical") && ($composer ne "")) {
		print CDDBFD "DTITLE=$composer / $album\n";
	} else {
		if (($artist ne "") && ($album ne "")) {
			print CDDBFD "DTITLE=$artist / $album\n";
		} elsif ($artist ne "") {
			print CDDBFD "DTITLE=$artist\n";
		} elsif ($album ne "") {
			print CDDBFD "DTITLE=$album\n";
		} else {
			# hmmm... this really shouldn't happen
			print "WARNING:  Both artist and album are NOT SET.\n";
			print CDDBFD "DTITLE=\n";
		}
	}
	$recdate[0] =~ m/(\d\d\d\d)/;  $year = $1;
	print CDDBFD "DYEAR=$year\n";
	print CDDBFD "DGENRE=$cgy\n";
	foreach $tnm (1 .. $totaltracks) {
		$tmp = $tnm - 1;
		print CDDBFD "TTITLE$tmp=$titlestr[$tnm]\n";
	}
	# Write EXTD and EXTT info
	if ($setlabels eq "list") {
		print CDDBFD "EXTD=\n";
		foreach $tnm (1 .. $totaltracks) {
			$tmp = $tnm - 1;
			# Find $listnum that track $tnm is in.
			$listnum = 0;  $index = -1;
			do {
				$index++;
				if ($index > $#{$list[$listnum]}) {
					$index = 0;  $listnum++;
				}
				if ($listnum > $#list) {   # This should never happen
					print "WTF?  Track $tnm isn't in any list!\n";
					break;
				}
			} until ($tnm == $list[$listnum][$index]);
			# Now write all user-specified tag info to each EXTT#
			# (hey, you gotta better idea?)
			$tagindex = 0;
			while ($tagindex <= $#{$tag[$listnum]}) {
				print CDDBFD "EXTT$tmp=$tag[$listnum][$tagindex]\n";
				$tagindex++;
			}
			if ($tagindex == 0) { print CDDBFD "EXTT$tmp=\n"; }
		}
	} else {
		$tagindex = 0;
		while ($tagindex <= $#tag) {
			print CDDBFD "EXTD=$tag[$tagindex]\n";
			$tagindex++;
		}
		if ($tagindex == 0) { print CDDBFD "EXTD=\n"; }
		foreach $tnm (1 .. $totaltracks) {
			$tmp = $tnm - 1;
			print CDDBFD "EXTT$tmp=\n";
		}
	}
	# End of EXTD and EXTT section
	print CDDBFD "PLAYORDER=\n";
	close(CDDBFD);

	# Verify with the user that the file is correct.
	do {
		# Print out original raw CDDB info
		print "\nORIGINAL CDDB RECORD:\n";
		$out = `cat -- $cddborig`;  print $out;
		# Print out the new CDDB submission data
		print "\nYOUR NEW SUBMISSION:\n";
		$out = `cat -- $cddbfile`;  print $out;
		print "\nPlease be absolutely sure that the above information is correct\n";
		print "and better than the original CDDB record before submitting it to\n";
		print "the CDDB database!";
		print "\nDoes it look good? (accept/edit/abort) [edit]: ";
		$inp = <STDIN>;  chop $inp;
		if (($inp eq "") || ($inp =~ m/edit/i)) {
			system "$editor $cddbfile";
		}
	} until (($inp =~ m/accept/i) || ($inp =~ m/abort/i));

	# Finally, mail off the file
	if ($inp =~ m/abort/i) {
		print "Aborted.\n\n";
	} else {
		print "\nmailing $cddbfile to $cddbsubmitaddr... ";
		`mail -a "Content-Type: text/plain; charset=\"$charset\"" -a "Content-Transfer-Encoding: 8bit" -s "cddb $cgy $discid" -c $cddbsubmitaddrCc $cddbsubmitaddr < $cddbfile`;
		print "Done.\n\n";
	}
}



# Now rip all of the tracks in each list
# cdparanoia command:

print "\n\nBeginning the rip...\n\n";
print "NOTE: You may press ctrl-C during the ripping process to abort\n";
print " the current rip and get the opportunity to rip with different\n";
print " cdparanoia command-line parameters.\n\n";

# Maintain a list of tracks that are possible flawed
#  (aborted and rerun without the "-z")
$posflawed = "";
$listnum = 0;
while ($listnum <= $#list) {
	$index = 0;
	while ($index <= $#{$list[$listnum]}) {
		if ($skiprip eq "on") {
			print "Creating empty file: $oname[$listnum][$index].wav\n";
			`cat /dev/null > $name[$listnum][$index].wav`;
		} else {
			$tmpcdflags = $cdparanoiaflags;
			$trackaborted = 0;
			do {
				print "\n---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----\n";
				print "Ripping track ", $list[$listnum][$index], "...\n\n";
				$cdpcmd="cdparanoia $tmpcdflags -d $cddevice $list[$listnum][$index]";
				print "Command: $cdpcmd\n";
				$cdpout = system "$cdpcmd";
				if ($cdpout != 0) {
					$trackaborted = 1;
					print "\n\nAborted.\n\n";
					print "Retry this track with different cdparanoia flags? (y/n) [y]: ";
					$inp = <STDIN>;  chop $inp;
					if ($inp =~ m/^\s*n/i) {   # If answer is no...
						# Just continue out of the loop.
						$cdpout = 0;
					} else {   # Presumably answer is yes... this is the safe path anyway.
						print "\nThe old cdparanoia flags are \"", $tmpcdflags, "\".\n";
						print "Enter the new flags to try for this track: [", $tmpcdflags, "]: ";
						$inp = <STDIN>;  chop $inp;
						if ($inp ne "") { $tmpcdflags = $inp; }
#						unless (($tmpcdflags =~ m/\b-z\b/) || ($trackposflawed)) {
# The \b-z\b part didn't work
#							# Track was aborted and reripped without the "-z" flag. Take note.
#							$posflawed = $posflawed . "   " . $list[$listnum][$index];
#							$posflawed = $posflawed . " - " . $oname[$listnum][$index] . "\n";
#							$trackposflawed = 1;   # Only take note once though.
#						}
					}
				} else {
					# Track successfully finished ripping
					# But if $tmpcdflags didn't have the '-z' flag in it then give a warning.
					if ($cdparanoiaflags =~ m/-z/) {
						unless ($tmpcdflags =~ m/-z/) {
							$posflawed = $posflawed . "   " . $list[$listnum][$index];
							$posflawed = $posflawed . " - " . $oname[$listnum][$index] . "\n";
						}
					}
				}
			} until ($cdpout == 0);
			# It would be nice to be able to get status/return codes from cdparanoia
			# to determine whether or not the rip is hanging, there's a skip ("V") in
			# the rip, or it otherwise failed or aborted WITHOUT having to get ctrl-C
			# signals from the user.  But until someone implements that in cdparanoia
			# I guess we'll just have to live with this glaring design flaw in cdparanoia.
			# I'd very-much like to implement something like this:
#			} else {
#				print "\n\n\n --- An Error Occurred in the ripping! ---\n\n\n";
#			}
			# ...but there is currently no way to do it!

			# Rename cdda.wav to $name[][].wav
			`mv -- cdda.wav $name[$listnum][$index].wav`;
		} # End if ($skiprip eq "on)

	    $index++;
	}
	$listnum++;
	print "\n";
}


print "Finished ripping.\n";
print "For the rest of this process crip will not need the CD.\n\n\n";
# Some of us will change CDs now and immediately start another instance
# of crip to rip another CD while this one is processing and encoding.
# I personally wait until the encoding starts so that all the major
# disk/IO is over with though.

# Eject CD if that option is turned on.
if ($ejectcd eq "on") {
	print "Attempting to eject CD...\n";
	$ejectcmd = "eject " . $cddevice;
	print "Running `$ejectcmd`\n";
	`$ejectcmd`;
	print "\n";
}


# Now's the time for a stop-gap prompt (i.e. if a user wants the chance
#  to edit the raw .wav files before the processing begins)...
if ($stopgap eq "on") {
	# The following line is just a stop-gap if needed.
	print "Ripping complete.\n";
	print "You may now edit the raw .wav files or whatever...\n";
	print "\nPress <enter> when ready to continue processing: ";
	$inp = <STDIN>; print "\n";
}


# Normalization and Trimming processing section
# If normalizing,  compute the $voladjust value for the list,  which
#  will be based off the list's highest peak-value.
if ($normalize eq "on") {
	print "Volume threshold is ", $volthresh, ".\n";
} else {
	print "Will not normalize.\n";
}
$listnum = 0;
while ($listnum <= $#list) {
	# Figure out what $volarg should be
	$volarg = "";
	if ($normalize eq "on") {
		print "\n\nFinding peak volume for list ", $listnum, "\n";
		$index = 0;  $voladjust = 999999;
		while ($index <= $#{$list[$listnum]}) {    # && ($voladjust >= $volthresh)) { # We want to get all the peaks anyway...
			$statout=`sox $name[$listnum][$index].wav -e stat -v 2>&1`;
			chop($statout);
			print "Volume adjust for ", $oname[$listnum][$index], ".wav: ", $statout, "\n";
			$origpeak[$list[$listnum][$index]] = 1.0 / $statout;
			if ($statout < $voladjust) { $voladjust = $statout; }
			$index++;
		}

		# Set the volume adjust argument for sox (or leave null)
		if ($voladjust >= $volthresh) {   # Add a volume adjust argument to sox.
			print "\n\nAdjusting volume of list ", $listnum, " to ", $voladjust, "\n";
			$normval[$listnum] = $voladjust;
			$volarg = "vol " . $voladjust;
		} else {
			print "\n\nNot adjusting volume of list ", $listnum, " to ", $voladjust, " (not worth it)\n";
			$normval[$listnum] = "";
		}
	}

	# Takes out any beginning or ending quiet time
	# (only take out quiet time if it's at the beginning or end of a group)
	# also normalize (if necessary) at the same time
	# Name of tracks should go from $name[][].wav to $name[][]
	$index = 0;
	while ($index <= $#{$list[$listnum]}) {

		$trimarg = "";
		if ($trimsilence eq "on") { $trimarg = $trimsilargs; }
		$soxcmd1 = "";  $soxcmd2 = "";

		# Set up the sox command(s):
		if ($trimsilence eq "on") {
			# Case 1:  track is the only track in list.
			#  Trim both beginning and end of file:
			if (($index == 0) && ($#{$list[$listnum]} == 0)) {
				$soxcmd1 = "sox $name[$listnum][$index].orig.wav -t wav $name[$listnum][$index].rev.wav $trimarg $volarg reverse";
				$soxcmd2 = "sox $name[$listnum][$index].rev.wav -t wav $name[$listnum][$index].wav $trimarg reverse";
			}
			# Case 2:  track is the 1st track of a group in list
			#  Trim only the beginning of the file
			if (($index == 0) && ($#{$list[$listnum]} > 0)) {
				$soxcmd1 = "sox $name[$listnum][$index].orig.wav -t wav $name[$listnum][$index].wav $trimarg $volarg";
			}
			# Case 3:  track is the last track of a group in list
			#  Trim only the end of the file
			if (($index == $#{$list[$listnum]}) && ($index > 0)) {
				$soxcmd1 = "sox $name[$listnum][$index].orig.wav -t wav $name[$listnum][$index].rev.wav $volarg reverse";
				$soxcmd2 = "sox $name[$listnum][$index].rev.wav -t wav $name[$listnum][$index].wav $trimarg reverse";
			}
			# Case 4:  track is in the middle somewhere of a group in list
			#  Do not trim at all
			if (($index > 0) && ($index < $#{$list[$listnum]})) {
				if ($volarg ne "") {
					# Still have to normalize...
					$soxcmd1 = "sox $name[$listnum][$index].orig.wav -t wav $name[$listnum][$index].wav $volarg";
				} else {
					print "Leaving $oname[$listnum][$index].wav alone.";
					print "  (not trimming nor normalizing)\n";
				}
			}
		} else {   # Silence trimming is off:
			if ($volarg ne "") {
				# Set the sox command to only do normalization:
				$soxcmd1 = "sox $name[$listnum][$index].orig.wav -t wav $name[$listnum][$index].wav $volarg";
			} else {
				# Trimming off & no volargs...
				print "Leaving $oname[$listnum][$index].wav alone.";
				print "  (not trimming nor normalizing)\n";
			}
		}

		# Now actually run the sox command(s):
		if ($soxcmd1 ne "") {
			# First move ripped file to .wav.orig:
			print "Moving $oname[$listnum][$index].wav to $oname[$listnum][$index].orig.wav\n";
			`mv -- $name[$listnum][$index].wav $name[$listnum][$index].orig.wav`;
			print "Running sox command:\n $soxcmd1\n";
			`$soxcmd1`;
			if ($soxcmd2 ne "") {
				print "Running sox command:\n $soxcmd2\n";
				`$soxcmd2`;
			}
		}

		$index++;
	}

	$listnum++;
	print "\n";
}   # End while ($listnum <= $#list)


# Encoding section
if ($encodeto eq "vorbis") {
	$encodestr = "oggenc " . $oggencflags;
} else {
	$encodestr = "flac " . $flacflags;
}
print "\nEncoding with: ", $encodestr, "\n";
$listnum = 0;
while ($listnum <= $#list) {
	# With flac, must encode the whole list on one command line so that
	# the replaygain option (if there) gets all the files in the same 'album'.
	if ($encodeto eq "flac") {
		# Build a filelist and encode them all at once
		$index = 0;  $filelist = "";
		while ($index <= $#{$list[$listnum]}) {
			$filelist = $filelist . $name[$listnum][$index] . ".wav ";
			$index++;
		}
		# Now encode the whole filelist
		print "\nEncoding list $listnum:\n$encodestr $filelist\n\n";
		`$encodestr $filelist`;
	}

	$index = 0;
	while ($index <= $#{$list[$listnum]}) {

		# Encode (if flac,  encoding already done)
		unless ($encodeto eq "flac") {
			print "\nEncoding ", $oname[$listnum][$index], ".wav\n";
			`$encodestr $name[$listnum][$index].wav`;
		}
		# Tag
		# Set the final comment string ($commentstr)
		$commentstr = "";
		# I used to have DiscID-track in the comment field (from the days when
		# I used to make MP3s) but now it's more appropriate the have them in
		# their separate Ogg tags.  So I commented out the next two lines:
		#if ($tmptrnum eq "") { $commentstr = $discid . " "; }
		#else { $commentstr = $discid . "-" . $tmptrnum . " "; }
		if ($normalize eq "on") {
			if ($normval[$listnum] eq "") {
				$peak = $origpeak[$list[$listnum][$index]];
				# Convert $peak to X.XXX format
				$peak = $peak + "0.0004999999";  $peak = substr($peak, 0, 5);
				$commentstr = $commentstr . "not normalized, ";
				$commentstr = $commentstr . "peak=" . $peak . ", ";
			} else {
				$newpeak = $origpeak[$list[$listnum][$index]] * $normval[$listnum];
				# Convert $newpeak to X.XXX format
				$newpeak = $newpeak + "0.0004999999";  $newpeak = substr($newpeak, 0, 5);
				$commentstr = $commentstr . "normalized x" . $normval[$listnum] . ", ";
				$commentstr = $commentstr . "new peak=" . $newpeak . ", ";
			}
		}
		$commentstr = $commentstr . $comment0;
		$timestamp = `date`;  chop $timestamp;
		$commentstr = $commentstr . $timestamp;

		open(TAGFILE, ">>$oname[$listnum][$index].tag");
		print TAGFILE "comment=$commentstr\n";
		if ($comment ne "") { print TAGFILE "comment=$comment\n"; }
		print TAGFILE "sourcemedia=$sourcemedia\n";
		print TAGFILE "discid=$discid\n";
		print TAGFILE "encoding=$encoding\n";
		close(TAGFILE);

		# Okay finally, print what the .tag file finally looks like,
		# and then tag the file with the .tag file
		if ($encodeto eq "vorbis") {
			print "Tagging $oname[$listnum][$index].ogg:\n";
			$out=`cat -- $name[$listnum][$index].tag`;  print $out, "\n";
			`vorbiscomment -w -c $name[$listnum][$index].tag $name[$listnum][$index].ogg`;
		} else {
			if ($flacflags =~ m/--ogg/i) {
				print "Tagging $oname[$listnum][$index].ogg:\n";
				$out=`cat -- $name[$listnum][$index].tag`;  print $out, "\n";
				`metaflac --import-vc-from=$name[$listnum][$index].tag $name[$listnum][$index].ogg`;
			} else {
				print "Tagging $oname[$listnum][$index].flac:\n";
				$out=`cat -- $name[$listnum][$index].tag`;  print $out, "\n";
				`metaflac --import-vc-from=$name[$listnum][$index].tag $name[$listnum][$index].flac`;
			}
		}

		# Delete the tag file (if $delfiles is "on")
		if ($delfiles eq "on") {
			# Delete .wav file(s)
			print "\nDeleting ", $oname[$listnum][$index], ".wav\n";
			`rm -- $name[$listnum][$index].wav`;
			$tmpfilename = $oname[$listnum][$index] . ".orig.wav";
			if (-e $tmpfilename) {
				print "Deleting $tmpfilename\n";
				`rm -- $name[$listnum][$index].orig.wav`;
			}
			$tmpfilename = $oname[$listnum][$index] . ".rev.wav";
			if (-e $tmpfilename) {
				print "Deleting $tmpfilename\n";
				`rm -- $name[$listnum][$index].rev.wav`;
			}
			# Delete .tag file
			print "Deleting $oname[$listnum][$index].tag\n";
			`rm -- $name[$listnum][$index].tag`;
			if (-e "$oname[$listnum][$index].tag~") {
				print "Deleting $oname[$listnum][$index].tag~\n";
				`rm -- $name[$listnum][$index].tag~`;
			}
		}

		print "Done.\n\n";
		$index++;
	}

	# Add replaygain tags.
	# If normalize is on,  don't bother with replaygain
	# (only for non-flac files,  which were done during the encode)
	if (($normalize eq "off") && ($encodeto eq "vorbis")) {
		# Now run replaygain (vorbisgain) on all the tracks in the list.
		# First build the list of files in the list.
		$index = 0;  $filelist = "";
		while ($index <= $#{$list[$listnum]}) {
			$filelist = $filelist . $name[$listnum][$index] . ".ogg ";
			$index++;
		}
		# If there is more than one track in the list,  run in album mode
		if ($index > 1) {
			print "Running vorbisgain on group ", $listnum, "...\n";
			`vorbisgain -a -- $filelist`;
		} else {
			print "Running vorbisgain on track ", $filelist, "...\n";
			`vorbisgain -- $filelist`;
		}
	}

	# Increment the list and go on...
	$listnum++;
	print "\n\n";
}

# Delete the filenames.txt file
if ($delfiles eq "on") {
	print "Deleting filenames.txt\n";
	`rm filenames.txt`;
	if (-e "filenames.txt~") {
		print "Deleting filenames.txt~\n";
		`rm filenames.txt~`;
	}
}


# Before printing "Done!" list the $posflawed tracks.
if ($posflawed ne "") {
	print "\n\n";
	print "REMINDER: The following tracks were aborted and re-ripped\n";
	print "without the \"-z\" option (and could be flawed):\n\n";
	print $posflawed;
	print "\nSo listen to them just to make sure...\n";
}


# Wait for user to hit <return>,  then either end or start all over again...

print "Done!\n";
#print "   Change CDs and hit <return>.\n";
#print "   Ctrl-C to quit...\n\n> ";

#$inp = <STDIN>;

# To change the behavior so that the script restarts from the beginning,
# uncomment the next line and comment out the "exit;" below.
#reset;

# Just exit for now...
exit;

}


# End of script