Blame sofort/tools/version.sh

5b49a6
#!/bin/sh
5b49a6
0bd54c
set -eu
0bd54c
5b49a6
usage()
5b49a6
{
5b49a6
cat << EOF >&2
5b49a6
5b49a6
Usage:
5b49a6
  -h            show this HELP message
5b49a6
  -s  SRCDIR    set source directory
5b49a6
  -o  OUTPUT    set output header
5b49a6
  -p  PREFIX    set macro prefix
5b49a6
5b49a6
EOF
5b49a6
exit 1
5b49a6
}
5b49a6
5b49a6
5b49a6
# one
5b49a6
workdir=$(pwd)
5b49a6
srcdir=
5b49a6
output=
5b49a6
prefix=
5b49a6
5b49a6
5b49a6
while getopts "hs:o:p:" opt; do
5b49a6
	case $opt in
5b49a6
	h)
5b49a6
  		usage
5b49a6
  		;;
5b49a6
	s)
5b49a6
    		srcdir="$OPTARG"
5b49a6
    		;;
5b49a6
	o)
5b49a6
    		output="$OPTARG"
5b49a6
    		;;
5b49a6
	p)
5b49a6
    		prefix="$OPTARG"
5b49a6
    		;;
5b49a6
	\?)
5b49a6
    		printf "Invalid option: -%s" "$OPTARG" >&2
5b49a6
    		usage
5b49a6
    		;;
5b49a6
	esac
5b49a6
done
5b49a6
5b49a6
5b49a6
# two
5b49a6
if [ -z "$srcdir" ] || [ -z "$output" ] || [ -z "$prefix" ]; then
5b49a6
	usage
5b49a6
fi
5b49a6
0bd54c
cd "$srcdir"
5b49a6
dc4618
gitver=$(git rev-parse --verify HEAD      2>/dev/null) || gitver="unknown"
dc4618
cvdate=$(git show -s --format=%ci $gitver 2>/dev/null) || cvdate=$(date)
5b49a6
35a21f
vmacro=$(printf '%s' "$prefix"'_GIT_VERSION' | tr '[:lower:]' '[:upper:]')
35a21f
dmacro=$(printf '%s' "$prefix"'_GIT_DATE   ' | tr '[:lower:]' '[:upper:]')
35a21f
0bd54c
cd "$workdir"
35a21f
01f474
01f474
# three
35a21f
printf '#define %s "%s"\n#define %s "%s"\n' \
35a21f
		"$vmacro" "$gitver" \
35a21f
		"$dmacro" "$cvdate" \
35a21f
	> "$output"
5b49a6
5b49a6
# all done
5b49a6
exit 0