This file is indexed.

/usr/bin/chronicle is in chronicle 4.6-2.

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
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
#!/usr/bin/perl -w

=head1 NAME

chronicle - A simple blog compiler.

=cut

=head1 SYNOPSIS

  chronicle [options]

  Path Options:

   --comments       Specify the path to the optional comments directory.
   --config         Specify a configuration file to read.
   --input          Specify the input directory to use.
   --output         Specify the directory to write output to.
   --theme-dir      Specify the path to the theme templates.
   --theme          Specify the theme to use.
   --pattern        Specify the pattern of files to work with.
   --url-prefix     Specify the prefix to the live blog.
   --sitemap-prefix Specify the prefix for the site map.

  Blog Entry Options:

   --format      Specify the format of your entries, HTML/textile/markdown.

  Pre & Post-Build Commands:

   --pre-build    Specify a command to execute prior to building the blog.
   --post-build   Specify a command to execute once the blog has been built.
   --pre-filter   A command to filter each blog entry before HTML conversion.
   --post-filter  A command to filter each blog entry after HTML conversion.


  Sorting Options:

   --recent-dates-first  Show recent entries first in the archive view.
   --recent-tags-first   Show recent entries first in the tag view.

  Counting Options:

   --entry-count=N       Number of posts to show on the index.
   --rss-count=N         Number of posts to include on the RSS index feed.

  Optional Features:

   --author       Specify the author's email address
   --comment-days Specify the number maximum age of posts to accept comments.
   --date-archive-path  Include the date in the archive.
   --force        Force the copying of static files from the blog theme.
   --lang         Specify the language to use for formatting dates.
   --lower-case   Lower-case all filenames which are output.
   --no-archive   Don't create an archive page.
   --no-cache     Don't use the optional memcached features, even if available.
   --no-calendar  Don't use the optional calendar upon the index.
   --no-comments  Don't allow comments to be posted.
   --no-sitemap   Don't generate a sitemap.
   --no-tags      Don't produce any tag pages.
   --no-xrefs     Don't produce any cross references.

  Help Options:

   --help        Show the help information for this script.
   --manual      Read the manual for this script.
   --verbose     Show useful debugging information.
   --version     Show the version number and exit.

=cut


=head1 ABOUT

  Chronicle is a simple tool to convert a collection of text files,
 located within a single directory, into a blog consisting of static
 HTML files.

  It supports only the bare minimum of features which are required
 to be useful:

   * Tagging support.

   * RSS support.

   * Archive support.

  The obvious deficiencies are:

   * Lack of support for instant commenting.

   * Lack of pingback/trackback support.

  Having said that it is a robust, stable, and useful system.

=cut


=head1 BLOG FORMAT

  The format of the text files we process is critical to the output
 pages.  Each entry should look something like this:

=for example begin

    Title:  This is the title of the blog post
    Date:  2nd March 2007
    Tags: one, two, three, long tag

    The text of your entry goes here.

=for example end

  NOTE: The header MUST be separated from the body of the entry by at least a
 single empty line.

  In this example we can see that the entry itself has been prefaced
 with a small header.  An entry header is contains three optional lines,
 if these are not present then there are sensible defaults as described
 below.

   The formatting of the output dates may be changed via the use of the
 B<--lang> command line option (or the matching "lang=french" option in the
 configuration file), but the date of the entry itself should be specified in
 English.

=over 8

=item Title:
Describes the title of the post.  If not present the filename of the entry
is used instead.  "Subject:" may also be used.

=item Subject:
This is a synonym for 'Title:'.

=item Date:
The date the post was written.  If not present the creation time of the
file is used instead.

=item Publish:
This header is removed from all entries, and is used by the chronicle-spooler script.

=item Tags:
Any tags which should be associated with the entry, separated by commas.

=back

  The format of the entry is assumed to be HTML, however there is support
 for writing your entries in both textile and markdown formats.

  The format of entries is specified via the B<--format> argument, or
 via a "format: foo" setting in your chroniclerc file.

  The format of entries is assumed to be global; that is all your entries
 will be assumed to be in the same format.  However you can add a
 "format: foo" pseudo header to specific entries if you wish to write
 specific entries in a different format.

  To allow flexibility in the handling of entries each blog entry will
 be passed through the filter script B<chronicle-entry-filter>
 which allows you to modify this handling in a single location. This
 script allows entries to be updated via filters both before and after
 the conversion to HTML.  For further details please see the manpage for
 that script.

=cut


=head1 CONFIGURATION

  The configuration of the software is minimal, and generally performed
 via the command line arguments.  However it is possible to save settings
 either in the file global /etc/chroniclerc or the per-user ~/.chroniclerc
 file.

  If you wish you may pass the name of another configuration file to
 the script with the B<--config> flag.  This will be read after the
 previous two files, and may override any settings which are present.

  The configuration file contains lines like these:

=for example begin

   input  = /home/me/blog

   output = /var/www/blog

   format = markdown

=for example end

  Keys which are unknown are ignored.

=cut


=head1 OPTIONAL CACHING

  To speed the rebuilding of a large blog the compiler may use a local
 Memcached daemon, if installed and available.

  To install this, under a Debian GNU/Linux system please run:

=for example begin

    apt-get update
    apt-get install memcached libcache-memcached-perl

=for example end

  You may disable this caching behaviour with --no-cache, and see the
 effect with --verbose.

=cut

=head1 OPTIONAL CALENDAR

  If the 'HTML::CalendarMonthSimple' module is available each blog will
 contain a simple month-view of the current month upon the index.

  To disable this invoke the program with '--no-calendar'.

=cut


=head1 OPTIONAL COMMENTING

  Included with the chronicle code you should find the file
 cgi-bin/comments.cgi.

  This file is designed to write submitted comments to the local
 filesystem of your web-server.  If you install that, and edit the
 path at the start of the script you should be able to include
 comments in your blog.

  In short there are three things you need to do:

=over 8

=item  Install the CGI script and edit the path at the start.

=item  Copy the output comments to your local blog source.

=item  Run this script again with --comments=./path/to/comments

=back

  This should include the comments in the static output.  More
 explicit instructions are provided within the file 'COMMENTS'
 included within the distribution.

=cut

=head1 AUTHOR

 Steve
 --
 http://www.steve.org.uk/

=cut

=head1 LICENSE

Copyright (c) 2007-2010 by Steve Kemp.  All rights reserved.

This module is free software;
you can redistribute it and/or modify it under
the same terms as Perl itself.
The LICENSE file contains the full text of the license.

=cut


use strict;
use warnings;


use Date::Format;
use Date::Language;
use Date::Parse;
use Digest::MD5 qw(md5_hex);
use Encode;
use File::Basename;
use File::Copy;
use File::Path;
use Getopt::Long;
use HTML::Template;
use Pod::Usage;
use Time::Local;


#
#  Release number
#
#  NOTE:  Set by 'make release'.
#
my $RELEASE = '4.6';

#
#  Setup default options.
#
my %CONFIG = setupDefaultOptions();


#
#  Know when we are running, so that we can tell the difference between
# cached data and data this process generated.
#
my $RUNNING_TIME = time();



#
#  Read the global and per-user configuration files, if they exist.
#
readConfigurationFile("/etc/chroniclerc");
readConfigurationFile( $ENV{ 'HOME' } . "/.chroniclerc" );


#
#  Parse the command line arguments.
#
parseCommandLineArguments();


#
#  Another configuration file?
#
readConfigurationFile( $CONFIG{ 'config' } ) if ( defined $CONFIG{ 'config' } );


#
#  Make sure we have arguments which are sane.
#
#  Specifically we want to cope with the "new", "theme-dir", and "theme"
# arguments.
#
#
sanityCheckArguments();


#
#  Setup the cache if we can.
#
setupCache() unless ( $CONFIG{ 'no-cache' } );


#
#  The names of the months we'll use.
#
my @names = getMonthNames();


#
#  Listing themes?
#
if ( $CONFIG{ 'list-themes' } )
{
    listThemes( $CONFIG{ 'theme-dir' } );
    exit;
}

#
#  Should we run something before we start?
#
if ( $CONFIG{ 'pre-build' } )
{
    $CONFIG{ 'verbose' } && print "Running command: $CONFIG{'pre-build'}\n";

    system( $CONFIG{ 'pre-build' } );
}


#
#  Parse each of the given text files, and build up a data-structure
# we can use to create our pages.
#
#  The data-structure is a hash of arrays.  The hash key is the blog
# entry's filename, and the array stored as the hash's value has
# keys such as:
#
#   tags  => [ 'test', 'testing' ]
#   date  => '1st july 2007'
#   title => 'Some title'
#
#
my %data = createDataStructure();

#
#  Find all cross references and build a hash of them.  This hash
# contains arrays of filenames that the key is linked by.  i.e. each
# filename in the array refers to the key filename.
#
my %all_xrefs;
%all_xrefs = findAllXRefs() unless ( $CONFIG{ 'no-xrefs' } );

#
#  Include cross-reference data, unless disabled
#
if ( !$CONFIG{ 'no-xrefs' } )
{
    buildXRefPairs();
}

#
#  Find each unique tag used within our entries.
#
my %all_tags;
%all_tags = findAllTags() unless ( $CONFIG{ 'no-tags' } );

#
#  Find each unique month + year we've used.
#
my %all_dates;
%all_dates = findAllMonths() unless ( $CONFIG{ 'no-archive' } );


#
#  Now create the global tag + date loops which are used for our
# sidebar.
#
my %CLOUD;
$CLOUD{ 'tag' } = createTagCloud(%all_tags) unless ( $CONFIG{ 'no-tags' } );
$CLOUD{ 'archive' } = createDateCloud(%all_dates)
  unless ( $CONFIG{ 'no-archive' } );



#
#  Create the output directories.
#
mkpath( $CONFIG{ 'output' }, 0, oct(755) ) if ( !-d $CONFIG{ 'output' } );
foreach my $tag ( keys %all_tags )
{
    mkpath( "$CONFIG{'output'}/tags/$tag", 0, oct(755) );
}
foreach my $date ( keys %all_dates )
{
    next unless ( $date =~ /^([0-9]{4})-([0-9]{2})/ );
    mkpath( "$CONFIG{'output'}/archive/$1/$2", 0, oct(755) );
}


#
#  Output each static page.
#
$CONFIG{ 'verbose' } && print "Creating static pages:\n";
foreach my $file ( keys %data )
{
    outputStaticPage($file);
}


#
#  Build an output page for every tag which has ever been used.
#
foreach my $tagName ( sort keys %all_tags )
{
    outputTagPage($tagName);
}



#
#  Now build the archive pages.
#
foreach my $date ( keys(%all_dates) )
{
    outputArchivePage($date);
}



#
#  Finally out the most recent entries for the front-page.
#
outputIndexPage();



#
#  Copy any static files into place.
#
copyStaticFiles();


#
#  Output the static sitemap unless:
#
if ( !$CONFIG{ 'no-sitemap' } )
{
    $CONFIG{ 'verbose' } && print "Outputing sitemap\n";
    outputSiteMap();
    $CONFIG{ 'verbose' } && print "Done.\n";
}

#
#  Post-build command?
#
if ( $CONFIG{ 'post-build' } )
{
    $CONFIG{ 'verbose' } && print "Running command: $CONFIG{'post-build'}\n";

    system( $CONFIG{ 'post-build' } );
}


#
#  Sending a ping?
#
if ( $CONFIG{ 'send-ping' } )
{
    $CONFIG{ 'verbose' } && print "Preparing to send pings\n";

    #
    #  Do we have a list of sites to send to?
    #
    my $count = 1;
    my $sent  = 0;

    while ( defined $CONFIG{ 'ping-server-$count' } )
    {
        my $ping = $CONFIG{ 'ping-server-$count' };

        $CONFIG{ 'verbose' } && print "Sending to: $ping\n";

        #
        # Send to just that specific service
        #
        system( "chronicle-ping",          "--service=$ping",
                "--url=$CONFIG{'prefix'}", "--name=$CONFIG{'blog-title'}" );

        # find next server
        $count += 1;

        # we've sent something now.
        $sent += 1;
    }

    #
    #  If we didn't send then we're going to use the default servers.
    #
    if ( !$sent )
    {
        $CONFIG{ 'verbose' } && print "Sending to: default ping servers\n";

        system( "chronicle-ping", "--url=$CONFIG{'prefix'}",
                "--name=$CONFIG{'blog-title'}" );
    }

    $CONFIG{ 'verbose' } && print "Pings complete.\n";
}



#
#  All done.
#
exit;



=begin doc

  Setup the default options we'd expect into our global configuration hash.

=end doc

=cut

sub setupDefaultOptions
{
    my %CONFIG;

    #
    #  Text directory.
    #
    $CONFIG{ 'input' }    = "./blog";
    $CONFIG{ 'comments' } = '';

    #
    #  Output directory.
    #
    $CONFIG{ 'output' } = "./output";

    #
    #  Theme setup
    #
    $CONFIG{ 'theme-dir' } = "./themes/";
    $CONFIG{ 'theme' }     = "default";

    #
    #  prefix for all links.
    #
    $CONFIG{ 'url-prefix' } = "";

    #
    #  Default input format
    #
    $CONFIG{ 'format' } = 'html';

    #
    #  Entries per-page for the index.
    #
    $CONFIG{ 'entry-count' } = 10;

    #
    #  Entries for the RSS index count.
    #
    $CONFIG{ 'rss-count' } = 10;

    #
    #  Don't overwrite files by default
    #
    $CONFIG{ 'force' } = 0;

    #
    #  UTF-8 for textile
    #
    $CONFIG{ 'charset' } = 'utf-8';

    #
    #  Comments enabled globally.
    #
    $CONFIG{ 'comment-days' } = 0;

    #
    #  No output prefix -- static pages in top level dir
    #
    $CONFIG{ 'date-archive-path' } = 0;

    #
    #  Default to making a sitemap
    #
    $CONFIG{ 'no-sitemap' } = 0;

    #
    #  English language by default
    #
    $CONFIG{ 'lang' } = 'english';

    #
    # English date format by default
    #
    $CONFIG{ 'date-format' } = '%o %B %Y';

    return (%CONFIG);
}



=begin doc

  Parse the command line arguments this script was given.

=end doc

=cut

sub parseCommandLineArguments
{
    my $HELP    = 0;
    my $MANUAL  = 0;
    my $VERSION = 0;

    #
    #  Parse options.
    #
    if (
        !GetOptions(

            # Help options
            "help",        \$HELP,
            "manual",      \$MANUAL,
            "verbose",     \$CONFIG{ 'verbose' },
            "version",     \$VERSION,
            "list-themes", \$CONFIG{ 'list-themes' },

            # paths
            "comments=s",  \$CONFIG{ 'comments' },
            "config=s",    \$CONFIG{ 'config' },
            "input=s",     \$CONFIG{ 'input' },
            "output=s",    \$CONFIG{ 'output' },
            "theme-dir=s", \$CONFIG{ 'theme-dir' },
            "theme=s",     \$CONFIG{ 'theme' },
            "pattern=s",   \$CONFIG{ 'pattern' },

            # limits
            "entry-count=s", \$CONFIG{ 'entry-count' },
            "rss-count=s",   \$CONFIG{ 'rss-count' },

            # output
            "date-archive-path", \$CONFIG{ 'date-archive-path' },

            # optional
            "author=s",       \$CONFIG{ 'author' },
            "lang=s",         \$CONFIG{ 'lang' },
            "force",          \$CONFIG{ 'force' },
            "no-xrefs",       \$CONFIG{ 'no-xrefs' },
            "no-tags",        \$CONFIG{ 'no-tags' },
            "no-cache",       \$CONFIG{ 'no-cache' },
            "no-calendar",    \$CONFIG{ 'no-calendar' },
            "no-sitemap",     \$CONFIG{ 'no-sitemap' },
            "no-comments",    \$CONFIG{ 'no-comments' },
            "no-archive",     \$CONFIG{ 'no-archive' },
            "lower-case",     \$CONFIG{ 'lower-case' },
            "comment-days=s", \$CONFIG{ 'comment-days' },

            # sorting
            "recent-tags-first",  \$CONFIG{ 'recent-tags-first' },
            "recent-dates-first", \$CONFIG{ 'recent-dates-first' },

            # input format.
            "format=s",  \$CONFIG{ 'format' },
            "charset=s", \$CONFIG{ 'charset' },

            # prefix
            "url-prefix=s",     \$CONFIG{ 'url_prefix' },
            "sitemap-prefix=s", \$CONFIG{ 'sitemap_prefix' },

            # commands
            "pre-build=s",   \$CONFIG{ 'pre-build' },
            "post-build=s",  \$CONFIG{ 'post-build' },
            "pre-filter=s",  \$CONFIG{ 'pre-filter' },
            "post-filter=s", \$CONFIG{ 'post-filter' },
            "send-ping",     \$CONFIG{ 'send-ping' },

            # title
            "blog-title=s",    \$CONFIG{ 'blog_title' },
            "blog-subtitle=s", \$CONFIG{ 'blog_subtitle' },


        ) )
    {
        exit;
    }

    pod2usage(1) if $HELP;
    pod2usage( -verbose => 2 ) if $MANUAL;

    if ($VERSION)
    {
        print("chronicle release $RELEASE\n");
        exit;
    }
}



=begin doc

  Create our global data-structure, by reading each of the blog
 files and extracting:

  1.  The title of the entry.

  2.  Any tags which might be present.

  3.  The date upon which it was made.

=end doc

=cut

sub createDataStructure
{
    my %results;

    #
    #  Did the user override the default pattern?
    #
    my $pattern = $CONFIG{ 'pattern' } || "*";



    #
    #  Find the filenames.
    #
    foreach my $file ( sort( glob( $CONFIG{ 'input' } . "/" . $pattern ) ) )
    {

        #
        #  Ignore directories.
        #
        next if ( -d $file );

        #
        #  Read the entry and store all the data away as a
        # hash element keyed upon the (unique) filename.
        #
        my $result = readBlogEntry($file);
        $results{ $file } = $result if ($result);

    }

    #
    #  Make sure we found some entries.
    #
    if ( scalar( keys(%results) ) < 1 )
    {
        print <<EOF;

  There were no files found in the input directory $CONFIG{'input'}
 which matched the pattern '$pattern'.

  Aborting.

EOF
        exit;
    }

    #
    #  Make sure we have no duplicate titles.
    #
    my %titles;
    foreach my $key ( keys(%results) )
    {

        #
        #  Get the title for this file.
        #
        my $title = $results{ $key }->{ 'title' };
        next if ( !defined($title) );

        #
        #  If we've seen this already then abort
        #
        if ( $titles{ $title } )
        {
            print "Non-unique title: $title\n";
            print "Shared by (at least):\n";
            print "\t" . $key . "\n";
            print "\t" . $titles{ $title } . "\n";
            print "\nAborting\n";
            exit;
        }
        else
        {
            $titles{ $title } = $key;
        }
    }

    return %results;
}


=begin doc

  For each src file in %data that has references in %all_xrefs,
 create a new key in %data called 'xrefpairs' that contains an array
 of hashes: { xreftitle => ref->title, xreflink => ref->link }

  Finally, update the 'running_time' on each referenced page if
 the referencing page is itself dirty.

=end doc

=cut

sub buildXRefPairs
{
    if ( !$CONFIG{ 'no-xrefs' } )
    {
        my $xrefs;
        foreach my $file ( keys %data )
        {
            my $hash = $data{ $file };
            my $refpair;

            if ( $all_xrefs{ $file } )
            {
                $xrefs = undef;

                #
                #  Add array of hashes of links and titles to each file
                # that has references pointing to it.
                #
                foreach my $ref ( @{ $all_xrefs{ $file } } )
                {
                    $refpair                  = undef;
                    $refpair->{ 'xreftitle' } = $data{ $ref }->{ 'title' };
                    $refpair->{ 'xreflink' }  = $data{ $ref }->{ 'link' };

                    push( @$xrefs, $refpair );

                    #
                    #  If referencing page is dirty, so must all
                    # the referenced pages be.
                    #
                    if ( $data{ $ref }->{ 'running_time' } == $RUNNING_TIME )
                    {
                        $hash->{ 'running_time' } = $RUNNING_TIME;
                    }
                }
                $hash->{ 'xrefpairs' } = $xrefs;
            }
        }
    }
}


=begin doc

  Build a cross reference hash... for each key of the
 referenced filename, and an array of files that reference it.

=end doc

=cut

sub findAllXRefs
{
    my %allRefs;

    foreach my $file ( keys %data )
    {
        my $hash = $data{ $file };
        my $xrefs = $hash->{ 'xrefs' } || undef;
        foreach my $x (@$xrefs)
        {
            push( @{ $allRefs{ $x } }, $file );
        }
    }

    return (%allRefs);
}


=begin doc

  Find each distinct tag which has been used within blog entries,
 and the number of times each one has been used.

=end doc

=cut

sub findAllTags
{
    my %allTags;

    foreach my $file ( keys %data )
    {
        my $hash = $data{ $file };
        my $tags = $hash->{ 'tags' } || undef;
        foreach my $t (@$tags)
        {
            if ( $t->{ 'tag' } )
            {
                $allTags{ $t->{ 'tag' } } += 1;
            }
        }
    }

    return (%allTags);
}



=begin doc

  Create a structure for a tag cloud.

=end doc

=cut

sub createTagCloud
{
    my (%unique) = (@_);

    my $results;

    foreach my $key ( sort keys(%unique) )
    {

        # count.
        my $count = $unique{ $key };

        # size for the HTML.
        my $size = 10 + ( $count * 5 );
        $size = 40 if ( $size >= 40 );

        push( @$results,
              {  tag   => $key,
                 count => $count,
                 size  => $size
              } );
    }
    return $results;

}



=begin doc

  Find each of the distinct Month + Year pairs for entries which
 have been created.

=end doc

=cut

sub findAllMonths
{
    my %allDates;

    foreach my $file ( keys %data )
    {
        my $hash = $data{ $file };
        next if ( !$hash );

        my $date = $hash->{ 'date' };

        #
        #  Not a date?  Use the ctime of the file.
        #
        if ( !defined($date) || !length($date) )
        {

            #
            # Test the file for creation time.
            #
            my ( $dev,   $ino,     $mode, $nlink, $uid,
                 $gid,   $rdev,    $size, $atime, $mtime,
                 $ctime, $blksize, $blocks
               ) = stat($file);

            $date = localtime($ctime);
        }

        $date = time2str( "%Y-%m", str2time($date) );

        $allDates{ $date } += 1;
    }

    return (%allDates);
}



=begin doc

  Create a data structure which can be used for our archive layout.

  This is a little messy.  Mostly because we want to have a nested
 loop so that we can place our entries in a nice manner.

=end doc

=cut

sub createDateCloud
{
    my (%entry_dates) = (@_);

    my $results;
    my $year;
    my $months;

    foreach my $date ( sort keys %entry_dates )
    {
        next unless ( $date =~ /^([0-9]{4})-([0-9]{2})/ );

        if ( $year and $1 ne $year )
        {
            push( @$results,
                  {  year   => $year,
                     months => $months
                  } );
            undef $months;
        }
        $year = $1;

        push( @$months,
              {  month      => $2,
                 month_name => $names[$2 - 1],
                 count      => $entry_dates{ $date } } );

    }

    push( @$results,
          {  year   => $year,
             months => $months
          } );

    #
    #  Make sure this is sorted by reverse chronological order.
    #
    my @sorted = sort {$b->{ 'year' } <=> $a->{ 'year' }} @$results;
    return \@sorted;
}



=begin doc

  Sort by date.

=end doc

=cut

sub bywhen
{

    #
    #  Parse and return the date
    #
    my ( $ss1, $mm1, $hh1, $day1, $month1, $year1, $zone1 ) =
      strptime( $a->{ 'pubdate' } );
    my ( $ss2, $mm2, $hh2, $day2, $month2, $year2, $zone2 ) =
      strptime( $b->{ 'pubdate' } );

    #
    # Abort if we didn't work.
    #
    die "Couldn't find first year:  $a->{'date'}" unless defined($year1);
    die "Couldn't find second year: $b->{'date'}" unless defined($year2);

    #
    #  Convert to compare
    #
    my $c = timelocal( $ss1, $mm1, $hh1, $day1, $month1, $year1 + 1900 );
    my $d = timelocal( $ss2, $mm2, $hh2, $day2, $month2, $year2 + 1900 );

    return $d <=> $c;
}



=begin doc

  Output the index page + index RSS feed.

=end doc

=cut

sub outputIndexPage
{

    #
    #  Holder for the blog entries.
    #
    my $entries;

    #
    #  Find all the entries and sort them to be most recent first.
    #
    my $tmp;
    foreach my $file ( keys(%data) )
    {
        my $blog = $data{ $file };

        if ( keys(%$blog) )
        {
            if ( $blog->{ 'body' } =~ /<cut/i )
            {
                $blog->{ 'body' } =
                  processCut( $blog->{ 'body' }, $blog->{ 'link' } );
            }
            push( @$tmp, $blog );
        }

    }
    my @tmp2 = sort bywhen @$tmp;

    #
    #  The number of entries to display upon the index.
    #
    my $max = $CONFIG{ 'entry-count' };
    foreach my $f (@tmp2)
    {
        my %copy = %{ $f };
        $copy{ 'date' } = dateI18n( $copy{ 'date' } );
        push( @$entries, \%copy ) if ( $max > 0 );
        $max -= 1;
    }

    #
    #  Open the index template.
    #
    my $template = loadTemplate( "index.template", die_on_bad_params => 0 );


    #
    # create the calendar if we can.
    #
    my $calendar = createCalendar();
    if ( defined($calendar) )
    {
        my $text = $calendar->as_HTML();

        $text =~ s/<\/?b>//g;
        $text =~ s/<\/?p>//g;

        $template->param( calendar       => 1,
                          calendar_month => $text );
    }


    #
    #  The entries.
    #
    $template->param( entries => $entries )
      if ($entries);

    #
    #  The clouds
    #
    $template->param( tagcloud => $CLOUD{ 'tag' } )
      if ( $CLOUD{ 'tag' } );
    $template->param( datecloud => $CLOUD{ 'archive' } )
      if ( $CLOUD{ 'archive' } );

    #
    #  Blog title and subtitle, if present.
    #
    $template->param( blog_title => $CONFIG{ 'blog_title' } )
      if ( $CONFIG{ 'blog_title' } );
    $template->param( blog_subtitle => $CONFIG{ 'blog_subtitle' } )
      if ( $CONFIG{ 'blog_subtitle' } );
    $template->param( release => $RELEASE );


    #
    #  Page to use
    #
    my $index = $CONFIG{ 'filename' } || "index.html";

    outputTemplate( $template, $index );

    #
    #  Output the RSS feed
    #
    #  We repeat this loop because --rss-count and --entry-count
    # might be different.
    #
    $entries = undef;
    $max     = $CONFIG{ 'rss-count' };
    foreach my $f (@tmp2)
    {
        push( @$entries, $f ) if ( $max > 0 );
        $max -= 1;
    }

    $template = loadTemplate( "index.xml.template", die_on_bad_params => 0 );
    $template->param( blog_title => $CONFIG{ 'blog_title' } )
      if ( $CONFIG{ 'blog_title' } );
    $template->param( blog_subtitle => $CONFIG{ 'blog_subtitle' } )
      if ( $CONFIG{ 'blog_subtitle' } );
    $template->param( entries => $entries ) if ($entries);
    outputTemplate( $template, "index.rss" );
}



=begin doc

  Write out a /tags/$foo/index.html containing each blog entry which has the
 tag '$foo'.

=end doc

=cut

sub outputTagPage
{
    my ($tagName) = (@_);

    my $dir = "tags/$tagName";

    #
    #  So we have %data being a hash of entries.
    #
    #  The key is a filename.
    #
    #  The value is a bunch of data as a hash reference.
    #
    #  We want to traverse this hash and reference each entry
    # which has a tag which matches the given tag - we'll store
    # that in the local array called '$matching'
    #
    #  We also write the filenames to a pointback file while we're
    # at it.
    #
    my $matching;

    #
    #  read pointback cache first
    #
    my $pointbacks = undef;
    my %newpointbacks;
    $newpointbacks{ 'mtime' } = $RUNNING_TIME;

    my $cache = $CONFIG{ 'cache' } || undef;
    my $c_key = $CONFIG{ 'output' } . "/$dir/$tagName.pb";
    if ($cache)
    {
        $pointbacks = $cache->get($c_key);
    }

    #
    #  If one of the files matching our tag was just generated
    # (RUNNING_TIME == 'running_time') then this tag must be generated.
    #
    my $dirty_file_forced = 0;

    #
    #  Iterate over the data-structure.
    #
    foreach my $file ( keys %data )
    {

        # get the hash
        my $hash = $data{ $file };

        # get the tags from that hash
        my $tags = $hash->{ 'tags' } || undef;

        my $matched = 0;

        #
        # for each tag, if it is a match then we save the data ref away.
        #
        foreach my $t (@$tags)
        {
            my $name = $t->{ 'tag' };

            if ( $name eq $tagName )
            {
                push( @$matching, $hash );

                $matched = 1;

            }
        }

        #
        # and add to the pointback list
        #
        if ($matched)
        {
            push( @{ $newpointbacks{ 'files' } }, $file );

            if ( $hash->{ 'running_time' } == $RUNNING_TIME )
            {
                $dirty_file_forced = 1;
            }
        }
    }

    #
    #  If $dirty_file_forced is false, we are close to being able to skip
    # this tag.  But if one of the files was edited in order to remove
    # a tag, then we need to check for that too.  It will be listed in
    # the pointback list.  If one of the files in the pointback list
    # is newer than the pointback file itself, then we are still dirty.
    #
    if ( $dirty_file_forced == 0 )
    {
        foreach my $pbname ( @{ $pointbacks->{ 'files' } } )
        {
            my $hash = $data{ $pbname };

            #  If the pb file doesn't exist (!$pbmtime)
            # or if the src file doesn't exist anymore (!defined($hash))
            # or if the src file is newer than the tag,
            # then we are dirty.
            if ( ( !$pointbacks->{ 'mtime' } ) ||
                 !defined($hash) ||
                 $hash->{ 'mtime' } > $pointbacks->{ 'mtime' } )
            {
                $dirty_file_forced = 1;
            }
        }
    }

    #
    #  Final check... if dirty_file_forced is false, no need to rebuild
    #
    if ( $dirty_file_forced == 0 )
    {
        $CONFIG{ 'verbose' } && print "Skipping clean tag: $tagName\n";
        return;
    }

    $CONFIG{ 'verbose' } && print "Creating tag page : $tagName\n";

    #
    #  We're dirty, so save a new pointback file
    #
    if ( defined($cache) )
    {
        $cache->set( $c_key, \%newpointbacks );
    }


    my $entries;

    #
    #  Now for each matching entries.
    #
    foreach my $blog (@$matching)
    {
        if ( keys(%$blog) )
        {
            if ( $blog->{ 'body' } =~ /<cut/i )
            {
                $blog->{ 'body' } =
                  processCut( $blog->{ 'body' }, $blog->{ 'link' } );
            }

            my %copy = %{ $blog };
            $copy{ 'date' } = dateI18n( $copy{ 'date' } );
            push( @$entries, \%copy );
        }
    }

    #
    #  Sort the entries by date
    #
    my @sortedEntries;

    if ( $CONFIG{ 'recent-tags-first' } )
    {
        @sortedEntries = sort bywhen @$entries;
    }
    else
    {
        @sortedEntries = reverse sort bywhen @$entries;
    }

    #
    #  Now write the output as a HTML page.
    #
    my $template = loadTemplate( "tags.template", die_on_bad_params => 0 );

    #
    #  The entries.
    #
    $template->param( entries => \@sortedEntries ) if (@sortedEntries);
    $template->param( tagname => $tagName );
    $template->param( release => $RELEASE );

    #
    #  The clouds
    #
    $template->param( tagcloud => $CLOUD{ 'tag' } )
      if ( $CLOUD{ 'tag' } );
    $template->param( datecloud => $CLOUD{ 'archive' } )
      if ( $CLOUD{ 'archive' } );

    #
    #  Blog title and subtitle, if present.
    #
    $template->param( blog_title => $CONFIG{ 'blog_title' } )
      if ( $CONFIG{ 'blog_title' } );
    $template->param( blog_subtitle => $CONFIG{ 'blog_subtitle' } )
      if ( $CONFIG{ 'blog_subtitle' } );


    #
    #  Page to use
    #
    my $index = $CONFIG{ 'filename' } || "index.html";

    outputTemplate( $template, "$dir/$index" );

    #
    #  Now output the .xml file
    #
    $template = loadTemplate( "tags.xml.template", die_on_bad_params => 0 );
    $template->param( blog_title => $CONFIG{ 'blog_title' } )
      if ( $CONFIG{ 'blog_title' } );
    $template->param( entries => \@sortedEntries ) if (@sortedEntries);
    $template->param( tagname => $tagName )        if ($tagName);
    outputTemplate( $template, "$dir/$tagName.rss" );
}



=begin doc

  Output the archive page for the given Month + Year.

  This function is a *mess* and iterates over the data structure much
 more often than it needs to.

=end doc

=cut

sub outputArchivePage
{
    my ($date) = (@_);

    #
    #  Should we abort?
    #
    if ( $CONFIG{ 'no-archive' } )
    {
        $CONFIG{ 'verbose' } && print "Ignoring archive page, as instructed.\n";
        return;
    }

    #
    #  The year & month for which we're going to output a page.
    #
    #  Entries dated on other month+year pairs will be ignored.
    #
    my $year  = '';
    my $month = '';
    if ( $date =~ /^([0-9]{4})-([0-9]{2})/ )
    {
        $year  = $1;
        $month = $2;
    }
    else
    {
        die "Internal error - failed to parse month+year to output!\n";
    }

    #
    #  The path to which we're going to output our entries to.
    #
    my $dir = "archive/$year/$month";

    #
    #  read pointback cache first
    #
    my $pointbacks = undef;
    my %newpointbacks;
    $newpointbacks{ 'mtime' } = $RUNNING_TIME;

    my $cache = $CONFIG{ 'cache' } || undef;
    my $c_key = $CONFIG{ 'output' } . "/$dir/pb";
    if ($cache)
    {
        $pointbacks = $cache->get($c_key);
    }

    #
    #  If one of the files matching our archive date was just generated
    # (RUNNING_TIME == 'running_time') then this date must be generated.
    #
    my $dirty_file_forced = 0;

    #
    #  So we have %data being a hash of entries.
    #
    #  The key is a filename.
    #
    #  The value is a bunch of data as a hash reference.
    #
    #  We want to traverse this hash and reference each entry
    # which has a tag which matches the given tag - we'll store
    # that in the local array called '$matching'
    #
    my $matching;

    foreach my $filename ( keys %data )
    {

        #
        #  Get the date.
        #
        my $hash    = $data{ $filename };
        my $date    = $hash->{ 'date' };
        my $matched = 0;

        #
        #  Not a date?  Use the file's modification date.
        #
        if ( !defined($date) || !length($date) )
        {

            #
            # Test the file for creation time.
            #
            my ( $dev,   $ino,     $mode, $nlink, $uid,
                 $gid,   $rdev,    $size, $atime, $mtime,
                 $ctime, $blksize, $blocks
               ) = stat($filename);

            $date = localtime($ctime);
        }

        $date = time2str( "%Y-%m", str2time($date) );

        #
        #  If the date matches then we'll save it
        #
        if ( $date eq "$year-$month" )
        {
            push( @$matching, $hash );
            $matched = 1;
        }

        if ($matched)
        {
            push( @{ $newpointbacks{ 'files' } }, $filename );

            if ( $hash->{ 'running_time' } == $RUNNING_TIME )
            {
                $dirty_file_forced = 1;
            }
        }
    }

    #
    #  If $dirty_file_forced is false, we are close to being able to skip
    # this date.  But if one of the files was edited in order to change the
    # date, then we need to check for that too.  It will be listed in
    # the pointback list.  If one of the files in the pointback list
    # is newer than the pointback file itself, then we are still dirty.
    #
    if ( $dirty_file_forced == 0 )
    {
        foreach my $pbname ( @{ $pointbacks->{ 'files' } } )
        {
            my $hash = $data{ $pbname };

            #  If the pb file doesn't exist (!$pbmtime)
            # or if the src file doesn't exist anymore (!defined($hash))
            # or if the src file is newer than the tag,
            # then we are dirty.
            if ( ( !$pointbacks->{ 'mtime' } ) ||
                 !defined($hash) ||
                 $hash->{ 'mtime' } > $pointbacks->{ 'mtime' } )
            {
                $dirty_file_forced = 1;
            }
        }
    }

    #
    #  Final check... if dirty_file_forced is false, no need to rebuild
    #
    if ( $dirty_file_forced == 0 )
    {
        $CONFIG{ 'verbose' } && print "Skipping clean archive: $date\n";
        return;
    }

    #
    #  We're dirty, so save our new pointback list
    #
    if ( defined($cache) )
    {
        $cache->set( $c_key, \%newpointbacks );
    }

    #
    #  The entries.
    #
    my $entries;

    #
    #
    #  Now process each matching entry
    #
    foreach my $blog (@$matching)
    {
        if ( keys(%$blog) )
        {

            #
            #  Test for the cut in a quick fashion.
            #
            if ( $blog->{ 'body' } =~ /<cut/i )
            {

                #
                #  Properly process a "cut"
                #
                $blog->{ 'body' } =
                  processCut( $blog->{ 'body' }, $blog->{ 'link' } );
            }

            my %copy = %{ $blog };
            $copy{ 'date' } = dateI18n( $copy{ 'date' } );
            push( @$entries, \%copy );
        }
    }

    #
    #  Sort the entries by date
    #
    my @sortedEntries;
    if ( $CONFIG{ 'recent-dates-first' } )
    {
        @sortedEntries = sort bywhen @$entries;
    }
    else
    {
        @sortedEntries = reverse sort bywhen @$entries;
    }


    $CONFIG{ 'verbose' } && print "Creating archive page : $date\n";

    #
    #  Now write the output as a HTML page.
    #
    my $template = loadTemplate( "month.template", die_on_bad_params => 0 );
    $template->param( release => $RELEASE );


    #
    #  The entries
    #
    $template->param( entries => \@sortedEntries ) if (@sortedEntries);

    #
    #  Output the month + year.
    #
    $template->param( year => $year, month => $month );
    $template->param( month_name => $names[$month - 1] );

    #
    #  The clouds
    #
    $template->param( tagcloud => $CLOUD{ 'tag' } )
      if ( $CLOUD{ 'tag' } );
    $template->param( datecloud => $CLOUD{ 'archive' } )
      if ( $CLOUD{ 'archive' } );

    #
    #  Blog title and subtitle, if present.
    #
    $template->param( blog_title => $CONFIG{ 'blog_title' } )
      if ( $CONFIG{ 'blog_title' } );
    $template->param( blog_subtitle => $CONFIG{ 'blog_subtitle' } )
      if ( $CONFIG{ 'blog_subtitle' } );

    #
    #  Page to use
    #
    my $index = $CONFIG{ 'filename' } || "index.html";
    outputTemplate( $template, "$dir/$index" );

    #
    #  Now the RSS page.
    #
    $template = loadTemplate( "month.xml.template", die_on_bad_params => 0 );
    $template->param( blog_title => $CONFIG{ 'blog_title' } )
      if ( $CONFIG{ 'blog_title' } );
    $template->param( entries => \@sortedEntries ) if (@sortedEntries);
    $template->param( month => $month, year => $year );
    $template->param( month_name => $names[$month - 1] );
    outputTemplate( $template, "$dir/$month.rss" );
}



=begin doc

  Process the body for any present cut tags which might be present.

  The cut text looks like this:

=for example begin

   Blah blah
   This is visible
   <cut [text="xx"]>
   This is hidden
   So is this
   </cut>
   This is visible

=for example end

=end doc

=cut

sub processCut
{
    my ( $body, $link ) = (@_);

    my $url = $CONFIG{ 'url_prefix' } || "";
    $url .= $link;

    my $cut_text = "";

    if ( $body =~ /(.*)<cut([^>]*)>(.*)<\/cut>(.*)/gis )
    {
        my $pre  = $1;
        my $cut  = $2;
        my $hid  = $3;
        my $post = $4;

        #
        #  See if they supplied text="xxxxx"
        #
        if ( defined($cut) && ( $cut =~ /text=['"]([^'"]+)['"]/i ) )
        {
            $cut_text = $1;
        }

        $body = $pre;

        if ( !length($cut_text) )
        {
            $cut_text = "This entry has been cut, click to read on.";
        }
        $body .= " <a href=\"$url\">$cut_text</a> ";
        $body .= $post;

    }

    return ($body);
}



=begin doc

  Output static page.

=end doc

=cut

sub outputStaticPage
{
    my ($filename) = (@_);

    #
    #  Just the name of the file.
    #
    my $basename = basename($filename);

    #
    #  Read the entry
    #
    my $static = $data{ $filename };

    #
    #  If the running time of this file is not the same as this process's,
    # then it was cached, and we don't need to write it again.
    #
    if ( $static->{ 'running_time' } != $RUNNING_TIME )
    {
        $CONFIG{ 'verbose' } && print "\tSkipping: $filename\n";
        return;
    }

    #
    #  Load the template
    #
    my $template = loadTemplate( "entry.template", die_on_bad_params => 0 );
    $template->param( release => $RELEASE );

    #
    #  Get the pieces of information.
    #
    my $title = $static->{ 'title' } || $basename;
    my $tags  = $static->{ 'tags' };
    my $body  = $static->{ 'body' };
    my $date  = $static->{ 'date' };

    if ( !defined($date) )
    {
        my ( $dev,   $ino,     $mode, $nlink, $uid,
             $gid,   $rdev,    $size, $atime, $mtime,
             $ctime, $blksize, $blocks
           ) = stat($filename);

        $date = localtime($ctime);
    }

    $CONFIG{ 'verbose' } && print "\t$filename\n";

    #
    #  Convert to suitable filename.
    #
    my $file = fileToTitle($title);

    #
    #  Update the basename
    #
    $basename = basename($file);

    #
    #  Include the comments in the output if they weren't disabled
    # and are present.
    #
    if ( !$CONFIG{ 'no-comments' } )
    {
        $template->param( comments       => $static->{ 'comments' },
                          comment_count  => $static->{ 'comment_count' },
                          comment_plural => $static->{ 'comment_plural' } )
          if ( ( defined $static->{ 'comment_count' } > 0 ) &&
               ( $static->{ 'comment_count' } > 0 ) );


        #
        #  If we have no date restrictions then enable comments
        #
        if ( $CONFIG{ 'comment-days' } == 0 )
        {
            $template->param( comments_enabled => 1 );
        }
        else
        {

            #
            #  The number of seconds past the epoch of today and the
            # date the blog is supposed to be published is used.
            #
            my $time  = str2time($date);
            my $today = time;

            #
            #  The number of days that should be allowd.
            #
            my $days = $CONFIG{ 'comment-days' } * 60 * 60 * 24;

            if ( ( $time + $days ) > $today )
            {
                $CONFIG{ 'verbose' } &&
                  print "Comments allowed on post dated $date.\n";
                $template->param( comments_enabled => 1 );
            }
        }
    }


    #
    #  The entry.
    #
    $template->param( title => $title );
    $template->param( tags  => $tags ) if ($tags);
    $template->param( date  => dateI18n($date) ) if ($date);
    $template->param( body  => $body );
    $template->param( link  => $static->{ 'link' } );
    $basename =~ s/\.(html|htm)$//g;
    $template->param( basename  => $basename );
    $template->param( xrefpairs => $static->{ 'xrefpairs' } )
      if ( $static->{ 'xrefpairs' } );

    #
    #  Our clouds
    #
    $template->param( tagcloud => $CLOUD{ 'tag' } ) if ( $CLOUD{ 'tag' } );
    $template->param( datecloud => $CLOUD{ 'archive' } )
      if ( $CLOUD{ 'archive' } );

    #
    #  Blog title and subtitle, if present.
    #
    $template->param( blog_title => $CONFIG{ 'blog_title' } )
      if ( $CONFIG{ 'blog_title' } );
    $template->param( blog_subtitle => $CONFIG{ 'blog_subtitle' } )
      if ( $CONFIG{ 'blog_subtitle' } );

    #
    #  If comments are disabled then set that.
    #
    $template->param( no_comments => 1 ) if ( $CONFIG{ 'no-comments' } );

    #
    #  Output the entry now.
    #
    if ( $CONFIG{ 'date-archive-path' } )
    {
        my $dir = "archive/" . time2str( "%Y/%m", str2time($date) );
        outputTemplate( $template, "${dir}/${file}" );
    }
    else
    {
        outputTemplate( $template, $file );
    }

    #
    # Create comment feed, unless comments are disabled.
    #
    if ( !$CONFIG{ 'no-comments' } )
    {
        $CONFIG{ 'verbose' } && print "\tCreating comment feed\n";

        #
        #  Load the template
        #
        my $comments =
          loadTemplate( "comments.xml.template", die_on_bad_params => 0 );

        #
        #  Setup the loop
        #
        $comments->param( title    => $title );
        $comments->param( comments => $static->{ 'comments' } )
          if ( $static->{ 'comments' } );


        my $xml = undef;

        if ( $CONFIG{ 'date-archive-path' } )
        {
            my $dir = "archive/" . time2str( "%Y/%m", str2time($date) );
            $xml = "${dir}/${file}";
        }
        else
        {
            $xml = $file;
        }

        #
        #  Give an .RSS suffix
        #
        $xml =~ s/\.html?$/.rss/g;
        outputTemplate( $comments, $xml );
    }
}


=begin doc

  Output a static sitemap, unless there is no URL or sitemap prefix
 setup.

  Note: We do this after all other things have been created, by
 reading the files in our output directory.  This is either sleazy
 or clever depending upon your point of view.

=end doc

=cut

sub outputSiteMap
{
    my $dir  = $CONFIG{ 'output' };
    my $file = $dir . "/sitemap.xml";
    my $home = '';


    if ( $CONFIG{ 'sitemap_prefix' } )
    {
        $home = $CONFIG{ 'sitemap_prefix' };
    }
    elsif ( $CONFIG{ 'url_prefix' } )
    {
        $home = $CONFIG{ 'url_prefix' };
    }
    else
    {
        $CONFIG{ 'verbose' } && print "No prefix defined - skipping sitemap\n";
        return;
    }

    #
    #  Print the header
    #
    open my $handle, ">", $file or
      die "Failed to write to $file $!";

    print $handle <<EOF;
<?xml version="1.0" encoding="UTF-8"?>
<urlset
  xmlns="http://www.google.com/schemas/sitemap/0.84"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84
                      http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">

 <url>
  <loc>$home</loc>
  <priority>0.75</priority>
  <changefreq>daily</changefreq>
 </url>
EOF

    #
    #  Find the files
    #
    my $suffix = $CONFIG{ 'suffix' } || ".html";

    foreach my $page ( glob( $dir . "/*" . $suffix ) )
    {
        next if ( -d $page );
        $page = basename($page);

        print $handle <<EOF;
 <url>
  <loc>$home$page</loc>
  <priority>0.50</priority>
  <changefreq>weekly</changefreq>
 </url>
EOF
    }

    #
    #  Print the footer
    #
    print $handle <<EOF;
</urlset>
EOF

    #
    #  All done
    #
    close($handle);
}


=begin doc

  Return a hash of interesting data from our blog file.

=end doc

=cut

sub readBlogEntry
{
    my ($filename) = (@_);

    my %entry;

    #
    #  Do we have the memcached module available and setup?
    #
    my $cache = $CONFIG{ 'cache' } || undef;
    my $c_key = "";
    if ($cache)
    {

        #
        # Get the data from the cache - which will include the
        # mtime of the file to allow natural expirey.
        #
        # This way if the file is modified the cache will be flushed
        # appropriately.
        #
        $c_key = $filename . "_" . ( stat($filename) )[9];
        my $cached = $cache->get($c_key);

        if ( defined($cached) )
        {
            $CONFIG{ 'verbose' } && print "memcache-get: $filename\n";
            return ( \%$cached );
        }
    }



    #
    #  Here we store the header values from the entry.
    #
    #  The body will be read & returned via `chronicle-entry-filter`, and
    # we don't care about it here.
    #
    my %meta;
    my $inHeader = 1;

    #
    #  Set running time to current process
    #
    $meta{ 'running_time' } = $RUNNING_TIME;


    #
    #  Read the actual entry
    #
    open my $handle, "<:utf8", $filename or
      die "Failed to read $filename $!";

    while ( my $line = <$handle> )
    {
        if ($inHeader)
        {

            #
            #  If the line has the form of "key: value"
            #
            if ( $line =~ /^([^:]+):(.*)/ )
            {
                my $key = $1;
                my $val = $2;

                $key = lc($key);
                $key =~ s/^\s+|\s+$//g;
                $val =~ s/^\s+|\s+$//g;

                #
                #  "subject" is a synonym for "title".
                #
                $key = "title" if ( $key eq "subject" );

                #
                #  Update the value if there is one present,
                # and we've not already saved that one away.
                #
                $meta{ $key } = $val
                  if ( defined($val) && length($val) && !$meta{ $key } );
            }
        }
        else
        {

            #
            #  Empty line == end of header
            #
            $inHeader = 0 if ( $line =~ /^$/ );
        }
    }
    close($handle);


    #
    # MJR - embargo the entry until the publish if it
    # is in the future.
    #
    # Steve prefers using chronicle-spooler, but I want
    # uploaded files to stay where I put them, else I get
    # my local copy confused.
    #
    if ( ( $meta{ 'publish' } ) && ( str2time( $meta{ 'publish' } ) > time() ) )
    {
        $CONFIG{ 'verbose' } &&
          print
          "Skipping future entry $filename - due to be published on $meta{'publish'}\n";
        return 0;
    }



    #
    #  Specify the global format if we didn't get one in this file
    #
    if ( !$meta{ 'format' } )
    {
        $meta{ 'format' } = ( $CONFIG{ 'format' } || "html" );
    }

    #
    #  Did we find tags?
    #
    #  If so replace them with the array of hashses that we'll use for
    # the template expansion.
    #
    if ( $meta{ 'tags' } )
    {
        my @tmpTags;

        foreach my $tag ( split( /,/, $meta{ 'tags' } ) )
        {

            # strip leading and trailing space.
            $tag =~ s/^\s+//;
            $tag =~ s/\s+$//;

            # skip empty tags.
            next if ( !length($tag) );

            # tags are lowercase.
            $tag = lc($tag);

            # store the tag away - so we can have a sorted list later.
            push( @tmpTags, $tag );
        }

        #
        #  Remove the tag list, so that we can replace it with an
        # array of hashes.
        #
        $meta{ 'tags' } = undef;

        #
        #  Push the sorted tags into the entry
        #
        foreach my $t ( sort @tmpTags )
        {
            push( @{ $meta{ 'tags' } }, { tag => $t } );
        }
    }

    #
    #  Did we find cross references?
    #
    #  If so, replace them with the array of full filename keys we'll
    # use for xref generation.
    #
    if ( $meta{ 'xrefs' } )
    {
        my @tmpRefs;

        foreach my $ref ( split( /,/, $meta{ 'xrefs' } ) )
        {

            # strip leading and trailing space.
            $ref =~ s/^\s+//;
            $ref =~ s/\s+$//;

            # skip empty tags.
            next if ( !length($ref) );

            # get full filename key
            $ref = $CONFIG{ 'input' } . "/" . $ref;

            # store the ref away, only if the file exists
            if ( -e "$ref" )
            {
                push( @tmpRefs, $ref );
            }
            else
            {
                print "Warning: cross reference not found: $ref\n";
            }
        }

        #
        #  Remove the xrefs list, so that we can replace it with an
        # array.
        #
        $meta{ 'xrefs' } = undef;

        #
        #  Push the sorted xrefs into the entry
        #
        foreach my $r ( sort @tmpRefs )
        {
            push( @{ $meta{ 'xrefs' } }, $r );
        }
    }

    #
    #  Find all the details from the file.
    #
    my ( $dev,  $ino,   $mode,  $nlink, $uid,     $gid, $rdev,
         $size, $atime, $mtime, $ctime, $blksize, $blocks
       ) = stat($filename);

    #
    #  If the date isn't set then use the mtime of the file.
    #
    if ( !defined( $meta{ 'date' } ) || ( !length( $meta{ 'date' } ) ) )
    {
        my @lt = gmtime($mtime);
        $meta{ 'date' } = strftime( "%d %B %Y", @lt );
    }

    #
    #  Store that mtime for later comparison
    #
    $meta{ 'mtime' } = $mtime;


    #
    #  Store the values from our pseudo-headers in our entry
    #
    foreach my $key ( keys %meta )
    {
        $entry{ $key } = $meta{ $key };
    }


    #
    #  Get the Hours::Mins::Seconds from the Date: pseudo-header
    #
    my $time = str2time( $meta{ 'date' } );
    my $hms  = time2str( "%H:%M:%S", $time );
    my $tz   = time2str( "%z", $time );


    #
    #  If the time is missing from the Date: pseudo-header
    # we need to magic them up.  Use the mtime of the file.
    #
    if ( $hms eq '00:00:00' )
    {
        $hms = time2str( "%H:%M:%S", $mtime, "GMT" );
        $tz = "GMT";
    }


    #
    #  If we found a time then we're OK, and can store away the various
    # XML versions of the date.
    #
    if ($time)
    {

        #
        #  RSS 2
        #
        $entry{ 'pubdate' } = time2str( "%a, %e %b %Y $hms $tz", $time );
        $entry{ 'pubdate' } =~ s/  +/ /g;

        #
        # Store the W3C date form
        # http://www.w3.org/TR/NOTE-datetime
        #
        my $w3ctz = $tz;
        $w3ctz =~ s/([-+]\d\d)(\d\d)/$1:$2/;
        $entry{ 'w3cdate' } = time2str( "%Y-%m-%dT${hms}$w3ctz", $time );

    }
    else
    {
        print
          "Failed to parse date: '$meta{'date'}' to generate pubDate of entry.\n";
    }


    #
    #  We now want to read the body of the entry from our filter program.
    #
    #  The filter program will read the specified file and return the
    # HTML output to STDOUT.
    #
    my $cmd = "chronicle-entry-filter ";

    #
    #  Propogate any global options to the command.
    #
    foreach my $k (qw! format pre-filter post-filter !)
    {
        if ( $CONFIG{ $k } )
        {
            $cmd .= "--$k='$CONFIG{$k}' ";
        }
    }

    $cmd .= " --filename=$filename";

    #
    #  Show what we're running.
    #
    $CONFIG{ 'verbose' } && print "Running: $cmd\n";


    #
    #  Run the command, reading stdout.
    #
    open my $han, "-|", $cmd or
      die "Failed to run filter: $!\n";

    #
    #  UTF-8 must be preserved
    #
    binmode( $han, ":utf8" );

    my $body = "";
    while (<$han>)
    {
        $body .= $_;
    }
    close($han);

    #
    #  Store the returned body, if any.
    #
    $entry{ 'body' } = $body if ($body);

    #
    #  No title?
    #
    if ( !defined( $entry{ 'title' } ) ||
         !length( $entry{ 'title' } ) )
    {
        my $basename = $filename;
        if ( $basename =~ /(.*)\/(.*)/ )
        {
            $basename = $2;
        }
        if ( $basename =~ /^(.*)\.(.*)$/ )
        {
            $basename = $1;
        }

        $entry{ 'title' } = $basename;
    }

    #
    #  Get the link - after ensuring we have a title.
    #
    my $link = fileToTitle( $entry{ 'title' } );
    $entry{ 'link' } = $link;

    #
    #  Rewrite the link if we're using a date-archive.
    #
    if ( $CONFIG{ 'date-archive-path' } )
    {
        my $date = $entry{ 'date' };
        $entry{ 'link' } =
          "archive/" . time2str( "%Y/%m", str2time($date) ) .
          "/$entry{ 'link' }";
    }



    #
    #  Find the comments associated with this entry, if any.
    #
    unless ( $CONFIG{ 'no-comments' } )
    {
        my $name = fileToTitle( $meta{ 'title' } );

        my $comments = getComments( $CONFIG{ 'comments' }, $name );

        if ( defined($comments) )
        {
            my $count  = scalar(@$comments);
            my $plural = 1;
            $plural = 0 if ( $count == 1 );

            $entry{ 'comments' }       = $comments;
            $entry{ 'comment_count' }  = $count;
            $entry{ 'comment_plural' } = $plural;
        }
    }

    #
    #  Store the data in the cache if we have it enabled - unless
    # there are comments associated with the entry which might have
    # changed behind our back.
    #
    #  It won't help this time, but the next run will rock ..
    #
    if ( defined($cache) )
    {
        my $update = 1;


        if ( ( $entry{ 'comment_count' } ) &&
             ( $entry{ 'comment_count' } ) > 0 )
        {

            #
            #  The number of seconds past the epoch of today and the
            # date the blog is supposed to be published is used.
            #
            my $time  = str2time( $entry{ 'date' } );
            my $today = time;

            #
            #  The number of days that should be allowd.
            #
            my $days = $CONFIG{ 'comment-days' } * 60 * 60 * 24;

            if ( ( $time + $days ) > $today )
            {
                $update = 0;
            }
        }

        #
        #  If we're updating, then store in the cache.
        #
        #  We don't store in the cache if:
        #
        #   * The entry has comments.
        #   * The entry is "recent", as set by "--comment-days".
        #
        #
        if ($update)
        {
            $CONFIG{ 'verbose' } && print "memcache-set: $filename\n";
            $cache->set( $c_key, \%entry );
        }
        else
        {
            $CONFIG{ 'verbose' } && print "NOT Updating cache for $filename\n";
        }
    }
    return \%entry;
}



=begin doc

  Create a filename for an URL which does not contain unsafe
 characters.

=end doc

=cut

sub fileToTitle
{
    my ($file) = (@_);

    #
    #  Get rid of non-alphanumeric Al characters
    #
    $file =~ s/[^a-z0-9]/_/gi;

    my $suffix = $CONFIG{ 'suffix' } || ".html";
    $file .= $suffix;

    #
    #  Lower case?
    #
    $file = lc($file) if ( $CONFIG{ 'lower-case' } );

    return ($file);

}



=begin doc

  Look for comments, for the given entry.  Return any found in a format
 suitable for the insertion into the output templates.

=end doc

=cut

sub getComments
{
    my ( $dir, $title ) = (@_);

    my $results;

    if ( $title =~ /^(.*)\.([^.]+)$/ )
    {
        $title = $1;
    }

    #
    #  Find each comment file.
    #
    my @entries;
    foreach my $file ( sort( glob( $dir . "/" . $title . "*" ) ) )
    {
        push( @entries, $file );
    }

    #
    # Sort them into order.
    #
    @entries = sort {( stat($a) )[9] <=> ( stat($b) )[9]} @entries;

    #
    #  Now process them.
    #
    foreach my $file (@entries)
    {
        my $date    = "";
        my $name    = "";
        my $link    = "";
        my $body    = "";
        my $mail    = "";
        my $pubdate = "";

        if ( $file =~ /^(.*)\.([^.]+)$/ )
        {
            $date = $2;

            if ( $date =~ /(.*)-([0-9:]+)/ )
            {
                my $d = $1;
                my $t = $2;

                $d =~ s/-/ /g;

                $date = "Submitted at $t on $d";
            }
        }

        open my $comment, "<:utf8", $file or
          next;

        foreach my $line (<$comment>)
        {
            next if ( !defined($line) );

            chomp($line);

            next if ( $line =~ /^IP-Address:/ );
            next if ( $line =~ /^User-Agent:/ );

            if ( !length($name) && $line =~ /^Name: (.*)/i )
            {
                $name = $1;
            }
            elsif ( !length($mail) && $line =~ /^Mail: (.*)/i )
            {
                $mail = $1;
            }
            elsif ( !length($link) && $line =~ /^Link: (.*)/i )
            {
                $link = $1;
            }
            else
            {
                $body .= $line . "\n";
            }
        }
        close($comment);

        if ( length($name) &&
             length($mail) &&
             length($body) )
        {

            #
            #  Add a gravitar link to the comment in case the
            # theme wishes to use it.
            #
            my $default  = "";
            my $size     = 32;
            my $gravitar = "http://www.gravatar.com/avatar.php?gravatar_id=" .
              md5_hex( lc $mail ) . "&size=" . $size;

            #
            # A comment which was submitted by the blog author might
            # have special theming.
            #
            my $author = 0;
            $author = 1
              if ( $CONFIG{ 'author' } &&
                   ( lc($mail) eq lc( $CONFIG{ 'author' } ) ) );

            #
            # Store the comment
            #
            push( @$results,
                  {  name     => $name,
                     author   => $author,
                     gravitar => $gravitar,
                     link     => $link,
                     mail     => $mail,
                     body     => $body,
                     date     => $date,
                  } );

        }
        else
        {
            $CONFIG{ 'verbose' } &&
              print
              "I didn't like length of \$name ($name), \$mail ($mail) or \$body ($body)\n";
        }
    }

    return ($results);
}



=begin doc

  Load a template file.

=end doc

=cut

sub loadTemplate
{
    my ( $file, %params ) = (@_);

    #
    #  Get the directory.
    #
    my $dir = $CONFIG{ 'theme-dir' };

    #
    #  XML files go in theme-dir/xml/
    #
    if ( $file =~ /\.xml\./i )
    {
        $dir .= "/xml/";
    }
    else
    {
        $dir .= "/" . $CONFIG{ 'theme' } . "/";
    }

    #
    #  Make sure the file exists.
    #
    if ( !-e $dir . $file )
    {
        print <<EOF;

  The template file $file was not found in the theme directory.

  Theme          : $CONFIG{'theme'}
  Theme Directory: $CONFIG{'theme-dir'}

  We expected to find $dir$file;

  Aborting.
EOF
        exit;
    }

    my $t = HTML::Template->new( filename               => $file,
                                 path                   => $dir,
                                 filter                 => sub {
                                         my $ref = shift;
                                         ${$ref} = Encode::decode_utf8(${$ref});
                                 },
                                 loop_context_vars      => 1,
                                 global_vars            => 1,
                                 search_path_on_include => 1,
                                 %params
                               );

    return ($t);
}



=begin doc

  Set URL for top directory and output a template.

=end doc

=cut

sub outputTemplate
{
    my ( $template, $path ) = (@_);

    my $reltop = $path;
    $reltop =~ s'[^/]+/'../'g;
    $reltop =~ s'[^/]*$'';
    $template->param( reltop => $reltop );

    #
    # Select relative/absolute URL prefix.
    #
    my $top;
    if ( $CONFIG{ 'url_prefix' } )
    {
        $top = $CONFIG{ 'url_prefix' };
    }
    else
    {
        $top = $reltop;
    }
    $template->param( top => $top );

    if ( $CONFIG{ 'date-archive-path' } )
    {
        ( -d dirname("$CONFIG{'output'}/${path}") ) ||
          mkdir( dirname("$CONFIG{'output'}/${path}") );
    }

    open my $handle, ">:utf8", "$CONFIG{'output'}/$path" or
      die "Failed to write output template to $CONFIG{'output'}/$path - $!";
    print $handle $template->output();
    close($handle);
}



=begin doc

  Read the specified configuration file if it exists.

=end doc

=cut

sub readConfigurationFile
{
    my ($file) = (@_);

    #
    #  If it doesn't exist ignore it.
    #
    return if ( !-e $file );


    my $line = "";

    open my $handle, "<:utf8", $file or die "Cannot read file '$file' - $!";
    while ( defined( $line = <$handle> ) )
    {
        chomp $line;
        if ( $line =~ s/\\$// )
        {
            $line .= <FILE>;
            redo unless eof(FILE);
        }

        # Skip lines beginning with comments
        next if ( $line =~ /^([ \t]*)\#/ );

        # Skip blank lines
        next if ( length($line) < 1 );

        # Strip trailing comments.
        if ( $line =~ /(.*)\#(.*)/ )
        {
            $line = $1;
        }

        # Find variable settings
        if ( $line =~ /([^=]+)=([^\n]+)/ )
        {
            my $key = $1;
            my $val = $2;

            # Strip leading and trailing whitespace.
            $key =~ s/^\s+//;
            $key =~ s/\s+$//;
            $val =~ s/^\s+//;
            $val =~ s/\s+$//;

            # command expansion?
            if ( $val =~ /(.*)`([^`]+)`(.*)/ )
            {

                # store
                my $pre  = $1;
                my $cmd  = $2;
                my $post = $3;

                # get output
                my $output = `$cmd`;
                chomp($output);

                # build up replacement.
                $val = $pre . $output . $post;
            }

            # Store value.
            $CONFIG{ $key } = $val;
        }
    }

    close($handle);
}



=begin doc

  Sanity check our arguments, and setup to make sure there is nothing
 obviously broken.

=end doc

=cut

sub sanityCheckArguments
{

    #
    #  Make sure we have an input directory.
    #
    if ( !-d $CONFIG{ 'input' } )
    {
        print <<EOF;

  The blog input directory $CONFIG{'input'} does not exist.

  Aborting.
EOF

        exit;
    }

    if ( !$CONFIG{ 'theme-dir' } )
    {
        print <<EOF;

  Error - You don't have a theme directory setup.

  Please specify --theme-dir=/some/path, or add this to your configuration
 file:

theme-dir = /path/to/use/
EOF

        exit;
    }

    if ( !-d $CONFIG{ 'theme-dir' } )
    {
        print "The theme directory you specified doesn't exist:\n";
        print "\t" . $CONFIG{ 'theme-dir' } . "\n";
        exit;
    }

    if ( !$CONFIG{ 'theme' } )
    {
        print <<EOF;

  You've not specified a theme.

  Please specify --theme=xx

  Or add this to your configuration file:

theme = xx


  [You may list themes with --list-themes]

EOF

        exit;
    }


    if ( !-d $CONFIG{ 'theme-dir' } . "/" . $CONFIG{ 'theme' } )
    {
        print
          "The theme directory you specified doesn't exist in the theme directory:\n";
        print "\tTheme    :" . $CONFIG{ 'theme' } . "\n";
        print "\tTheme dir:" . $CONFIG{ 'theme-dir' } . "\n";
        print "\tExpected :" . $CONFIG{ 'theme-dir' } . "/" .
          $CONFIG{ 'theme' } . "\n";
        exit;
    }

}


=begin doc

 If we have the memcached module(s) available then load them
and connect to the cache on the local host.

=end doc

=cut

sub setupCache
{
    my $test = "use Cache::Memcached;";

    ## no critic (Eval)
    eval($test);
    ## use critic

    if ( !$@ )
    {

        # create the cache object
        $CONFIG{ 'cache' } =
          new Cache::Memcached { 'servers' => ["localhost:11211"] };

        $CONFIG{ 'verbose' } && print "Cache setup.\n";

    }

}



=begin doc

  Copy any static files from the theme directory into the "live" location
 in the output.

  This only works for a top-level target directory.

  Unless --force is specified we skip copying files which already exist,
 or that are older than the target.

=end doc

=cut

sub copyStaticFiles
{

    #
    #  Source and destination for the copy
    #
    my $input  = $CONFIG{ 'theme-dir' } . "/" . $CONFIG{ 'theme' };
    my $output = $CONFIG{ 'output' };

    foreach my $pattern (qw! *.css *.jpg *.gif *.png *.js *.ico !)
    {
        foreach my $file ( glob( $input . "/" . $pattern ) )
        {

            #
            #  Get the name of the file.
            #
            if ( $file =~ /(.*)\/(.*)/ )
            {
                $file = $2;
            }
            if ( $CONFIG{ 'force' } ||
                 ( !-e "$output/$file" ) ||
                 ( stat("$input/$file") )[9] > ( stat("$output/$file") )[9] )
            {
                $CONFIG{ 'verbose' } && print "Copying static file: $file\n";
                copy( "$input/$file", "$output/$file" );
            }
        }
    }
}




=begin doc

  List the names of all globally installed themes.

=end doc

=cut

sub listThemes
{
    my ($dir) = (@_);

    $CONFIG{ 'verbose' } && print "Listing themes beneath : $dir\n";

    foreach my $name ( sort( glob( $dir . "/*" ) ) )
    {
        next unless ( -d $name );

        next if ( $name =~ /\/xml$/ );

        if ( $name =~ /^(.*)\/([^\/\\]*)$/ )
        {
            print $2 . "\n";
        }
    }
}



=begin doc

Create and configure a calendar for the index, if and only iff the
HTML::CalendarMonthSimple module is installed.

=end doc

=cut

sub createCalendar
{

    #
    #  Attempt to load the module.
    #
    my $test = "use HTML::CalendarMonthSimple;";

    ## no critic (Eval)
    eval($test);
    ## use critic

    #
    #  If there was an error, or the calendar is disabled then
    # return undef.
    #
    if ( ($@) || ( $CONFIG{ 'no-calendar' } ) )
    {
        return;
    }

    #
    #  Continue
    #
    my $cal = new HTML::CalendarMonthSimple();

    # configuration of the calendar
    $cal->border(0);
    $cal->weekstartsonmonday(1);
    $cal->showweekdayheaders(1);
    $cal->sunday('Sun');
    $cal->saturday('Sat');
    $cal->weekdays( 'Mo', 'Tue', 'We', 'Thu', 'Fr' );

    # get 4th element from localtime aka month in form of (0..11)
    my $curmonth = (localtime)[4] + 1;

    foreach my $f (%data)
    {
        my $h = $data{ $f };
        next if ( !$h );
        my $entrydate = $h->{ 'date' };

        if ( !$entrydate )
        {
            my ( $dev,   $ino,     $mode, $nlink, $uid,
                 $gid,   $rdev,    $size, $atime, $mtime,
                 $ctime, $blksize, $blocks
               ) = stat($f);

            $entrydate = localtime($ctime);
        }

        my $date = time2str( "%Y-%m-%d", str2time($entrydate) );
        my ( $year, $month, $day ) = split( /-/, $date );


        if ( $month eq $curmonth )
        {
            $cal->setdatehref( $day, fileToTitle( $data{ $f }->{ 'title' } ) );
        }
    }
    return ($cal);
}



=begin doc

  Return the appropriate Data::Language instance according to the lang config
 parameter.

=end doc

=cut

sub getDateLanguageModule
{
    my $modulename = ucfirst lc $CONFIG{ 'lang' };
    my $fullmodulename = sprintf( 'Date::Language::%s', $modulename );

    ## no critic (Eval)
    eval "require $fullmodulename" or die("Not supported language");
    ## use critic

    return Date::Language->new($modulename);
}



=begin doc

  Translate a date in the appropriate language.

=end doc

=cut

sub dateI18n
{
    my $date = shift;

    my $lang = getDateLanguageModule;
    my $time = str2time($date);

    #
    # Format timestamp to readable date, using the date-format config parameter
    #
    $date = $lang->time2str( $CONFIG{ 'date-format' }, $time );

    #
    # Data::Language outputs iso-8859-1 string,
    # we force the conversion to UTF-8
    #
    #    $date = &Encode::encode_utf8($date);

    return $date;
}



=begin doc

  Return array of month names.

=end doc

=cut

sub getMonthNames
{

    #
    # Find the appropriate Data::Language module name
    #
    my $lang           = getDateLanguageModule;
    my $langmodulename = ref($lang);

    #
    # Get month names using the @MoY variable of the module
    #
    my $names_var = sprintf( '%s::MoY', $langmodulename );
    my @names;

    {
        ## no critic (ProhibitNoStrict)
        no strict 'refs';
        @names = @{ $names_var };
        use strict 'refs';
        ## use critic
    }

    #    @names = map {&Encode::encode_utf8($_)} @names;

    return @names;
}