Blame sofort/tools/version.sh

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