Blame sofort/tools/version.sh

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