Blame sofort/tools/version.sh

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