chiark / gitweb /
auto-version.in: Factor out Debian version checking.
[cfd] / build / auto-version
1 #! /bin/sh
2 ### -*-sh-*-
3 ###
4 ### Make autoconf-like substitutions in files
5 ###
6 ### (c) 2008 Mark Wooding
7 ###
8
9 ###----- Licensing notice ---------------------------------------------------
10 ###
11 ### This file is part of the Common Files Distribution (`common').
12 ###
13 ### `Common' is free software; you can redistribute it and/or modify
14 ### it under the terms of the GNU General Public License as published by
15 ### the Free Software Foundation; either version 2 of the License, or
16 ### (at your option) any later version.
17 ###
18 ### `Common' is distributed in the hope that it will be useful,
19 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 ### GNU General Public License for more details.
22 ###
23 ### You should have received a copy of the GNU General Public License
24 ### along with `common'; if not, write to the Free Software Foundation,
25 ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26
27 set -e
28
29 ###--------------------------------------------------------------------------
30 ### Parse command line arguments.
31
32 while [ $# -gt 0 ]; do
33   case $1 in
34     -h | --h | --he | --hel | --help)
35       cat <<EOF
36 Usage: auto-verison
37
38 Writes on standard output the program's version number.
39 EOF
40       exit 0
41       ;;
42     --)
43       shift
44       break
45       ;;
46     -)
47       break
48       ;;
49     -*)
50       echo "auto-version: unknown option \`$1'" >&2
51       exit 1
52       ;;
53     *)
54       break
55       ;;
56   esac
57   shift
58 done
59
60 if [ $# -ne 0 ]; then
61   echo >&2 "Usage: auto-version"
62   exit 1
63 fi
64
65 ###--------------------------------------------------------------------------
66 ### Main program.
67
68 ## Get the Debian version, if there is one.
69 if [ -f debian/changelog ]; then
70   debver=$(sed -n '/^.*(\(.*\)).*$/ { s::\1:p; q; }' debian/changelog)
71 else
72   unset debver
73 fi
74
75 ## If this is a Git checkout then Git should be able to identify the version.
76 ## If there's also a Debian version, and that ends in a `~', then prefix the
77 ## Git version with this.  Note that `pkg-config' is not very good, and, in
78 ## particular, doesn't support the convention that `~' sorts before anything
79 ## else, even the empty string, despite claiming to implement the RPM
80 ## version-comparison algorithm which specifies this behaviour, so one must
81 ## be careful when choosing `~' prefixes.
82 if [ -e .git ] && version=$(git describe --abbrev=4 --dirty=+ 2>/dev/null); then
83   case ${debver-nil} in *~) version=$debver$version ;; esac
84   echo "$version"
85   exit 0
86 fi
87
88 ## If this was unpacked from a source distribution, the RELEASE file should
89 ## say what the version was.
90 if [ -f RELEASE ]; then
91   cat RELEASE
92   exit 0
93 fi
94
95 ## If we're Debianized, then the Debian changelog ought to know.
96 case ${debver+t} in
97   t) echo "$debver"; exit 0 ;;
98 esac
99
100 ## Otherwise we're screwed.
101 echo UNKNOWN
102 exit 0
103
104 ###----- That's all, folks --------------------------------------------------