Blame sofort/tools/version.sh

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