#!/bin/sh

die()
{
	echo "$*" 2>&1
	exit 1
}

test "$1" || die "Usage: updver [version]"

vv=$(printf '%s\n' "$1" | sed 's/v//')

major=$(printf '%s\n' "$vv" | sed 's/\..*//')
minor=$(printf '%s\n' "$vv" | sed 's/[^.]*\.\([^.]*\)\..*/\1/')
patch=$(printf '%s\n' "$vv" | sed 's/.*\.//')

vs="$major.$minor.$patch"

oldhdr=$(head -1 NEWS)

msgfile=$(mktemp)

printf '%s (%s)\n\n' "$vs" "$(date +'%Y.%m.%d')" > "$msgfile"
cat NEWS >> "$msgfile"

test -n "$EDITOR" || die 'missing $EDITOR env var'
$EDITOR "$msgfile"

printf 'Ok? (y/n) '
read -r ok
case "$ok" in
y|Y)	;;
*)	die "Aborted. (File is $msgfile.)" ;;
esac

cp "$msgfile" NEWS
echo "/version/c
version       = \"$vs\"
.
p
wq" | ed -s chame.nimble

echo "/Major/s/[0-9]*$/$major/
/Minor/s/[0-9]*$/$minor/
/Patch/s/[0-9]*$/$patch/
g/.*/p
wq" | ed -s chame/version.nim

git add .
git commit -m "Version $vs"

tmp2=$(mktemp)

while read line
do	if test "$line" = "$oldhdr"; then break; fi
	printf '%s\n' "$line"
done <NEWS >"$tmp2"

git tag -faeF "$tmp2" "v$vs"
