summaryrefslogtreecommitdiffhomepage
path: root/sysinfo/version.sh
blob: 02dd5d576e3a19d8cbe266735f56b33ca534d85a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/sh

set -eu

usage()
{
cat << EOF >&2

Usage:
  -h            show this HELP message
  -s  SRCDIR    set source directory
  -o  OUTPUT    set output header
  -p  PREFIX    set macro prefix

EOF
exit 1
}


# one
workdir=$(pwd)
srcdir=
output=
prefix=


while getopts "hs:o:p:" opt; do
	case $opt in
	h)
  		usage
  		;;
	s)
    		srcdir="$OPTARG"
    		;;
	o)
    		output="$OPTARG"
    		;;
	p)
    		prefix="$OPTARG"
    		;;
	\?)
    		printf "Invalid option: -%s" "$OPTARG" >&2
    		usage
    		;;
	esac
done


# two
if [ -z "$srcdir" ] || [ -z "$output" ] || [ -z "$prefix" ]; then
	usage
fi

cd "$srcdir"

gitver=$(git rev-parse --verify HEAD 2>/dev/null) || gitver="unknown"
cvdate=$(git show -s --format=%ci $gitver)

vmacro=$(printf '%s' "$prefix"'_GIT_VERSION' | tr '[:lower:]' '[:upper:]')
dmacro=$(printf '%s' "$prefix"'_GIT_DATE   ' | tr '[:lower:]' '[:upper:]')

cd "$workdir"


# three
printf '#define %s "%s"\n#define %s "%s"\n' \
		"$vmacro" "$gitver" \
		"$dmacro" "$cvdate" \
	> "$output"

# all done
exit 0