Blame sofort/tools/version.sh

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