tfortune-1.0.1.
[tfortune.git] / version-gen.sh
1 #!/bin/sh
2
3 # SPDX-License-Identifier: GPL-3.0-only
4
5 package="$1"
6 version_file="$2"
7
8 ver='unnamed_version'
9 # First try git, then gitweb, then default.
10 if [ -e '.git' -o -e '../.git' ]; then
11 git_ver=$(git describe --abbrev=4 HEAD 2>/dev/null)
12 [ -z "$git_ver" ] && git_ver="$ver"
13 # update stat information in index to match working tree
14 git update-index -q --refresh > /dev/null
15 # if there are differences (exit code 1), the working tree is dirty
16 git diff-index --quiet HEAD || git_ver=$git_ver-dirty
17 ver=$git_ver
18 elif [ "${PWD%%-*}" = $package- ]; then
19 ver=${PWD##*/$package-}
20 fi
21 ver=${ver#v}
22
23 echo "$ver"
24 [ -z "${version_file}" ] && exit 0
25 # update version file if necessary
26 content="const char *${package}_version(void) {return \"$ver\";};"
27 [ -r "$version_file" ] && echo "$content" | cmp -s - $version_file && exit 0
28 echo "$content" > $version_file