Blame sofort/tools/version.sh

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