This file is indexed.

/usr/bin/disc-cover is in disc-cover 1.5.6-1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

   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
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
#!/usr/bin/perl -w
#
# Disc-Cover - using CDDB to create a cover for music cds. Default is
# to output a postscript file called 'artist_-_album.ps' provided the
# album is found in the CDDB database.
# Copyright © 1999-2005 J.I. van Hemert
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Author: J.I. van Hemert <jvhemert@cwi.nl>
#
# Physical mail:
# J.I. van Hemert
# CWI
# P.O. Box 94079
# NL-1090 GB Amsterdam
# The Netherlands
#

my $cvs_id_string = '$Id: disc-cover,v 1.6 2006/06/20 12:15:55 jvhemert Exp $';


################################
# Default configuration values #
################################

my ($config_homedir) = "";
$config_homedir = "$ENV{HOME}" if (defined $ENV{HOME});
my ($config_tmp_dir) = '/tmp';
my (@config_template_dirs) = ('/usr/share/disc-cover/templates','/usr/lib/disc-cover/templates','templates');
my ($config_output_format) = 'ps';
my ($config_device) = "/dev/cdrom";
my ($config_cddb_cache_directory) = "$config_homedir/.cddb";
my ($config_flaptext) = split ',', (getpwuid($<))[6];
my ($config_version_config) = undef;
my ($latex_user_packages) = '';
my ($latex_language_encoding) = 'latin1';
my ($flag_genre) = 0;
my ($flag_with_extended_track_info) = 0;
my (%config_latex_colors) = ( title => 'black', artist => 'black', discinfo => 'black', track_number => 'black', track_string => 'black', track_time => 'black', flaptext => 'black', track_extended => 'black', track_artist => 'black', 'genre' => 'black' );
my ($config_casetype) = "jewel";
my ($flag_various_artist_cd) = 0;
my ($flag_double_disc_cdtitle) = 1;
my ($flag_picture_use_allmusic) = 0;
my ($flag_uppercase_fix) = 0;
my ($flag_verbose) = 0;
my ($flag_remove_temporary_files) = 0; # 0 means yes, remove files
my ($latex_global_oddsidemargin, $latex_global_evenside_margin, $latex_global_voffset, $latex_global_footskip, $latex_global_textheight, $latex_global_textwidth) = ("-25pt", "-25pt", "-100pt", "0pt", "780pt", "170mm");



############################
# Read configuration files #
############################

my ($config_version) = '1.5.6';
eval `cat "/etc/disc-cover.conf"` if (-f "/etc/disc-cover.conf");
if (defined($config_version_config) and $config_version_config ne $config_version)
{
	warn "Warning: version of system configuration file \"/etc/disc-cover.conf\"\n($config_version_config) does not match current configuration version of Disc-Cover ($config_version)\n"
}
eval `cat "$config_homedir/.disc-coverrc"` if (-f "$config_homedir/.disc-coverrc");
if (defined($config_version_config) and $config_version_config ne $config_version)
{
	warn "Warning: version of user configuration file \"$config_homedir/.disc-coverrc\"\n($config_version_config) does not match current configuration version of Disc-Cover ($config_version)\n"
}

#################################
# Non advisable to change these #
#################################

my ($bottomtextfront) = undef;
my ($config_cdserverrc) = "$config_homedir/.cdserverrc";
my ($version) = '1.5.6';
my ($process_number) = $$;
my ($flag_move_end_file) = 0;
my ($config_output_configuration_filename) = undef;
my ($outputfile) = undef;
my ($inputfile) = undef;
my ($config_second_inputfile) = undef;
my ($config_all_available_output_types) = 'tex dvi ps pdf cddb txt lbl html';
my ($latex_global_packages) = 'ifthen,graphicx,color,multicol';
my ($config_cover_picture) = undef;
my ($config_cover_picture_no_convert_needed) = undef;


########################
# Start of main progam #
########################

use strict;
use Getopt::Long;

#TODO use Image::Magick;


my @command_line = @ARGV;

if ($version =~ "beta")
{
	warn	"+--------------------------------------+\n".
		"| This is a beta version ($version). |\n".
		"| It is for testing purposes only.     |\n".
		"+--------------------------------------+\n";
}

my $flag_show_help = 0;
my $flag_show_cvs_version = 0;
my $flag_show_manual = 0;
my $flag_show_version = 0;
my $flag_show_releaseversion = 0;
my $flag_create_new_cddb = 0;
my $flag_create_cdserverrc = 0;
my $flag_template_list = 0;
Getopt::Long::Configure("permute", "no_ignore_case");
GetOptions	("pic=s" => \$config_cover_picture_no_convert_needed,
		"p=s" => \$config_cover_picture,
		"t|type=s" => \$config_output_format,
		"f|file=s" => \$inputfile, 
		"D|Device=s" => \$config_device, 
		"o|output=s" => \$outputfile, 
		"a|additional=s" => \$bottomtextfront, 
		"V|Verbose" => \$flag_verbose, 
		"R|Remove" => \$flag_remove_temporary_files, 
		"S|Server" => \$flag_create_cdserverrc, 
		"e|extended" => \$flag_with_extended_track_info, 
		"n|new!" => \$flag_create_new_cddb, 
		"h|help!" => \$flag_show_help, 
		"H|Help!" => \$flag_show_manual, 
		"u|uppercase!" => \$flag_uppercase_fix,
		"v|version!" => \$flag_show_version, 
		"releaseversion!" => \$flag_show_releaseversion,
		"b|flaptext=s" => \$config_flaptext, 
		"g|genre!" => \$flag_genre, 
		"C|Configuration=s" => \$config_output_configuration_filename,
		"c|casetype=s" => \$config_casetype,
		"va|variousartists!" => \$flag_various_artist_cd,
		"allmusic!" => \$flag_picture_use_allmusic,
		"2|second=s" => \$config_second_inputfile,
		"CVS!" => \$flag_show_cvs_version,
		"template_list" => \$flag_template_list);
($flag_template_list) and print_template_list() and exit;
($flag_show_cvs_version) and print "$cvs_id_string" and exit;
($flag_show_help) and print print_help() and exit;
($flag_show_manual) and print_manual() and exit;
($flag_show_version) and print print_version_info() and exit;
($flag_show_releaseversion) and print "$version" and exit;
($flag_create_cdserverrc) and create_cdserverrc() and exit;
if ($config_output_configuration_filename)
{
	open CONF, "> $config_output_configuration_filename" or die "Could not open \"$config_output_configuration_filename\" for writing";
	print CONF print_configuration_file();
	close CONF;
	exit 0;
}

unless ($inputfile)
{
	require Audio::CD;
}

print STDERR "Disc-Cover: running verbose version $version (config $config_version)\n" if ($flag_verbose);

if ($flag_verbose)
{
	print STDERR "Disc-Cover: command line string: ".join(' ',@command_line)."\n";
	print STDERR "Disc-Cover: SYSTEM INFO\n";
	print STDERR "  OS version: ".`uname -a`;
	print STDERR "  Perl version: ".`perl -V:version`;
	print STDERR "  libcdaudio version: ".`echo /usr/lib/libcdaudio.so.*`;
	print STDERR "  Audio-CD version: ".Audio::CD->VERSION."\n";
	print STDERR "  LaTeX version: ".`latex -v | head -1`;
	print STDERR "  Dvips version: ".`dvips -v`;
	print STDERR "  Convert version: ".`convert -v | head -1`;
}

$config_tmp_dir .= "/disc-cover-$process_number";
print STDERR "Disc-Cover: creating tmp dir ($config_tmp_dir)\n" if $flag_verbose;
mkdir ("$config_tmp_dir", 0777) or die "Error: could not create temporary directory \"$config_tmp_dir\".\n\nSolution: check if you can create the directory yourself.\n";


my %discinfo = ();


if ($flag_create_new_cddb)
{
	my $cddb_data;
	my $audio_cd = Audio::CD->init($config_device);
	end_program("Device initialization failed.","Make sure \"$config_device\" points to a valid Cdrom device.") unless $audio_cd;
	my $cddb = $audio_cd->cddb;
	my $cdinfo = $audio_cd->stat;
	%discinfo = parse_cddb_file(create_new_cddb_entry($cdinfo, $cddb)) or end_program("Could not parse newly created cddb entry", "Internal error, please contact the author.");
}
elsif (!defined($inputfile))
{
	my $cddb_data;
	my $audio_cd = Audio::CD->init($config_device);
	end_program("Device initialization failed.","Make sure \"$config_device\" points to a valid Cdrom device.") unless $audio_cd;
	my $cddb = $audio_cd->cddb;
	my $cdinfo = $audio_cd->stat;


	end_program("Could not read cd.", "Insert an audio cd into cdrom drive \"$config_device\".", "Use the '-f file.cddb' option.") unless $cdinfo->present;

	$cddb->verbose($flag_verbose);

	print STDERR "Disc-Cover: calling Audio-CD library...\n" if ($flag_verbose);
	$cddb_data = $cddb->lookup;
	print STDERR "Disc-Cover: ...back from call to Audio-CD library.\n" if ($flag_verbose);
	if (!$cddb_data->title)
	{
		if (!-e $config_cdserverrc)
		{
			end_program("Could not find a \"$config_cdserverrc\" file.","Use \'disc-cover -S\' to create a default server configuration.");
		}
		else
		{
			end_program("Could not fetch cdrom information.","Check if the connection to the internet works.", "If the file is not in the cddb database you can make a new file\nas follows: 'disc-cover -n -t cddb -o newfile.cddb'. Edit this file and\ninsert the titles and artist, then do: 'disc-cover -f newfile.cddb'.");
		}
	}
	print STDERR "Disc-Cover: Audio-CD library says success, let's continue\n" if $flag_verbose;

	$discinfo{artist} = $cddb_data->artist;
	$discinfo{artist} =~ s/
//g;
	$discinfo{title} = $cddb_data->title;
	$discinfo{title} =~ s/
//g;
	$discinfo{extended} = $cddb_data->extended;
	$discinfo{extended} =~ s/
//g;
	$discinfo{genre} = $cddb_data->genre;
	$discinfo{genre} =~ s/
//g;
	$discinfo{discid} = sprintf("%x", $cddb->discid);
	$discinfo{discid} =~ s/
//g;

	$discinfo{length_minutes} = ($cdinfo->length)[0];
	$discinfo{length_seconds} = ($cdinfo->length)[1];
	$discinfo{total_tracks} = $cdinfo->total_tracks;

	my @track_names; my @track_extended; my @track_artist;
	my $various_artist_warning = 0;
	#if ($flag_various_artist_cd)
	#{
	#	$cddb_data->parse_track_artist = 1;
	#}
	foreach my $track (@{$cddb_data->tracks($cdinfo)})
	{
		my $track_name = $track->name;
		if ($track->artist)
		{
			if ($flag_various_artist_cd)
			{
				push (@track_artist, $track->artist);
			}
			else
			{
				$track_name = $track->artist." / ".$track->name;
				$various_artist_warning++;
			}

		}
		push (@track_names, $track_name);
		push (@track_extended, $track->extended);
	}
	if ($various_artist_warning == $discinfo{total_tracks})
	{
		warn "Warning: all the tracks of this cd contain a slash ' / ' in their track titles,\nwhich could mean it is a various artist cd that contains the form\n'artist / title' for each track. Use the flag '-va' to make a nicer format.\n";
	}
	@track_names = map { s/
//g; $_ } @track_names;
	$discinfo{track_names} = \@track_names;
	@track_extended = map { s/
//g; $_ } @track_extended;
	$discinfo{track_extended} = \@track_extended;
	@track_artist = map { s/
//g; $_ } @track_artist;
	$discinfo{track_artist} = \@track_artist;
	
	my @track_length_minutes; my @track_length_seconds;
	foreach my $track (@{$cdinfo->tracks})
	{
		push (@track_length_minutes, ($track->length)[0]);
		push (@track_length_seconds, ($track->length)[1]);
	}
	$discinfo{track_length_minutes} = \@track_length_minutes;
	$discinfo{track_length_seconds} = \@track_length_seconds;

	print STDERR "Disc-Cover: filled in disc information\n" if $flag_verbose;
}
else
{
	print STDERR "Disc-Cover: you supplied me with the file \"$inputfile\"\n" if $flag_verbose;
	open (CDDB_FILE, "< $inputfile") or end_program("Could not open file \"$inputfile\"", "Check if this file exists and if it is readable.");
	my $cddb_contents = "";
	while (<CDDB_FILE>)
	{
		$cddb_contents = $cddb_contents.$_;
	}
	close CDDB_FILE;
	%discinfo = parse_cddb_file($cddb_contents) or end_program("Could not parse file \"$inputfile\".", "Check if this is a cddb file.");
	print STDERR "Disc-Cover: file sucessfully read as a CDDB entry\n" if $flag_verbose;
}

my %second_discinfo;
if ($config_second_inputfile)
{
	print STDERR "Disc-Cover: you supplied me with the second file \"$config_second_inputfile\"\n" if $flag_verbose;
	open (CDDB_FILE, "$config_second_inputfile") or end_program("Could not open file \"$config_second_inputfile\"", "Check if this file exists and if it is readable.");
	my $cddb_contents = "";
	while (<CDDB_FILE>)
	{
		$cddb_contents = $cddb_contents.$_;
	}
	close CDDB_FILE;
	%second_discinfo = parse_cddb_file($cddb_contents) or end_program("Could not parse file \"$config_second_inputfile\".", "Check if this is a cddb file.");
	uppercase_fix_on_discinfo(\%second_discinfo) if $flag_uppercase_fix;
	print STDERR "Disc-Cover: second file sucessfully read as a CDDB entry\n" if $flag_verbose;
	if ($flag_double_disc_cdtitle)
	{
		my $disctitle;
		my @discinfo_array1 = split '', $discinfo{title};
		my @discinfo_array2 = split '', $second_discinfo{title};
		my $counter = 0;
		my $smallest_array = $#discinfo_array1 < $#discinfo_array2 ? $#discinfo_array1 : $#discinfo_array2;
		while (($counter <= $smallest_array) and ($discinfo_array1[$counter] eq $discinfo_array2[$counter]))
		{
			$counter++;
		}
		#while ($discinfo_array1[$counter] ne " ")
		#{
		#	$counter--;
		#}
		$disctitle = join'', @discinfo_array1[0..$counter-1];
		$disctitle =~ s/disc//g; # This could lead to problems, e.g., 'Super disc'
		$disctitle =~ s/disk//g;
		$disctitle =~ s/Disc//g;
		$disctitle =~ s/Disk//g;
		$disctitle =~ s/DISC//g;
		$disctitle =~ s/DISK//g;
		$disctitle =~ s/(^[\. -]*)|([\. -]*$)//g;
		$discinfo{title} = $disctitle;
		print STDERR "Disc-Cover: double disc title becomes \"$disctitle\"\n" if $flag_verbose;
	}
}

($bottomtextfront) = $discinfo{extended} if (!defined($bottomtextfront));
{
	# Make sure that extended disc info fits front cover
	my (@test) = split("\n", $bottomtextfront);
	my $max_bottom_front_lines = 19;
	$max_bottom_front_lines = 4 if ($flag_picture_use_allmusic or $config_cover_picture_no_convert_needed or $config_cover_picture);
	if( $#test > $max_bottom_front_lines )
	{
	
		warn "Warning: had to trim the extended disc info from ".(1+$#test)." to $max_bottom_front_lines to make it fit the front cover.\n";
		splice (@test, $max_bottom_front_lines);
	}
	$bottomtextfront = join("\n", @test);
}
print STDERR "Disc-Cover: text on bottom of front cover: \"$bottomtextfront\"\n" if $flag_verbose;


$bottomtextfront .= "\\n genre: $discinfo{genre}" if ($flag_genre);

if (!defined($outputfile))
{
	$outputfile = $discinfo{artist}.' - '.$discinfo{title};
	$outputfile =~ s/ /_/g;
	$outputfile =~ s#(["`'])##g;
	$outputfile =~ s#/#:#g;
	chomp $outputfile;
}

if (defined($outputfile) and ($outputfile ne "-"))
{
	if($config_all_available_output_types =~ /\b$config_output_format\b/)
	{
		$outputfile =~ s/(\.$config_output_format)?$/.$config_output_format/;
	}
	if ($config_second_inputfile)
	{
		print STDERR "Output for cddb entries \"$discinfo{discid}\" and \"$second_discinfo{discid}\" goes to \"$outputfile\"\n";
	}
	else
	{
		print STDERR "Output for cddb entry \"$discinfo{discid}\" goes to \"$outputfile\"\n";
	}
}

if ($config_output_format =~ /lbl|txt|tex|html|cddb/)
{
	open OUT, ">$outputfile" or end_program("Could not open \"$outputfile\"", "Check if a file or directory with the same name already exists","Is the directory writable?");
	print STDERR "Disc-Cover: successfully opened file \"$outputfile\" for output\n" if $flag_verbose;
}


uppercase_fix_on_discinfo(\%discinfo) if $flag_uppercase_fix;

#######
# LBL #
#######
if ($config_output_format eq 'lbl')
{
	print STDERR "Disc-Cover: starting output of type lbl\n" if $flag_verbose;

	print OUT "cdlabelgen \\\n";
	if ($discinfo{artist} eq "")
	{
		printf OUT "-c \" \" \\\n";
	}
	else
	{
		printf OUT "-c \"%s\" \\\n", $discinfo{artist};
	}
	printf OUT "-s \"%s\" \\\n", $discinfo{title};
	print OUT "-S 0.96 -e penguin.eps \\\n";
	print OUT "-i \"";
	if ($config_second_inputfile) { print OUT "%Disc1%\\\n" };

	for (my $i = 0; $i < $discinfo{total_tracks}; $i++)
	{
		printf OUT "%2d. %s  %02d:%02d%%\\\n", ($i+1), $discinfo{track_names}[$i], $discinfo{track_length_minutes}[$i], $discinfo{track_length_seconds}[$i];
	}
	printf OUT "%%CD length: %02d:%02d\n", $discinfo{length_minutes}, $discinfo{length_seconds};

	if ($config_second_inputfile)
	{
		print OUT "%%Disc2%\\\n";
		for (my $i = 0; $i < $second_discinfo{total_tracks}; $i++)
		{
			printf OUT "%2d. %s  %02d:%02d%%\\\n", ($i+1), $second_discinfo{track_names}[$i], $second_discinfo{track_length_minutes}[$i], $second_discinfo{track_length_seconds}[$i];
		}
		printf OUT "%%CD length: %02d:%02d\n", $second_discinfo{length_minutes}, $second_discinfo{length_seconds};
	}

	print OUT "\"";

	print STDERR "Disc-Cover: finished output of type lbl\n" if $flag_verbose;
}
########
# HTML #
########
elsif ($config_output_format eq 'html')
{
	print STDERR "Disc-Cover: starting output of type html\n" if $flag_verbose;

	printf OUT "<html>\n<head>\n<title>%s : %s</title>\n</head>\n\n", $discinfo{artist}, $discinfo{title};
	printf OUT "<body bgcolor=\"#FFFFFF\" text=\"#882200\" link=\"#000000\" vlink=\"#555500\" alink=\"#FF0000\">\n\n";
	printf OUT "<p><br><br><br>\n\n";
	printf OUT "<table width=600 align=center border=0 cellpadding=1>\n";
	printf OUT "<tr>\n";
	printf OUT "<td align=center bgcolor=\"#DDC0C0\">%s</td>\n", $discinfo{artist};
	if ($bottomtextfront eq "")
	{
		printf OUT "<td align=center bgcolor=\"#DDC0C0\">%s</td>\n", $discinfo{title};
	}
	else
	{
		printf OUT "<td align=center bgcolor=\"#DDC0C0\">%s", $discinfo{title};
		printf OUT "&nbsp;-&nbsp;<i>%s</i></td>\n", $bottomtextfront;
	}
	printf OUT "</tr>\n";
	printf OUT "</table>\n\n";
	printf OUT "<p>\n\n";
	printf OUT "<table width=600 align=center border=0 cellpadding=1>\n";
	printf OUT "<tr><td>\&nbsp;</td></tr>\n<tr>\n<td colspan=4 align=left bgcolor=\"#DDC0C0\">Disc 1</td>\n</tr>\n" if ($config_second_inputfile);
	for (my $i = 0; $i < $discinfo{total_tracks}; $i++)
	{
		printf OUT "<tr>\n";
		printf OUT "<td align=left bgcolor=\"#FFFFFF\">%02d</td>\n", ($i+1);
		printf OUT "<td align=left bgcolor=\"#EEEEEE\">%s</td>\n", $discinfo{track_names}[$i];
		if (!$flag_various_artist_cd or $discinfo{track_artist}[$i] eq "")
		{
			printf OUT "<td align=left bgcolor=\"#FFEEEE\">&nbsp;</td>\n";
		}
		else
		{
			printf OUT "<td align=left bgcolor=\"#FFEEEE\">%s</td>\n", $discinfo{track_artist}[$i];
		}
		if ($discinfo{track_extended}[$i] eq "" or !$flag_with_extended_track_info)
		{
			printf OUT "<td align=left bgcolor=\"#EEEEFF\">&nbsp;</td>\n";
		}
		else
		{
			printf OUT "<td align=left bgcolor=\"#EEEEFF\">%s</td>\n", $discinfo{track_extended}[$i];
		}
		printf OUT "<td align=right bgcolor=\"#EEFFEE\">%02d:%02d</td>\n", $discinfo{track_length_minutes}[$i], $discinfo{track_length_seconds}[$i];
		printf OUT "</tr>\n";
	}
	printf OUT "<tr>\n<td colspan=5 align=right bgcolor=\"#DDC0C0\">\n";
	printf OUT "CD length: %02d:%02d\n", $discinfo{length_minutes}, $discinfo{length_seconds};
	printf OUT "</td>\n</tr>\n";
	if ($config_second_inputfile)
	{
		printf OUT "<tr><td>\&nbsp;</td></tr>\n<tr>\n<td colspan=4 align=left bgcolor=\"#DDC0C0\">Disc 2</td>\n</tr>\n";
		for (my $i = 0; $i < $second_discinfo{total_tracks}; $i++)
		{
			printf OUT "<tr>\n";
			printf OUT "<td align=left bgcolor=\"#FFFFFF\">%02d</td>\n", ($i+1);
			printf OUT "<td align=left bgcolor=\"#EEEEEE\">%s</td>\n", $second_discinfo{track_names}[$i];
			if (!$flag_various_artist_cd or $second_discinfo{track_artist}[$i] eq "")
			{
				printf OUT "<td align=left bgcolor=\"#FFEEEE\">&nbsp;</td>\n";
			}
			else
			{
				printf OUT "<td align=left bgcolor=\"#FFEEEE\">%s</td>\n", $second_discinfo{track_artist}[$i];
			}
			if ($second_discinfo{track_extended}[$i] eq "" or !$flag_with_extended_track_info)
			{
				printf OUT "<td align=left bgcolor=\"#EEEEFF\">&nbsp;</td>\n";
			}
			else
			{
				printf OUT "<td align=left bgcolor=\"#EEEEFF\">%s</td>\n", $second_discinfo{track_extended}[$i];
			}
			printf OUT "<td align=right bgcolor=\"#EEFFEE\">%02d:%02d</td>\n", $second_discinfo{track_length_minutes}[$i], $second_discinfo{track_length_seconds}[$i];
			printf OUT "</tr>\n";
		}
		printf OUT "<tr>\n<td colspan=5 align=right bgcolor=\"#DDC0C0\">\n";
		printf OUT "CD length: %02d:%02d\n", $second_discinfo{length_minutes}, $second_discinfo{length_seconds};
		printf OUT "</td>\n</tr>\n";
	}
	printf OUT "</table>\n";
	printf OUT "</body>\n</html>";

	print STDERR "Disc-Cover: finished output of type html\n" if $flag_verbose;
}
#######
# TXT #
#######
elsif ($config_output_format eq 'txt')
{
	print STDERR "Disc-Cover: starting output of type txt\n" if $flag_verbose;

	printf OUT "Artist: %s\n", $discinfo{artist};
	printf OUT "Title:  %s\n", $discinfo{title};

	print OUT "Disc 1\n" if ($config_second_inputfile);
	for (my $i = 0; $i < $discinfo{total_tracks}; $i++)
	{
		my $textended = ""; my $tartist = "";
		if ($flag_with_extended_track_info and $discinfo{track_extended}[$i] ne "")
		{
			$textended = " - ".$discinfo{track_extended}[$i];
		}
		if ($flag_various_artist_cd)
		{
			$tartist = $discinfo{track_artist}[$i]." / ";
		}
		printf OUT "%02d %s%s%s %02d:%02d\n", ($i+1), $tartist, $discinfo{track_names}[$i], $textended, $discinfo{track_length_minutes}[$i], $discinfo{track_length_seconds}[$i];
	}
	printf OUT "CD length: %02d:%02d\n", $discinfo{length_minutes}, $discinfo{length_seconds};

	if ($config_second_inputfile)
	{
		print OUT "Disc 2\n";
		for (my $i = 0; $i < $second_discinfo{total_tracks}; $i++)
		{
			if ($second_discinfo{track_extended}[$i] eq "" or !$flag_with_extended_track_info)
			{
				printf OUT "%02d %s %02d:%02d\n", ($i+1), $second_discinfo{track_names}[$i], $second_discinfo{track_length_minutes}[$i], $discinfo{track_length_seconds}[$i];
			}
			else
			{
				printf OUT "%02d %s %s %02d:%02d\n", ($i+1), $second_discinfo{track_names}[$i], $second_discinfo{track_extended}[$i], $second_discinfo{track_length_minutes}[$i], $second_discinfo{track_length_seconds}[$i];
			}
		}
		printf OUT "CD length: %02d:%02d\n", $second_discinfo{length_minutes}, $second_discinfo{length_seconds};
	}

	print STDERR "Disc-Cover: finished output of type txt\n" if $flag_verbose;
}
########
# CDDB #
########
elsif ($config_output_format eq 'cddb')
{
	print STDERR "Disc-Cover: starting output of type cddb\n" if $flag_verbose;

	my $cddb_cache_file = undef;
	end_program("CDDB type not supported with two cds", "do not use \'-t cddb\' and \'-2\' at the same time.\n") if ($config_second_inputfile);

	print OUT create_existing_cddb_entry(\%discinfo);

	# OBSOLETE CODE (FINALLY): made redundant because we have a function to create our own cddb
	#end_program("standard in to standard out with CDDB entries not possible", "do not use \'-f -\' and \'-o -\' together with \'-f cddb\'") if ($inputfile eq "-");
	#if (defined($inputfile))
	#{
	#	$cddb_cache_file = $inputfile;
	#}
	#else
	#{
	#	my (@subdirs, $sub);
	#	if (-d $config_cddb_cache_directory)
	#	{
	#		opendir(DIR, $config_cddb_cache_directory) or end_program("Could not open $config_cddb_cache_directory for reading.", "Check the permissions and the contents of the directory $config_cddb_cache_directory.");
	#		@subdirs = readdir(DIR);
	#		closedir(DIR); 
	#	}
	#	else
	#	{
	#		end_program("Could not find $config_cddb_cache_directory.","Check if this directory contains cddb entries.","If the directory is somewhere else, specify it in your configuration file or in the system's configuration file.");
	#	}
	#
	#	FILES:
	#	{
	#		foreach $sub (@subdirs)
	#		{
	#			if ($sub !~ /^\.\./)
	#			{
	#				my $try_file = "$config_cddb_cache_directory/$sub/$discinfo{discid}";
	#				if (-e $try_file)
	#				{
	#					$cddb_cache_file = $try_file;
	#					last FILES;
	#				}
	#			}
	#		}
	#	}
	#}
	#
	#end_program("Could not find a cddb file entry in the cache directory \'$config_cddb_cache_directory\".", "Are there any entries in the directory?") unless defined $cddb_cache_file;
	#
	#open IN, "<$cddb_cache_file" or end_program("Could not read file \"$cddb_cache_file\"");
	#while (<IN>)
	#{
	#	print OUT $_;
	#}
	#close IN;

	print STDERR "Disc-Cover: finished output of type cddb\n" if $flag_verbose;
}
#######
# TEX #
#######
elsif ($config_output_format eq 'tex')
{
	print STDERR "Disc-Cover: starting output of type tex\n" if $flag_verbose;

	print OUT print_whole_cover();

	print STDERR "Disc-Cover: finished output of type tex\n" if $flag_verbose;
}
#######
# PDF #
#######
elsif ($config_output_format eq 'pdf')
{
	print STDERR "Disc-Cover: starting output of type pdf\n" if $flag_verbose;

	run_latex("pdflatex");
	$flag_move_end_file = 1;

	print STDERR "If you use Acrobat Reader to print this file, make sure the option 'Fit to Page' is switched off.\n";

	print STDERR "Disc-Cover: finished output of type pdf\n" if $flag_verbose;
}
#######
# DVI #
#######
elsif ($config_output_format eq 'dvi')
{
	print STDERR "Disc-Cover: starting output of type dvi\n" if $flag_verbose;

	run_latex("latex");
	$flag_move_end_file = 1;

	print STDERR "Disc-Cover: finished output of type dvi\n" if $flag_verbose;
}
######
# PS #
######
elsif ($config_output_format eq 'ps')
{
	print STDERR "Disc-Cover: starting output of type ps\n" if $flag_verbose;

	run_latex("latex");


	print STDERR "Disc-Cover: starting external dvi run\n" if $flag_verbose;
	my $dvi_string = $flag_verbose ? "(cd $config_tmp_dir ; dvips \"$config_tmp_dir/disc-cover.dvi\" -o \"$config_tmp_dir/disc-cover.ps\")" : "(cd $config_tmp_dir ; dvips \"$config_tmp_dir/disc-cover.dvi\" -o \"$config_tmp_dir/disc-cover.ps\" 2> /dev/null)";
	if (system ($dvi_string) > 0)
	{
		end_program("Something went wrong while running dvips.", "Run disc-cover with argument '-r' as argument, then check if a .dvi\nfile is produced in \"$config_tmp_dir\".");
	}
	$flag_move_end_file = 1;
	print STDERR "Disc-Cover: finished external dvi run\n" if $flag_verbose;

	print STDERR "Disc-Cover: finished output of type ps\n" if $flag_verbose;
}
################################
# error: no valid format given #
################################
else
{
	end_program("The type of format \"".$config_output_format."\" does not exist.", "Use \"-t <option>\" where <option> is one of:\n$config_all_available_output_types.");
}

if ($flag_move_end_file == 1)
{
	if (defined($outputfile) and $outputfile eq "-")
	{
		open OUT,"$config_tmp_dir/disc-cover.$config_output_format" or end_program("Could not open the file \"$config_tmp_dir/disc-cover.$config_output_format\".", "Run disc-cover with argument '-r', then\ncheck if a .$config_output_format file is produced in \"$config_tmp_dir\".");
		while (<OUT>)
		{
			print;
		};
		close OUT;
		unlink "$config_tmp_dir/disc-cover.$config_output_format";
	}
	else
	{
		system("mv", "--", "$config_tmp_dir/disc-cover.$config_output_format", "$outputfile") and end_program("Could not move the file \"$config_tmp_dir/disc-cover.$config_output_format\"\nto \"$outputfile\".", "Check if the directory is writable.", "Run disc-cover with argument '-r', then\ncheck if a .$config_output_format file is produced in \"$config_tmp_dir\".");
	}
}
else
{
	close OUT;
}
end_program();


#######################
# Clean exit function #
#######################


# DESC: primary function is to remove temporary files
#	secondary funtion is to report errors and give solutions
# NOTE: no args means clean exit (no errors reported)
# ARGS: 1 error string
#	2- solutions to error
sub end_program
{
	my ($error) = defined($_[0]) ? $_[0] : undef ;
	shift;
	my (@solutions) = defined(@_) ? @_ : undef;

	warn "Error: $error\n" if defined ($error);

	if (defined($solutions[0]))
	{
		if ($#solutions == 0)
		{
			warn "\nSolution: $solutions[0]\n" if ($solutions[0] ne "");
		}
		else
		{
			my ($j) = 1;
			foreach my $solution (@solutions)
			{
				warn "\nSolution $j: $solution\n" if ($solution ne "");
				$j++;
			}
		}
	}

	if ($flag_remove_temporary_files == 0)
	{
		print STDERR "Disc-Cover: cleaning up tmp dir ($config_tmp_dir)\n" if $flag_verbose;
		my ($ext);
		foreach $ext ('.ps', '.log', '.dvi', '.aux', '.tex', '_front-picture.eps', '_front-picture.pdf', '_allmusic.jpg')
		{
			unlink "$config_tmp_dir/disc-cover$ext";
		}
		if (defined($config_cover_picture_no_convert_needed))
		{
			my $ugly_tmp = "$config_tmp_dir/".safe_exec('basename', "$config_cover_picture_no_convert_needed");
			chop $ugly_tmp;
			unlink "$ugly_tmp" or print STDERR "Disc-Cover: error, could not remove temporary file $ugly_tmp\n";
		}
		rmdir $config_tmp_dir or print STDERR "Disc-Cover: error, could not remove $config_tmp_dir\n";
	}
	exit;
}


sub uppercase_fix_on_discinfo
{
	my $discinfo = shift;

	$discinfo->{artist} = uppercase_fix($discinfo->{artist});
	$discinfo->{title} = uppercase_fix($discinfo->{title});

	my @track_names = map { uppercase_fix($_) } @{ $discinfo->{track_names} };
	$discinfo->{track_names} = \@track_names;

	my @track_artists = map { uppercase_fix($_) } @{ $discinfo->{track_artists} };
	$discinfo->{track_artists} = \@track_artists;
}

sub uppercase_fix
{
	my $i = shift || return;

	$i = lc $i;
	$i =~ s/([ \(\[{])([a-z])/$1\u$2/g;
	$i =~ s/(^[a-z])/\u$1/g;

	return $i;
}

#################
# LaTeX filters #
#################

sub filter_input_for_latex_multiple_lines
{
# Changes allmost all the naughty characters in a string returning a string
# safely parsed by LaTeX.

	my ($return) = $_[0];
	return "" unless defined $return;

	# recode $ as $$$
	$return =~ s/\$/\$\$\$/mg;
	# recode \ as $\backslash$
	$return =~ s/\\\\/\$\\backslash\$/mg;
	#FIXME recode \t (tabs) to spaces (should be LaTeX tabs again)
	$return =~ s/\\t/ /g;
	# recode \n as \\\\
	$return =~ s/\n/\\n/mg;
	$return =~ s/\\n/\\\\/mg;
	# now recode $$ as \$
	$return =~ s/\$\$\$/\\\$/mg;
	# recode every x in _#%^&{} as \x{}
	$return =~ s/([_#%&\^~{}])/\\$1\{\}/mg;
	# recode every x in <>| as $x$
	$return =~ s/([<>\|])/\$$1\$/mg;
	# protect square brackets
	$return =~ s/[\[\]]/\\protect$&/g;

	# remove any \\ in front of the first line
	$return =~ s/^\\+//g;

	# special characters
	$return =~ s/°/\${}^{\\circ}\$/mg;
	$return =~ s/µ/\$\\mu\$/mg;
	$return =~ s/ª/\${}^{\\textrm{a}}\$/mg;
	$return =~ s/º/\${}^{\\textrm{o}}\$/mg;
	$return =~ s/«/\$\\ll\$/mg;
	$return =~ s/»/\$\\gg\$/mg;
	$return =~ s/¬/\$\\neg\$/mg;
	$return =~ s/­/--/mg;
	$return =~ s/¯/\$\\bar{ }\$/mg;

	#FIXME Needs AMS: $return =~ s/¥/\$\\yen\$/mg;
	#FIXME Needs AMS: $return =~ s/®/\$\\circledR\$/mg;
	#FIXME ?: $return =~ s/¢/\$ \$/mg;
	#FIXME ?: $return =~ s/¤/\$ \$/mg;
	#FIXME ?: $return =~ s/¦/\$ \$/mg;

	return ($return);
}

sub filter_input_for_latex_one_line
{
# Changes all the naughty characters in a string returning a string
# safely parsed by LaTeX.

	my ($return) = $_[0];

	# only read _one_ line!
	($return) = split ("\n", $return);
	return "" unless defined $return;
	$return =~ s/(.*?)\\n.*/$1/mg;
	return "" unless defined $return;

	# recode $ as $$$
	$return =~ s/\$/\$\$\$/mg;
	# recode \ as $\backslash$
	$return =~ s/\\/\$\\backslash\$/mg;
	# recode \t (tabs) to spaces (we only have one line!)
	$return =~ s/\\t/ /g;

	# now recode $$ as \$
	$return =~ s/\$\$\$/\\\$/mg;
	# recode every x in _#%^&{} as \x{}
	$return =~ s/([_#%&\^~{}])/\\$1\{\}/mg;
	# recode every x in <>| as $x$
	$return =~ s/([<>\|])/\$$1\$/mg;
	# protect square brackets
	$return =~ s/[\[\]]/\\protect$&/g;

	# special characters
	$return =~ s/°/\${}^{\\circ}\$/mg;
	$return =~ s/µ/\$\\mu\$/mg;
	$return =~ s/ª/\${}^{\\textrm{a}}\$/mg;
	$return =~ s/º/\${}^{\\textrm{o}}\$/mg;
	$return =~ s/«/\$\\ll\$/mg;
	$return =~ s/»/\$\\gg\$/mg;
	$return =~ s/¬/\$\\neg\$/mg;
	$return =~ s/­/--/mg;
	$return =~ s/¯/\$\\bar{ }\$/mg;

	#FIXME Needs AMS: $return =~ s/¥/\$\\yen\$/mg;
	#FIXME Needs AMS: $return =~ s/®/\$\\circledR\$/mg;
	#FIXME ?: $return =~ s/¢/\$ \$/mg;
	#FIXME ?: $return =~ s/¤/\$ \$/mg;
	#FIXME ?: $return =~ s/¦/\$ \$/mg;

	return ($return);
}

#####################################################
# Start of all the print functions for LaTeX output #
#####################################################

sub print_cd_header_info
# Prints latex code that declares the commands \DCartist, \DCtitle and \DCdiscinfo.
{
	print STDERR "Disc-Cover: creating latex header\n" if ($flag_verbose);
	my ($return) = '
%%%%%%%%%%%%%%%%%%
% CD HEADER INFO %
%%%%%%%%%%%%%%%%%%
';

	$return .= '\newcommand{\DCtitle}{\color{'.$config_latex_colors{title}.'}'.filter_input_for_latex_one_line($discinfo{title}).'}'."\n";

	$return .= '\newcommand{\DCartist}{\color{'.$config_latex_colors{artist}.'}'.filter_input_for_latex_one_line($discinfo{artist}).'}'."\n";

	$return .= '\newcommand{\DCdiscinfo}{\color{'.$config_latex_colors{discinfo}.'}'.filter_input_for_latex_multiple_lines($bottomtextfront).'}'."\n";

	$return .= '\newcommand{\DCgenre}{\color{'.$config_latex_colors{genre}.'}'.filter_input_for_latex_multiple_lines($bottomtextfront).'}'."\n";

	$return .= '\newcommand{\DCflaptext}{\color{'.$config_latex_colors{flaptext}.'}'.filter_input_for_latex_one_line($config_flaptext).'}'."\n";

	$return .= '\newcommand{\DCpicture}{'.print_picture().'}'."\n";

	# 114mm for width of cases, hopefully a good guess...
	$return .= '\newlength{\Test}
\settowidth{\Test}{\emph{\DCartist}\hfill~{\DCtitle}}
\ifthenelse{\lengthtest{\Test > 114mm}}
{
	\settowidth{\Test}{\small \emph{\DCartist}\hfill~{\DCtitle}}
	\ifthenelse{\lengthtest{\Test > 114mm}}
	{
		\settowidth{\Test}{\footnotesize \emph{\DCartist}\hfill~{\DCtitle}}
		\ifthenelse{\lengthtest{\Test > 114mm}}
		{
			\settowidth{\Test}{\scriptsize \emph{\DCartist}\hfill~{\DCtitle}}
			\ifthenelse{\lengthtest{\Test > 114mm}}
			{
				\newcommand{\DCsidetext}{\parbox{114mm}{\tiny \emph{\DCartist}\hfill~{\DCtitle}}}
			}
			{
				\newcommand{\DCsidetext}{\parbox{114mm}{\scriptsize \emph{\DCartist}\hfill~{\DCtitle}}}
			}
		}
		{
			\newcommand{\DCsidetext}{\parbox{114mm}{\footnotesize \emph{\DCartist}\hfill~{\DCtitle}}}
		}
	}
	{
		\newcommand{\DCsidetext}{\parbox{114mm}{\small \emph{\DCartist}\hfill~{\DCtitle}}}
	}
}
{
	\newcommand{\DCsidetext}{\parbox{114mm}{\emph{\DCartist}\hfill~{\DCtitle}}}
}';

	$return .= '\newcommand{\DCformattedhead}[1]
{
\noindent
\begin{center}
{
\parbox[t][1.2\totalheight][t]{#1}{\raggedright{\Large\DCtitle}}

\rule{#1}{1pt}

\noindent
\parbox{#1}{\vspace*{0pt}\raggedleft{\hfill\large\emph{\DCartist}}}
}
\end{center}
}';

	print STDERR "Disc-Cover: finished latex header\n" if ($flag_verbose);
	return $return;
}

sub print_cd_info
{
	print STDERR "Disc-Cover: creating latex cd contents\n" if ($flag_verbose);
	my ($return) = "%%%%%%%%%%%%%%%\n% CD CONTENTS %\n%%%%%%%%%%%%%%%\n";
	$return .= '\newcommand{\DCtracks}'."\n";
	$return .= '{'."\n";

	my @lines;
	my $lines_counter; # because one line can have both title and extt
	
	if ($config_second_inputfile)
	{
		push (@lines, "\\parbox[t][1.2\\totalheight]{0.97\\linewidth}{\\vspace*{0em}{\\bfseries\\sffamily\\color{$config_latex_colors{track_number}}Disc~1}}\\par");
		$lines_counter++;
	}

	for (my $i = 0; $i < $discinfo{total_tracks}; $i++)
	{
		my ($line, $number_of_lines) = print_one_track_line(\%discinfo, $i);
		push (@lines, $line);
		$lines_counter += $number_of_lines;
	}
	push(@lines, print_total_time($discinfo{length_minutes}, $discinfo{length_seconds}));


	if ($config_second_inputfile)
	{
		push (@lines, "\\parbox[t][1.2\\totalheight]{0.97\\linewidth}{\\vspace*{0em}{\\bfseries\\sffamily\\color{$config_latex_colors{track_number}}Disc~2}}\\\\");
		$lines_counter++;
		for (my $i = 0; $i < $second_discinfo{total_tracks}; $i++)
		{
			my ($line, $number_of_lines) = print_one_track_line(\%second_discinfo, $i);
			push (@lines, $line);
			$lines_counter += $number_of_lines;
		}
		push(@lines, print_total_time($second_discinfo{length_minutes}, $second_discinfo{length_seconds}));
	}

	#FIXME 20 is still just a guess, but rescaling just a few lines would screw everything up.
	($lines_counter > 19) and $return .= '\resizebox*{\linewidth}{9.5cm}{';
	
	$return .= '\begin{minipage}{\linewidth}';

	# 26 is enough to make sure that the max tracks (99) is scaled neatly into 3 cols,
	# could be dreadful when -e is used (198 lines?). But hey, that's what you get for
	# cramming to much info into one jewel case.
	(int($lines_counter/26) > 1) and $return .= "\\begin{multicols}{".int($lines_counter/26)."}\n";

	$return .= '\color{'.$config_latex_colors{track_string}."}\\center\n";

	foreach my $line (@lines) 
	{
		$return .= $line;
	}
	
	(int($lines_counter/26) > 1) and $return .= '\end{multicols}'."\n";
	
	$return .= '\end{minipage}';
	
	($lines_counter > 19) and $return .= '}';

	$return .= '}'."\n\n";

	print STDERR "Disc-Cover: finished latex cd contents\n" if ($flag_verbose);
	return ($return);
}


sub print_total_time
{
	# Cumbersome way of printing the total time, but it makes sure it
	# uses the same space as normal lines.
	my $minutes = shift;
	my $seconds = shift;
	my $line = sprintf("\\parbox[t][1.2\\totalheight]{0.97\\linewidth}{
				\\vspace*{0em}
			\\parbox[t]{\\linewidth}
			{
				\\vspace*{0em}
				\\addtolength{\\linewidth}{-1.7em}%%
				\\parbox[t]{1.7em}
				{
					\\vspace*{0em}
					~
				}%%
				\\parbox[t]{\\linewidth}
				{
					\\vspace*{0pt}
					\\addtolength{\\linewidth}{-2.3em}%%
					\\parbox[b]{\\linewidth}
					{
						\\vspace*{0pt}
						\\sffamily\\raggedright~\\mbox{}
					}%%
					\\parbox[b]{2.3em}
					{
						\\vspace*{0pt}
						\\color{$config_latex_colors{track_time}}\\sffamily%02i:%02i
					}
				}
			}\n",
			$minutes, $seconds);
		$line .= "}\\par\n";
	return $line;
}


sub print_one_track_line
{
	# The [-1pt] in the track's name-parbox is there to eliminate additional vspace in case
	# of foreign capital characters. Removed again as it makes lines overlap!
	my %disc_info = %{$_[0]};
	my $i = $_[1];

	my $lines_counter = 1;

		my $line = sprintf (
			"\\parbox[t][1.2\\totalheight]{0.97\\linewidth}{
				\\vspace*{0em}
			\\parbox[t]{\\linewidth}
			{
				\\vspace*{0em}
				\\addtolength{\\linewidth}{-1.7em}%%
				\\parbox[t]{1.7em}
				{
					\\vspace*{0em}
					\\makebox[\\linewidth][r]{\\bfseries\\sffamily\\color{$config_latex_colors{track_number}} %2i\.~}
				}%%
				\\parbox[t]{\\linewidth}
				{
					\\vspace*{0pt}
					\\addtolength{\\linewidth}{-2.3em}%%
					\\parbox[b]{\\linewidth}
					{
						\\vspace*{0pt}
						\\sffamily\\raggedright %s~\\dotfill\\mbox{}
					}%%
					\\parbox[b]{2.3em}
					{
						\\vspace*{0pt}
						\\color{$config_latex_colors{track_time}}\\sffamily%02i:%02i
					}
				}
			}\n",
			($i+1),
			filter_input_for_latex_one_line($disc_info{track_names}[$i]),
			$disc_info{track_length_minutes}[$i],
			$disc_info{track_length_seconds}[$i]);

		if ($flag_various_artist_cd and ($disc_info{track_artist}[$i] ne ""))
		{
			$lines_counter++;
			$line .= sprintf(
			"\\parbox[t]{1.7em}{\\vspace*{0em}~}%%
			 \\addtolength{\\linewidth}{-1.7em}%%
			 \\parbox[t]{\\linewidth}
			 {
				\\vspace*{0pt}
				\\footnotesize\\em\\color{$config_latex_colors{track_artist}}\\raggedright %s\\hfill
				\\vspace*{-2pt}
			 }\n",
			 filter_input_for_latex_one_line($disc_info{track_artist}[$i]));
		}
		if ($flag_with_extended_track_info and ($disc_info{track_extended}[$i] ne ""))
		{
			$lines_counter++;
			$line .= sprintf(
			"\\parbox[t]{1.7em}{\\vspace*{0em}~}%%
			 \\addtolength{\\linewidth}{-1.7em}%%
			 \\parbox[t]{\\linewidth}
			 {
				\\vspace*{0pt}
				\\footnotesize\\em\\color{$config_latex_colors{track_extended}}\\raggedright %s\\hfill
				\\vspace*{-2pt}
			 }\n",
			 filter_input_for_latex_one_line($disc_info{track_extended}[$i]));
		}

		$line .= "}\\par\n";

	return ($line, $lines_counter);
}


############################
# Help and version dialogs #
############################

sub print_version_info
{
	return '
Disc-Cover version '.$version.' Copyright © 1999-2005 J.I. van Hemert
This piece of software comes with ABSOLUTELY NO WARRANTY
This is free software, and you are welcome to redistribute it
under certain conditions, see the file COPYING for details.
For more information read the documentation that comes with the
distribution.
';
}


sub print_manual
{
	#system("pod2man --release=\"J.I. van Hemert\" --center=\"Version $version\" $0 | nroff -man");
	system("pod2text $0");
	return 1;
}

sub print_help
{
	return print_version_info().'
Program switches (short style), capital options are not for
daily use, see -H for the whole manual
 -2 <filename>   cddb file to use for second cd of double album
 -a "<text>"     additional text at the bottom of the front cover
 -allmusic       search on www.allmusic.com for a front cover
 -b "<text>"     text on the additional flap at the back cover
 -c <filename>   type of case (template to use)
 -C <filename>   output configuration file with current options
 -D <device>     scan the cd in this device
 -e              enable extended track info
 -f <filename>   use this cddb file as input instead of a cddb connection 
 -g              include genre as last line in front bottom text
 -h              this help
 -H              more help, shows the manual page
 -n              create a new cddb entry (if it does not exist in the database)
 -o <filename>   output to this filename (extension is added accordingly)
 -p <filename>   picture (almost any format) to be put on the front cover
 -pic <filename> picture on the front cover in same format as ouput format
 -R              do not remove temporary files from '.$config_tmp_dir.' (for debugging)
 -S              write a .cdserverrc file in your homedir
 -t <type>       output format, choose: '.$config_all_available_output_types.'
 -u              fix entries that are all in lowercase
 -v              version and program information
 -V              enable verbose output (for debugging)
 -va             enable various artist output
 -template_list  prints a list of outpout template types and their descriptions
';
}


#########################
# LaTeX print functions #
#########################

sub print_latex_head
{
	my ($return) = '\documentclass[]{article}';
	if ($config_output_format eq 'pdf')
	{
		$return = '\documentclass[pdftex]{article}';
	}

	$return .= '
\usepackage{'.$latex_global_packages.','.$latex_user_packages.'}
\usepackage['.$latex_language_encoding.']{inputenc}
\graphicspath{{'.$config_tmp_dir.'/}}
\renewcommand{\thepage}{}
\setlength{\oddsidemargin}{'.$latex_global_oddsidemargin.'}
\setlength{\evensidemargin}{'.$latex_global_evenside_margin.'}
\setlength{\voffset}{'.$latex_global_voffset.'}
\setlength{\footskip}{'.$latex_global_footskip.'}
\setlength{\textheight}{'.$latex_global_textheight.'}
\setlength{\textwidth}{'.$latex_global_textwidth.'}
\hyphenpenalty=10000
\renewcommand{\dotfill}{\leaders\hbox to 2mm{\hfil.\hfil}\hfill}

%\newcommand{\mydate}{\the\year/\the\month/\the\day}

';

	return ($return);
}

sub print_picture
{
	# Search for a cover picture on AllMusic.com and use it
	if ($flag_picture_use_allmusic)
	{
		print STDERR "Disc-Cover (AllMusic): searching on AllMusic.com for a cover picture\n" if $flag_verbose;
		my $web_search = $discinfo{title};
		$web_search =~ s/ /+/g;
		$web_search =~ s/[tT]he//g;
		my $result = safe_exec('wget', "-O-", "-o/dev/null", "http://www.allmusic.com/cg/amg.dll?P=amg&opt1=2&sql=$web_search");

		if ($result =~ /albumsearchresults/)
		{
			my $artist = $discinfo{artist};
			$artist =~ s/^[tT]he //g; # AllMusic strips 'The/La/..' from artists...
			$artist =~ s/^[Ll]a //g; 
			print STDERR "Disc-Cover (AllMusic): more than one album found\n" if $flag_verbose;

			#my @entries = ($result =~ /<td class="cell" style="width:172px;word-wrap:break-word;"><a href="\/cg\/amg.dll\?p=amg&sql=([^"]*)">/mig);
			my @entries = ($result =~ /$artist<\/TD><td class="cell-img"><a href="[^"]*"><img src="[^"]*" border="0" alt="Listen Now!" title="Listen Now!" style="padding-top:1px;padding-right:2px;" \/><\/a><\/TD><td class="cell" style="width:172px;word-wrap:break-word;"><a href="\/cg\/amg.dll\?p=amg&sql=([^"]*)">/mig);
			print STDERR "Disc-Cover (AllMusic): using artist to determine correct album, this leaves ".(1+$#entries)." album(s)\n" if $flag_verbose;
			print STDERR join(' / ',@entries)."\n";
			if (defined $entries[0])
			{
				print STDERR "Disc-Cover (AllMusic): downloading AllMusic info page of the first album (key: $entries[0])\n" if $flag_verbose;
				$result = safe_exec('wget', "-O", "-",  "-o", "/dev/null", "http://www.allmusic.com/cg/amg.dll?P=amg&sql=$entries[0]");
			}
		}


		my @entries = ($result =~ /src="(http:\/\/image\.allmusic\.com\/00\/amg\/cov[^"]*)"/mg);

		#print STDERR join(' / ',@entries)."\n";

		if (defined $entries[0])
		{
			print STDERR "Disc-Cover (AllMusic): found a cover picture on AllMusic.com: $entries[0], downloading...\n" if $flag_verbose;
			system('wget',  '-O', "$config_tmp_dir/disc-cover_allmusic.jpg", '-o', '/dev/null', "$entries[0]");

			$config_cover_picture = "$config_tmp_dir/disc-cover_allmusic.jpg";
			print STDERR "Disc-Cover (AllMusic): successfully finished AllMusic procedure.\n" if $flag_verbose;
		}
		else
		{
			print STDERR "Disc-Cover (AllMusic): could not find a cover picture on AllMusic.com\n" if $flag_verbose;
		}
		
		print STDERR "Disc-Cover (AllMusic): finished searching on AllMusic.com for a cover picture\n" if $flag_verbose;
	}

	my ($picture) = ""; # Or 'no picture'...
	if (defined($config_cover_picture_no_convert_needed))
	{
		# FIXME
		# Difficult situation: when using tex we want the picture filename in tact,
		# in case the user wants to run latex. When using pdf or ps we want to use
		# it from the $tmp dir. For dvi we want both! (user does dvips or xdvi).
		# Solution for now: just copy it to the $tmp dir and warn the user.
		end_program("Something went wrong trying to copy \"$config_cover_picture_no_convert_needed\" to \"$config_tmp_dir/disc-cover_front-picture.pdf\".", "Check if the file is readable for you.") if (system("cp", "--", "$config_cover_picture_no_convert_needed", "$config_tmp_dir"));
		warn "Note: I copied the file \"$config_cover_picture_no_convert_needed\" to \"$config_tmp_dir\" and used it. If you save in dvi or latex please add this file to the same directory.\n";
		#$picture = "\\parbox{0.9\\linewidth}{\\raggedleft\\framebox{\\includegraphics[height=0.6\\linewidth]{$config_cover_picture_no_convert_needed}}}\\vspace*{\\fill}\\\\";
		$picture = "\\includegraphics[height=0.6\\linewidth]{$config_cover_picture_no_convert_needed}\\vspace*{\\fill}\\\\";
	}
	elsif (defined($config_cover_picture))
	{
		if (-f "$config_cover_picture")
		{
			if ($config_output_format eq 'pdf')
			{
				if ($flag_verbose)
				{
					print STDERR "Disc-Cover: starting external convert (to pdf) run\n";
					end_program("Failed converting the cover picture \"$config_cover_picture\" to PDF.", "Check if the file is readable for you.") if (system ("convert -verbose -size 200x200 \"$config_cover_picture\" EPDF:$config_tmp_dir/disc-cover_front-picture.pdf 2>&1"));
					print STDERR "Disc-Cover: finished external convert (to pdf) run\n";
				}
				else
				{
					end_program("Failed converting the cover picture \"$config_cover_picture\" to PDF.", "Check if the file is readable for you.") if (system ("convert -size 200x200 \"$config_cover_picture\" EPDF:$config_tmp_dir/disc-cover_front-picture.pdf 2> /dev/null"));
				}
				
			}
			else
			{
				if ($flag_verbose)
				{
					print STDERR "Disc-Cover: starting external convert (to eps) run\n";
					end_program("Failed converting the cover picture \"$config_cover_picture\" to EPS.", "Check if the program convert (comes with ImageMagick) is installed.", "Check if you can convert the cover picture to EPS yourself.") if (system ("convert -verbose -geometry 200x200 \"$config_cover_picture\" EPS:$config_tmp_dir/disc-cover_front-picture.eps 2>&1"));
					print STDERR "Disc-Cover: finished external convert (to eps) run\n";
				}
				else
				{
					end_program("Failed converting the cover picture \"$config_cover_picture\" to EPS.", "Check if the program convert (comes with ImageMagick) is installed.", "Check if you can convert the cover picture to EPS yourself.") if (system ("convert -geometry 200x200 \"$config_cover_picture\" EPS:$config_tmp_dir/disc-cover_front-picture.eps 2> /dev/null"));
				}
				### TODO: THIS DOES NOT WORK YET!
				#my ($magick) = new Image::Magick;
				#$magick->Read($config_cover_picture);
				#$magick->SetAttributes(sample=>"200x200");
				#$magick->Display();
				#$magick->Write("EPSI:$config_tmp_dir/disc-cover_front-picture.eps");
				###

			}
			#$picture = "\\parbox{0.9\\linewidth}{\\raggedleft\\framebox{\\includegraphics[height=0.6\\linewidth]{disc-cover_front-picture}}}\\vspace*{\\fill}\\\\";
			$picture = "\\includegraphics[height=0.6\\linewidth]{disc-cover_front-picture}";
		}
		else
		{
			end_program("Failed to open cover picture \"$config_cover_picture\".", "Check if the file exists and if it is readable.");
		}
	}
	return $picture;
}



sub print_disccoverheader
{
	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
	my $datetime = sprintf "%4d-%02d-%02dT%02d:%02d", 1900+$year,$mon+1,$mday,$hour,$min;

	my $header;
	if ($config_second_inputfile)
	{
		$header = '\parbox[c]{\linewidth}{\small Printed entries \texttt{'.$discinfo{discid}.'} and \texttt{'.$second_discinfo{discid}.'}';
	}
	else
	{
		$header = '{\small Printed entry \texttt{'.$discinfo{discid}.'}';
	}

	$header .= ' with \emph{Disc-Cover} version '.$version.' on '.$datetime.' using template \textsf{'.$config_casetype.'}.  For suggestions mail the author at \texttt{jvhemert@cwi.nl}. For the latest version check \texttt{http://www.cwi.nl/\~{}jvhemert/disc-cover.html}}';
	
	return $header;
}



sub print_whole_cover
{
	my $latex_body = "\\begin{document}\\begin{center}\n";
	
	my $no_template_found = 1;

	if (-f $config_casetype)
	{
		print STDERR "Disc-Cover: opening and using \"$config_casetype\" as a template file.\n" if $flag_verbose;
		$no_template_found = ! open LATEX_TEMPLATE, "$config_casetype";
	}
	my @directories = @config_template_dirs;
	while ($no_template_found and @directories)
	{
		my $directory = shift @directories;
		print STDERR "Disc-Cover: looking for template \"$config_casetype\" in \"$directory\"..." if $flag_verbose;
		$no_template_found = ! open LATEX_TEMPLATE, "$directory/$config_casetype";
		if ($flag_verbose)
		{
			print STDERR "not found.\n" if ($no_template_found);
			print STDERR "found.\n" unless ($no_template_found);
		}
	}
			
	end_program("You have requested an unknown cover template.", "Check the directories @config_template_dirs for any templates and use one of these.", "If your templates are not stored in one of the above directories then change the \@config_template_dirs variable accordingly.") if $no_template_found;

	while (<LATEX_TEMPLATE>) { $latex_body .= $_ };
	close LATEX_TEMPLATE;
		
	$latex_body .= "\n\n\\vspace{\\baselineskip}".print_disccoverheader()."\\end{center}\\end{document}\n";

	return (print_latex_head().print_cd_header_info().print_cd_info().$latex_body);
}

sub run_latex
{
	print STDERR "Disc-Cover: starting external latex run\n" if $flag_verbose;
	
	my ($latex_command) = $_[0];

	open OUT,">$config_tmp_dir/disc-cover.tex" or end_program("Could not write temporary latex file", "Does \"$config_tmp_dir\" exist?", "Is the $config_tmp_dir directory writable?");
	print OUT print_whole_cover() ;
	close OUT;

	my $latex_string = $flag_verbose ? "(cd $config_tmp_dir ; echo q | $latex_command \"disc-cover.tex\" )" : "(cd $config_tmp_dir ; echo q | $latex_command \"disc-cover.tex\" > /dev/null)";
	if (system ($latex_string) > 0)
	{
		end_program("Something went wrong while running $latex_command.", "Check if the necessary LaTeX packages are installed:\ninputenc,$latex_global_packages.", "Run disc-cover with the option \'-t tex\' and run $latex_command on the\noutput file.");
	}

	print STDERR "Disc-Cover: finished external latex run\n" if $flag_verbose;
}

# FIXME: get rid of this function, not supported by libcdaudio yet
sub parse_cddb_file
{
	print STDERR "Disc-Cover: parsing cddb file\n" if $flag_verbose;

	my $input = shift;

	my %output;

	my $tmp = $input;
	$tmp =~ /^\#\s*Disc length:\s*(\d+)/mg;
	$output{length_minutes} = int($1/60);
	$output{length_seconds} = $1 % 60;

	$output{genre} = (split(' ', $tmp))[1];

	$tmp = $input;

	my ($discid) = ($tmp =~ m%^DISCID=([0-9A-Fa-f]*)%mg);

	my (@tmp2) = ($tmp =~ m%^DTITLE=(.*)%mg);

	my ($dtitle) = "";
	my ($i) = "";
	foreach $i (@tmp2)
	{
		$dtitle .= $i;
	}

	my ($artist, $title) = (undef, undef);
	($artist, $title) = ($dtitle =~ m%(.*?) / (.*)%mg);

	if (!defined($artist) or !defined($title))
	{
		# Catch artist/title disks without / 
		($title) = ($tmp =~ m%^DTITLE=(.*)%m);
		$artist = $title;
	}
	$artist =~ s/
//g;
	$title =~ s/
//g;

	my ($extend, @extd, $j);
	@extd = ($tmp =~ m%^EXTD=([^\n\r]*)%mg);
	foreach $j (@extd) {
		$extend .= "$j";
	}
	$extend =~ s/\\n/\n/g;
	$extend =~ s/\\t/\t/g;

	my (@extended) = undef;
	foreach $i ($tmp =~ m%^EXTT(\d+=.*)[\n\r]*%mg)
	{
		# do not use split on = on next line!
		my ($trackno, $ttitle) = ($i =~ /(\d+)=(.*)/);
		$ttitle =~ s/
//g;
		$extended[$trackno] .= $ttitle;
	}

	my (@tracks) = undef;
	my (@tartist) = undef;
	my $various_artist_warning = 0;
	foreach $i ($tmp =~ m%^TTITLE(\d+=.*)[\n\r]*%mg)
	{
		my ($trackno, $ttitle) = ($i =~ /(\d+)=(.*)/);
		$ttitle =~ s/
//g;

		if ($ttitle =~ / \/ /)
		{
			if ($flag_various_artist_cd)
			{
				($tartist[$trackno], $ttitle) = ($ttitle =~ m%(.*?) / (.*)%mg);
			}
			else
			{
				$various_artist_warning++;
			}
		}
		$tracks[$trackno] .= $ttitle;
	}
	#return 0 if ($tracks[0] eq "");
	
	if ($various_artist_warning == $#tracks+1)
	{
		warn "Warning: all the tracks of this cd contain a slash ' / ' in their track titles,\nwhich could mean it is a various artist cd that contains the form\n'artist / title' for each track. Use the flag '-va' to make a nicer format.\n";
	}

	for ($i = 0; $i <= $#tracks; $i++)
	{
		$tracks[$i] = "" unless defined $tracks[$i];
		$extended[$i] = "" unless defined $extended[$i];
		$tartist[$i] = "" unless defined $tartist[$i];
	}

	my (@offsets) = ($tmp =~ /^\#\s+(\d+)/mg);
	my ($disclength) = ($tmp =~ /^\#\s*Disc length:\s*(\d+)/mg);
	

	my ($lastOffset) = $offsets[0];
	my (@lengths) = ();

	foreach my $offset (@offsets[1..$#offsets])
	{
		@lengths = (@lengths, ($offset - $lastOffset));
		$lastOffset = $offset;
	}

	foreach my $length (@lengths)
	{
		$length /= 75;
	}

	@lengths = (@lengths, $disclength - ( $lastOffset / 75 ) );

	# This rounds times out properly.
	@lengths = map { my $m = $_ - int($_); if (substr($m,0,3) >= .5) { $_++; } int($_); } @lengths;

	my @lengths_minutes; my @lengths_seconds;
	foreach my $length (@lengths)
	{
		push (@lengths_minutes, int($length/60));
		push (@lengths_seconds, $length%60);
	}
		
	$output{discid} = $discid;
	$output{artist} = $artist;
	$output{title} = $title;
	$output{extended} = $extend;
	$output{track_artist} = \@tartist;
	$output{track_extended} = \@extended;
	$output{track_names} = \@tracks;
	$output{track_offsets} = \@offsets;
	$output{track_length_minutes} = \@lengths_minutes;
	$output{track_length_seconds} = \@lengths_seconds;
	$output{total_tracks} = $#tracks+1;

	print STDERR "Disc-Cover: finished parsing cddb file\n" if $flag_verbose;


	return %output;
}


sub create_existing_cddb_entry
{
	my %discinfo = %{$_[0]};
	my $return = "";

	$return .= "# xmcd CD database file generated by Disc-Cover ".$version."\n#\n";
	$return .= "# Track frame offsets:\n";
	my ($tracknumber) = undef;
	my $position = 150;
	for ($tracknumber = 0; $tracknumber < $discinfo{total_tracks}; $tracknumber++)
	{
		$return .= "# ".$position."\n";
		$position += ($discinfo{track_length_minutes}->[$tracknumber] * 60 + $discinfo{track_length_seconds}->[$tracknumber] ) * 75;
	}
	$return .= "#\n# Disc length: ".( $discinfo{length_minutes}*60 + $discinfo{length_seconds} )." seconds\n";

	$return .= "#\n# Revision: 0\n";
	$return .= "# Submitted via: none 1.0\n";
	$return .= "#\n";


	$return .= sprintf("DISCID=%x\n",hex($discinfo{discid}));
	$return .= "DTITLE=$discinfo{artist} / $discinfo{title}\n";
	for ($tracknumber = 0; $tracknumber < $discinfo{total_tracks}; $tracknumber++)
	{
		if ($flag_various_artist_cd and $discinfo{track_artist}->[$tracknumber])
		{
			$return .= "TTITLE".($tracknumber)."=$discinfo{track_artist}->[$tracknumber] / $discinfo{track_names}->[$tracknumber]\n";
		}
		else
		{
			$return .= "TTITLE".($tracknumber)."=$discinfo{track_names}->[$tracknumber]\n";
		}
	}
	$return .= "EXTD=\n";
	for ($tracknumber = 0; $tracknumber < $discinfo{total_tracks}; $tracknumber++)
	{
		$return .= "EXTT".($tracknumber)."=$discinfo{track_extended}->[$tracknumber]\n"
	}
	$return .= "PLAYORDER=\n";

	return $return;
}

sub create_new_cddb_entry
{
	my $cdinfo = shift;
	my $cddb = shift;
	my $return = "";

	$return .= "# xmcd CD database file generated by Disc-Cover ".$version."\n#\n";
	$return .= "# Track frame offsets:\n";
	my ($tracknumber) = undef;
	my @offsets = @{$cdinfo->tracks};
	for ($tracknumber = 0; $tracknumber <= $#offsets; $tracknumber++)
	{
		$return .= "# ".((($offsets[$tracknumber]->pos)[0] * 60 + ($offsets[$tracknumber]->pos)[1] ) * 75)."\n";
	}
	$return .= "#\n# Disc length: ".( ($cdinfo->length)[0]*60 + ($cdinfo->length)[1] )." seconds\n";

	$return .= "#\n# Revision: 0\n";
	$return .= "# Submitted via: none 1.0\n";
	$return .= "#\n";


	$return .= sprintf("DISCID=%x\n",$cddb->discid);
	$return .= "DTITLE=Artist / Disctitle\n";
	for ($tracknumber = 0; $tracknumber <= $#offsets; $tracknumber++)
	{
		$return .= "TTITLE".($tracknumber)."=Untitled\n"
	}
	$return .= "EXTD=\n";
	for ($tracknumber = 0; $tracknumber <= $#offsets; $tracknumber++)
	{
		$return .= "EXTT".($tracknumber)."=\n"
	}
	$return .= "PLAYORDER=\n";

	return $return;
}
sub print_configuration_file
{
	my $result = "#######################################\n# Configuration values for Disc-Cover #\n#   unset values will get a default   #\n#######################################\n\n";
	$result .= "\$config_version_config = \"$config_version\";\n";

	$result .= "\n#########\n# Paths #\n#########\n";
	$result .= "\$config_tmp_dir = \'$config_tmp_dir\';\n";
	$result .= "\$config_device = \'$config_device';\n";
	$result .= "\@config_template_dirs = (\'".join ('\', \'', @config_template_dirs)."\');\n";
	$result .= "\$config_cddb_cache_directory = \'$config_cddb_cache_directory\';\n";

	$result .= "\n#############\n# Latex packages #\n#############\n";
	$result .= "\$latex_user_packages = \'$latex_user_packages\';\n";
	$result .= "\$latex_language_encoding = \'$latex_language_encoding\';\n";

	$result .= "\n##########\n# Output #\n##########\n";
	$result .= "\$config_output_format = \'$config_output_format\';\n";
	$result .= "\$config_casetype = \'$config_casetype\';\n";
	$result .= "\$flag_genre = $flag_genre;\n";
	$result .= "\$config_flaptext = \"$config_flaptext\";\n";
	$result .= "\$flag_with_extended_track_info = $flag_with_extended_track_info;\n";
	$result .= "\$flag_various_artist_cd = $flag_various_artist_cd;\n";
	$result .= "\$flag_double_disc_cdtitle = $flag_double_disc_cdtitle;\n";
	$result .= "\$flag_picture_use_allmusic = $flag_picture_use_allmusic;\n";
	$result .= "\$flag_uppercase_fix = $flag_uppercase_fix;\n";
	$result .= "\%config_latex_colors = (";
	foreach my $color (keys %config_latex_colors)
	{
		$result .= " $color => \"$config_latex_colors{$color}\","
	}
	chop($result);
	$result .= " );\n";


}


sub create_cdserverrc
{

	if (-e $config_cdserverrc)
	{
		$flag_remove_temporary_files = 1;
		end_program("A \"$config_cdserverrc\" file already exists.", "Check the contents of the file and move it to another location,\nthen run this again.");
		
	}
	else
	{
		open CDSERVERRC, ">$config_cdserverrc" or end_program("Could not open \"$config_cdserverrc\"", "Check if you can write this file yourself.");

		print CDSERVERRC "# Created by Disc-Cover $version\n";
		print CDSERVERRC "SERVER=cddbp://freedb.freedb.org:888/\n";
		print CDSERVERRC "ACCESS=REMOTE\n";

		close CDSERVERRC;
	}

	return 1;
}

sub safe_exec
{
	my $program = shift;
	
	my $result;
	my $pid = undef;
	die "Error: cannot fork $!" unless defined ($pid = open(SAFE_KID, "-|"));
	if ($pid == 0)
	{
		#exec('wget', "-O-", "-o/dev/null", "http://www.allmusic.com/cg/amg.dll?P=amg&opt1=2&sql=$web_search") or die "Error: can not exec wget: $!";
		exec($program, @_) or die "Error: can not exec $program $!";
	}
	else
	{
		$/ = undef;
		$result = <SAFE_KID>;
		close SAFE_KID;
		
	}
	return $result;
}

sub print_template_list {
    # We output a list of template types that we find in our template
    # directories.  We print the template name, a colon `:' a space
    # and then the template description as found in the actual
    # template file.

    my %template_list = ();
    foreach my $dir (@config_template_dirs) {
	opendir(DIR, $dir) || next;
	my @templates = grep(/[a-z]+/i, readdir(DIR));
	foreach my $template (@templates) {
	    open(F, "<$dir/$template") || 
		die "Couldn't open template: $dir/$template for read: $!\n";
	    while (<F>) {
		if (/^% Description:(.*)$/) {
		    $template_list{$template} = $1;
		    last;
		}
	    }
	    close(F);
	}
	close(DIR);
    }

    for (sort keys %template_list) {
	print "$_: $template_list{$_}\n";
    }
    
    return 1;
}


###############
# MANUAL PAGE #
###############

=head1 NAME

Disc-Cover - create front and back covers for audio CDs

=head1 SYNOPSIS

disc-cover 
S<[-2|second F<filename>]>
S<[-b|flaptext text]>
S<[-a|additional text]>
S<-allmusic>
S<[-c|-casetype (jewel|slim|x-slim|tevion-slim|letter-slim)]> 
S<[-C|Configuration F<filename>]>
S<[-D|Device F<device>]>
S<[-e|extended]>
S<[-f|file F<filename>]>
S<[-h|help]>
S<[-H|Help]>
S<[-n|new]>
S<[-o|output F<filename>]>
S<[-p F<filename>|-pic F<filename>]>
S<[-R|Remove]>
S<[-S|Server]>
S<[-t|type txt|dvi|tex|ps|pdf|cddb|lbl|html]>
S<[-u|uppercase]>
S<[-v|version]>
S<[-V|Verbose]>
S<[-va|-variousartists]>
S<-template_list>


=head1 DESCRIPTION

Disc-Cover creates front and back covers for audio CDs. The CD has to be
present in the CD-ROM drive, or alternatively a valid CDDB file can be used.
Disc-Cover searches the CDDB database for an entry corresponding to the CD's
CDDB ID. It starts by looking for a local CDDB entry in F<~/.cddb> (or another
directory pointed to by your cddb installation). If no local CDDB entry matches
the CD, disc-cover continues to search the online CDDB databases or CDINDEX
databases as configured in the AudioCD library. It then formats the entry to
produce a Latex, Dvi, Postscript or PDF file, which contains the front and back
covers on a single page. Other formats supported include a simple text output,
a CDDB compatible format, HTML and an output format that can be used with
cdlabelgen (http://www.red-bean.com/~bwf/software/cdlabelgen/), another cover
builder.

=head1 QUICKSTART

The easiest way of using Disc-Cover is to put an audio cd in your cdrom drive
and then run disc-cover without options. Disc-Cover will tell you what it does
and if it is able to create a set of covers it tells you the name of the file
it creates. Normally this is a PostScript file that is ready for printing.

=head1 LAYOUT

The front cover shows artist, album title and when available extended disc
info. Optionally a picture can be added to the front cover. The back cover 
holds the title and artist in the same fashion. In addition to those, the back
cover lists the individual tracks, preceded by a track number and followed by
their running time. The total running time of the CD is given at the bottom of
the back cover. The sides of the back cover contain the artist and CD title.
Another flap hangs on the side of the right of the back cover. When using
fully transparent jewel cases this flap is visible from the front. By default
it holds the user's full name. Colour is also supported.

=head1 FEATURES

=over 4

=item - Uses Latex to produce high quality output.

=item - Outputs in Latex, Dvi, Postscript, Pdf, Cddb entry, HTML, Text and a format
  to use with cdlabelgen.

=item - Supports caching of cddb entries in a directory that can be shared with other
  cddb-aware programs.

=item - Connects with a cddb or cdindex server to get the disc title, artist and list
  of track titles and extended information where available.

=item - Optionally put a picture on the front cover, supports almost any image
  format.

=item - Optionally let Disc-Cover search on allmusic.com for a small version of the
  corresponding front cover of the cd, which it will put on your front cover.
  

=item - Assign different colours to different items such as artist name and track
  numbers.

=item - Supports double albums (two cds in one jewel case).

=item - Creates covers for jewel cases and various slim cases.

=back

=head1 OPTIONS

=over 4

=item B<-2, -second> F<filename>

When using this option disc-cover goes into double album mode, meaning it
will print the front cover using the first cddb entry, either reading the
cd in the drive or by using a file as described in L<B<-f, -file> F<filename>> option. Then it prints
the back cover with two halves, the upper half consists the title/artist and
then the tracks of the first cd, the lower half contains the tracks of the second
cd.

=item B<-a, -additional> I<text>

Add text to the bottom of the front cover. Default is to put extended disc
information here. You can use this to cancel the extended disc information
by doing 'B<-a> " "'.

=item B<-allmusic>

Search on wwww.allmusic.com for the front cover of the album. This picture will
be put on the front cover, just like with B<-p> and B<-pic>. These last options
override this flag. This way you can enable -allmusic by default in the config file
and override it whenever you need to.

=item B<-b, -flaptext> I<text>

Add text to the additional flap at the side of the back cover. This flap is
visible when used with a fully transparent jewel case. These are becoming more
common everyday. By default text from the password entry is used. We take the
string from the comments field up until the first comma. Most times this should
be the user's full name. Cancel this text by using the configuration file or
with 'B<-b> " "'.

=item B<-c, -casetype> (I<jewel|slim|x-slim|tevion-slim|letter-slim>)

These options correspond with files in one of the @config_template_dirs which
are templates that describe case types. You can also copy one of these files to
your current directory, change it and let disc-cover use your own template.
The cases described next are delivered together with Disc-Cover.
Setting this to slim will have Disc-Cover output covers in a format suitable
for slim cases. These are thinner cases often used for single cds or EPs. Use x-slim
if you want the side and flap exchanged. Even
more slim are the cases provided by Tevion (use: tevion-slim). For people using
letter format that have trouble with not getting the whole case fitted on paper,
please use letter-slim.

=item B<-C, -Configuration> F<filename>

Output configuration to a file. The current flags and options set in existing
configuration files or given on the commandline are used to set the values.
To see the current settings do ;disc-cover -C -'.
For example, you can change the default output format to pdf as follows:

  disc-cover -t pdf -C ~/.disc-coverrc

=item B<-D, -Device> I<device>

Specify the CD-ROM device. Default is to use F</dev/cdrom>

=item B<-e, -extended>

This flag enables extended track information, when available. This extended
information will be added below the track names. It is mostly used in
compilation cds for artist information. Sometimes this extended track
information has been used for lyrics, needles to say this will not fit at the
back of one cd cover and hence to prevent the destruction of the layout only the
first line will be used. That is why this is an option instead of default behaviour.

=item B<-f, -file> F<filename>

Use filename as input instead of searching the local and online CDDB database. File
should be a valid CDDB entry.

=item B<-g, -genre>

Include the genre of the disc as the last line of the bottom text on the front cover.
The line looks like this: "genre: F<genre>". Default is off, as often the wrong genre is
used or even just misc or data has been chosen.

=item B<-h, -help>

Print help message and exit.

=item B<-H, -Help>

Show the manual page using pod2text, this should be installed and working (you can check this
by typing 'pod2text /path/to/disc-cover')

=item B<-n, -new>

Creates a new cddb entry. This works with any format, but should be used in
conjunction with '-t cddb' to create a template for you to fill in. See the L<FREQUENTLY ASKED QUESTIONS>
to create covers for your custom cds or for cds that are not in the CDDB
database yet.

=item B<-o, -output> F<filename>

The -o switch allows to specify the name of the output file. By default the
filename will be Artist_Title.xxx, where xxx is txt, tex, dvi, ps, pdf or html,
depending on the file format. See -t option for supported file formats. Using
"B<-o> -" will send the output to standard output.

=item B<-p> F<filename>

Includes the picture F<filename> on the front cover in a framed box
right aligned with the artist name. The format of the picture has to be known to
the program I<convert> that comes with I<ImageMagick>. Also the graphicx package for
LaTeX has to be available. The picture is scaled to 200x200 and converted into
Adobe Encapsulated PostScript Interchange format. Remember that output in tex,
dvi, txt, html and labelgen are completely useless with pictures. In case of output
in pdf the picture is transformed into Encapsulated Portable Document Format (EPDF).

=item B<-pic> F<filename>

Include a graphics file on the front cover without converting it. This only
works when the format of the file is the same as the output format. In case of
PostScript (PS) output you can use Encapsulated PostScript (EPS,EPSI) files. In
case of Portable Document Format (PDF) you can use Encapsulated Portable
Document Format (EPDF). Note that this is essentially the same option as in L<B<-p> F<filename>>
but no scaling or converting is performed. More responsibility goes to the user
this way.

=item B<-R, -Remove>

By default disc-cover deletes all temporary files it creates before it exits.
This behaviour can be overwritten by specifying the this option. For debugging
purposes only.

=item B<-S, -Server>

Create a default configuration for the selection of CDDB server. This configuration
is used by other CDDB aware programs too, so be careful. Note that Disc-Cover will not
overwrite the configuration file.

=item B<-t, -type> I<txt|tex|dvi|ps|pdf|cddb|lbl|html>

The -t switch allows to specify the output format. By default disc-cover will
create a Postscript file. Other formats supported include ASCII text (txt),
LaTeX (tex), DVI (dvi), PDF (pdf), cddb database format (cddb), a cdlabelgen
compatible format (lbl) and in Hypertext Markup Language (html).

=item B<-u, -uppercase>

Fixes those annoying entries that are written without capitals. It capitalises every single word in titles and artists.

=item B<-v, -version>

Print version and program information and exit.

=item B<-V, -Verbose>

Enable verbose output of Disc-Cover, the libaudiocd library and all third party software,
such as LaTeX, dvips and convert. For debugging purposes.

=item B<-va, -variousartists>

Some CDDB entries code every track in the 'artist / title' format. Mostly, this is used for
artists that contain various artists. By enabling this option Disc-Cover will decode and use
that information.

=item B<-template_list>

Prints a list of output template types and their descriptions.

=back

=head1 CONFIGURATION

Disc-cover first checks the file F</etc/disc-cover.conf> for system wide parameters and
then looks at F<~/.disc-coverrc>. Following are all the different variables that can be
set. The file F<~/.cdserverrc> is used indirectly through the AudioCD library. This file
contains the CDDB and CDINDEX servers. Furthermore it can be used to control certain
other options of this library. See the appropriate documentation.

You can generate a configuration file with the L</B<-C, -Configuration> F<filename>> option. Keep in mind that the values
in this file will be set by looking at the current settings. That is, any existing configuration
files or options on the commandline will be used.

To make a clean configuration file, first remove the ~/.disc-coverrc file and then run Disc-Cover without any option but '-C': disc-cover -C ~/.disc-coverrc

=over 4

=item $config_version_config

This will be used in the future by new versions of Disc-Cover to check for inconsistencies
in changed, removed or added configuration options.

 Default: '1.4.0';

=item $config_tmp_dir

This sets the directory that is used for temporary files.

 Default: '/tmp';

=item $config_output_format

This is the default output format. Although you can choose any of the types listed
in 'disc-cover -h' option the most common would be 'ps' or 'pdf'.

 Default: 'ps';

=item $config_device

The default cdrom drive that is used to scan a cd. See L<B<-D, -Device> I<device>>

 Default: '/dev/cdrom';

=item $config_cddb_cache_directory

The path where cddb entries are stored in and retrieved from. This can be shared with
other programs that also use cddb.

 Default: "$ENV{HOME}/.cddb";

=item $flag_with_extended_track_info

Setting this to one will force disc-cover to include extended track
information. See L</B<-e, -extended>> option. It is advised to keep
this 0 as there are lots of entries that contain ugly and meaningless extended
information. Most of the time it is only useful in case of various artists cds.

 Default: 0;

=item $flag_various_artist_cd

Setting this to one will force disc-cover to parse every track the same way
it parses the 'artist / title' label. This might be useful for various artist
cds where every track is formatted in the same way. It is adviced to leave this
option 0, as disc-cover will issue a warning whenever it encounters such a disc.
You can easily turn it on with L</B<-va, -variousartists>>

 Default: 0;

=item $flag_double_disc_cdtitle

Setting this flag during the processing of a double cd (using the L</B<-2, -second>>
option) enables a 'smart' algorithm that attempts to find a nice title for the cd, using
the titles of both cddb entries. It will remove all characters unequal in both strings,
and gets rid of any characters surrounding uncommon parts. Last, it removes the words 'disc'
and 'disk', plus any whitespace at the beginning or end., -second>>
option) enables a 'smart' algorithm that attempts to find a nice title for the cd, using
the titles of both cddb entries. It will remove all characters unequal in both strings,
and gets rid of any characters surrounding uncommon parts. Last, it removes the words 'disc'
and 'disk', plus any whitespace at the beginning or end.

 Default: 1;

=item $flag_genre

By setting this an additional line will be added to the bottom of the bottom text on the front cover
with the genre (taken from the database) of the disc. See L<B<-g, -genre>> option.

 Default: 0;

=item $flag_picture_use_allmusic

Setting this flag does the same as the L<B<-allmusic>> option. That is, Disc-Cover will search for
a cover picture on allmusic to be put on the front cover.

 Default: 0;

=item $flag_uppercase_fix

Setting this flag does the same as the L<B<-u,-uppercase>> option. That is, Disc-Cover will capitalise every single word in titles and artists.

 Default: 0;

=item %config_latex_colors

This is a list of colours that can be set to alter most of the different items that appear on
the covers. You can change the colour of the title, artist, discinfo, track strings, track numbers
and track times independently. Use colours from the color.sty Latex file: black, white, red, green,
blue, cyan, magenta and yellow.
 
 Default: ( title => 'black',
            artist => 'black',
	    discinfo => 'black',
	    track_number => 'black',
	    track_string => 'black',
	    track_time => 'black',
	    track_extended => 'black'
	    track_artist => 'black'
	    flaptext => 'black',
	  );

=item $config_flaptext

This takes the string from the comments field of the user's password entry. We
use only the text up until the first comma, which normally is set to the user's
full name. You can also put a simple string here or an empty one to cancel the
output. See also L<B<-b, -flaptext> I<text>> option.

 Default: split ',', (getpwuid($<)))[6];

=item $config_casetype

This determines for which type of case output is generated. It searches for a template
file in all the @config_template_dirs with the same name.

 Default: "jewel";

=item $latex_user_packages

If you create a template that requires special LaTeX packages, they can be added here. Generally not advised if you want to share your templates with others as they need to own those LaTeX packages, and add them to Disc-Cover's configuration file.

 Default: "";

=item $latex_language_encoding

Whenever you create cds with languages not normally supported by LaTeX you should
add or change this. It calls \usepackage[$latex_language_encoding]{inputenc}. Main encodings are, latin1 for ISO Latin-1, ascii for pure ASCII, ansinew and cp1252 (they are synonyms) for Windows 3.1 ANSI (an MS extension of ISO Latin-1) and applemac for Apple MacIntosh.

 Default: "latin1";

=back


=head1 FREQUENTLY ASKED QUESTIONS

Here you might find an answer between the FAQs.

=head2 Q: I get the following error (or similar), what's wrong?

 "Can't locate HTTP/Request.pm in @INC (@INC contains:
 /usr/lib/perl5/5.00502/i586-linux /usr/lib/perl5/5.00502
 /usr/lib/perl5/site_perl/5.005/i586-linux /usr/lib/perl5/site_perl/5.005 .)
 at ./disc-cover line 811.
 BEGIN failed--compilation aborted at ./disc-cover line 811."

A: Install the Perl modules called libwww-perl, HTML-Parser, URI, and
MIME-Base64, which can be found at www.cpan.org or at the 'more stuff...'
directory on the homepage of Disc-Cover. See the file INSTALL for more details.

=head2 Q: I get the following error with the option -V, what's wrong?

 "Disc-Cover: running verbose version x.x.x (config y.y.y)
 Disc-Cover: creating tmp dir (/tmp/disc-cover-4436)
 Disc-Cover: calling Audio-CD library...
 Trying CDDB server cddbp://:0/
 Connection error: No such file or directory
 Could not establish connection with any CDDB servers!
 Disc-Cover: ...back from call to Audio-CD library.
 Disc-Cover: Audio-CD library says success, let's continue"

A: You have no CDDB servers configured. Either use the wonderful
program cdcd to create such a configuration or run disc-cover with
the options -S.

=head2 Q: How do I make a cover for homemade cds?

A: Follow these steps.

1. Insert the homemade cd.
 
2. Run disc-cover like this:
    disc-cover -n -t cddb -o myfile.cddb
 
3. Edit the myfile.cddb with some text editor, the tracks will all be named
   "Untitled". This should be very easy.

4. For printable output run disc-cover again:
    disc-cover -f myfile.cddb

=head2 Q: I have been using 1.0.1. under SuSE 7.3 for a while an want to
upgrade to 1.3.X or higher However, Audio-CD does not compile.

A:  You have to install lbcdaudio first. Your distribution does not
contain it, or it does not install it with the standard setup.


=head2 Q: After installation all my CD's are reread from the internet.

A: Run the following commands (from a script, if you like)

  cd ~/.cddb
  ln -s . blues
  ln -s . classical
  ln -s . country
  ln -s . data
  ln -s . folk
  ln -s . jazz
  ln -s . newage
  ln -s . reggae
  ln -s . rock
  ln -s . soundtrack
  ln -s . misc


=head2 Q: Q3. Mike Oliphant's grip creates cddb-files with track-artist held in a special field called TARTIST0, TARTIST1, ...  Why can't disc-cover print these lables appropriately by the "-va" option? 

A: Unfortunately Mike's idea to invent new tags for the track artist
violates the freedb-conventions for handling samplers. There will be no
support for this.




=head2 Q: How to cut the covers?

A: This is my routine for jewel cases, just one way of doing it. I start with
the front cover.  I cut it with two cuts along the width of the paper. This
results in a front cover with two pieces of paper along the sides. These are
folded backwards to help the cover keep in place when it is inserted in the
jewel case. The back cover is just cut with four cuts.

=head2 Q: When I print my cover from PDF in Acrobat Reader the resulting covers are too small. How come?

A: Hendrik Neumann E<lt>h-n@gmx.netE<gt>: The problem is the option 'Fit to page' that
makes Acrobat Reader scale the page before printing. Turn it off before
attempting to print.

=head2 Q: I'm not root and I want to install Audio::CD

A: Adam Spiers E<lt>adam@spiers.netE<gt>: make sure that the correct parameters are
given to the perl Makefile.PL file as follows:

  perl Makefile.PL LIBS="-L/nfs-home/adams/local/lib -lcdaudio" \
  INC=-I/nfs-home/adams/software/libcdaudio-0.99.6/source PREFIX=~

=head2 Q: How do I make covers for double cds?

A: Follow these steps:

1. insert the I<second> disc

2. Run disc-cover:
    disc-cover -t cddb -o disc2.cddb
   
3. replace the second disc with the first

4. Run disc-cover again:
    disc-cover -2 disc2.cddb 

=head2 Q: The quality of the picture is reduced!

A: The image is resized to 200x200 pixels, this is to make sure that the size
of the resulting eps or pdf picture is not too large. You can work around this
by converting your picture yourself, for example with jpeg2ps, and then including
this in your cover using the '-pic' option. This options includes files as they are.

=head2 Q: I want Disc-Cover to have this specific feature, what now?

A: Send me an e-mail with your feature or better yet, implement it yourself
and send me a patch file. This does not guarantee that your feature will be
in any future version though. I would like to keep Disc-Cover as simple to use
as possible. But, sometimes I can't resist to put in something new, or the number
of e-mails on one feature grows large enough for me to add it to Disc-Cover.

=head2 Q: I run Disc-Cover and it does not produce a correct cover, what's wrong?

A: I don't know! If you e-mail the problem I can try to fix it. When you mail
some error please remember to state the problem as exact as you can and to
include the cddb entry (save it with 'disc-cover -t cddb'). Also, two debug
options are included. Use L</B<-V, -Verbose>> to get verbose output of what the
program is doing. This helps me locate the problem if you email me your
problem. Use L</B<-R, -Remove>> to prevent disc-cover from deleting temporary
files. This enables you to examine intermediate files (.tex, .div, pictures).


=head1 FILES

=over 4

=item F</etc/disc-cover.conf>

System wide configuration file (for its format see L</CONFIGURATION>)). All its items can be overridden in a user configuration file.

=item F<$HOME/.disc-coverrc>

User configuration file (for its format see L</CONFIGURATION>). 

=item F<$HOME/.cdserverrc>

Configuration of the AudioCD library. Use this to select which servers are to be used.

=back

=head1 BUGS

=over 4

=item 1

Whenever there is more than one file in the cddb cache directory disc-cover
will issue a warning and just use the first one it found.

=item 2

Not possible to use CDINDEX files as input.

=item 3

The layout of the tracks can be slightly deformed when special international
characters or any other tall characters appear in the title.

=back

=head1 AUTHORS

disc-cover is written and maintained by J.I. van Hemert E<lt>jvhemert@cwi.nlE<gt>
You can find the latest version on http://www.cwi.nl/~jvhemert/disc-cover.html
The program is licensed under the GNU Public License. More information about this license
is in the source package in the file F<COPYING> or on http://www.gnu.org/copyleft/gpl.html

=cut

# Eof disc-cover