Blame sofort/tools/version.sh

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