#!/bin/sh -efu
# Copyright (c) 2025 Dmitry V. Levin <ldv@strace.io>
# All rights reserved.
#
# SPDX-License-Identifier: LGPL-2.1-or-later

print_help()
{
	cat <<__EOF__
$0 FILE...
$0 [-h|--help]

Update generated xlat files.

Options:
  -h, --help    Print this message and exit.
__EOF__

exit "$@"
}

print_usage()
{
	print_help 1 >&2
}

while :; do
	case "${1:-}" in
	-h|--help)
		print_help 0
		;;
	-*)
		print_usage
		;;
	*)
		break
		;;
	esac

	shift
done

[ $# -gt 0 ] ||
	print_usage

by='(maint\/[[:alpha:]][[:alnum:]_]+\.sh)'
from='"enum ([[:alpha:]][[:alnum:]_]+)"'
in='(bundled\/[[:alpha:]][[:alnum:]_/-]+\.h)'
expr='1 s/^#Generated by '"$by from $from in $in"'; do not edit\.$/\1 --stdout \3 \2/p'

for file; do
	cmd="$(sed -r -n "$expr" "$file")"

	[ -n "$cmd" ] ||
		continue

	eval $cmd > "$file"
done
