Blame sofort/tools/version.sh

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