File: main.rb

package info (click to toggle)
samidare 0.7-1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze, wheezy
  • size: 196 kB
  • ctags: 116
  • sloc: ruby: 1,883; makefile: 51
file content (1504 lines) | stat: -rwxr-xr-x 43,024 bytes parent folder | download
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
#!/usr/bin/env ruby

# main.rb - samidare main program
#
# Copyright (C) 2003,2004,2005,2006 Tanaka Akira  <akr@fsij.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#$KCODE = 'e'

require 'open-uri'
require 'uri'
require 'time'
require 'yaml'
require 'mconv'
require 'timeout'
require 'net/http'
require 'pp'
require 'optparse'
require 'zlib'
require 'lirs'
require 'autofile'
require 'htree'
require 'string-util'
require 'tempfile'
require 'presen'

CONFIG_FILENAME = 'config.yml'
STATUS_FILENAME = 'status.rm'

TEMPLATE_LATEST_FILENAME = 't.latest.html'
OUTPUT_LATEST_FILENAME = 'latest.html'
OUTPUT_LIRS_FILENAME = 'sites.lirs.gz'

AutoFile.directory = 'tmp' # xxx: should be configurable.

def Time.httpdate_robust(str)
  Time.httpdate(str) rescue Time.parse(str)
end

class Entry
  ENTRIES = {}

  def initialize(hash, config)
    @status = hash
    @config = config
    related_uris.each {|uri|
      ENTRIES[uri] ||= []
      ENTRIES[uri] << self
    }
  end
  attr_reader :status
  attr_reader :config

  def uri
    @config['URI']
  end

  def related_uris
    result = []
    result << @config['URI']
    result << @config['LinkURI'] if @config['LinkURI']
    result.concat @config['RelatedURI'].split(/\s+/) if @config['RelatedURI']
    if logseq = @status['_log']
      logseq.each {|log|
        result << log['baseURI'] if log['baseURI']
        result << log['extractedLinkURI'] if log['extractedLinkURI']
      }
    end
    result.uniq!
    result
  end

  def update_info?
    @config.include? 'UpdateInfo'
  end

  def should_check?
    ARGV.empty? or
    @status['_update_info'] or
    !(related_uris & ARGV).empty?
  end

  DefaultMinimumInterval = 5 * 60
  DefaultMaximumInterval = 24 * 60 * 60
  def timing_check
    t1 = next_timing
    t2 = Time.now
    t1 <= t2
  end

  def expect_next_by_periodical
    h = find_last_200
    return nil unless h
    periodical = h['lastModifiedSequence'] || h['periodical']
    return nil unless periodical 
    return nil if periodical.length < 2
    last_modified = periodical.last
    intervals = []
    prev = nil
    periodical.each {|curr|
      intervals << curr - prev if prev && prev < curr
      prev = curr
    }
    t = last_modified + intervals.min
    begin # adjust server/client time difference
      t += (h['clientDateEnd'] || h['clientDateBeg']) - Time.httpdate_robust(h['serverDateString'])
    rescue
    end
    t
  end

  def next_timing
    min = @config['MinimumInterval'] # xxx: should support units other than second.
    max = @config['MaximumInterval']
    if !min
      if max && max < DefaultMinimumInterval
        min = max
      else
        min = DefaultMinimumInterval
      end
    end
    if !max
      if min && DefaultMaximumInterval < min
        max = min
      else
        max = DefaultMaximumInterval
      end
    end
    max = min if max < min
    if @status['_update_info']
      Time.now
    elsif e1 = find_first_error
      e2 = find_last_error
      e1t = e1['clientDateEnd']
      e2t = e2['clientDateEnd']
      if e1.equal? e2
        r = e1t + min
      else
        r = e2t + (e2t - e1t)
      end
      if r < e1t + min
        r = e1t + min
      end
      r
    elsif s1 = find_first_200_with_current_content # Is there any 200?
      s2 = find_last_success
      s2t = s2['clientDateEnd'] || s2['clientDateBeg']

      if @config['Periodical']
        s1t = expect_next_by_periodical
        return s1t if s1t && s2t < s1t
      end
      if !s1t
        begin
          s1_client_date = s1['clientDateEnd'] || s1['clientDateBeg']
          s1_server_date = Time.httpdate_robust(s1['serverDateString'])
          s1t = Time.httpdate_robust(s1['lastModifiedString'])
          s1t += s1_client_date - s1_server_date
        rescue
          s1t = s1['clientDateEnd'] || s1['clientDateBeg']
        end
        s1t = s2t if s2t < s1t
      end

      begin
        r = s2t + (s2t - s1t)
      rescue RangeError
        r = s2t + max
      end
      if r < s2t + min
        s2t + min
      elsif s2t + max < r
        s2t + max
      else
        r
      end
    else
      Time.now
    end
  end

  def check
    uri = @config['URI']
    @status['_log'] = [] unless @status['_log']
    logseq = @status['_log']

    log = @config.dup

    log['clientDateBeg'] = client_date_1 = Time.now
    STDERR.puts "#{client_date_1.iso8601} fetch start #{uri}\n" if $VERBOSE
    page, meta = fetch(log)
    log['clientDateEnd'] = client_date_2 = Time.now
    if $VERBOSE
      if log['trouble']
        STDERR.puts "#{client_date_2.iso8601} fetch end ERROR: #{log['trouble']} #{uri}\n"
        if log['backtrace']
          STDERR.puts "|#{client_date_2.iso8601} ERROR: #{log['trouble']} (#{log['exception_class']}) #{uri}\n"
          log['backtrace'].each {|pos| STDERR.puts "| #{pos}\n" }
        end
      else
        STDERR.puts "#{client_date_2.iso8601} fetch end #{log['status']} #{log['statusMessage']} #{uri}\n"
      end
    end

    begin
      if page && !log['trouble'] && !log['backtrace'] && !log['exception_class']
        examine(page, meta, log)
      end
    rescue
      STDERR.puts "|#{Time.now.iso8601} examine ERROR: #{$!.message} (#{$!.class}) #{uri}\n"
      $!.backtrace.each {|pos| STDERR.puts "| #{pos}\n" }

      log['trouble'] = "exception"
      log['backtrace'] = $!.backtrace
      log['exception_class'] = $!.class
    end

    add_log(log)
    @status.delete '_update_info'
  end

  def fetch(log)
    uri = @config['URI']
    opts = {
      "Accept-Encoding"=>"gzip, deflate",
      "User-Agent" => @config.fetch('UserAgent', 'samidare')
    }
    if h = find_last_200 and @status['_log'].last['status'] != '412'
      # DJB's publicfile rejects requests which have If-None-Match as:
      #   412 I do not accept If-None-Match
      # Since 412 means `Precondition Failed', samidare tries request without
      # precondition.  If it success, samidare may know new valid condition.
      # For example apache is replaced with publicfile,
      # ETag should be forgotten.  Above 412 handling handle this case.
      if @config.fetch('UseIfModifiedSince', true)
        opts['If-Modified-Since'] = h['lastModifiedString'] if h['lastModifiedString']
      end
      opts['If-None-Match'] = h['eTag'] if h['eTag']
    end
    if @config.include? 'Header'
      opts.update @config['Header']
    end
    #PP.pp([uri, opts], STDERR) if $VERBOSE
    page = nil
    meta = nil
    status = nil
    status_message = nil
    exception_class = nil
    trouble = nil
    backtrace = nil
    begin
      page = timeout(@config['Timeout'] || 200) { URI.parse(uri).read(opts) }
      if page.empty?
        trouble = "empty page"
      end
      meta = page.meta
      status = page.status[0]
      status_message = page.status[1]
    rescue OpenURI::HTTPError
      if $!.io.status.first == '304'
        meta = $!.io.meta
        status = $!.io.status[0]
        status_message = $!.io.status[1]
      else
        meta = $!.io.meta
        status = $!.io.status[0]
        status_message = $!.io.status[1]
        trouble = "#{status} #{status_message}"
        exception_class = $!.class.name
      end
    rescue StandardError, TimeoutError
      trouble = $!.message
      backtrace = $!.backtrace unless TimeoutError === $!
      exception_class = $!.class.name
    end

    log['status'] = status
    log['statusMessage'] = status_message if status_message
    log['serverDateString'] = meta['date'] if meta && meta['date']
    log['trouble'] = trouble if trouble
    log['backtrace'] = backtrace if backtrace
    log['exception_class'] = exception_class if exception_class

    if @config['LogMeta']
      log['logSendHeader'] = opts
      log['logRecvHeader'] = meta
    end

    return page, meta
  end

  def examine(page, meta, log)
    uri = @config['URI']
    log['baseURI'] = page.base_uri.to_s if page.base_uri.to_s != uri
    log['lastModifiedString'] = meta['last-modified'] if meta['last-modified']
    log['eTag'] = meta['etag'] if meta['etag']
    content_type = log['contentType'] = page.content_type if page.content_type

    content, content_encoding = decode_content_encoding(page, log)
    log['content'] = AutoFile.new(uri, content, content_type)

    if 1000000 < content.length
      raise ArgumentError, "page too big (#{content.length}) : #{uri}"
    end

    content_charset = @config.fetch('ForceCharset') {
      c = page.charset { nil }
      c = nil if c && !Mconv.valid_charset?(c)
      c = @config['DefaultCharset'] if !c
      c = content.guess_charset if !c && !content_encoding
      c
    }
    content_charset = content_charset.downcase
    log['contentCharset'] = content_charset if content_charset

    if "".respond_to? :force_encoding
      if content && content_charset
        begin
          enc = Encoding.find(content_charset)
          content.force_encoding(enc)
        rescue ArgumentError
        end
      end
    end

    # checksum for gzip/deflate decoded content.
    # compression level is not affected.
    log['checksum'] = content.sum

    return if content_encoding

    decoded_content = content.decode_charset(content_charset) if content_charset

    examine_html(content_type, decoded_content, log)
    examine_lirs(decoded_content, log)

    case @config['UpdateInfo']
    when 'lirs'
      check_lirs(content)
    when 'html'
      check_html(log)
    end
  end

  def examine_html(content_type, decoded_content, log)
    if %r{\A(?:text/html|text/xml|application/(?:[A-Za-z0-9.-]+\+)?xml)\z} !~ content_type &&
      /\A<\?xml/ !~ decoded_content
      return
    end

    t = HTree.parse(decoded_content)

    title = t.title
    log['extractedTitle'] = title.to_s.strip if title

    author = t.author
    log['extractedAuthor'] = author.to_s if author

    t.traverse_element('meta', '{http://www.w3.org/1999/xhtml}meta') {|e|
      begin
        next unless e.fetch_attr("http-equiv").downcase == "last-modified"
        log['extractedLastModified'] = Time.httpdate_robust(e.fetch_attr("content"))
        break
      rescue IndexError, ArgumentError
      end
    }

    root = (t.root rescue nil)
    if root and
       root.name == 'rss' ||
       root.name == '{http://www.w3.org/1999/02/22-rdf-syntax-ns#}RDF'
      if link = t.find_element('link',
                               '{http://purl.org/rss/1.0/}link',
                               '{http://my.netscape.com/rdf/simple/0.9/}link')
        base_uri = URI.parse(log['baseURI'] || @config['URI'])
        link_uri = base_uri + link.extract_text.to_s.strip
        if link_uri.scheme == 'http' && base_uri.host.downcase == link_uri.host.downcase # xxx: refine safety test
          log['extractedLinkURI'] = link_uri.to_s
        end
      end
    end

    if root
      ['http://www.w3.org/2005/Atom', 'http://purl.org/atom/ns#'].each {|xmlns|
        if root.name == "{#{xmlns}}feed"
          link_uri = nil
          t.traverse_element("{#{xmlns}}link") {|e|
            begin
              next unless e.fetch_attr('rel').downcase == 'alternate'
              base_uri = URI.parse(log['baseURI'] || @config['URI'])
              link_uri = base_uri + e.fetch_attr('href').to_s.strip
              break
            rescue IndexError, ArgumentError
            end
          }
          if link_uri and
              link_uri.scheme == 'http' && base_uri.host.downcase == link_uri.host.downcase # xxx: refine safety test
            log['extractedLinkURI'] = link_uri.to_s
            break
          end
        end
      }
    end

    t, checksum_filter = ignore_tree(t)
    log['checksum_filter'] = checksum_filter unless checksum_filter.empty?
    log['checksum_filtered'] = t.extract_text.rcdata.gsub(/\s+/, '').sum
  end

  def ignore_tree(tree, config=@config)
    if ignore_path = config['IgnorePath']
      ignore_path = ignore_path.split(/\s+/)
    else
      ignore_path = []
    end
    ignore_pattern = path2pattern(*ignore_path)

    if ignore_class = config['IgnoreClass']
      ignore_class = ignore_class.split(/\s+/)
    else
      ignore_class = []
    end

    if ignore_id = config['IgnoreID']
      ignore_id = ignore_id.split(/\s+/)
    else
      ignore_id = []
    end

    if ignore_element = config['IgnoreElement']
      ignore_element = ignore_element.split(/\s+/)
    else
      ignore_element = []
    end

    ignore_text = []
    if ignore_text_raw = config['IgnoreText']
      ignore_text_raw.scan(%r{/((?:[^/\\]|\\.)*)/}) { ignore_text << Regexp.compile($1) }
    end

    t = tree.make_loc.filter {|e|
      path = e.path.sub(%r{^doc\(\)}, '')
      e = e.to_node
      not(
        (e.text? && (/\A\s*\z/ =~ e.to_s ||
                     ignore_text.any? {|pat| pat =~ e.to_s })) ||
        (e.elem? && (%r[\A(?:\{http://www.w3.org/1999/xhtml\})?style\z] =~ e.name ||
                     %r[\A(?:\{http://www.w3.org/1999/xhtml\})?script\z] =~ e.name ||
                     ignore_element.include?(e.name))) ||
        ignore_pattern =~ path ||
        (e.elem? && ((cs = e.get_attr('class') and ignore_class & cs.split(/\s+/) != []) ||
                     ignore_id.include?(e.get_attr('id'))))
      )
    }

    # xxx: checksum_filter format should be changed.
    checksum_filter = []
    checksum_filter.concat ['IgnorePath', *ignore_path] if !ignore_path.empty?
    checksum_filter.concat ['IgnoreClass', *ignore_class] if !ignore_class.empty?
    checksum_filter.concat ['IgnoreID', *ignore_id] if !ignore_id.empty?
    checksum_filter.concat ['IgnoreElement', *ignore_element] if !ignore_element.empty?
    checksum_filter.concat ['IgnoreText', *ignore_text] if !ignore_text.empty?

    [t, checksum_filter]
  end

  def examine_lirs(decoded_content, log)
    uri = URI.parse(@config['URI'])
    return if %r{\.lirs(?:.gz)?\z} !~ uri.path
    return if log['extractedTitle'] && log['extractedAuthor']

    lirs = LIRS.decode(decoded_content)

    if $VERBOSE
      lirs.each {|record|
        now = Time.now
        if record.last_modified != '0' &&
           record.last_detected != '0' &&
           record.last_modified.to_i > record.last_detected.to_i
          STDERR.puts "#{now.iso8601} info: strange LIRS: Last-Modified/Detected inversion: #{record.last_modified.to_i - record.last_detected.to_i}sec #{uri} #{record.encode.inspect}\n"
        end
        if record.last_modified != '0' && record.last_modified.to_i > now.to_i
          STDERR.puts "#{now.iso8601} info: strange LIRS: future Last-Modified: #{record.last_modified.to_i - now.to_i}sec #{uri} #{record.encode.inspect}\n"
        end
      }
    end

    return if lirs.size != 1
    
    lirs.each {|record|
      target_uri = URI.parse(record.target_url)
      return if target_uri.scheme != uri.scheme
      return if target_uri.host.downcase != uri.host.downcase
      log['extractedLinkURI'] = record.target_url unless log['extractedLinkURI']
      log['extractedTitle'] = record.target_title unless log['extractedTitle']
      log['extractedAuthor'] = record.target_maintainer unless log['extractedAuthor']
      log['extractedLinkURI'] = record.target_url unless log['extractedLinkURI']
    }
  rescue LIRS::Error
    # ignore invalid lirs file.
    STDERR.puts "#{Time.now.iso8601} LIRS Error: #{uri}\n" if $VERBOSE
  end

  def path2pattern(*paths)
    /\A#{Regexp.union(*paths.map {|path|
      Regexp.new(path.gsub(%r{[^/]+}) {|step|
        if /\[(\d+)\]\z/ =~ step
          n = $1.to_i
          if $1.to_i == 1
            Regexp.quote($`) + "(?:\\[#{n}\\])?"
          else
            Regexp.quote(step)
          end
        else
          Regexp.quote(step) + '(\[\d+\])?'
        end
      }.gsub(%r{//+}) {
        "/(?:[^/]+/)*"
      })
    })}\z/
  end

  def decode_content_encoding(page, log)
    content = page
    if page.content_encoding.empty?
      content = content.dup.force_encoding("ascii-8bit") if content.respond_to? :force_encoding
      if /\A\x1f\x8b/n =~ content # gziped?
        begin
          content = content.decode_gzip
        rescue Zlib::Error
        end
      end
    else
      content_encoding = page.content_encoding.dup
      while !content_encoding.empty?
        case content_encoding.last
        when 'gzip', 'x-gzip'
          begin
            content = content.decode_gzip
          rescue Zlib::Error
            break
          end
        when 'deflate'
          content = content.decode_deflate
        else
          break
        end
        content_encoding.pop
      end
      content_encoding = nil if content_encoding.empty?
      log['contentEncoding'] = content_encoding if content_encoding
    end
    return content, content_encoding
  end

  def check_lirs(new_lirs)
    logseq = @status['_log']
    old_lirs = nil
    logseq.reverse_each {|h|
      if h['content']
        begin
          old_lirs = h['content'].content
          break
        rescue Errno::ENOENT
        end
      end
    }
    begin
      l1 = LIRS.decode(old_lirs) if old_lirs
      l2 = LIRS.decode(new_lirs)
      count_all = 0
      count_interest = 0
      count_update = 0
      l2.each {|r2|
        count_all += 1
        uri = r2.target_url
        if es = ENTRIES[uri]
          es.each {|e|
            count_interest += 1
            if !old_lirs or (r1 = l1[uri] and r1.last_modified != r2.last_modified)
              t1 = Time.at(r1.last_modified.to_i) if old_lirs
              t2 = Time.at(r2.last_modified.to_i)
              last_success = e.find_last_success
              if last_success && last_success['clientDateEnd'] < t2 #xxx: not so acculate.
                count_update += 1
                p [:LIRS_UPDATE, uri, t1, t2, @config['URI']]
                e.status['_update_info'] = true
              end
            end
          }
        end
      }
      STDERR.puts "LIRS total: #{count_update} / #{count_interest} / #{count_all} - #{@config['URI']}\n" if $VERBOSE
    rescue
      # External update information is a just hint.
      # So it is ignorable even if it has some trouble.
      STDERR.puts "check_lirs: error on #{@config['URI']}: #$!\n"
      #pp $!.backtrace
    end
  end

  def extract_html_update_info_rec(elt, result, base_uri_cell)
    hrefs = []

    if elt.elem? && %r[\A(?:\{http://www.w3.org/1999/xhtml\})?base\z] =~ elt.name
      if href = elt.get_attr('href')
        base_uri_cell[0] = URI.parse(href)
      end
    elsif elt.elem? && %r[\A(?:\{http://www.w3.org/1999/xhtml\})?a\z] =~ elt.name
      if href = elt.get_attr('href')
        href = (base_uri_cell[0] + URI.parse(href)).to_s
        hrefs << href if ENTRIES[href]
      end
    else
      elt.children.each {|e|
        next unless e.elem?
        hrefs.concat extract_html_update_info_rec(e, result, base_uri_cell)
      }
    end

    hrefs.uniq!

    if elt.elem? && @config.fetch('UpdateElement', %r[\A(?:\{http://www.w3.org/1999/xhtml\})?a\z]) === elt.name
      hrefs.each {|uri|
        result[uri] ||= []
        result[uri] << elt
      }
      return []
    else
      return hrefs
    end
  end

  def extract_html_update_info(log)
    return {} unless log && log['content'] && log['contentCharset']
    begin
      content = log['content'].content
    rescue Errno::ENOENT
      return {}
    end
    content = content.decode_charset(log['contentCharset'])
    tree = HTree.parse(content)
    tree, checksum_filter = ignore_tree(tree, log)
    base_uri = URI.parse(log['baseURI'] || log['URI'])
    extract_html_update_info_rec(tree, info={}, [base_uri])
    info
  end

  def compare_html_update_info(old_log, new_log)
    count_update = 0
    count_interest = 0

    old_info = extract_html_update_info(old_log)
    new_info = extract_html_update_info(new_log)

    (old_info.keys & new_info.keys).each {|uri|
      count_interest += 1
      old_str = old_info[uri].map {|elt| elt.extract_text }.join
      new_str = new_info[uri].map {|elt| elt.extract_text }.join
      if old_str != new_str
        #pp [uri, old_info[uri]]
        #pp [uri, new_info[uri]]
        count_update += 1
        p [:HTML_UPDATE, uri, old_str, new_str, @config['URI']]
        yield uri
      end
    }

    STDERR.puts "HTML total: #{count_update} / #{count_interest} - #{@config['URI']}\n" if $VERBOSE
  end

  def check_html(new_log=nil)
    begin
      if new_log
        old_log = find_last_200
      else
        new_log = find_last_200
        old_log = find_last_200_with_previous_content
      end
      compare_html_update_info(old_log, new_log) {|uri|
        ENTRIES[uri].each {|e|
          e.status['_update_info'] = true
        }
      }
    rescue
      # External update information is a just hint.
      # So it is ignorable even if it has some trouble.
      STDERR.puts "check_html error on #{@config['URI']}: #$!\n"
    end
  end

  StatusMap = {
    '200' => 's', # Success
    '304' => 'n', # Not-Modified
  }
  StatusMap.default = 'e' # Error

  MaxPeriodicalNum = 30
  def add_periodical_info(h)
    return if h['status'] != '200'

    logseq = @status['_log']

    periodical = nil
    logseq.reverse_each {|l|
      if l['lastModifiedSequence']
        periodical = l['lastModifiedSequence'].dup
        break
      elsif l['periodical']
        periodical = l['periodical'].dup
        break
      end
    }
    periodical ||= []

    begin
      t = Time.httpdate_robust(h['lastModifiedString'])
    rescue
    end

    if t && (periodical.empty? || periodical.last != t)
      periodical << t
    end

    if MaxPeriodicalNum < periodical.length
      periodical = periodical[(-MaxPeriodicalNum)..(-1)]
    end

    h['lastModifiedSequence'] = periodical if !periodical.empty?
  end

  def content_unchanged(log1, log2)
    return true if log1['checksum'] &&
                   log2['checksum'] &&
                   log1['checksum'] == log2['checksum']
    return true if log1['checksum_filtered'] &&
                   log2['checksum_filtered'] &&
                   log1['checksum_filter'] == log2['checksum_filter'] &&
                   log1['checksum_filtered'] == log2['checksum_filtered']
    false
  end

  def add_log(log)
    @status['_log'] = [] unless @status['_log']

    #add_periodical_info(log) if @config['Periodical']
    add_periodical_info(log)

    logseq = @status['_log']

    case StatusMap[log['status']]
    when 'e'
      history = logseq.map {|l| StatusMap[l['status']] }.join + ' ' + StatusMap[log['status']]
      if /ee e/ =~ history
        logseq.pop
      end
      logseq << log
    when 's', 'n'
      logseq.reject! {|l| StatusMap[l['status']] == 'e' }
      logseq.shift while !logseq.empty? && StatusMap[logseq.first['status']] == 'n'

      logseq << log

      #pp logseq.map {|l| StatusMap[l['status']] + "#{l['checksum']}" }
      logs = [[]]
      logseq.each {|l|
        if logs.last.empty?
          logs.last << l
        elsif StatusMap[l['status']] == 'n'
          logs.last << l
        elsif content_unchanged(logs.last.last, l)
          logs.last << l
        else
          logs << [l]
        end
      }
      #pp logs.map {|ll| ll.map {|l| StatusMap[l['status']] + "#{l['checksum']}" } }

      logs[0...-1].each {|ll|
        ll.reject! {|l| StatusMap[l['status']] == 'n' }
        ll[1..-1] = [] if 2 <= ll.length
      }

      ll = logs.last
      ll.reject! {|l| StatusMap[l['status']] == 'n' } # removes `log' if it is 'n'.
      ll[1...-1] = [] if 2 < ll.length 
      if StatusMap[log['status']] == 'n'
        ll << log # re-add `log'.
      end

      num_discards = 0

      nlogs = @config.fetch('NumLogs', 2)
      #pp logs.map {|ll| ll.map {|l| StatusMap[l['status']] + "#{l['checksum']}" } }
      if nlogs < logs.length
        num_discards = logs.length - nlogs
      end
      #pp logs.map {|ll| ll.map {|l| StatusMap[l['status']] + "#{l['checksum']}" } }

      if log_expire = @config['LogExpire']
        log_expire = parse_time_suffix(log_expire) || 0 if String === log_expire
        limit = Time.now - log_expire
        while 0 < num_discards && limit < logs[num_discards-1][0]['clientDateBeg']
          num_discards -= 1
        end
      end

      logs[0, num_discards] = []

      logseq.replace logs.flatten
    else
      raise "unrecognized log-status [bug]: #{StatusMap[log['status']].inspect}"
    end

    unless logseq.last.equal? log
      raise "current log is not added [bug]"
    end
  end

  def parse_time_suffix(str)
    case str
    when /\A(\d+)(s|sec|second)?\z/
      $1.to_i
    when /\A(\d+)(m|min|minute)\z/
      $1.to_i * 60
    when /\A(\d+)(h|hour)\z/
      $1.to_i * 60 * 60
    when /\A(\d+)(d|day)\z/
      $1.to_i * 60 * 60 * 24
    else
      nil
    end
  end

  def find_last_200_with_previous_content
    current = nil
    @status['_log'].reverse_each {|log|
      next unless log['status'] == '200'
      if !current
        current = log
      elsif !content_unchanged(current, log)
        return log
      end
    }
    nil
  end

  def find_first_200_with_current_content
    result = nil
    @status['_log'].reverse_each {|h|
      if h['status'] == '200'
        if !result
          result = h
        elsif !content_unchanged(result, h)
          return result
        else
          result = h
        end
      end
    }
    result
  end

  def find_last_200
    @status['_log'].reverse_each {|h|
      return h if h['status'] == '200'
    }
    nil
  end

  def find_last_success # 200 or 304
    @status['_log'].reverse_each {|h|
      return h if StatusMap[h['status']] != 'e'
    }
    nil
  end

  def find_first_error
    @status['_log'].each {|h|
      return h if StatusMap[h['status']] == 'e'
    }
    nil
  end

  def find_last_error
    @status['_log'].reverse_each {|h|
      return h if StatusMap[h['status']] == 'e'
    }
    nil
  end

  def presentation_data
    h = @config.dup
    logseq = @status['_log']
    h.update @status # xxx
    h['title'] = h['Title']
    h['author'] = h['Author']
    h['last-modified'] = nil
    h['info'] = ''
    h['linkURI'] = h['LinkURI']
    unless logseq.empty?
      if l = find_first_200_with_current_content
        h['last-modified-found'] = l['clientDateBeg'] # xxx: clientDateEnd is better?
        if l.include?('lastModifiedString') and
           begin
            h['last-modified'] = Time.httpdate_robust(l['lastModifiedString'])
           rescue
             false
           end
          h['last-modified'].localtime
          l2 = find_last_200
          unless l.equal? l2
            if l['lastModifiedString'] != l2['lastModifiedString']
              h['info'] << '[Touch]'
            else
              h['info'] << '[NoIMS]'
            end
          end
        elsif l.include? 'extractedLastModified'
          h['last-modified'] = l['extractedLastModified'].getlocal
        else
          h['last-modified'] = l['clientDateBeg'].getlocal
          h['info'] << '[NoLM]'
        end
        if l.include? 'baseURI'
          h['info'] << '[Redirect]'
        end
        h['title'] ||= l['extractedTitle'] if l['extractedTitle']
        h['author'] ||= l['extractedAuthor'] if l['extractedAuthor']
        h['linkURI'] ||= l['extractedLinkURI'] if l['extractedLinkURI']
      end
      if StatusMap[logseq.last['status']] == 'e'
        l = logseq.last
        if l['status']
          if l['statusMessage']
            h['info'] << "[#{l['status']} #{l['statusMessage']}]"
          else
            h['info'] = "[#{l['status']}]"
          end
        elsif l['trouble']
          h['info'] = "[#{l['trouble']}]"
        else
          h['info'] = '[no status]'
        end
      end
    end
    h['title'] ||= h['LinkURI'] if h['LinkURI']
    h['title'] ||= h['URI']
    h['linkURI'] ||= h['URI']

    h
  end

  def merge(hash)
    @status = hash.dup.update(@status)
  end

  def recent_log2
    if logseq = @status['_log']
      log1 = log2 = nil
      logseq.reverse_each {|log|
        next unless log['content']
        if log2 == nil
          log2 = log
        else
          log1 = log
          break unless content_unchanged(log1, log2)
        end
      }
      [log1, log2]
    end
  end

  def dump_filenames2
    log1, log2 = recent_log2
    puts "#{log1['content'].pathname}\n" if log1
    puts "#{log2['content'].pathname}\n" if log2
  end

  def diff_content
    log1, log2 = recent_log2
    return unless log1 && log2
    filename1 = log1['content'].pathname
    filename2 = log2['content'].pathname
    tree1, checksum_filter1 = ignore_tree(HTree.parse(File.read(filename1).decode_charset_guess), log1)
    tree2, checksum_filter2 = ignore_tree(HTree.parse(File.read(filename2).decode_charset_guess), log2)

    text1 = []
    tree1.make_loc.traverse_text {|n|
      path = n.path.sub(%r{^doc\(\)}, '')
      n = n.to_node
      text1 << [n.to_s, path]
    }

    text2 = []
    tree2.make_loc.traverse_text {|n|
      path = n.path.sub(%r{^doc\(\)}, '')
      n = n.to_node
      text2 << [n.to_s, path]
    }

    puts "checksum1: #{tree1.extract_text.to_s.sum} #{checksum_filter1.inspect} #{filename1}\n"
    puts "checksum2: #{tree2.extract_text.to_s.sum} #{checksum_filter2.inspect} #{filename2}\n"

    [text1.length, text2.length].min.times {
      t1, p1 = text1.last
      t2, p2 = text2.last
      t1 = t1.gsub(/\s+/, '') if t1
      t2 = t2.gsub(/\s+/, '') if t2
      if t1 == t2
        text1.pop
        text2.pop
      else
        break
      end
    }

    num = 10
    0.upto([text1.length, text2.length].max - 1) {|i|
      t1, p1 = text1[i]
      t2, p2 = text2[i]
      t1 = t1.gsub(/\s+/, '') if t1
      t2 = t2.gsub(/\s+/, '') if t2
      if t1 != t2
        pp [text1[i], text2[i]]
        num -= 1
        if num == 0
          puts "...\n"
          break
        end
      end
    }

    tf1 = Tempfile.new('htmldiff1')
    PP.pp(tree1, tf1)
    tf1.close

    tf2 = Tempfile.new('htmldiff2')
    PP.pp(tree2, tf2)
    tf2.close

    system("diff -u #{tf1.path} #{tf2.path}")
  end
end

module Enumerable
  $opt_max_threads = 8
  def concurrent_map(max_threads=$opt_max_threads, &block)
    arr = self.to_a.dup
    if max_threads == 1 || arr.length == 1
      self.map(&block)
    else
      queue = (0...arr.length).to_a

      max_threads = arr.length if arr.length < max_threads

      threads = []
      max_threads.times {
        threads << Thread.new {
          while i = queue.shift
            arr[i] = yield arr[i]
          end
        }
      }

      threads.each {|t| t.join }
      arr
    end
  end
end

class Samidare
  def open_lock(filename, nonblock=false)
    dirname = File.dirname filename
    basename = File.basename filename
    tmpname = "#{dirname}/.,#{basename},#$$"

    1.times {
      begin
        target = File.open(filename, File::RDWR|File::CREAT)
        stat1 = target.stat
        if nonblock
          unless target.flock(File::LOCK_EX | File::LOCK_NB)
            STDERR.puts "fail to lock: #{filename}\n"
            return
          end
        else
          target.flock(File::LOCK_EX)
        end
        stat2 = File.stat(filename)
        redo if stat1.ino != stat2.ino

        begin
          File.open(tmpname, 'w') {|tmp|
            yield target, tmp
          }
          stat2 = File.stat(filename) # manually unlocked?
          File.rename(tmpname, filename) if stat1.ino == stat2.ino
        ensure
          File.unlink tmpname if FileTest.exist? tmpname
        end
      ensure
        target.close if target
      end
    }
  end

  def config_flatten(arr, default={})
    result = []
    arr.each {|elt|
      case elt
      when Hash
        if elt.include? 'URI'
          result << default.dup.update(elt)
        else
          default = default.dup.update(elt)
        end
      when Array
        result.concat config_flatten(elt, default)
      when String
        result << default.dup.update({'URI'=>elt})
      end
    }
    result
  end

  def deep_copy(o)
    Marshal.load(Marshal.dump(o))
  end

  def deep_freeze(o)
    objs = []
    o = Marshal.load(Marshal.dump(o), lambda {|obj| objs << obj; obj })
    objs.each {|obj| obj.freeze }
    o
  end

  def load_config
    @configuration = {}
    config = config_flatten(File.open(CONFIG_FILENAME) {|f| YAML.load(f) })
    config.each_with_index {|h, i|
      h.reject! {|k, v| /\A[A-Z]/ !~ k }
      uri = h['URI']
      if @configuration.include? uri
        @configuration[uri].update h
        config[i] = nil
      else
        @configuration[uri] = h
      end
    }
    config.compact!
    config
  end

  #def load_status(f) YAML.load(f) end
  #def save_status(f, d) f.puts d.to_yaml end
  def load_status(f) Marshal.load(f) end
  def save_status(d, f) Marshal.dump(d, f) end

  def open_status(readonly=false)
    if readonly
      open(STATUS_FILENAME) {|f|
        if f.stat.size == 0
          status = []
        else
          status = load_status(f)
        end
        status = deep_freeze(status)
        yield status
      }
    else
      open_lock(STATUS_FILENAME, true) {|f, out|
        if f.stat.size == 0
          status = []
        else
          status = load_status(f)
          AutoFile.clear
        end
        yield status
        save_status(status, out)
      }
    end
  end

  def output_file(filename, content)
    dir = File.dirname filename
    if FileTest.writable? dir
      filename_new = filename + '.new'
      open(filename_new, 'w') {|f|
        f.print content
      }
      File.rename filename_new, filename
    else
      open(filename, 'w') {|f|
        f.print content
      }
    end
  end

  class GuessCharsetPathname
    def initialize(filename)
      @filename = filename
    end

    def read
      result = File.read(@filename)
      result.decode_charset_guess
    end
  end

  def generate_output(data)
    data = Presen.new(data)
    result = HTree.expand_template(GuessCharsetPathname.new(@opt_template), data, '')
    result << "\n" if /\n\z/ !~ result
    if @opt_output != '-'
      output_file(@opt_output, result)
      output_file(@opt_output + '.gz', result.encode_gzip)
    else
      puts "#{result}\n"
    end
  end

  def generate_lirs(data)
    str = ''
    data["antenna"].each {|h|
      next unless h['last-modified'] && h['last-modified-found']
      #p ['LIRS', h['last-modified'], h['last-modified-found'], h['URI'], h['title'], h['author']]
      str << 'LIRS,'
      str << h['last-modified'].to_i.to_s << ','
      str << h['last-modified-found'].to_i.to_s << ','
      str << '32400,'
      str << '0,'
      str << h['linkURI'].gsub(/[,\\]/) { "\\#$&" } << ','
      str << h['title'].gsub(/[,\\]/) { "\\#$&" }.strip.gsub(/\s/, ' ') << ','
      str << (h['author'] || '0').gsub(/[,\\]/) { "\\#$&" }.strip.gsub(/\s/, ' ') << ','
      str << '0,'
      str << "\n"
    }
    str = str.encode_charset('euc-jp')
    str = str.encode_gzip

    output_file(@opt_output_lirs, str)
  end

  def parse_options
    @opt_output = OUTPUT_LATEST_FILENAME
    @opt_output_lirs = OUTPUT_LIRS_FILENAME
    @opt_dont_check = nil
    @opt_force_check = nil
    @opt_timing = nil
    @opt_dump_config = nil
    @opt_dump_status = nil
    @opt_dump_template_data = nil
    @opt_template = TEMPLATE_LATEST_FILENAME
    @opt_remove_entry = nil
    @opt_dump_filenames = nil
    @opt_dump_filenames2 = nil
    @opt_diff_content = nil
    ARGV.options {|q|
      q.banner = 'samidare [opts]'
      q.def_option('--help', 'show this message') {puts q; exit(0)}
      q.def_option('--verbose', '-v', 'verbose') { $VERBOSE = true }
      q.def_option('--no-check', '-n', 'don\'t check web') { @opt_dont_check = true }
      q.def_option('--force', '-f', 'force check (avoid timing control mechanism)') { @opt_force_check = true }
      q.def_option('--output=filename', '-o', 'specify output html file') {|filename| @opt_output = filename }
      q.def_option('--output-lirs=filename', 'specify output lirs file') {|filename| @opt_output_lirs = filename }
      q.def_option('--template=filename', '-T', 'specify template') {|filename| @opt_template = filename }
      q.def_option('--timing', '-t', 'show timings') { @opt_timing = true }
      q.def_option('--dump-config', 'dump flatten configuration') { @opt_dump_config = true }
      q.def_option('--dump-status', 'dump status') { @opt_dump_status = true }
      q.def_option('--dump-template-data', 'dump data for expand template') { @opt_dump_template_data = true }
      q.def_option('--dump-filenames', 'dump filenames of specified entry') { @opt_dump_filenames = true }
      q.def_option('--dump-filenames2', 'dump two recent filenames') { @opt_dump_filenames2 = true }
      q.def_option('--remove-entry', 'remove entry') { @opt_remove_entry = true }
      q.def_option('--single-thread', 'disable multi-threading') { $opt_max_threads = 1 }
      q.def_option('--diff-content', 'show difference') { @opt_diff_content = true }
      q.parse!
    }
    require 'resolv-replace' if $opt_max_threads != 1
  end

  def dump_status(status, entries)
    if ARGV.empty?
      status.each {|ent|
        pp ent
      }
    else
      entries.each {|ent|
        unless (ARGV & ent.related_uris).empty?
          pp ent.status
          puts "next time: #{ent.next_timing.localtime}"
        end
      }
    end
  end

  def dump_filenames(entries)
    entries.each {|ent|
      unless (ARGV & ent.related_uris).empty?
        if logseq = ent.status['_log']
          logseq.each {|log|
            if content = log['content']
              puts content.pathname
            end
          }
        end
      end
    }
  end

  def dump_filenames2(entries)
    entries.each {|ent|
      unless (ARGV & ent.related_uris).empty?
        ent.dump_filenames2
      end
    }
  end

  def diff_content(entries)
    entries.each {|ent|
      unless (ARGV & ent.related_uris).empty?
        ent.diff_content
      end
    }
  end

  def create_entries(config, status, readonly=false)
    logs = {}
    status.each {|status_ent|
      if status_ent.include?('URI') && status_ent.include?('_log')
        logs[status_ent['URI']] = status_ent['_log']
      end
    }

    status.clear unless readonly

    entries = []
    config.each {|config_ent|
      uri = config_ent['URI']
      logseq = logs[uri] || []
      status_ent = { 'URI' => uri, '_log' => logseq }
      status << status_ent unless readonly
      entries << Entry.new(status_ent, config_ent)
    }
    entries
  end

  def main
    parse_options
    config = load_config
    if @opt_dump_config
      puts config.to_yaml
      return
    end
    data = nil
    readonly =
      @opt_timing ||
      @opt_dont_check ||
      @opt_dump_status ||
      @opt_dump_template_data ||
      @opt_dump_filenames ||
      @opt_dump_filenames2 ||
      @opt_diff_content
    open_status(readonly) {|status|
      entries = create_entries(config, status, readonly)
      if @opt_dump_status
        dump_status(status, entries)
      elsif @opt_dump_filenames
        dump_filenames(entries)
      elsif @opt_dump_filenames2
        dump_filenames2(entries)
      elsif @opt_diff_content
        diff_content(entries)
      elsif @opt_timing
        entries = entries.map {|entry|
          [entry.next_timing.localtime, entry]
        }.sort
        now = Time.now
        entries.each {|timing, entry|
          if now && timing > now
            puts "#{now}  --- now ---"
            now = nil
          end
          h = entry.presentation_data
          s = "#{timing}: #{h['title']}"
          s << " (#{h['Author']})" if h['Author']
          puts s
        }
	if now
	  puts "#{now}  --- now ---"
	end
      elsif @opt_remove_entry
        removing_uris = {}
        entries.each {|e|
          unless (e.related_uris & ARGV).empty? || e.status['_log'].empty?
            removing_uris[e.uri] = true
          end
        }
        status.reject! {|log|
          uri = log['URI']
          if removing_uris[uri]
            STDERR.puts "removed: #{uri}" if $VERBOSE
            true
          else
            false
          end
        }
      else
        unless readonly
          update_info_entries, non_update_info_entries = entries.partition {|e| e.update_info? }
          update_info_entries.concurrent_map {|entry|
            next unless entry.update_info?
            entry.check if entry.should_check? && (@opt_force_check || entry.timing_check)
          }
          non_update_info_entries.concurrent_map {|entry|
            next if entry.update_info?
            entry.check if entry.should_check? && (@opt_force_check || entry.timing_check)
          }
        end
        update_infos, entries = entries.partition {|entry| entry.update_info? }
        data = {
          "antenna" =>
          entries.map {|entry| entry.presentation_data }.sort_by {|h|
            # [h['last-modified'], h['title']]
            if h['last-modified-found']
              [2, h['last-modified-found'], h['last-modified'], h['title']]
            elsif h['last-modified']
              [1, h['last-modified'], h['title']]
            else
              [0, h['title']]
            end
          }.reverse,
          "update_info" =>
          update_infos.map {|update_info| update_info.presentation_data }.sort_by {|h|
            # [h['last-modified'], h['title']]
            if h['last-modified-found']
              [2, h['last-modified-found'], h['last-modified'], h['title']]
            elsif h['last-modified']
              [1, h['last-modified'], h['title']]
            else
              [0, h['title']]
            end
          }.reverse
        }
        if @opt_dump_template_data
          pp data
          return
        end
        STDERR.print "generating output..." if $VERBOSE
        t1 = Time.now
        generate_output(data)
        t2 = Time.now
        STDERR.puts " #{t2-t1}sec" if $VERBOSE
        STDERR.print "generating lirs..." if $VERBOSE
        t1 = Time.now
        generate_lirs(data)
        t2 = Time.now
        STDERR.puts " #{t2-t1}sec" if $VERBOSE
      end
    }
    #PP.pp(data, STDERR) if $VERBOSE
  end

end

class Hash
  v = $VERBOSE
  $VERBOSE = nil
  def pretty_print(q)
    q.group(1, '{', '}') {
      keys = self.keys
      keys.sort! if keys.all? {|k| String === k }
      q.seplist(keys) {|k|
        v = self[k]
        q.group {
          q.pp k
          q.text '=>'
          q.group(1) {
            q.breakable ''
            q.pp v
          }
        }
      }
    }
  end
  $VERBOSE = v
end

if $0 == __FILE__
  Samidare.new.main
end