Blame sofort/tools/version.sh

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