Small fixes to allow compilation with OpenBSD
[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 sourceHash="$(cat $(find . -name *.[ch] | grep -v -E '[/\\]?builddata.c$') | "$md5Command" | awk '{ print $1; }')"
27 hostName="$(hostname)"
28 buildDate="$(date +"%Y-%m-%d %H:%M:%S")"
29
30 tmpBuildDataTxt="$(mktemp -t olsrd.hash_source.XXXXXXXXXX)"
31 cat > "$tmpBuildDataTxt" << EOF
32 const char olsrd_version[] = "olsr.org - $version-git_$gitSha-hash_$sourceHash";
33 const char build_host[]    = "$hostName";
34 const char build_date[]    = "$buildDate";
35 EOF
36
37
38 if [ ! -e "$buildDataTxt" ]; then
39   echo "[CREATE] $buildDataTxt"
40   if [ "$verbose" = "0" ]; then
41     cp -p "$tmpBuildDataTxt" "$buildDataTxt"
42   else
43     cp -p -v "$tmpBuildDataTxt" "$buildDataTxt"
44   fi
45 elif [ -n "$(diff -I "^const char build_date\[\].*\$" "$tmpBuildDataTxt" "$buildDataTxt" | sed 's/"/\\"/g')" ]; then
46   echo "[UPDATE] $buildDataTxt"
47   if [ "$verbose" = "0" ]; then
48     cp -p "$tmpBuildDataTxt" "$buildDataTxt"
49   else
50     cp -p -v "$tmpBuildDataTxt" "$buildDataTxt"
51   fi
52 fi
53 rm -f "$tmpBuildDataTxt"
54