1#!/bin/sh
2
3# Add metadata to generated docs
4
5# Make section title for man pages.
6title="`echo "$@" | \
7    sed 's/.*-o *\([^ ]*\).*/\1/'
8`"
9
10title="`basename \"$title\"`"
11
12# Capitalize.
13title="`echo \"$title\" | tr 'a-z' 'A-Z'`"
14
15# FOO.1 -> FOO(1) Vimpager User Manual
16title="`echo \"$title\" | sed \
17    -e 's/\.\([0-9][0-9]*\)/(\1)/' \
18    -e 's/\..*//' \
19    -e 's/$/ Vimpager User Manual/'
20`"
21
22# Only do this for man pages.
23title="`echo \"$title\" | \
24    sed -e 's/^[^()]*Vimpager User Manual$/Vimpager User Manual/'
25`"
26
27# use markdown instead of markdown_github on older versions
28# and markdown_github+pandoc_title_block on newer ones
29old_pandoc=0
30if ! ( pandoc --help | grep markdown_github >/dev/null ); then
31    old_pandoc=1
32fi
33
34use_smart=
35# newer pandoc does not support -S
36if ! (pandoc --help | grep -- -S >/dev/null); then
37    use_smart=1
38fi
39
40
41first_arg=1
42for arg in "$@"; do
43    [ $first_arg -eq 1 ] && set -- && first_arg=0
44
45    if [ "$arg" = "-f" ]; then
46        if [ -z "$use_smart" ]; then
47            set -- "$@" -S -f
48        else
49            set -- "$@" -f
50        fi
51    elif [ "$arg" = markdown_github ]; then
52	if [ $old_pandoc -eq 1 ]; then
53	    set -- "$@" markdown
54        elif [ -n "$use_smart" ]; then
55	    set -- "$@" markdown_github+pandoc_title_block+smart
56        else
57	    set -- "$@" markdown_github+pandoc_title_block
58	fi
59    else
60	set -- "$@" "$arg"
61    fi
62done
63
64# find source file and write out file with meta block
65
66for arg in "$@"; do
67    case "$arg" in
68	*.md)
69	    source_file="$arg"
70	    ;;
71	*.md.*)
72	    source_file="$arg"
73	    ;;
74    esac
75done
76
77meta_file="`dirname \"$source_file\"`"/"with_meta_`basename \"$source_file\"`"
78
79printf '%% %s\n' "$title" >> "$meta_file"
80
81OLDIFS="$IFS"
82IFS='
83'
84
85first=1
86for author in `sed -ne 's/^- //p' DOC_AUTHORS.yml`; do
87    if [ $first -eq 1 ]; then
88	printf '%% %s' "$author" >> "$meta_file"
89	first=0
90    else
91	printf '; %s' "$author" >> "$meta_file"
92    fi
93done
94
95printf '\n' >> "$meta_file"
96
97IFS="$OLDIFS"
98
99printf '%% %s\n' "`date +'%B %d, %Y'`" >> "$meta_file"
100
101cat "$source_file" >> "$meta_file"
102
103# replace source file with created file with meta block in arg list
104first_arg=1
105for arg in "$@"; do
106    [ $first_arg -eq 1 ] && set -- && first_arg=0
107
108    if [ "$arg" = "$source_file" ]; then
109	set -- "$@" "$meta_file"
110    else
111	set -- "$@" "$arg"
112    fi
113done
114
115pandoc "$@"
116exit_status=$?
117
118rm -f -- "$meta_file"
119
120exit $exit_status
121