Blame sofort/tools/version.sh

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