Blame sofort/tools/version.sh

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