|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# update-elf2tag-manpage - update the elf2tag.1 manpage from adoc source |
| 4 | +# |
| 5 | +# Usage: |
| 6 | +# ./path/to/update-elf2tag-manpage |
| 7 | +# ./path/to/update-elf2tag-manpage --verify |
| 8 | +# |
| 9 | +# Without a command line argument given, changes into the directory |
| 10 | +# where update-elf2tag-manpage and elf2tag.1.adoc are, runs |
| 11 | +# asciidoctor to produce a man page elf2tag.1 from elf2tag.1.adoc, but |
| 12 | +# only updates the elf2tag.1 file in the case of actual changes. |
| 13 | +# |
| 14 | +# Just the asciidoctor version or the current date being different |
| 15 | +# from the last asciidoctor run is not considered an actual change. |
| 16 | +# |
| 17 | +# WIth the --verify argument given, only verify that elf2tag.1 and |
| 18 | +# elf2tag.1.adoc are consistent with each other, i.e. elf2tag.1 would |
| 19 | +# not be changed. |
| 20 | +# |
| 21 | +# Requires asciidoctor to be installed. |
| 22 | +# |
| 23 | +# Environment variables used (if unset, uses the command from PATH): |
| 24 | +# ASCIIDOCTOR the asciidoctor command to run |
| 25 | +# CMP the cmp command to run (e.g. "busybox cmp") |
| 26 | +# DIFF the diff command to run with -u (used with --verify only) |
| 27 | +# SED the sed command to run (e.g. "busybox sed") |
| 28 | + |
| 29 | +# This script uses the shell feature called "process substitution" which |
| 30 | +# is implemented by bash and busybox sh, but not by e.g. dash and can |
| 31 | +# therefore not be a /bin/sh script. |
| 32 | + |
| 33 | +set -e |
| 34 | + |
| 35 | +prog="$(basename "$0")" |
| 36 | + |
| 37 | +if test "$#" -gt 1; then |
| 38 | + echo "$prog: Too many command line arguments" |
| 39 | + exit 2 |
| 40 | +fi |
| 41 | + |
| 42 | +verify=false |
| 43 | +if test "$#" -eq 1; then |
| 44 | + case "$1" in |
| 45 | + -h | --help ) |
| 46 | + ${SED-sed} -n '/^#\( .*\)$/,$p' "$0" \ |
| 47 | + | ${SED-sed} '/^#\( .*\)\?$/!q' \ |
| 48 | + | ${SED-sed} '/^$/d' \ |
| 49 | + | ${SED-sed} 's|^#$||; s|^# ||' |
| 50 | + exit 0 |
| 51 | + ;; |
| 52 | + --verify ) |
| 53 | + verify=: |
| 54 | + ;; |
| 55 | + * ) |
| 56 | + echo "$prog: Unhandled command line argument." |
| 57 | + exit 2 |
| 58 | + ;; |
| 59 | + esac |
| 60 | +fi |
| 61 | + |
| 62 | +cd "$(dirname "$0")" |
| 63 | + |
| 64 | +test -s elf2tag.1.adoc |
| 65 | +test -s elf2tag |
| 66 | + |
| 67 | +# Usage: normalize_manpage original.1 normalized.1 |
| 68 | +# |
| 69 | +# Normalizes a man page generated by asciidoctor by replacing data not |
| 70 | +# present in the sources such as generation date, asciidoctor version. |
| 71 | +normalize_manpage() { |
| 72 | + ${SED-sed} -f <(cat<<EOF |
| 73 | +#s|^\.\\\\|==TITLE==| |
| 74 | +s|^\.\\\\" Generator: Asciidoctor .*|.\\" Generator: GENERATOR| |
| 75 | +s|^\.\\\\" Date: 20[0-9][0-9]-[0-1][0-9]-[0-3][0-9]|.\\" Date: DATE| |
| 76 | +s|^\.TH "ELF2TAG" "1" "20[0-9][0-9]-[0-1][0-9]-[0-3][0-9]" "avrdude" "avrdude Manual"|\.\\" TH HEADER| |
| 77 | +EOF |
| 78 | +) < "$1" > "$2" |
| 79 | +} |
| 80 | + |
| 81 | +tmpdir="tmp$$" |
| 82 | + |
| 83 | +if ! ${ASCIIDOCTOR-asciidoctor} -b manpage -D "$tmpdir" elf2tag.1.adoc; then |
| 84 | + echo "$prog: Error updating elf2tag.1" |
| 85 | + rm -rf "$tmpdir" |
| 86 | + exit 2 |
| 87 | +fi |
| 88 | + |
| 89 | +# Have sed ensure a trailing newline character by appending empty string |
| 90 | +# |
| 91 | +# This is necessary as asciidoctor likes to create files without a |
| 92 | +# trailing newline, while it is good form for text editors to add one. |
| 93 | +if ${SED-sed} '$a\' < "$tmpdir/elf2tag.1" > "$tmpdir/elf2tag.1.newline"; then |
| 94 | + mv -f "$tmpdir/elf2tag.1.newline" "$tmpdir/elf2tag.1" |
| 95 | +else |
| 96 | + echo "$prog: Error ensuring trailing newline character" |
| 97 | + rm -rf "$tmpdir" |
| 98 | + exit 2 |
| 99 | +fi |
| 100 | + |
| 101 | +if ! test -e elf2tag.1; then |
| 102 | + if "$verify"; then |
| 103 | + echo "$prog: Cannot ensure consistency if there is no old elf2tag.1 file" |
| 104 | + rm -rf "$tmpdir" |
| 105 | + exit 1 |
| 106 | + fi |
| 107 | + echo "$prog: Generate elf2tag.1" |
| 108 | + mv -f "$tmpdir/elf2tag.1" elf2tag.1 |
| 109 | + rmdir "$tmpdir" |
| 110 | + exit 0 |
| 111 | +fi |
| 112 | + |
| 113 | +normalize_manpage elf2tag.1 "$tmpdir/elf2tag.1.norm-old" |
| 114 | +normalize_manpage "$tmpdir/elf2tag.1" "$tmpdir/elf2tag.1.norm-new" |
| 115 | + |
| 116 | +if ${CMP-cmp} "$tmpdir/elf2tag.1.norm-old" "$tmpdir/elf2tag.1.norm-new" > /dev/null; then |
| 117 | + echo "$prog: elf2tag.1 is up to date" |
| 118 | + rm -f "$tmpdir/elf2tag.1" |
| 119 | + rm -f "$tmpdir/elf2tag.1.norm-old" |
| 120 | + rm -f "$tmpdir/elf2tag.1.norm-new" |
| 121 | + rmdir "$tmpdir" |
| 122 | + exit 0 |
| 123 | +fi |
| 124 | + |
| 125 | +if "$verify"; then |
| 126 | + echo "$prog: Error: src/elf2tag.1 is inconsistent with src/elf2tag.1.adoc" |
| 127 | + ${DIFF-diff} -u "$tmpdir/elf2tag.1.norm-old" "$tmpdir/elf2tag.1.norm-new" ||: |
| 128 | + exit 1 |
| 129 | + rm -f "$tmpdir/elf2tag.1" |
| 130 | + rm -f "$tmpdir/elf2tag.1.norm-old" |
| 131 | + rm -f "$tmpdir/elf2tag.1.norm-new" |
| 132 | + rmdir "$tmpdir" |
| 133 | +fi |
| 134 | + |
| 135 | +echo "Updating elf2tag.1" |
| 136 | +mv -f "$tmpdir/elf2tag.1" elf2tag.1 |
| 137 | +rm -f "$tmpdir/elf2tag.1.norm-old" |
| 138 | +rm -f "$tmpdir/elf2tag.1.norm-new" |
| 139 | +rmdir "$tmpdir" |
| 140 | +exit 0 |
0 commit comments