info: make the INFO_PLUGIN_CONFIG_PLUGIN_PARAMETERS a bit safer
[olsrd.git] / make / hash_source.sh
1 #!/bin/sh
2
3 if [ $# -ne 3 ]; then
4         echo "ERROR: Supply filename, version and verbosity"
5         echo "       Example: $0 builddata.txt pre-0.6.7 1"
6         exit 1
7 fi
8
9 buildDataTxt="$1"
10 version="$2"
11 verbose="$3"
12
13 md5Command="md5sum"
14 osName="$(uname)"
15 if [ "x$osName" = "xDarwin" ] ; then
16   md5Command="md5"
17 elif [ "x$osName" = "xOpenBSD" ] ; then
18   md5Command="md5"
19 fi
20
21
22 gitSha="$(git log -1 --pretty=%h 2> /dev/null)"
23 if [ -z "$gitSha" ]; then
24   gitSha="0000000"
25 fi
26
27 gitShaFull="$(git rev-list -1 HEAD 2> /dev/null)"
28 if [ -z "$gitShaFull" ]; then
29   gitShaFull="0000000000000000000000000000000000000000"
30 fi
31
32 gitDescriptor="$(git describe --dirty --always 2> /dev/null)"
33
34 sourceHash="$(cat $(find . -name *.[ch] | grep -v -E '[/\\]?builddata.c$') | "$md5Command" | awk '{ print $1; }')"
35 hostName="$(hostname)"
36 buildDate="$(date +"%Y-%m-%d %H:%M:%S")"
37
38 tmpBuildDataTxt="$(mktemp -t olsrd.hash_source.XXXXXXXXXX)"
39 cat > "$tmpBuildDataTxt" << EOF
40 const char olsrd_version[]   = "olsr.org - $version-git_$gitSha-hash_$sourceHash";
41
42 const char build_date[]      = "$buildDate";
43 const char build_host[]      = "$hostName";
44 const char git_descriptor[]  = "$gitDescriptor";
45 const char git_sha[]         = "$gitShaFull";
46 const char release_version[] = "$version";
47 const char source_hash[]     = "$sourceHash";
48 EOF
49
50
51 if [ ! -e "$buildDataTxt" ]; then
52   echo "[CREATE] $buildDataTxt"
53   if [ "$verbose" = "0" ]; then
54     cp -p "$tmpBuildDataTxt" "$buildDataTxt"
55   else
56     cp -p -v "$tmpBuildDataTxt" "$buildDataTxt"
57   fi
58 elif [ -n "$(diff -I "^const char build_date\[\].*\$" "$tmpBuildDataTxt" "$buildDataTxt" | sed 's/"/\\"/g')" ]; then
59   echo "[UPDATE] $buildDataTxt"
60   if [ "$verbose" = "0" ]; then
61     cp -p "$tmpBuildDataTxt" "$buildDataTxt"
62   else
63     cp -p -v "$tmpBuildDataTxt" "$buildDataTxt"
64   fi
65 fi
66 rm -f "$tmpBuildDataTxt"
67