comparison tools/update_updated.sh @ 0:71dd8d58a8e5 default tip

cleaned up re-init from net_wobblyhead_www project tooling
author ark
date 2026-10-22 14:59 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:71dd8d58a8e5
1 #!/bin/sh
2
3 USAGE="$0 [-n] [<filespec> ...]
4
5 compare the updated_at header of markdown sources with the file modification
6 time. print each difference and, unless -n is given, set updated_at to the
7 date of the modification time as an ISO date, for example 2026-10-22. the
8 modification time itself is kept, so a second run finds nothing to change.
9
10 a value with a time counts as different. a source without updated_at gets
11 one as the last header. a markdown file without a title header warns and is
12 left alone. files that are not .md are ignored.
13
14 options:
15 -n, --dry-run report only, change nothing
16 -h, --help print this help
17
18 positional:
19 filespec files and directories to check, directories recursively.
20 default: src
21 "
22
23 DRY_RUN=0
24 SPECS=
25 NL='
26 '
27
28 while [ "$#" != 0 ]
29 do
30 case $1 in
31 -n|--dry-run) DRY_RUN=1 ;;
32 -h|--help) echo "$USAGE"; exit 0 ;;
33 -*) echo "unrecognized option '$1'" >&2; exit 1 ;;
34 *) SPECS="$SPECS$NL$1" ;;
35 esac
36 shift
37 done
38
39 if [ -z "$SPECS" ] ; then
40 SPECS=src
41 fi
42
43 # print 1 or 0 for a title header, the line number of the last updated_at
44 # header or 0 if there is none, and its value. the first blank line ends the
45 # header block; folded continuation lines are skipped.
46 READ_AWK='
47 /^[ \t]*$/ { exit }
48 /^[ \t]/ { next }
49 /^title[ \t]*:/ { has_title = 1 }
50 /^updated_at[ \t]*:/ {
51 line = NR
52 value = $0
53 sub(/^[^:]*:[ \t]*/, "", value)
54 sub(/[ \t]+$/, "", value)
55 }
56 END { printf "%d %d %s\n", has_title, line, value }
57 '
58
59 # replace the updated_at header on the given line with the stamp, dropping
60 # folded continuations of the old value. with line 0, add updated_at as the
61 # last header instead.
62 WRITE_AWK='
63 BEGIN { in_header = 1 }
64 in_header && /^[ \t]*$/ {
65 if (line == 0) print "updated_at: " stamp
66 in_header = 0
67 }
68 in_header && dropping && /^[ \t]/ { next }
69 { dropping = 0 }
70 NR == line {
71 print "updated_at: " stamp
72 dropping = 1
73 next
74 }
75 { print }
76 END { if (in_header && line == 0) print "updated_at: " stamp }
77 '
78
79 # expand directories to the markdown files below them, drop other files
80 FILES=$(
81 while IFS= read -r SPEC
82 do
83 if [ -z "$SPEC" ] ; then
84 continue
85 elif [ -d "$SPEC" ] ; then
86 find "$SPEC" -type f -name '*.md'
87 elif [ -f "$SPEC" ] ; then
88 # the opening parenthesis keeps the pattern from closing $( )
89 # in older shells
90 case $SPEC in (*.md) echo "$SPEC" ;; esac
91 else
92 echo "warning: $SPEC: not found" >&2
93 fi
94 done <<END | sort -u
95 $SPECS
96 END
97 )
98
99 TMP=$(mktemp) || exit 1
100 trap 'rm -f "$TMP"' EXIT
101
102 CHECKED=0
103 CHANGED=0
104
105 while IFS= read -r FILE
106 do
107 if [ -z "$FILE" ] ; then
108 continue
109 fi
110 CHECKED=$((CHECKED + 1))
111
112 read -r HAS_TITLE LINE VALUE <<END
113 $(awk "$READ_AWK" "$FILE")
114 END
115
116 if [ "$HAS_TITLE" = 0 ] ; then
117 echo "warning: $FILE: no title header, skipped" >&2
118 continue
119 fi
120
121 MTIME=$(date -r "$FILE" "+%Y-%m-%d")
122
123 if [ "$LINE" != 0 ] && [ "$VALUE" = "$MTIME" ] ; then
124 continue
125 fi
126
127 if [ "$LINE" = 0 ] ; then
128 echo "$FILE: updated_at missing -> $MTIME"
129 else
130 echo "$FILE: updated_at '$VALUE' -> $MTIME"
131 fi
132 CHANGED=$((CHANGED + 1))
133
134 if [ "$DRY_RUN" = 1 ] ; then
135 continue
136 fi
137
138 # rewrite in place, so the file keeps its permissions, then put back the
139 # modification time the stamp was taken from
140 awk -v line="$LINE" -v stamp="$MTIME" "$WRITE_AWK" "$FILE" > "$TMP" &&
141 touch -r "$FILE" "$TMP" &&
142 cat "$TMP" > "$FILE" &&
143 touch -r "$TMP" "$FILE" ||
144 {
145 echo "error: $FILE: update failed" >&2
146 exit 1
147 }
148 done <<END
149 $FILES
150 END
151
152 if [ "$DRY_RUN" = 1 ] ; then
153 echo "checked $CHECKED files, $CHANGED would be modified"
154 else
155 echo "checked $CHECKED files, modified $CHANGED"
156 fi