Blame sofort/tools/version.sh

383aa6
#!/bin/sh
383aa6
9a8e72
# version.sh: detect git repository info, output defs as a C header.
9a8e72
# this file is covered by COPYING.SOFORT.
9a8e72
43ac97
set -eu
43ac97
383aa6
usage()
383aa6
{
383aa6
cat << EOF >&2
383aa6
383aa6
Usage:
383aa6
  -h            show this HELP message
383aa6
  -s  SRCDIR    set source directory
383aa6
  -o  OUTPUT    set output header
383aa6
  -p  PREFIX    set macro prefix
383aa6
383aa6
EOF
383aa6
exit 1
383aa6
}
383aa6
383aa6
383aa6
# one
c6f60f
workdir=$(pwd -P)
383aa6
srcdir=
383aa6
output=
383aa6
prefix=
383aa6
383aa6
383aa6
while getopts "hs:o:p:" opt; do
383aa6
	case $opt in
383aa6
	h)
383aa6
  		usage
383aa6
  		;;
383aa6
	s)
383aa6
    		srcdir="$OPTARG"
383aa6
    		;;
383aa6
	o)
383aa6
    		output="$OPTARG"
383aa6
    		;;
383aa6
	p)
383aa6
    		prefix="$OPTARG"
383aa6
    		;;
383aa6
	\?)
f6cb52
		printf 'Invalid option: -%s' "$OPTARG" >&2
383aa6
    		usage
383aa6
    		;;
383aa6
	esac
383aa6
done
383aa6
383aa6
383aa6
# two
383aa6
if [ -z "$srcdir" ] || [ -z "$output" ] || [ -z "$prefix" ]; then
383aa6
	usage
383aa6
fi
383aa6
4bd0de
cd -- "$srcdir"
383aa6
e9f5b6
gitver=$(git rev-parse --verify HEAD      2>/dev/null) || gitver="unknown"
e9f5b6
cvdate=$(git show -s --format=%ci $gitver 2>/dev/null) || cvdate=$(date)
383aa6
314402
vmacro=$(printf '%s' "$prefix"'_GIT_VERSION' | tr '[:lower:]' '[:upper:]')
314402
dmacro=$(printf '%s' "$prefix"'_GIT_DATE   ' | tr '[:lower:]' '[:upper:]')
314402
4bd0de
cd -- "$workdir"
314402
46f669
46f669
# three
314402
printf '#define %s "%s"\n#define %s "%s"\n' \
314402
		"$vmacro" "$gitver" \
314402
		"$dmacro" "$cvdate" \
314402
	> "$output"
383aa6
383aa6
# all done
383aa6
exit 0