chiark / gitweb /
007da8be538bb57ae33f0bdc00a04f96af336ba1
[cfd] / auto-version.in
1 #! /bin/sh
2 ### -*-sh-*-
3 ###
4 ### Guess at the project's version number
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 VERSION="@VERSION@"
29
30 ###--------------------------------------------------------------------------
31 ### Parse command line arguments.
32
33 while [ $# -gt 0 ]; do
34   case $1 in
35     -h | --h | --he | --hel | --help)
36       cat <<EOF
37 Usage: auto-verison
38
39 Writes on standard output the program's version number.
40 EOF
41       exit 0
42       ;;
43     -v | --v | --ve | --ver | --vers | --versi | --versio | --version)
44       echo "auto-version: Common Files Distribution version $VERSION"
45       exit 0
46       ;;
47     --)
48       shift
49       break
50       ;;
51     -)
52       break
53       ;;
54     -*)
55       echo "auto-version: unknown option \`$1'" >&2
56       exit 1
57       ;;
58     *)
59       break
60       ;;
61   esac
62   shift
63 done
64
65 if [ $# -ne 0 ]; then
66   echo >&2 "Usage: auto-version"
67   exit 1
68 fi
69
70 ###--------------------------------------------------------------------------
71 ### Main program.
72
73 ## Get the Debian version, if there is one.
74 if [ -f debian/changelog ]; then
75   debver=$(sed -n '/^.*(\(.*\)).*$/ { s::\1:p; q; }' debian/changelog)
76 else
77   unset debver
78 fi
79
80 ## If this is a Git checkout then Git should be able to identify the version.
81 ## If there's also a Debian version, and that ends in a `~', then prefix the
82 ## Git version with this.  Note that `pkg-config' is not very good, and, in
83 ## particular, doesn't support the convention that `~' sorts before anything
84 ## else, even the empty string, despite claiming to implement the RPM
85 ## version-comparison algorithm which specifies this behaviour, so one must
86 ## be careful when choosing `~' prefixes.
87 if [ -e .git ] && version=$(git describe --abbrev=4 --dirty=+ 2>/dev/null); then
88   case ${debver-nil} in *~) version=$debver$version ;; esac
89   echo "$version"
90   exit 0
91 fi
92
93 ## If this was unpacked from a source distribution, the RELEASE file sould
94 ## say what the version was.
95 if [ -f RELEASE ]; then
96   cat RELEASE
97   exit 0
98 fi
99
100 ## If we're Debianized, then the Debian changelog ought to know.
101 case ${debver+t} in
102   t) echo "$debver"; exit 0 ;;
103 esac
104
105 ## Otherwise we're screwed.
106 echo UNKNOWN
107 exit 0
108
109 ###----- That's all, folks --------------------------------------------------