view 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
line wrap: on
line source

#!/bin/sh

USAGE="$0 [-n] [<filespec> ...]

compare the updated_at header of markdown sources with the file modification
time. print each difference and, unless -n is given, set updated_at to the
date of the modification time as an ISO date, for example 2026-10-22. the
modification time itself is kept, so a second run finds nothing to change.

a value with a time counts as different. a source without updated_at gets
one as the last header. a markdown file without a title header warns and is
left alone. files that are not .md are ignored.

options:
    -n, --dry-run   report only, change nothing
    -h, --help      print this help

positional:
    filespec        files and directories to check, directories recursively.
                    default: src
"

DRY_RUN=0
SPECS=
NL='
'

while [ "$#" != 0 ]
do
    case $1 in
        -n|--dry-run) DRY_RUN=1 ;;
        -h|--help) echo "$USAGE"; exit 0 ;;
        -*) echo "unrecognized option '$1'" >&2; exit 1 ;;
        *) SPECS="$SPECS$NL$1" ;;
    esac
    shift
done

if [ -z "$SPECS" ] ; then
    SPECS=src
fi

# print 1 or 0 for a title header, the line number of the last updated_at
# header or 0 if there is none, and its value. the first blank line ends the
# header block; folded continuation lines are skipped.
READ_AWK='
    /^[ \t]*$/ { exit }
    /^[ \t]/ { next }
    /^title[ \t]*:/ { has_title = 1 }
    /^updated_at[ \t]*:/ {
        line = NR
        value = $0
        sub(/^[^:]*:[ \t]*/, "", value)
        sub(/[ \t]+$/, "", value)
    }
    END { printf "%d %d %s\n", has_title, line, value }
'

# replace the updated_at header on the given line with the stamp, dropping
# folded continuations of the old value. with line 0, add updated_at as the
# last header instead.
WRITE_AWK='
    BEGIN { in_header = 1 }
    in_header && /^[ \t]*$/ {
        if (line == 0) print "updated_at: " stamp
        in_header = 0
    }
    in_header && dropping && /^[ \t]/ { next }
    { dropping = 0 }
    NR == line {
        print "updated_at: " stamp
        dropping = 1
        next
    }
    { print }
    END { if (in_header && line == 0) print "updated_at: " stamp }
'

# expand directories to the markdown files below them, drop other files
FILES=$(
    while IFS= read -r SPEC
    do
        if [ -z "$SPEC" ] ; then
            continue
        elif [ -d "$SPEC" ] ; then
            find "$SPEC" -type f -name '*.md'
        elif [ -f "$SPEC" ] ; then
            # the opening parenthesis keeps the pattern from closing $( )
            # in older shells
            case $SPEC in (*.md) echo "$SPEC" ;; esac
        else
            echo "warning: $SPEC: not found" >&2
        fi
    done <<END | sort -u
$SPECS
END
)

TMP=$(mktemp) || exit 1
trap 'rm -f "$TMP"' EXIT

CHECKED=0
CHANGED=0

while IFS= read -r FILE
do
    if [ -z "$FILE" ] ; then
        continue
    fi
    CHECKED=$((CHECKED + 1))

    read -r HAS_TITLE LINE VALUE <<END
$(awk "$READ_AWK" "$FILE")
END

    if [ "$HAS_TITLE" = 0 ] ; then
        echo "warning: $FILE: no title header, skipped" >&2
        continue
    fi

    MTIME=$(date -r "$FILE" "+%Y-%m-%d")

    if [ "$LINE" != 0 ] && [ "$VALUE" = "$MTIME" ] ; then
        continue
    fi

    if [ "$LINE" = 0 ] ; then
        echo "$FILE: updated_at missing -> $MTIME"
    else
        echo "$FILE: updated_at '$VALUE' -> $MTIME"
    fi
    CHANGED=$((CHANGED + 1))

    if [ "$DRY_RUN" = 1 ] ; then
        continue
    fi

    # rewrite in place, so the file keeps its permissions, then put back the
    # modification time the stamp was taken from
    awk -v line="$LINE" -v stamp="$MTIME" "$WRITE_AWK" "$FILE" > "$TMP" &&
    touch -r "$FILE" "$TMP" &&
    cat "$TMP" > "$FILE" &&
    touch -r "$TMP" "$FILE" ||
    {
        echo "error: $FILE: update failed" >&2
        exit 1
    }
done <<END
$FILES
END

if [ "$DRY_RUN" = 1 ] ; then
    echo "checked $CHECKED files, $CHANGED would be modified"
else
    echo "checked $CHECKED files, modified $CHANGED"
fi