Blame sofort/tools/version.sh

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