Blame sofort/tools/version.sh

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