Blame sofort/tools/version.sh

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