Blame sofort/tools/version.sh

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