summaryrefslogtreecommitdiff
path: root/tools/yelp-check.in
blob: 9510f5a139052c5010c663d42a4da354ac4b4bcb (plain)
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
#!/bin/sh
# -*- indent-tabs-mode: nil -*-
#
# yelp-check
# Copyright (C) 2011-2015 Shaun McCance <shaunm@gnome.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.

xsl_mal_link='@XSL_MAL_LINK@'
xsl_mal_license='@DATADIR@/xslt/mal-license.xsl'
xsl_mal_status='@DATADIR@/xslt/mal-status.xsl'
xsl_mal_rng='@DATADIR@/xslt/mal-rng.xsl'
xsl_comments='@DATADIR@/xslt/comments.xsl'
xsl_media='@DATADIR@/xslt/media.xsl'

yelp_check_retval="0"

urlencode () {
    # We usually don't want to urlencode slashes, because we're
    # usually converting file paths to URIs. But we do want to
    # urlencode slases for names of RNG files in validate_page.
    if [ "x$2" = "x/" ]; then
        urlencode_slash=''
    else
        urlencode_slash='\/'
    fi
    echo "$1" | LC_ALL=C awk '
BEGIN {
  for (i = 1; i <= 255; i++) chars[sprintf("%c", i)] = i;
}
{
  ret = "";
  for (i = 1; i <= length($0); i++) {
    c = substr($0, i, 1);
    if (c ~ /['$urlencode_slash'a-zA-Z0-9._-]/)
      ret = ret c;
    else
      ret = ret sprintf("%%%X%X", int(chars[c] / 16), chars[c] % 16);
  }
  print ret;
}'
}

urldecode () {
    echo "$1" | LC_ALL=C awk '
BEGIN {
  for(i = 0; i < 10; i++) hex[i] = i;
  hex["A"] = hex["a"] = 10;
  hex["B"] = hex["b"] = 11;
  hex["C"] = hex["c"] = 12;
  hex["D"] = hex["d"] = 13;
  hex["E"] = hex["e"] = 14;
  hex["F"] = hex["f"] = 15;
}
{
  ret = "";
  for (i = 1; i <= length($0); i++) {
    c = substr($0, i, 1);
    if (c == "+") {
      ret = ret " ";
    }
    else if (c == "%") {
      c = sprintf("%c", hex[substr($0, i + 1, 1)] * 16 + hex[substr($0, i + 2, 1)]);
      ret = ret c;
      i += 2;
    }
    else {
      ret = ret c;
    }
  }
  print ret;
}'
}

docbook_version='
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:db="http://docbook.org/ns/docbook"
    version="1.0">
<xsl:output method="text"/>
<xsl:template match="/">
  <xsl:choose>
    <xsl:when test="/db:*/@version">
      <xsl:value-of select="/db:*/@version"/>
    </xsl:when>
    <xsl:when test="/db:*">
      <xsl:text>5.0</xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text>4</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>
</xsl:stylesheet>
'

yelp_usage() {
    (
        echo "Usage: yelp-check <COMMAND> [OPTIONS] [FILES]"
        echo ""
        echo "Commands:"
        echo "  comments      Print the editorial comments in a document"
        echo "  hrefs         Find broken external links in a document"
        echo "  ids           Find Mallard page IDs that do not match file names"
        echo "  license       Report the license of Mallard pages"
        echo "  links         Find broken xref or linkend links in a document"
        echo "  media         Find broken references to media files"
        echo "  orphans       Find orphaned pages in a Mallard document"
        echo "  status        Report the status of Mallard pages"
        echo "  validate      Validate files against a DTD or RNG"
    ) 1>&2
}
yelp_usage_hrefs () {
    (
        echo "Usage: yelp-check hrefs <FILES>"
        echo ""
        echo "  Find broken href links in FILES in a Mallard document, or"
        echo "  broken ulink or XLink links in FILES in a DocBook document."
        echo ""
        echo "Options:"
        echo "  -s            Treat pages as belonging to a Mallard site"
    ) 1>&2
}
yelp_usage_ids () {
    (
        echo "Usage: yelp-check ids <FILES>"
        echo ""
        echo "  Find pages in a Mallard document whose page ID does not match"
        echo "  the base file name of the page file."
    ) 1>&2
}
yelp_usage_links () {
    (
        echo "Usage: yelp-check links <FILES>"
        echo ""
        echo "  Find broken xref links in FILES in a Mallard document,"
        echo "  or broken linkend links in FILES in a DocBook document."
        echo ""
        echo "Options:"
        echo "  -c CACHE      Use the existing Mallard cache CACHE"
        echo "  -i            Ignore xrefs where href is present"
    ) 1>&2
}
yelp_usage_media () {
    (
        echo "Usage: yelp-check media <FILES>"
        echo ""
        echo "  Find broken references to media files. In Mallard, this"
        echo "  checks media and thumb elements. In DocBook, this checks"
        echo "  audiodata, imagedata, and videodata elements."
    ) 1>&2
}
yelp_usage_orphans () {
    (
        echo "Usage: yelp-check orphans <FILES>"
        echo ""
        echo "  Locate orphaned pages among FILES in a Mallard document."
        echo "  Orphaned pages are any pages that cannot be reached by"
        echo "  topic links alone from the index page."
        echo ""
        echo "Options:"
        echo "  -c CACHE      Use the existing Mallard cache CACHE"
    ) 1>&2
}
yelp_usage_comments () {
    (
        echo "Usage: yelp-check comments <FILES>"
        echo ""
        echo "  Print the editorial comments in the files FILES, using the"
        echo "  comment element in Mallard and the remark element in DocBook."
        echo ""
        echo "Options:"
        echo "  -s            Treat pages as belonging to a Mallard site"
    ) 1>&2
}
yelp_usage_license () {
    (
        echo "Usage: yelp-check license <FILES>"
        echo ""
        echo "  Report the license of the Mallard page files FILES. Each"
        echo "  matching page is reporting along with its license, reported"
        echo "  based on the href attribute of the license element. Common"
        echo "  licenses use a shortened identifier. Pages with multiple"
        echo "  licenses have the identifiers separated by spaces. Pages"
        echo "  with no license element report 'none'. Licenses with no"
        echo "  href attribute are reported as 'unknown'."
        echo ""
        echo "Options:"
        echo "  -s                  Treat pages as belonging to a Mallard site"
        echo "  --only LICENSES     Only show pages whose license is in LICENSES"
        echo "  --except LICENSES   Exclude pages whose license is in LICENSES"
        echo "  --totals            Show total counts for each license"
        echo "LICENSES may be a comma- and/or space-separated list."
    ) 1>&2
}
yelp_usage_status () {
    (
        echo "Usage: yelp-check status <FILES>"
        echo ""
        echo "  Report the status of the Mallard page files FILES. Each"
        echo "  matching page is reporting along with its status."
        echo ""
        echo "Options:"
        echo "  --version VER       Select revisions with the version attribute VER"
        echo "  --docversion VER    Select revisions with the docversion attribute VER"
        echo "  --pkgversion VER    Select revisions with the pkgversion attribute VER"
        echo "  --older DATE        Only show pages older than DATE"
        echo "  --newer DATE        Only show pages newer than DATE"
        echo "  --only STATUSES     Only show pages whose status is in STATUSES"
        echo "  --except STATUSES   Exclude pages whose status is in STATUSES"
        echo "  --totals            Show total counts for each status"
        echo "VER and STATUSES may be comma- and/or space-separated lists."
    ) 1>&2
}
yelp_usage_validate () {
    (
        echo "Usage: yelp-check validate <FILES>"
        echo ""
        echo "  Validate FILES against the appropriate DTD or RNG."
        echo "  For Mallard pages, perform automatic RNG merging"
        echo "  based on the version attribute."
        echo ""
        echo "Options:"
        echo "  -s                  Treat pages as belonging to a Mallard site"
        echo "  --strict            Disallow unknown namespaces"
        echo "  --allow NS          Explicitly allow namespace NS in strict mode"
    ) 1>&2
}

if [ $# = 0 ]; then
    yelp_usage
    exit 1
fi

yelp_check_iter_site () {
    for dir in "$1"/*; do
        if [ -d "$dir" ]; then
            if [ $(basename "$dir") != "__pintail__" ]; then
                yelp_check_iter_site "$dir"
            fi
        fi
    done
    for page in "$1"/*.page; do
        if [ -e "$page" ]; then
            $check_page "$page" || yelp_check_retval="$?"
        fi
    done
}

yelp_check_iter_args () {
    for arg in "$@"; do
        ext=$(echo "$arg" | sed -e 's/.*\.//')
        if [ -d "$arg" ]; then
            if [ "x$check_site" = "x1" ]; then
                yelp_check_iter_site "$arg" 
            else
                for page in "$arg"/*.page; do
                    if [ -e "$page" ]; then
                        $check_page "$page"
                    fi
                done
            fi
        elif [ "x$ext" = "xpage" -o "x$ext" = "xstub" -o "x$ext" = "xcache" ]; then
            $check_page "$arg" || yelp_check_retval="$?"
	elif [ "x$check_db" != "x" -a \( "x$ext" = "xdocbook" -o "x$ext" = "xxml" \) ]; then
	    $check_db "$arg" || yelp_check_retval="$?"
        else
            echo "Unrecognized page $arg" 1>&2
            exit 1
        fi
    done
    return $yelp_check_retval
}

yelp_hrefs_page () {
    base=$(dirname "$1")
    if [ "x$check_site" = "x1" ]; then
        sdir=$(cd $(dirname "$1") && pwd)
        sdir=${sdir##${check_site_root}}/
    fi
    (
        echo '<xsl:stylesheet'
        echo ' xmlns:xsl="http://www.w3.org/1999/XSL/Transform"'
        echo ' xmlns:mal="http://projectmallard.org/1.0/"'
        echo ' xmlns:db="http://docbook.org/ns/docbook"'
        echo ' xmlns:xlink="www.w3.org/1999/xlink"'
        echo ' version="1.0">'
        echo '<xsl:output method="text"/>'
        echo '<xsl:template match="/mal:page">'
        echo ' <xsl:for-each select="//*[@href]">'
        echo '  <xsl:if test="not(starts-with(@href, '\''mailto:'\''))">'
        echo '   <xsl:value-of select="/mal:page/@id"/>'
        echo '   <xsl:text> </xsl:text>'
        echo '   <xsl:value-of select="@href"/>'
        echo '   <xsl:text>&#x000A;</xsl:text>'
        echo '  </xsl:if>'
        echo ' </xsl:for-each>'
        echo '</xsl:template>'
        echo '<xsl:template match="/*[namespace-uri(.) = '\'\''] | /db:*">'
        echo ' <xsl:for-each select="//ulink/@url | //*/xlink:href">'
        echo '  <xsl:if test="not(starts-with(string(.), '\''mailto:'\''))">'
        echo '   <xsl:value-of select="(ancestor-or-self::*/@id | ancestor-or-self::*/@xml:id)[last()]"/>'
        echo '   <xsl:text> </xsl:text>'
        echo '   <xsl:value-of select="string(.)"/>'
        echo '   <xsl:text>&#x000A;</xsl:text>'
        echo '  </xsl:if>'
        echo ' </xsl:for-each>'
        echo '</xsl:template>'
        echo '</xsl:stylesheet>'
    ) | xsltproc --xinclude - "$1" | sort | uniq | \
        while read id url; do
            colon=`echo "$url" | cut -d: -f1`
            if [ "x$colon" = "x$url" ]; then
                test -f "$base/"$(urldecode "$url") || echo "$sdir$id: $url"
            else
                (curl -s -I -L "$url" | \
                        grep '^HTTP/' | tail -n 1 | head -n 1 | \
                        grep -q 'HTTP/.\.. 200 .*') || \
                    echo "$sdir$id: $url"
            fi
        done
}

yelp_hrefs () {
    if [ "$#" = "0" -o "x$1" = "x--help" -o "x$1" = "x-h" ]; then
        yelp_usage_hrefs
        exit 1
    fi
    while [ "$#" != "0" ]; do
        case "$1" in
            "-s")
                check_site="1"
                check_site_root=$(pwd)
                shift
                ;;
            *)
                break
                ;;
        esac
    done
    if [ "$#" = "0" -o "x$1" = "x--help" -o "x$1" = "x-h" ]; then
        yelp_usage_hrefs
        exit 1
    fi
    check_out_file=`mktemp "${TMPDIR:-/tmp}"/yelp-XXXXXXXX`
    check_db=yelp_hrefs_page
    check_page=yelp_hrefs_page
    yelp_check_iter_args "$@" > "$check_out_file"
    ret=$(cat "$check_out_file" | wc -l)
    if test "x$ret" != "x0"; then ret="1"; fi
    cat "$check_out_file"
    rm "$check_out_file"
    exit $ret
}

yelp_ids_page () {
    pageid=$(
    (
        echo '<xsl:stylesheet'
        echo ' xmlns:xsl="http://www.w3.org/1999/XSL/Transform"'
        echo ' xmlns:mal="http://projectmallard.org/1.0/"'
        echo ' version="1.0">'
        echo '<xsl:output method="text"/>'
        echo '<xsl:template match="/mal:page">'
        echo '<xsl:value-of select="@id"/>'
        echo '</xsl:template>'
        echo '</xsl:stylesheet>'
    ) | xsltproc --xinclude - "$1")
    if [ "$pageid.page" != `basename "$1"` ]; then
	if [ "x$check_site" = "x1" ]; then
            sdir=$(cd $(dirname "$1") && pwd)
            sdir=${sdir##${check_site_root}}/
            echo $sdir$(basename "$1")": $pageid"
	elif [ `dirname "$1"` = '.' ]; then
            echo $(basename "$1")": $pageid"
        else
            echo "$1: $pageid"
        fi
        exit 1
    fi
}

yelp_ids () {
    if [ "$#" = "0" -o "x$1" = "x--help" -o "x$1" = "x-h" ]; then
        yelp_usage_ids
        exit 1
    fi
    ret=0
    for xml in "$@"; do
        if [ -d "$xml" ]; then
            for page in "$xml"/*.page; do
                yelp_ids_page "$page" || ret=$?
            done
        else
            yelp_ids_page "$xml" || ret=$?
        fi
    done
    exit $ret
}

yelp_links_db () {
    (
        echo '<xsl:stylesheet'
        echo ' xmlns:xsl="http://www.w3.org/1999/XSL/Transform"'
        echo ' xmlns:db="http://docbook.org/ns/docbook"'
        echo ' xmlns:exsl="http://exslt.org/common"'
        echo ' extension-element-prefixes="exsl"'
        echo ' version="1.0">'
        echo '<xsl:output method="text"/>'
        echo '<xsl:key name="idkey" match="*[@id or @xml:id]" use="@id | @xml:id"/>'
        echo '<xsl:template match="/">'
        echo ' <xsl:for-each select="//*[@linkend]">'
        echo '  <xsl:if test="not(key('"'idkey'"', @linkend))">'
        echo '   <xsl:value-of select="(ancestor-or-self::*/@id | ancestor-or-self::*/@xml:id)[last()]"/>'
        echo '   <xsl:text>: </xsl:text>'
        echo '   <xsl:value-of select="@linkend"/>'
        echo '   <xsl:text>&#x000A;</xsl:text>'
        echo '  </xsl:if>'
        echo ' </xsl:for-each>'
        echo '</xsl:template>'
        echo '</xsl:stylesheet>'
    ) | xsltproc --xinclude - "$1"
}

yelp_links_page () {
    (
        echo '<xsl:stylesheet'
        echo ' xmlns:xsl="http://www.w3.org/1999/XSL/Transform"'
        echo ' xmlns:mal="http://projectmallard.org/1.0/"'
        echo ' xmlns:exsl="http://exslt.org/common"'
        echo ' extension-element-prefixes="exsl"'
        echo ' version="1.0">'
        xsl='file://'`urlencode "$xsl_mal_link"`
        echo '<xsl:import href="'"$xsl"'"/>'
        check_cache_url='file://'`urlencode "$check_cache_file"`
        echo '<xsl:param name="mal.cache.file" select="'"'$check_cache_url'"'"/>'
        echo '<xsl:output method="text"/>'
        echo '<xsl:template match="/mal:page">'
        echo ' <xsl:variable name="page" select="@id"/>'
        if [ "x$check_links_ignore" = "x1" ]; then
            echo ' <xsl:for-each select="//*[@xref][not(@href)]">'
        else
            echo ' <xsl:for-each select="//*[@xref]">'
        fi
        echo '  <xsl:variable name="xref" select="@xref"/>'
        echo '  <xsl:variable name="linkid">'
        echo '   <xsl:call-template name="mal.link.xref.linkid"/>'
        echo '  </xsl:variable>'
        echo '  <xsl:for-each select="$mal.cache">'
        echo '   <xsl:variable name="target" select="key('"'mal.cache.key'"', $linkid)"/>'
        echo '   <xsl:if test="count($target) = 0">'
        echo '    <xsl:value-of select="$page"/>'
        echo '    <xsl:text>: </xsl:text>'
        echo '    <xsl:value-of select="$xref"/>'
        echo '    <xsl:text>&#x000A;</xsl:text>'
        echo '   </xsl:if>'
        echo '  </xsl:for-each>'
        echo ' </xsl:for-each>'
        echo '</xsl:template>'
        echo '</xsl:stylesheet>'
    ) | xsltproc --xinclude - "$1"
}

yelp_links () {
    if [ "$#" = "0" -o "x$1" = "x--help" -o "x$1" = "x-h" ]; then
        yelp_usage_links
        exit 1
    fi
    while [ "$#" != "0" ]; do
        case "$1" in
            "-c")
                shift
                check_cache_file="$1"
                shift
                ;;
            "-i")
                shift
                check_links_ignore="1"
                ;;
            *)
                break
                ;;
        esac
    done
    if [ "$#" = "0" -o "x$1" = "x--help" -o "x$1" = "x-h" ]; then
        yelp_usage_links
        exit 1
    fi
    if [ "x$check_cache_file" != "x" ]; then
        check_cache_file=`(cd $(dirname "$check_cache_file") && pwd)`/`basename "$check_cache_file"`
    fi
    check_out_file=`mktemp "${TMPDIR:-/tmp}"/yelp-XXXXXXXX`
    for xml in "$@"; do
        if [ -d "$xml" ]; then
            if [ "x$check_cache_file" = "x" ]; then
                check_cache_file_is_tmp="yes"
                check_cache_file=`mktemp "${TMPDIR:-/tmp}"/yelp-XXXXXXXX`
                yelp-build cache -o "$check_cache_file" "$@"
            fi
            for page in "$xml"/*.page; do
                yelp_links_page "$page"
            done
        else
            ext=`echo "$1" | sed -e 's/.*\.//'`
            if [ "x$ext" = "xxml" -o "x$ext" = "xdocbook" ]; then
                yelp_links_db "$xml"
            else
                if [ "x$check_cache_file" = "x" ]; then
                    check_cache_file_is_tmp="yes"
                    check_cache_file=`mktemp "${TMPDIR:-/tmp}"/yelp-XXXXXXXX`
                    yelp-build cache -o "$check_cache_file" "$@"
                fi
                yelp_links_page "$xml"
            fi
        fi
    done > "$check_out_file"
    ret=`cat "$check_out_file" | wc -l`
    if test "x$ret" != "x0"; then ret="1"; fi
    cat "$check_out_file"
    if [ "x$check_cache_file_is_tmp" = "xyes" ]; then
        rm "$check_cache_file"
    fi
    rm "$check_out_file"
    exit $ret
}

yelp_media_page () {
    ext=`echo "$1" | sed -e 's/.*\.//'`
    bname=`basename "$1" ".$ext"`
    dname=$(dirname "$xml")
    if [ x"$dname" = "." ]; then
        dname=""
    else
        dname="$dname"/
    fi;
    xsltproc --stringparam basename "$bname" \
             --stringparam dirname "$dname" \
             "$xsl_media" "$1"
}

yelp_media () {
    if [ "$#" = "0" -o "x$1" = "x--help" -o "x$1" = "x-h" ]; then
        yelp_usage_media
        exit 1
    fi
    for xml in "$@"; do
        if [ -d "$xml" ]; then
            for page in "$xml"/*.page; do
                yelp_media_page "$page"
            done
        else
            yelp_media_page "$xml"
        fi
    done | while read line; do
        dname=$(dirname $(echo "$line" | cut -d' ' -f1))
        src=$(urldecode $(echo "$line" | cut -d' ' -f2))
        if [ ! -f "$dname"/"$src" ]; then
            echo $line
        fi
    done
}

yelp_orphans_page () {
    (
        echo '<xsl:stylesheet'
        echo ' xmlns:xsl="http://www.w3.org/1999/XSL/Transform"'
        echo ' xmlns:mal="http://projectmallard.org/1.0/"'
        echo ' xmlns:exsl="http://exslt.org/common"'
        echo ' extension-element-prefixes="exsl"'
        echo ' version="1.0">'
        xsl='file://'`urlencode "$xsl_mal_link"`
        echo '<xsl:import href="'"$xsl"'"/>'
        check_cache_url='file://'`urlencode "$check_cache_file"`
        echo '<xsl:param name="mal.cache.file" select="'"'$check_cache_url'"'"/>'
        echo '<xsl:output method="text"/>'
        echo '<xsl:template match="/mal:page">'
        echo ' <xsl:variable name="trails">'
        echo '  <xsl:call-template name="mal.link.linktrails"/>'
        echo ' </xsl:variable>'
        echo ' <xsl:if test="@id != '"'index'"' and count(exsl:node-set($trails)/*) = 0">'
        echo '  <xsl:value-of select="@id"/>'
        echo '  <xsl:text>&#x000A;</xsl:text>'
        echo ' </xsl:if>'
        echo '</xsl:template>'
        echo '</xsl:stylesheet>'
    ) | xsltproc --xinclude - "$1"
}

yelp_orphans () {
    if [ "$#" = "0" -o "x$1" = "x--help" -o "x$1" = "x-h" ]; then
        yelp_usage_orphans
        exit 1
    fi
    while [ "$#" != "0" ]; do
        case "$1" in
            "-c")
                shift
                check_cache_file="$1"
                shift
                ;;
            *)
                break
                ;;
        esac
    done
    if [ "$#" = "0" -o "x$1" = "x--help" -o "x$1" = "x-h" ]; then
        yelp_usage_orphans
        exit 1
    fi
    if [ "x$check_cache_file" != "x" ]; then
        check_cache_file=`(cd $(dirname "$check_cache_file") && pwd)`/`basename "$check_cache_file"`
    else
        check_cache_file_is_tmp="yes"
        check_cache_file=`mktemp "${TMPDIR:-/tmp}"/yelp-XXXXXXXX`
        yelp-build cache -o "$check_cache_file" "$@"
    fi
    check_out_file=`mktemp "${TMPDIR:-/tmp}"/yelp-XXXXXXXX`
    for xml in "$@"; do
        if [ -d "$xml" ]; then
            for page in "$xml"/*.page; do
                yelp_orphans_page "$page"
            done
        else
            yelp_orphans_page "$xml"
        fi
    done > "$check_out_file"
    ret=`cat "$check_out_file" | wc -l`
    if test "x$ret" != "x0"; then ret="1"; fi
    cat "$check_out_file"
    if [ "x$check_cache_file_is_tmp" = "xyes" ]; then
        rm "$check_cache_file"
    fi
    rm "$check_out_file"
    exit $ret
}

yelp_comments_page () {
    ext=$(echo "$1" | sed -e 's/.*\.//')
    bname=$(basename "$1" ".$ext")
    if [ "x$check_site" = "x1" ]; then
        sdir=$(cd $(dirname "$1") && pwd)
        sdir=${sdir##${check_site_root}}/
    fi
    xsltproc --stringparam basename "$bname" \
             --stringparam site.dir "$sdir" \
             "$xsl_comments" "$1"
}

yelp_comments () {
    if [ "$#" = "0" -o "x$1" = "x--help" -o "x$1" = "x-h" ]; then
        yelp_usage_comments
        exit 1
    fi
    while [ "$#" != "0" ]; do
        case "$1" in
            "-s")
                check_site="1"
                check_site_root=$(pwd)
                shift
                ;;
            *)
                break
                ;;
        esac
    done
    if [ "$#" = "0" -o "x$1" = "x--help" -o "x$1" = "x-h" ]; then
        yelp_usage_comments
        exit 1
    fi
    check_db=yelp_comments_page
    check_page=yelp_comments_page
    yelp_check_iter_args "$@"
    exit $yelp_check_retval
}

yelp_license_page () {
    if [ "x$check_site" = "x1" ]; then
        sdir=$(cd $(dirname "$1") && pwd)
        sdir=${sdir##${check_site_root}}/
    fi
    xsltproc --xinclude \
        --stringparam only "$check_only" \
        --stringparam except "$check_except" \
        --stringparam totals "$check_totals" \
        --stringparam site.dir "$sdir" \
        "$xsl_mal_license" "$1"
}

yelp_license () {
    if [ "$#" = "0" -o "x$1" = "x--help" -o "x$1" = "x-h" ]; then
        yelp_usage_license
        exit 1
    fi
    while [ "$#" != "0" ]; do
        case "$1" in
            "-s")
                check_site="1"
                check_site_root=$(pwd)
                shift
                ;;
            "--only")
                shift
                check_only="$1"
                shift
                ;;
            "--except")
                shift
                check_except="$1"
                shift
                ;;
            "--totals")
                check_totals="1"
                shift
                ;;
            *)
                break
                ;;
        esac
    done
    if [ "$#" = "0" -o "x$1" = "x--help" -o "x$1" = "x-h" ]; then
        yelp_usage_license
        exit 1
    fi
    check_db=
    check_page=yelp_license_page
    if [ "x$check_totals" = "x1" ]; then
        yelp_check_iter_args "$@" | \
            sort | uniq -c | sed -e 's/^ *//' | awk '{print $2 ": " $1}'
    else
        yelp_check_iter_args "$@" | sort
    fi
}

yelp_status () {
    if [ "$#" = "0" -o "x$1" = "x--help" -o "x$1" = "x-h" ]; then
        yelp_usage_status
        exit 1
    fi
    while [ "$#" != "0" ]; do
        case "$1" in
            "--version")
                shift
                check_version="$1"
                shift
                ;;
            "--docversion")
                shift
                check_docversion="$1"
                shift
                ;;
            "--pkgversion")
                shift
                check_pkgversion="$1"
                shift
                ;;
            "--older")
                shift
                check_older="$1"
                shift
                ;;
            "--newer")
                shift
                check_newer="$1"
                shift
                ;;
            "--only")
                shift
                check_only="$1"
                shift
                ;;
            "--except")
                shift
                check_except="$1"
                shift
                ;;
            "--totals")
                check_totals="1"
                shift
                ;;
            *)
                break
                ;;
        esac
    done
    if [ "$#" = "0" -o "x$1" = "x--help" -o "x$1" = "x-h" ]; then
        yelp_usage_status
        exit 1
    fi
    check_cache_file=`mktemp "${TMPDIR:-/tmp}"/yelp-XXXXXXXX`
    yelp-build cache -o "$check_cache_file" "$@"
    xsltproc \
        --stringparam version "$check_version" \
        --stringparam docversion "$check_docversion" \
        --stringparam pkgversion "$check_pkgversion" \
        --stringparam newer "$check_newer" \
        --stringparam older "$check_older" \
        --stringparam only "$check_only" \
        --stringparam except "$check_except" \
        --stringparam totals "$check_totals" \
        "$xsl_mal_status" "$check_cache_file"
    rm "$check_cache_file"
    return 0
}

yelp_validate_db () {
    version=$(echo "$docbook_version" | xsltproc - "$1")
    major=$(echo "$version" | cut -c1)
    if [ "x$major" = "x5" ]; then
        check_out_file=`mktemp "${TMPDIR:-/tmp}"/yelp-XXXXXXXX`
        rng_uri="http://docbook.org/xml/$version/rng/docbook.rng"
        xmllint --noout --xinclude --noent --relaxng "$rng_uri" "$1" > "$check_out_file" 2>&1
        yelp_check_retval="$?"
        cat "$check_out_file" | grep -v 'validates$'
        rm "$check_out_file"
    elif xmllint --nocdata "$1" | grep -q '<!DOCTYPE'; then
        xmllint --noout --xinclude --noent --postvalid "$1" || yelp_check_retval="$?"
    else
        dtd_uri='http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd'
        xmllint --noout --xinclude --noent --dtdvalid "$dtd_uri" "$1" || yelp_check_retval="$?"
    fi
}

yelp_validate_page () {
    # Using temp files because pipes create subshells, making it really
    # hard to return the right exit status in a portable way.
    if [ "x$check_rng_dir" = "x" ]; then
        check_rng_dir=`mktemp -d "${TMPDIR:-/tmp}"/yelp-XXXXXXXX`
    fi
    check_out_file=`mktemp "${TMPDIR:-/tmp}"/yelp-XXXXXXXX`
    check_rng_file=`(
            echo '<xsl:stylesheet'
            echo ' xmlns:cache="http://projectmallard.org/cache/1.0/"'
            echo ' xmlns:xsl="http://www.w3.org/1999/XSL/Transform"'
            echo ' version="1.0">'
            echo '<xsl:output method="text"/>'
            echo '<xsl:template match="/*">'
            echo '<xsl:choose>'
            echo '<xsl:when test="string(@version) != '"''"'">'
            echo '<xsl:value-of select="@version"/>'
            echo '</xsl:when>'
            echo '<xsl:when test="/cache:cache">'
            echo '<xsl:text>cache/1.0 1.0</xsl:text>'
            echo '</xsl:when>'
            echo '<xsl:otherwise>'
            echo '<xsl:text>1.0</xsl:text>'
            echo '</xsl:otherwise>'
            echo '</xsl:choose>'
            echo '</xsl:template>'
            echo '</xsl:stylesheet>'
            ) | xsltproc - "$1"`
    check_rng_file=`urlencode "$check_rng_file" /`.rng
    if [ ! -f "$check_rng_dir/$check_rng_file" ]; then
        # If we've already made an RNG file for this version string, don't
        # do it again. We've urlencoded the file name + slashes, because
        # version strings often contain slashes. But xsltproc treats the
        # -o option as a URL and urldecodes, so doubly urlencode, because
        # we want the urlencoded string to be the on-disk name.
        xsltproc -o "$check_rng_dir/"`urlencode "$check_rng_file"` \
            --param rng.strict "$check_strict" \
            --stringparam rng.strict.allow "$check_strict_allow" \
            "$xsl_mal_rng" "$1"
    fi
    xmllint --noout --xinclude --noent --relaxng "$check_rng_dir/$check_rng_file" "$1" > "$check_out_file" 2>&1
    ret="$?"
    cat "$check_out_file" | grep -v 'validates$'
    rm "$check_out_file"
    return $ret;
}

yelp_validate () {
    if [ "$#" = "0" -o "x$1" = "x--help" -o "x$1" = "x-h" ]; then
        yelp_usage_validate
        exit 1
    fi
    check_strict="false()"
    check_strict_allow=""
    while [ "$#" != "0" ]; do
        case "$1" in
            "-s")
                check_site="1"
                check_site_root=$(pwd)
                shift
                ;;
            "--strict")
                check_strict="true()"
                shift
                ;;
            "--allow")
                shift
                check_strict_allow="$check_strict_allow $1"
                shift
                ;;
            *)
                break
                ;;
        esac
    done
    if [ "$#" = "0" -o "x$1" = "x--help" -o "x$1" = "x-h" ]; then
        yelp_usage_validate
        exit 1
    fi
    check_db=yelp_validate_db
    check_page=yelp_validate_page
    yelp_check_iter_args "$@"
    if [ "x$check_rng_dir" != "x" ]; then
        rm "$check_rng_dir"/*.rng
        rmdir "$check_rng_dir"
    fi
    exit $yelp_check_retval
}

cmd="$1"
shift
case "x$cmd" in
    "xcomments")
        yelp_comments "$@"
        ;;
    "xhrefs")
        yelp_hrefs "$@"
        ;;
    "xids")
        yelp_ids "$@"
        ;;
    "xlinks")
        yelp_links "$@"
        ;;
    "xorphans")
        yelp_orphans "$@"
        ;;
    "xlicense")
        yelp_license "$@"
        ;;
    "xmedia")
        yelp_media "$@"
        ;;
    "xstatus")
        yelp_status "$@"
        ;;
    "xvalidate")
        yelp_validate "$@"
        ;;
    *)
        yelp_usage
        ;;
esac