#!/bin/bash
#
#	fix document syntax
#
# ::do_not_edit::
# ::maintainer::
#
# basically replaces some escapes with html equivalent
#
CMD=$(basename $0)
CMDVER="1.0"
CMDSTR="$CMD v$CMDSTR (2026-07-25)"

set -e -u

[ "X${PRJ:-}" = "X" ] && {
	PRJ=$(dirname "$0")
	cd "$PRJ/.."
	export PRJ=$(pwd)
}

instdir=$(cat $PRJ/etc/instdir)
cd "$instdir"

find content -type f | while read file
do
	case $file in
	  *.tmp|*.old|*.bak)	continue ;;
	esac

	tmp=$file.tmp
	sed \
		-e 's/a`/à/g' \
		-e 's/e`/è/g' \
		-e 's/i`/ì/g' \
		-e 's/o`/ò/g' \
		-e 's/u`/ù/g' \
		-e 's/E`/È/g' \
		-e 's/&agrave;/à/g' \
		-e 's/&egrave;/è/g' \
		-e 's/&ograve;/ì/g' \
		-e 's/&ograve;/ò/g' \
		-e 's/&ugrave;/ù/g' \
	"$file" >"$tmp"

	cmp "$file" "$tmp" >/dev/null 2>&1 && {
		rm -f "$tmp"
		continue
	}
	echo "  fixed: $file"
	mv "$file" "$file.old"
	mv "$tmp" "$file"
	touch --reference "$file.old" "$file"
	chown --reference "$file.old" "$file"
	rm "$file.old"
done

exit 0
