Blame sofort/version.sh

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