File: ebook.pl

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


=head1 NAME

ebook - create and manipulate e-books from the command line

=head1 SYNOPSIS

 ebook COMMAND arg1 arg2 --opt1 --opt2

See also L</EXAMPLES>.

=cut

use Config::IniFiles;
use Cwd qw(chdir getcwd realpath);
use EBook::Tools qw(:all);
use EBook::Tools::IMP qw(:all);
use EBook::Tools::Mobipocket qw(:all);
use EBook::Tools::MSReader qw(:all);
use EBook::Tools::Unpack;
use File::Basename qw(dirname fileparse);
use File::Path;              # Exports 'mkpath' and 'rmtree'
use File::Slurp qw(slurp);   # Also exports 'read_file' and 'write_file'
use File::Temp qw(tempfile tempdir);
use Getopt::Long qw(:config bundling);

# Exit values
use constant EXIT_SUCCESS       => 0;   # Success
use constant EXIT_BADCOMMAND    => 1;   # Invalid main command
use constant EXIT_BADOPTION     => 2;   # Invalid subcommand or option
use constant EXIT_BADINPUT      => 10;  # Bad input data
use constant EXIT_BADOUTPUT     => 11;  # Bad/unexpected output data
use constant EXIT_TOOLSERROR    => 20;  # Internal EBook::Tools error
use constant EXIT_MISSINGHELPER => 30;  # Required helper file not found
use constant EXIT_HELPERERROR   => 31;  # Helper command exited improperly


########################################
########## CONFIGURATION FILE ##########
########################################

my $defaultconfig = slurp(\*DATA);
my $configdir = userconfigdir();
my $configfile = $configdir . '/config.ini';
my $config;
if(-f $configfile)
{
    $config = Config::IniFiles->new( -file => $configfile );
}
$config = Config::IniFiles->new() unless($config);
#$config->read($configfile);

# Tidysafety requires special handling, since 0 is a valid value
my $tidysafety = $config->val('config','tidysafety');
undef($tidysafety) if(defined($tidysafety) and $tidysafety eq '');


#####################################
########## OPTION HANDLING ##########
#####################################

my %opt = (
    'author'      => '',
    'category'    => undef,
    'compression' => undef,
    'dir'         => '',
    'erotic'      => 0,
    'fileas'      => '',
    'firstname'   => undef,
    'fix'         => 1,
    'format'      => '',
    'help'        => 0,
    'htmlconvert' => 0,
    'identifier'  => undef,
    'input'       => '',
    'key'         => '',
    'lastname'    => undef,
    'middlename'  => undef,
    'mimetype'    => '',
    'mobi'        => 0,
    'mobigencmd'  => $config->val('helpers','mobigen') || undef,
    'names'	  => 0,
    'nosave'      => 0,
    'noscript'    => 0,
    'oeb12'       => 0,
    'opf20'       => 0,
    'opffile'     => '',
    'raw'         => 0,
    'subcategory' => undef,
    'tidy'        => 0,
    'tidycmd'     => $config->val('helpers','tidy') || undef,
    'tidysafety'  => $tidysafety,
    'title'       => undef,
    'verbose'     => $config->val('config','debug') || 0,
    );

GetOptions(
    \%opt,
    'author=s',
    'category|cat=s',
    'compression|c=i',
    'delete|del',
    'dir|d=s',
    'erotic|sex',
    'fileas=s',
    'firstname=s',
    'fix',
    'format=s',
    'help|h|?',
    'htmlconvert',
    'identifier|id=s',
    'input|i=s',
    'key|pid=s',
    'lastname=s',
    'middlename=s',
    'mimetype|mtype=s',
    'mobi|m',
    'mobigencmd|mobigen=s',
    'names',
    'nosave',
    'noscript',
    'raw',
    'oeb12',
    'opf20',
    'opffile|opf=s',
    'output|o=s',
    'subcategory|subcat=s',
    'tidy',
    'tidycmd',
    'tidysafety|ts=i',
    'title=s',
    'verbose|v+',
    );

if($opt{oeb12} && $opt{opf20})
{
    print "Options --oeb12 and --opf20 are mutually exclusive.\n";
    exit(EXIT_BADOPTION);
}

# Default to OPF20 if neither format is specified
if(!$opt{oeb12} && !$opt{opf20}) { $opt{opf20} = 1; }

$EBook::Tools::debug = $opt{verbose};
$EBook::Tools::tidycmd = $opt{tidycmd} if($opt{tidycmd});
$EBook::Tools::tidysafety = $opt{tidysafety} if(defined $opt{tidysafety});


######################################
########## COMMAND HANDLING ##########
######################################

my %dispatch = (
    'adddoc'      => \&adddoc,
    'additem'     => \&additem,
    'bisac'       => \&bisac,
    'blank'       => \&blank,
    'convert'     => \&convert,
    'dc'          => \&downconvert,
    'dlbisac'     => \&dlbisac,
    'downconvert' => \&downconvert,
    'config'      => \&config,
    'fix'         => \&fix,
    'genepub'     => \&genepub,
    'genimp'      => \&genimp,
    'genmobi'     => \&genmobi,
    'genncx'	  => \&genncx,
    'impmeta'     => \&impmeta,
    'setcover'    => \&setcover,
    'setmeta'     => \&setmeta,
    'splitmeta'   => \&splitmeta,
    'splitpre'    => \&splitpre,
    'stripscript' => \&stripscript,
    'tidyxhtml'   => \&tidyxhtml,
    'tidyxml'     => \&tidyxml,
    'unpack'      => \&unpack_ebook,
    );

my $cmd = shift;

if(!$cmd)
{
    print "No command specified.\n";
    print "Valid commands are: ",join(" ",sort keys %dispatch),"\n";
    exit(EXIT_BADCOMMAND);
}
if(!$dispatch{$cmd})
{
    print "Invalid command '",$cmd,"'\n";
    print "Valid commands are: ",join(" ",sort keys %dispatch),"\n";
    exit(EXIT_BADCOMMAND);
}

$dispatch{$cmd}(@ARGV);
exit(EXIT_SUCCESS);


#########################################
########## COMMAND SUBROUTINES ##########
#########################################

=head1 COMMANDS

=head2 C<adddoc>

Adds a documents to both the book manifest and spine.

=head3 Options

=over

=item C<--opffile>

=item C<--opf>

The OPF file to modify.  If not specified one will be searched for in
the current directory.

=item C<--identifier>

=item C<--id>

The ID attribute to use for the added manifest item.  This is
required, and ebook will abort if it is not specified.

=item C<--mimetype>

=item C<--mtype>

The mime type string to use for the added manifest item.  If not
specified, it will be autodetected via File::Mimeinfo::Magic.  This
may not result in an optimal string.

=back

=head3 Example

 ebook adddoc --opf mybook.opf --id 'text-ch1' chapter1.html

=cut

sub adddoc
{
    my ($newdoc) = @_;
    my $opffile = $opt{opffile};
    my $id = $opt{identifier};
    my $mtype = $opt{mimetype};

    my $ebook = EBook::Tools->new();
    $ebook->init($opffile);
    $ebook->add_document($newdoc,$id,$mtype);
    if($ebook->errors)
    {
        print {*STDERR} "Unrecoverable errors found.  Aborting.\n";
        $ebook->print_errors;
    }
    $ebook->save;
    $ebook->print_warnings;
    return 0;
}


=head2 C<additem>

Add an item to the book manifest, but not the spine.

Note that the L</fix> command will automatically insert manifest items
for any local files referenced by existing manifest items.

=head3 Options

=over

=item C<--opffile>

=item C<--opf>

The OPF file to modify.  If not specified one will be searched for in
the current directory.

=item C<--identifier>

=item C<--id>

The ID attribute to use for the added manifest item.  This is
required, and ebook will abort if it is not specified.

=item C<--mimetype>

=item C<--mtype>

The mime type string to use for the added manifest item.  If not
specified, it will be autodetected via File::Mimeinfo::Magic.  This
may not result in an optimal string.

=back

=head3 Example

 ebook additem --opf mybook.opf --id 'illus-ch1' chapter1-illus.jpg

=cut

sub additem
{
    my ($newitem) = @_;
    my $opffile = $opt{opffile};
    my $id = $opt{identifier};
    my $mtype = $opt{mimetype};

    if(!$id)
    {
        print {*STDERR} "You must specify an ID when adding a document!\n";
        exit(EXIT_BADOPTION);
    }

    my $ebook = EBook::Tools->new();
    $ebook->init($opffile);
    $ebook->add_item($newitem,$id,$mtype);
    if($ebook->errors)
    {
        print {*STDERR} "Unrecoverable errors found.  Aborting.\n";
        $ebook->print_errors;
        exit(EXIT_TOOLSERROR);
    }
    $ebook->save;
    $ebook->print_warnings;
    return 0;
}


=head2 C<bisac>

Search for a BISAC code matching a case-insensitive regular expression.

=head3 Options

=over

=item C<regexp>

The first argument is taken as a regular expression to use for the
search.  If this is either '.' or not specified, the entire list of
valid codes is printed.

This requires that the BISAC codes be downloaded ahead of time.  (See
C<dlbisac>.)

=back

=cut

sub bisac
{
    my ($regexp) = @_;
    my $bisg = EBook::Tools::BISG->new();

    my @list = $bisg->find($regexp);

    foreach my $code (@list) {
        print $code,"\n";
    }
    return 0;
}


=head2 C<blank>

Create a blank e-book structure.

=head3 Options

=over

=item C<--opffile filename.opf>

=item C<--opf filename.opf>

Use the specified OPF file.  This can also be specified as the first
non-option argument, which will override this option if it exists.  If
no file is specified, the program will abort with an error.
=item C<--author> "Author Name"

The author of the book.  If not specified, defaults to "Unknown
Author".

=item C<--title> "Title Name"

The title of the book.  If not specified, defaults to "Unknown Title".

=item C<--dir directory>

=item C<-d directory>

Output the OPF file in this directory, creating it if necessary.

=back

=head3 Example

 ebook blank newfile.opf --author "Me Myself" --title "New File"
 ebook blank --opffile newfile.opf --author "Me Myself" --title "New File"

Both of those commands have the same effect.

=cut

sub blank
{
    my ($opffile) = @_;
    my %args;
    my $ebook;

    $opffile = $opt{opffile} if(!$opffile);

    $args{opffile} = $opffile;
    $args{author} = $opt{author} if($opt{author});
    $args{title} = $opt{title} if($opt{title});

    useoptdir() unless($opt{nosave});
    $ebook = EBook::Tools->new();
    $ebook->init_blank(%args);
    $ebook->save unless($opt{nosave});
    return 0;
}


=head2 C<config>

Make changes to the EBook::Tools configuration file.

The configuration file itself is located as either
C<$ENV{HOME}/.ebooktools/config.ini> or as
C<$ENV{USERPROFILE}\Application Data\EBook-Tools>, depending on
platform and which directory is found first.  See
L<EBook::Tools/userconfigdir()> for details.

=head3 Arguments / Subcommands

Configuration is always handled in the format of:

 ebook config subcommand value

=over

=item * C<default>

Replace any existing configuration file with a default template.  This
creates the file if it does not exist.  This should be done once
before any other configuration manipulation is done, unless a
configuration file has been manually created ahead of time.

=item * C<debug>

Sets the default debugging level when no verbosity is specified.  Note
that verbosity can only be increased, not decreased, with the C<-v>
option.

=item * C<tidysafety>

Sets the default safety level when tidy is used.  Valid values are
from 0-4.  See L</unpack> for details on what each value means.

=item * C<mobipids>

A comma-separated list of Mobipocket PIDs to try to use to decrypt
e-books.  This value is only used if the appropriate plug-in modules
or helper applications are available, as DRM is not supported natively
by EBook::Tools.  Note that if the PID includes a $ character, the
entire PID string has to be enclosed in single quotes.

=back

=head3 Examples

 ebook config default
 ebook config debug 2
 ebook config mobipids '1234567890,2345678$90'

=cut

sub config
{
    my $subcommand = shift;
    my $value = shift;
    my $subname = ( caller(0) )[3];

    my %valid_subcommands = (
        'default' => 1,
        'debug' => 1,
        'tidysafety' => 1,
        'mobipids' => 1,
        );

    if(!$subcommand)
    {
        print {*STDERR} ("You must specify what configuration change you",
                         " want to make.\n");
        print {*STDERR} "Valid config commands are:\n";
        foreach my $subcom (keys %valid_subcommands)
        {
            print {*STDERR} "  config ",$subcom,"\n";
        }
        exit(EXIT_BADCOMMAND);
    }
    if(!$valid_subcommands{$subcommand})
    {
        print {*STDERR} "Invalid command 'config ",$subcommand,"'\n";
        print {*STDERR} "Valid config commands are:\n";
        foreach my $subcom (keys %valid_subcommands)
        {
            print {*STDERR} "  config ",$subcom,"\n";
        }
        exit(EXIT_BADCOMMAND);
    }

    if($subcommand eq 'default')
    {
        my $fh_config;
        local $/;

        print "Creating new configuration file '",$configfile,"'\n";
        open($fh_config,'>',$configfile)
            or die("Unable to open config file '",$configfile,"' for writing!\n");
        print {*$fh_config} $defaultconfig;
        close($fh_config)
            or die("Unable to close config file '",$configfile,"'!\n");
    }
    elsif($subcommand eq 'debug')
    {
        if(not defined $value)
        {
            print {*STDERR} "You must specify a debugging level.\n";
            exit(EXIT_BADOPTION);
        }
        $config->setval('config','debug',$value);
        $config->RewriteConfig;
    }
    elsif($subcommand eq 'tidysafety')
    {
        if(not defined $value)
        {
            print {*STDERR} "You must specify a tidy safety level.\n";
            exit(EXIT_BADOPTION);
        }
        $config->setval('config','tidysafety',$value);
        $config->RewriteConfig;
    }
    elsif($subcommand eq 'mobipids')
    {
        my @pids;
        if(!$value)
        {
            print {*STDERR} "You must specify at least one PID.\n";
            exit(EXIT_BADOPTION);
        }
        @pids = split(/,/,$value);
        foreach my $pid (@pids)
        {
            if(!pid_is_valid($pid))
            {
                print {*STDERR} "PID '",$pid,"' is not valid!  Aborting!\n";
                exit(EXIT_BADOPTION);
            }
        }
        $config->setval('drm','mobipids',$value);
        $config->RewriteConfig;
    }
    return 0;
}


=head2 C<convert>

Unpacks the ebook specified as the first argument, runs standard fixes
on the contents, and repacks it into a new format in the output file
specified as the second argument.  Currently the only supported output
format is epub, which is the format you will get irrespective of the
extension you actually give the output file.

=head3 Options

=over

All options from C<unpack> and C<fix> are technically valid here as
well, though of course some options are nonsensical in this context
and will likely break the conversion (e.g. --nosave).

=back

=head3 Example

 ebook convert MyBook.prc MyBook.epub
 ebook convert --name MyBook.lit /home/myname/MyBook.epub

=cut

sub convert
{
    my ($infile,$outfile) = @_;
    my $subname = ( caller(0) )[3];

    my $tempdir = File::Temp->newdir();
    $tempdir->unlink_on_destroy( 1 );
    my $cwd_orig = getcwd();

    if(!$infile) { die("You must specify an input file.\n"); }
    if(!$outfile) { die("You must specify an output file.\n"); }

    my $ebook;
    my $bookname;
    my $format;

    ($bookname,undef,undef) = fileparse($infile,'\.\w+$');
    (undef,undef,$format) = fileparse($outfile,'\.\w+$');

    # TODO: actually check $format

    $infile = realpath($infile);
    $outfile = realpath($outfile);

    $dispatch{'unpack'}($infile,$tempdir);

    chdir($tempdir);

    $dispatch{'fix'}();

    $opt{output} = $outfile;
    $dispatch{'genepub'}();

    chdir($cwd_orig);
    return 0;
}


=head2 C<dlbisac>

Downloads and caches the Book Industry Study Group BISAC codes into a
local database.  This will destroy the existing contents of that table
if this has been done previously.

=cut

sub dlbisac
{
    my $subname = ( caller(0) )[3];

    my $bisg = EBook::Tools::BISG->new();
    $bisg->download_bisac;
    $bisg->save_bisac;
    return;
}


=head2 C<downconvert>

=head2 C<dc>

If the appropriate helpers or plugins are available, write a copy of
the input file with the DRM restrictions removed.

NOTE: no actual DRM-removal code is present in this package.  This is
just presents a unified interface to other programs that have that
capability.

=head3 Arguments

=over

=item * C<infile>

The first non-option argument is taken to be the input file.  If not
specified, the program exits with an error.

=item * C<outfile>

The second non-option argument is taken to be the output file.  If not
specified, the program will use a name based on the input file,
appending '-nodrm' to the basename and keeping the extension.  In the
special case of Mobipocket files ending in '-sm', the '-sm' portion of
the basename is simply removed, and nothing else is appended.

=item * C<key>

The third non-option argument is taken to be either the decryption
key/PID, or in the case of Microsoft Reader (.lit) files, the
C<keys.txt> file containing the decryption keys.

If not specified, this will be looked up from the configuration file.
Convertlit keyfiles will be looked for in standard locations.  If no
key is found, the command aborts and exits with an error.

=back

=head3 Example

 ebook downconvert NewBook.lit NewBook-readable.lit mykeys.txt
 ebook dc MyBook-sm.prc

=cut

sub downconvert
{
    my ($infile,$outfile,$key) = @_;
    my $suffix;
    my $mobidedrm = find_mobidedrm();
    my $convertlit = find_convertlit();
    my $status;

    if(!$infile)
    {
        print {*STDERR} "You must specify a file to downconvert.\n";
        exit(EXIT_BADOPTION);
    }
    unless(-f $infile)
    {
        print {*STDERR} "Could not find '",$infile,"' to downconvert!\n";
        exit(EXIT_BADINPUT);
    }

    my $unpacker = EBook::Tools::Unpack->new(
        'file' => $infile,
        'format' => $opt{format},
        );

    if($unpacker->format eq 'mobipocket')
    {
        if(!$mobidedrm)
        {
            print {*STDERR} <<'END';
Downconverting Mobipocket books requires that MobiDeDRM be available,
either on the path, in the configuration directory, or specified in
the configuration file.
END
            exit(EXIT_MISSINGHELPER);
        }
        if($key)
        {
            if(!pid_is_valid($key))
            {
                print {*STDERR} "PID '",$key,"' is not valid!\n";
                exit(EXIT_BADOPTION);
            }
            $outfile = system_mobidedrm(infile => $infile,
                                        outfile => $outfile,
                                        pid => $key);
            if(!$outfile)
            {
                print {*STDERR} "Failed to downconvert '",$infile,"'!\n";
                exit(EXIT_HELPERERROR);
            }
        }
        else
        {
            my @pids = split(/,/,$config->val('drm','mobipids'));
            if(!@pids)
            {
                print {*STDERR}
                "No PID specified, and none found in configuration file!\n";
                exit(EXIT_BADOPTION);
            }
            foreach my $pid (@pids)
            {
                if(!pid_is_valid($pid))
                {
                    print {*STDERR} "PID '",$pid,"' is not valid, skipping!\n";
                    next;
                }
                $outfile = system_mobidedrm(infile => $infile,
                                            outfile => $outfile,
                                            pid => $pid);
                last if($outfile);
            }
            if($outfile)
            {
                print("Successfully downconverted '",$infile,
                    "' into '",$outfile,"'\n");
                return 0;
            }
            else
            {
                print "Unable to downconvert '",$infile,"'!\n";
                exit(EXIT_BADOUTPUT);
            }
        } # if($key) / else
    } # if($unpacker->format eq 'mobipocket')

    elsif($unpacker->format eq 'msreader')
    {
        if(!$convertlit)
        {
            print {*STDERR} <<'END';
Downconverting MS Reader books requires that ConvertLit be available,
either on the path, in the configuration directory, or specified in
the configuration file.
END
            exit(EXIT_MISSINGHELPER);
        }
        if(!$outfile)
        {
            ($outfile,undef,$suffix) = fileparse($infile,'\.\w+$');
            $outfile .= '-nodrm' . $suffix;
        }
        my $retval = system_convertlit(infile => $infile,
                                       outfile => $outfile,
                                       keyfile => $key);
        if($retval == 0)
        {
            print("Successfully downconverted '",$infile,
                "' into '",$outfile,"'\n");
            return 0;
        }
        else
        {
            print("Failed to downconvert '",$infile,
                  " [system_convertlit returned ",$retval,"]\n");
            exit(EXIT_HELPERERROR);
        }
    }
    else
    {
        print {*STDERR} "Cannot downconvert format '",$unpacker->format,"'\n";
        exit(EXIT_BADINPUT);
    }
    return 0;
}


=head2 C<fix>

Find and fix problems with an e-book, including enforcing a standard
specification and ensuring that all linked objects are present in the
manifest.

=head3 Options

=over

=item C<--opffile filename.opf>

=item C<--opf filename.opf>

Use the specified OPF file.  This can also be specified as the first
non-option argument, which will override this option if it exists.  If
no file is specified, one will be searched for.

=item C<--oeb12>

Force the OPF to conform to the OEB 1.2 standard.  This is the default.

=item C<--opf20>

Force the OPF to conform to the OPF 2.0 standard.  If both this and
C<--oeb12> are specified, the program will abort with an error.

=item C<--mobi>

Correct Mobipocket-specific elements, creating an output element to
force UTF-8 output if one does not yet exist.

=item C<--erotic> or C<--sex>

Enable special handling for erotic fiction (most notably special
subject normalization rules).

=item C<--names>

Normalize names to standard capitalization and format (primary name
display is "First Middle Last", but file-as is "Last, First Middle".

This is not done by default as it can damage unusual but correct
names.

=back

=cut

sub fix
{
    my ($opffile) = @_;
    my $ebook;
    my $retval;
    my ($filebase,$filedir,$fileext);
    my $tidyfile;
    my $warncount;

    $opffile = $opt{opffile} if(!$opffile);
    $ebook = EBook::Tools->new();
    $ebook->init($opffile);
    if($opt{erotic}) { $ebook->set_erotic(1); }
    $ebook->fix_oeb12 if($opt{oeb12});
    if($opt{opf20}) {
        if(-f 'META-INF/container.xml') {
            $ebook->gen_epub_files;
        }
        else {
            $ebook->fix_opf20;
        }
    }
    $ebook->fix_misc;
    $ebook->fix_mobi if($opt{mobi});
    if($opt{names}) {
        $ebook->fix_creators;
    }
    $ebook->save unless($opt{nosave});

    if($ebook->errors) {
        $ebook->print_errors;
        print "Unrecoverable errors while fixing '",$opffile,"'!\n";
        exit(EXIT_TOOLSERROR);
    }

    $ebook->print_warnings if($ebook->warnings);
    return 0;
}


=head2 C<genepub>

Generate a .epub book from existing OPF data.

=head3 Options

=over

=item C<--input filename.opf>

=item C<--i filename.opf>

=item C<--opffile filename.opf>

=item C<--opf filename.opf>

Use the specified OPF file.  If no file is specified, one will be
searched for.

=item C<--output bookname.epub>

=item C<-o bookname.epub>

Use the specified name for the final output file.  This can also be
specified as the first non-option argument, which will override this
option if it exists.  If not specified, the book will have the same
filename as the OPF file, with the extension changed to C<.epub>.

=item C<--dir directory>

=item C<-d directory>

Output the final .epub book into the specified directory.  The default
is to use the current working directory.

=back

=head3 Example

 ebook genepub mybook.opf -o my_special_book.epub -d ../epubbooks

or in the simplest case:

 ebook genepub

=cut

sub genepub
{
    my ($outfile) = @_;
    my $ebook;

    if(!$outfile) { $outfile = $opt{output}; }

    if($opt{opffile}) {
        $ebook = EBook::Tools->new($opt{opffile});
    }
    else {
        $ebook = EBook::Tools->new();
        $ebook->init();
    }

    if(! $ebook->gen_epub(filename => $opt{output},
                          dir => $opt{dir}) )
    {
        print {*STDERR} "Failed to generate .epub file!\n";
        $ebook->print_errors;
        $ebook->print_warnings;
        exit(EXIT_BADOUTPUT);
    }
    $ebook->print_warnings if($ebook->warnings);
    return 0;
}


=head2 C<genimp>

Generate a eBookwise .imp book from a .RES directory

=head3 Options

=over

=item C<--input DIRNAME.RES>

=item C<-i DIRNAME.RES>

Specifies the resource directory to use for input.  A valid resource
directory will contain at least a C<RSRC.INF> file, a C<DATA.FRK>
file, and several other files with four-capital-letter filenames.

This can also be specified as the first non-option argument, which
will override this option if it exists.  If not specified, the current
directory will be used.

=item C<--output bookname.epub>

=item C<-o bookname.epub>

Use the specified name for the final output file.  If not specified,
the book will have the same filename as the input, with the extension
changed to C<.imp>.

=back

=head3 Examples

 ebook genimp MyUnpackedBook.RES MyBook.imp
 ebook genimp --resdir ../MyUnpackedBook.RES -f imp/MyBook.imp

=cut

sub genimp
{
    my ($input,$output) = @_;
    my $ebook;
    my $retval;

    $input ||= $opt{input};
    $input ||= '.';
    $output ||= $opt{output};

    if(! $input)
    {
        print {*STDERR} "Resource directory not specified!\n";
        exit(EXIT_BADOPTION);
    }

    if(! -d $input)
    {
        print {*STDERR} "Resource directory '",$input,"' not found!\n";
        exit(EXIT_BADOPTION);
    }

    if(! $output)
    {
        print {*STDERR} "Output file not specified!\n";
        exit(EXIT_BADOPTION);
    }

    my $imp = EBook::Tools::IMP->new();
    if(! $imp->load_resdir($input) )
    {
        print {*STDERR} ("Failed to load from resource directory '",
                         $input,"'!\n");
        exit(EXIT_BADINPUT);
    }
    if(! $imp->write_imp($output) or ! -f $output)
    {
        print {*STDERR} ("Failed to generate '",$output,"'!\n");
        exit(EXIT_BADOUTPUT);
    }
    return 0;
}


=head2 C<genmobi>

Generate a Mobipocket .mobi/.prc book from OPF, HTML, or ePub input.

=head3 Options

=over

=item C<--input filename>

=item C<--i filename>

Use the specified file for input.  Valid formats are OPF, HTML, and
ePub.  This can also be specified as the first non-option argument,
which will override this option if it exists.  If no file is
specified, an OPF file in the current directory will be searched for.

=item C<--output bookname.prc>

=item C<-o bookname.prc>

Use the specified name for the final output file.  If not specified,
the book will have the same filename as the input file, with the
extension changed to C<.mobi> (this file is always created by
C<mobigen>, specifying a different filename only causes it to be
renamed afterwards).

This can also be specified as the second non-option argument, which
will override this option if it exists.

=item C<--dir directory>

=item C<-d directory>

Output the final book into the specified directory.  The default is to
use the current working directory, which is where C<mobigen> will
always place it initially; if specified this only forces the file to
be moved after generation.

=item C<--compression x>

=item C<-c x>

Use the specified compression level C<x>, where 0 is no compression, 1
is PalmDoc compression, and 2 is HUFF/CDIC compression.  If not
specified, defaults to 1 (PalmDoc compression).

=back

=head3 Example

 ebook genmobi mybook.opf -o my_special_book.prc -d ../mobibooks
 ebook genmobi mybook.html mybook.prc -c2

or in the simplest case:

 ebook genmobi

=cut

sub genmobi
{
    my ($infile,$outfile) = @_;
    my $ebook;
    my $retval;

    $infile = $opt{input} if(!$infile);
    $infile = find_opffile() if(!$infile);

    if(!$infile)
    {
        print {*STDERR} "No input file specified or detected!\n";
        exit(EXIT_BADOPTION);
    }

    $outfile = $opt{output} if(!$outfile);

    if(!find_mobigen())
    {
        print {*STDERR} "No mobigen executable available!\n";
        exit(EXIT_MISSINGHELPER);
    }

    $retval = system_mobigen(infile => $infile,
                             outfile => $outfile,
                             dir => $opt{dir},
                             compression => $opt{compression});

    if($retval)
    {
        print {*STDERR} "Error during generation: ",$retval,"\n";
        exit(EXIT_HELPERERROR);
    }
    return 0;
}


=head2 C<genncx>

Given an OPF file, creates a NCX-format table of contents from the
package unique-identifier, the dc:title, dc:creator, and spine
elements, and then add the NCX entry to the manifest if it is not
already referenced.

The OPF file will be cleaned to OPF20 format before this happens.

=head3 Options

=over

=item C<--opffile filename.opf>

=item C<--opf filename.opf>

Use the specified OPF file.  This can also be specified as the first
non-option argument, which will override this option if it exists.  If
no file is specified, one will be searched for.

=back

=cut

sub genncx {
    my ($opffile) = @_;
    my $ebook;

    $opffile = $opt{opffile} if(!$opffile);
    $ebook = EBook::Tools->new();
    $ebook->init($opffile);
    $ebook->fix_opf20;
    $ebook->gen_ncx;
    unless($opt{nosave})
    {
        useoptdir();
        $ebook->save;
    }
    if($ebook->errors)
    {
        $ebook->print_errors;
        print "Unrecoverable errors while fixing '",$opffile,"'!\n";
        exit(EXIT_TOOLSERROR);
    }

    $ebook->print_warnings if($ebook->warnings);
    return 0;
}


=head2 C<impmeta>

Set specific metadata values in an ETI .imp file.

=head3 Options

=over

=item * C<--input filename.imp>

=item * C<-i filename.imp>

Specify the input filename.  This can also be specified as the first
argument, in which case the -i option will be ignored.

=item * C<--output modified.imp>

=item * C<-o modified.imp>

Specify the output filename.  If not specified, the input file will be
overwritten.

=item * C<--identifier>

Specify the identifier metadata.

=item * C<--category>

=item * C<--cat>

Specify the category metadata.

=item * C<--subcategory>

=item * C<--subcat>

Specify the subcategory metadata.

=item * C<--title>

Specify the title metadata.

=item * C<--lastname>

Specify the author last name metadata.

=item * C<--middlename>

Specify the author middle name metadata.

=item * C<--firstname>

Specify the author first name metadata.  Note that IMP files commonly
place the full name in this component, and leave the middlename and
lastname entries blank.

=back

=head3 Examples

 ebook impmeta mybook.imp --title 'Fixed Title' --lastname 'John Q. Brandy'
 ebook impmeta -i mybook.imp -o fixed.imp --title 'Fixed Title'

=cut

sub impmeta
{
    my ($input) = @_;
    $input ||= $opt{input} if($opt{input});
    my $output = $opt{output};

    unless($input)
    {
        print "You must specify an input file.\n";
        exit(EXIT_BADOPTION);
    }
    $output ||= $input;

    my $imp = EBook::Tools::IMP->new();
    if(! $imp->load($input))
    {
        print "Failed to load '",$input,"' -- aborting!\n";
        exit(EXIT_BADINPUT);
    }

    $imp->set_book_properties(
        'identifier'  => $opt{identifier},
        'category'    => $opt{category},
        'subcategory' => $opt{subcategory},
        'title'       => $opt{title},
        'lastname'    => $opt{lastname},
        'middlename'  => $opt{middlename},
        'firstname'   => $opt{firstname}
        );

    if(! $imp->write_imp($output) )
    {
        print "Failed to write '",$output,"' -- aborting!\n";
        exit(EXIT_BADOUTPUT);
    }
    return 0;
}

=head2 C<setcover>

Sets the cover image

Takes as a single argument the href of the file to use.

=head3 Options

=over

=item * C<--opffile>

=item * C<--opf>

Specifies the OPF file to modify.  If not specified, the script will
attempt to find one

=item * C<--identifier>

=item * C<--id>

Specifies the ID to assign to the associated manifest item.

=back

=cut

sub setcover {
    my ($href) = @_;

    unless($href) {
        print "You must specify the href (filename) of the cover image!\n";
        exit(EXIT_BADOPTION);
    }

    my $opffile = $opt{opffile};
    my $id = $opt{identifier};

    my $ebook = EBook::Tools->new();
    $ebook->init($opffile);
    $ebook->set_cover(href => $href,
                      id => $id);
    $ebook->save;
    $ebook->print_errors;
    $ebook->print_warnings;
    return 0;
}


=head2 C<setmeta>

Set specific metadata values on an OPF file, creating a new entry if
none exists.

Both the element to set and the value are specified as additional
arguments, not as options.

The elements that can be set are currently:

=over

=item author

=item date

=item description

=item publisher

=item rights

=item series

=item subject

=item title

=item type

=back

The 'series' values can take an extra argument containing the series
index position.

The 'subject' elements can be added multiple times (including in a
single command-line, though --id will only set the ID on the first one
specified).  Other entries will be overwritten.

The element argument can be shortened to the minimum number of letters
necessary to uniquely identify it.

=head3 Options

=over

=item * C<--opffile>
=item * C<--opf>

Specifies the OPF file to modify.  If not specified, the script will
attempt to find one in the current directory.

=item * C<--delete>

Allows the deletion of subject and series metadata.  Has no effect on other elements.

=item * C<--fileas>

Specifies the 'file-as' attribute when setting an author.  Has no
effect on other elements.

=item * C<--identifier>

=item * C<--id>

Specifies the ID to assign to the element.

=back

=head3 Examples

 ebook setmeta series 'Some Other Series' 03
 ebook setmeta title 'My Great Title'
 ebook setmeta t 'My Great Title'
 ebook --opf newfile.opf setmeta author 'John Smith' --fileas 'Smith, John' --id mainauthor

=cut

sub setmeta
{
    my ($element,$value,@extra) = @_;

    unless($element)
    {
        print "You must specify which element to set.\n";
        print "Example: ebook setmeta title 'My Great Title'\n";
        exit(EXIT_BADOPTION);
    }
    $value ||= '';

    my $opffile = $opt{opffile};
    my $fileas = $opt{fileas};
    my $id = $opt{identifier};

    my $ebook = EBook::Tools->new();
    $ebook->init($opffile);

    if ($element =~ /^a/) {
        $ebook->set_primary_author('text' => $value,
                                   'fileas' => $fileas,
                                   'id' => $id );
    }
    elsif ($element =~ /^da/) {
        $value = fix_datestring($value);
        $ebook->set_date('text' => $value,
                         'id' => $id);
    }
    elsif ($element =~ /^de/) {
        $ebook->set_description('text' => $value,
                                'id' => $id);
    }
    elsif ($element =~ /^p/) {
        $ebook->set_publisher('text' => $value,
                              'id' => $id);
    }
    elsif ($element =~ /^r/) {
        $ebook->set_rights('text' => $value,
                           'id' => $id);
    }
    elsif ($element =~ /^se/) {
        # Unfortunately, there are no readers as of this writing
        # that support the EPub 3.0 collection standard, so the
        # only way to currently designate series is via the
        # calibre:series and calibre:series_index meta tags.

        if($opt{delete}) {
            $ebook->set_meta(name => 'calibre:series',
                             content => undef);
            $ebook->set_meta(name => 'calibre:series_index',
                             content => undef);
        }
        else {
            $ebook->set_meta(name => 'calibre:series',
                             content => $value);
            if($extra[0]) {
                $ebook->set_meta(name => 'calibre:series_index',
                                 content => $extra[0]);
            }
        }
    }
    elsif ($element =~ /^su/) {
        if($opt{delete}) {
            $ebook->delete_subject('text' => $value,
                                   'id' => $id);
            foreach my $subject(@extra) {
                $ebook->delete_subject('text' => $subject);
            }
        }
        else {
            $ebook->add_subject('text' => $value,
                                'id' => $id);
            foreach my $subject(@extra) {
                $ebook->add_subject('text' => $subject);
            }
        }
    }
    elsif ($element =~ /^ti/) {
        $ebook->set_title('text' => $value,
                          'id' => $id);
    }
    elsif ($element =~ /^ty/) {
        $ebook->set_type('text' => $value,
                         'id' => $id);
    }
    else {
        print "Unrecognized metadata element '",$element,"'!\n";
        exit(EXIT_BADOPTION);
    }

    $ebook->save;
    $ebook->print_errors;
    $ebook->print_warnings;
    return 0;
}


=head2 C<splitmeta>

Split the <metadata>...</metadata> block out of a pseudo-HTML file
that contains one.

=cut

sub splitmeta
{
    my ($infile,$opffile) = @_;
    if(!$infile) { die("You must specify a file to parse.\n"); }
    $opffile = $opt{opffile} if(!$opffile);

    my $ebook;

    $opffile = split_metadata($infile,$opffile);

    if(!$opffile)
    {
        print {*STDERR} "No metadata block was found in '",$infile,"'\n";
        exit(EXIT_BADINPUT);
    }

    $ebook = EBook::Tools->new($opffile);
    $ebook->fix_oeb12 if($opt{oeb12});
    $ebook->fix_opf20 if($opt{opf20});
    $ebook->fix_mobi if($opt{mobi});

    # The split metadata never includes manifest/spine info, so add in the
    # HTML file now
    $ebook->add_document($infile,'item-maintext');
    $ebook->fix_misc;
    $ebook->save;

    if($ebook->errors)
    {
        $ebook->print_errors;
        $ebook->print_warnings if($ebook->warnings);
        exit(EXIT_TOOLSERROR);
    }
    $ebook->print_warnings if($ebook->warnings);
    return 0;
}


=head2 C<splitpre>

Split <pre>...</pre> blocks out of an existing HTML file, wrapping
each one found into a separate HTML file.

The first non-option argument is taken to be the input file.  The
second non-option argument is taken to be the basename of the output
files.

=cut

sub splitpre
{
    my ($infile,$outfilebase) = @_;
    if(!$infile)
    {
        print {*STDERR} "You must specify a file to parse.\n";
        exit(EXIT_BADOPTION);
    }
    split_pre($infile,$outfilebase);
    return 0;
}


=head2 C<stripscript>

Strips <script>...</script> blocks out of a HTML file.

The first non-option argument is taken to be the input file.  The
second non-option argument is taken to be the output file.  If the
latter is not specified, the input file will be overwritten.

=head3 Options

=over

=item * C<--noscript>

Strips <noscript>...</noscript> blocks as well.

=back

=cut

sub stripscript
{
    my ($infile,$outfile) = @_;

    if(!$infile)
    {
        print "You must specify an input file.\n";
        exit(EXIT_BADOPTION);
    }
    my %args;
    $args{infile} = $infile;
    $args{outfile} = $outfile;
    $args{noscript} = $opt{noscript};

    strip_script(%args);
    return 0;
}

=head2 C<tidyxhtml>

Run tidy on a HTML file to enforce valid XHTML output (required by the
OPF 2.0 specification).

=cut

sub tidyxhtml
{
    my ($inputfile,$tidyfile) = @_;
    my $retval;

    if(!$inputfile)
    {
        print "You must specify an input file to tidy.\n";
        exit(EXIT_BADOPTION);
    }

    if($tidyfile) { $retval = system_tidy_xhtml($inputfile,$tidyfile); }
    else { $retval = system_tidy_xhtml($inputfile); }
    exit($retval);
}


=head2 C<tidyxml>

Run tidy an a XML file (for neatness).

=cut

sub tidyxml
{
    my ($inputfile,$tidyfile) = @_;
    my $retval;

    if(!$inputfile)
    {
        print "You must specify an input file to tidy.\n";
        exit(EXIT_BADOPTION);
    }

    if($tidyfile) { $retval = system_tidy_xml($inputfile,$tidyfile); }
    else { $retval = system_tidy_xml($inputfile); }
    exit($retval);
}


=head2 C<unpack_ebook>

Unpacks an ebook into its component parts, creating an OPF for them if
necessary.

=head3 Options

=over

=item C<--input>
=item C<-i>

The filename of the ebook to unpack.  This can also be specified as
the first non-option argument, in which case it will override the
option if it exists.

=item C<--dir>
=item C<-d>

The directory to unpack into, which will be created if it does not
exist, defaulting to the filename with the extension removed.  This
can also be specified as the second non-option argument, in which case
it will override the option if it exists.

=item C<--format>

The unpacking routines should autodetect the type of book under normal
conditions.  If autodetection fails, a format can be forced here.  See
L<EBook::Tools::Unpack> for a list of available formats.

=item C<--htmlconvert>

Attempt to convert the extracted text to HTML.  This is obviously only
of value if the format doesn't use HTML normally.

=item C<--raw>

This causes a lot of raw, unparsed, unmodified data to be dumped into
the directory along with everything else.  It's useful for debugging
exactly what was in the file being unpacked, but not for much else.

=item C<--author>

Set the primary author of the unpacked e-book, overriding what is
detected.  Not all e-book formats contain author metadata, and if none
is found and this is not specified the primary author will be set to
'Unknown Author'.

=item C<--title>

Set the title of the unpacked e-book, overriding what is detected.  A
title will always be detected in some form from the e-book, but the
exact text can be overridden here.

=item C<--opffile>

=item C<--opf>

The filename of the OPF metadata file that will be generated.  If not
specified, defaults to C<content.opf>.

=item C<--tidy>

Run tidy on any HTML output files to convert them to valid XHTML.  Be
warned that this can occasionally change the formatting, as Tidy isn't
very forgiving on certain common tricks (such as empty <pre> elements
with style elements) that abuse the standard.

=item C<--tidycmd>

The tidy executable name.  This has to be a fully qualified pathname
if tidy isn't on the path.  Defaults to 'tidy'.

=item C<--tidysafety>

The safety level to use when running tidy (default is 1).  Potential
values are:

=item C<$tidysafety < 1>:

No checks performed, no error files kept, works like a clean tidy -m

This setting is DANGEROUS!

=item C<$tidysafety == 1>:

Overwrites original file if there were no errors, but even if there
were warnings.  Keeps a log of errors, but not warnings.

=item C<$tidysafety == 2>:

Overwrites original file if there were no errors, but even if there
were warnings.  Keeps a log of both errors and warnings.

=item C<$tidysafety == 3>:

Overwrites original file only if there were no errors or warnings.
Keeps a log of both errors and warnings.

=item C<$tidysafety >= 4>:

Never overwrites original file.  Keeps a log of both errors and
warnings.

=back

=head3 Examples

 ebook unpack mybook.pdb My_Book --author "By Me"
 ebook unpack -i mybook.pdb -d My_Book --author "By Me"

Both of the above commands do the same thing

=cut

sub unpack_ebook
{
    my ($filename,$dir) = @_;
    $filename = $filename || $opt{input};
    $dir = $dir || $opt{dir};

    unless($filename)
    {
        print {*STDERR} "You must specify a file to unpack!\n";
        exit(EXIT_BADOPTION);
    }

    unless(-f $filename)
    {
        print {*STDERR} "Could not find '",$filename,"' to unpack!\n";
        exit(EXIT_BADINPUT);
    }

    if( $opt{key} && ! pid_is_valid($opt{key}) )
    {
        print {*STDERR} "Invalid PID '",$opt{key},"'\n";
        exit(EXIT_BADOPTION);
    }

    my $unpacker = EBook::Tools::Unpack->new(
        'file' => $filename,
        'dir' => $dir,
        'format' => $opt{format},
        'htmlconvert' => $opt{htmlconvert},
        'raw' => $opt{raw},
        'author' => $opt{author},
        'title' => $opt{title},
        'opffile' => $opt{opffile},
        'tidy' => $opt{tidy},
        'nosave' => $opt{nosave},
        );

    $unpacker->unpack;

    return 0;
}

########## PRIVATE PROCEDURES ##########

sub useoptdir
{
    if($opt{dir})
    {
        if(! -d $opt{dir})
        {
            mkpath($opt{dir})
                or die("Unable to create working directory '",$opt{dir},"'!");
        }
        chdir($opt{dir})
            or die("Unable to chdir to working directory '",$opt{dir},"'!");
    }
    return 1;
}

########## END CODE ##########

=head1 EXAMPLES

 ebook splitmeta book.html mybook.opf
 ebook tidyxhtml book.html
 ebook tidyxml mybook.opf
 ebook fix mybook.opf --oeb12 --mobi
 ebook genepub

 ebook blank newbook.opf --title "My Title" --author "My Name"
 ebook adddoc myfile.html
 ebook fix newbook.opf --opf20 -v
 ebook genepub

 ebook unpack mybook.pdb my_book
 cd my_book
 ebook addoc new_document.html
 ebook fix
 ebook genepub

=head1 BUGS/TODO

=over

=item * Need to implement a one-pass conversion from one format to
another.  This will wait until more formats are supported by the
underlying modules, however.

=item * Documentation is incomplete

=item * Not all configuration file options are actually used

=back

=head1 COPYRIGHT

Copyright 2008 Zed Pobre

=head1 LICENSE

Licensed to the public under the terms of the GNU GPL, version 2.

=cut


########## DATA ##########

__DATA__
#
# config.ini
#
# Configuration file for EBook-Tools
#
#

# The [config] section holds general configuration values for
# EBook::Tools.
[config]
#
# debug sets the default debugging level if no verbosity is specified
# Note that this can only be raised, not lowered, from the command line.
debug=0
#
# tidysafety sets the safety level when running system_tidy_xhtml or
# system_tidy_xml.  See the EBook::Tools documentation for possible
# values and what they mean.
tidysafety=1

# The [drm] section holds user-specific information needed to decrypt,
# encrypt, or inscribe protected e-books.  Additional plug-in modules
# or helper applications may be needed for some of these values to
# have any effect.
[drm]
#
# ereaderkeys is a comma-separated list of EReader decryption keys to
# try, in the order that they will be tried.
ereaderkeys=
#
# litkeyfile marks the full path and filename to the keys.txt file
# needed by convertlit to downconvert or unpack MS Reader files.  If
# not specified, a keys.txt file will be searched for in the
# configuration directory and the current working directory.
litkeyfile=
#
# mobpids is a comma-separated list of Mobipocket/Kindle PIDs to try,
# in the order that they will be tried.
mobipids=
#
# username is the full name that will be used when decrypting EReader
# books and when inscribing MS Reader .lit files
username=

# The [helpers] section holds the locations of helper files, including
# the complete path.  If not specified here, they will be searched
# for in the configuration directory and other likely locations.
[helpers]
convertlit=
mobidedrm=
mobigen=
pdbshred=
tidy=