r23605: Update mkversion.sh to support git and git-svn checkouts.
[jelmer/samba4-debian.git] / source / script / mkversion.sh
1 #!/bin/sh
2
3 VERSION_FILE=$1
4 OUTPUT_FILE=$2
5
6 if test -z "$VERSION_FILE";then
7         VERSION_FILE="VERSION"
8 fi
9
10 if test -z "$OUTPUT_FILE";then
11         OUTPUT_FILE="version.h"
12 fi
13
14 SOURCE_DIR=$3
15
16 SAMBA_VERSION_MAJOR=`sed -n 's/^SAMBA_VERSION_MAJOR=//p' $SOURCE_DIR$VERSION_FILE`
17 SAMBA_VERSION_MINOR=`sed -n 's/^SAMBA_VERSION_MINOR=//p' $SOURCE_DIR$VERSION_FILE`
18 SAMBA_VERSION_RELEASE=`sed -n 's/^SAMBA_VERSION_RELEASE=//p' $SOURCE_DIR$VERSION_FILE`
19
20 SAMBA_VERSION_REVISION=`sed -n 's/^SAMBA_VERSION_REVISION=//p' $SOURCE_DIR$VERSION_FILE`
21
22 SAMBA_VERSION_TP_RELEASE=`sed -n 's/^SAMBA_VERSION_TP_RELEASE=//p' $SOURCE_DIR$VERSION_FILE`
23 SAMBA_VERSION_PRE_RELEASE=`sed -n 's/^SAMBA_VERSION_PRE_RELEASE=//p' $SOURCE_DIR$VERSION_FILE`
24 SAMBA_VERSION_RC_RELEASE=`sed -n 's/^SAMBA_VERSION_RC_RELEASE=//p' $SOURCE_DIR$VERSION_FILE`
25
26 SAMBA_VERSION_IS_SVN_SNAPSHOT=`sed -n 's/^SAMBA_VERSION_IS_SVN_SNAPSHOT=//p' $SOURCE_DIR$VERSION_FILE`
27
28 SAMBA_VERSION_RELEASE_NICKNAME=`sed -n 's/^SAMBA_VERSION_RELEASE_NICKNAME=//p' $SOURCE_DIR$VERSION_FILE`
29
30 SAMBA_VERSION_VENDOR_SUFFIX=`sed -n 's/^SAMBA_VERSION_VENDOR_SUFFIX=//p' $SOURCE_DIR$VERSION_FILE`
31 SAMBA_VERSION_VENDOR_PATCH=`sed -n 's/^SAMBA_VERSION_VENDOR_PATCH=//p' $SOURCE_DIR$VERSION_FILE`
32
33 echo "/* Autogenerated by script/mkversion.sh */" > $OUTPUT_FILE
34
35 echo "#define SAMBA_VERSION_MAJOR ${SAMBA_VERSION_MAJOR}" >> $OUTPUT_FILE
36 echo "#define SAMBA_VERSION_MINOR ${SAMBA_VERSION_MINOR}" >> $OUTPUT_FILE
37 echo "#define SAMBA_VERSION_RELEASE ${SAMBA_VERSION_RELEASE}" >> $OUTPUT_FILE
38
39
40 ##
41 ## start with "3.0.22"
42 ##
43 SAMBA_VERSION_STRING="${SAMBA_VERSION_MAJOR}.${SAMBA_VERSION_MINOR}.${SAMBA_VERSION_RELEASE}"
44
45
46 ##
47 ## maybe add "3.0.22a" or "4.0.0tp11" or "3.0.22pre1" or "3.0.22rc1"
48 ## We do not do pre or rc version on patch/letter releases
49 ##
50 if test -n "${SAMBA_VERSION_REVISION}";then
51     SAMBA_VERSION_STRING="${SAMBA_VERSION_STRING}${SAMBA_VERSION_REVISION}"
52     echo "#define SAMBA_VERSION_REVISION \"${SAMBA_VERSION_REVISION}\"" >> $OUTPUT_FILE
53 elif test -n "${SAMBA_VERSION_TP_RELEASE}";then
54     SAMBA_VERSION_STRING="${SAMBA_VERSION_STRING}tp${SAMBA_VERSION_TP_RELEASE}"
55     echo "#define SAMBA_VERSION_TP_RELEASE ${SAMBA_VERSION_TP_RELEASE}" >> $OUTPUT_FILE
56 elif test -n "${SAMBA_VERSION_PRE_RELEASE}";then
57     ## maybe add "3.0.22pre2"
58     SAMBA_VERSION_STRING="${SAMBA_VERSION_STRING}pre${SAMBA_VERSION_PRE_RELEASE}"
59     echo "#define SAMBA_VERSION_PRE_RELEASE ${SAMBA_VERSION_PRE_RELEASE}" >> $OUTPUT_FILE
60 elif test -n "${SAMBA_VERSION_RC_RELEASE}";then
61     SAMBA_VERSION_STRING="${SAMBA_VERSION_STRING}rc${SAMBA_VERSION_RC_RELEASE}"
62     echo "#define SAMBA_VERSION_RC_RELEASE ${SAMBA_VERSION_RC_RELEASE}" >> $OUTPUT_FILE
63 fi
64
65 ##
66 ## SVN revision number? 
67 ##
68 if test x"${SAMBA_VERSION_IS_SVN_SNAPSHOT}" = x"yes";then
69     _SAVE_LANG=${LANG}
70     LANG=""
71     HAVEVER="no"
72
73     if test x"${HAVEVER}" != x"yes";then
74         HAVESVN=no
75         SVN_INFO=`svn info ${SOURCE_DIR} 2>/dev/null`
76         TMP_REVISION=`echo -e "${SVN_INFO}" | grep 'Last Changed Rev.*:' |sed -e 's/Last Changed Rev.*: \([0-9]*\).*/\1/'`
77         if test -n "$TMP_REVISION"; then
78             HAVESVN=yes
79             HAVEVER=yes
80         fi
81     fi
82
83     if test x"${HAVEVER}" != x"yes";then
84         HAVESVK=no
85         SVK_INFO=`svk info ${SOURCE_DIR} 2>/dev/null`
86         TMP_REVISION=`echo -e "${SVK_INFO}" | grep 'Last Changed Rev.*:' |sed -e 's/Last Changed Rev.*: \([0-9]*\).*/\1/'`
87         if test -n "$TMP_REVISION"; then
88             HAVESVK=yes
89             HAVEVER=yes
90         fi
91         TMP_MIRRORED_REVISION=`echo -e "${SVK_INFO}" | grep 'Mirrored From:.*samba\.org.*' |sed -e 's/Mirrored From: .* Rev\..* \([0-9]*\).*/\1/'`
92     fi
93
94     if test x"${HAVEVER}" != x"yes";then
95         HAVEBZR=no
96         BZR_INFO=`bzr version-info --check-clean ${SOURCE_DIR} 2>/dev/null`
97         TMP_REVISION=`echo -e "${BZR_INFO}" | grep 'revno:' |sed -e 's/revno: \([0-9]*\).*/\1/'`
98         if test -n "$TMP_REVISION"; then
99             HAVEBZR=yes
100             HAVEVER=yes
101         fi
102         TMP_MIRRORED_REVISION=`echo -e "${BZR_INFO}" | grep 'revision-id: svn-v1:.*@0c0555d6-39d7-0310-84fc-f1cc0bd64818' |sed -e 's/revision-id: svn-v1:\([0-9]*\)@0c0555d6-39d7-0310-84fc-f1cc0bd64818.*/\1/'`
103         TMP_BRANCH_NICK=`echo -e "${BZR_INFO}" | grep 'branch-nick:' |sed -e 's/branch-nick: \(.*\)$/\1/'`
104         TMP_CLEAN_TREE=`echo -e "${BZR_INFO}" | grep 'clean:' |sed -e 's/clean: \([a-zA-Z]*\).*/\1/'`
105     fi
106
107     if test x"${HAVEVER}" != x"yes";then
108         HAVEGIT=no
109         GIT_INFO=`git show --abbrev-commit HEAD 2>/dev/null`
110         TMP_REVISION=`echo -e "${GIT_INFO}" | grep 'commit ' |sed -e 's/commit \([0-9a-f]*\).*/\1/'`
111         if test -n "$TMP_REVISION";then
112             HAVEGIT=yes
113             HAVEVER=yes
114         fi
115         TMP_MIRRORED_REVISION=`echo -e "${GIT_INFO}" | grep 'git-svn-id' |sed -e 's#^[ ^t]*git-svn-id: svn+ssh://svn.samba.org/data/svn/samba/branches/\(SAMBA_[34]_[0-9]\(_[0-9]\+\)\?@[0-9]*\).*#\1#'`
116     fi
117         
118
119     if test x"${HAVESVN}" = x"yes";then
120         SAMBA_VERSION_STRING="${SAMBA_VERSION_STRING}-SVN-build-${TMP_REVISION}"
121         echo "#define SAMBA_VERSION_SVN_REVISION ${TMP_REVISION}" >> $OUTPUT_FILE
122     elif test x"${HAVESVK}" = x"yes";then
123         if test -n "$TMP_MIRRORED_REVISION"; then
124             TMP_SVK_REVISION_STR="${TMP_REVISION}-${USER}@${HOSTNAME}-[SVN-${TMP_MIRRORED_REVISION}]"
125         else
126             TMP_SVK_REVISION_STR="${TMP_REVISION}-${USER}@${HOSTNAME}"
127         fi
128         SAMBA_VERSION_STRING="${SAMBA_VERSION_STRING}-SVK-build-${TMP_SVK_REVISION_STR}"
129     elif test x"${HAVEBZR}" = x"yes";then
130         TMP_BZR_REVISION_STR="${TMP_REVISION}"
131
132         if test -n "$TMP_BRANCH_NICK"; then
133             TMP_BZR_REVISION_STR="${TMP_BZR_REVISION_STR}${TMP_MOD_STR}@${TMP_BRANCH_NICK}"
134         fi
135
136         if test -n "$TMP_MIRRORED_REVISION"; then
137             TMP_BZR_REVISION_STR="${TMP_BZR_REVISION_STR}-[SVN-${TMP_MIRRORED_REVISION}]"
138         fi
139
140         if test  x"$TMP_CLEAN_TREE" != x"True"; then
141             TMP_BZR_REVISION_STR="${TMP_BZR_REVISION_STR}-[modified]"
142         fi
143
144         SAMBA_VERSION_STRING="${SAMBA_VERSION_STRING}-BZR-${TMP_BZR_REVISION_STR}"
145     elif test x"${HAVEGIT}" = x"yes";then
146         TMP_GIT_REVISION_STR="${TMP_REVISION}"
147
148         if test -n "$TMP_MIRRORED_REVISION"; then
149             TMP_GIT_REVISION_STR="${TMP_GIT_REVISION_STR}-[SVN-${TMP_MIRRORED_REVISION}]"
150         fi
151
152         SAMBA_VERSION_STRING="${SAMBA_VERSION_STRING}-GIT-${TMP_GIT_REVISION_STR}"
153     else
154         SAMBA_VERSION_STRING="${SAMBA_VERSION_STRING}-SVN-build-UNKNOWN"
155     fi
156     LANG=${_SAVE_LANG}
157 fi
158
159 echo "#define SAMBA_VERSION_OFFICIAL_STRING \"${SAMBA_VERSION_STRING}\"" >> $OUTPUT_FILE
160
161 ##
162 ## Add the vendor string if present
163 ##
164 if test -n "${SAMBA_VERSION_VENDOR_SUFFIX}";then
165     echo "#define SAMBA_VERSION_VENDOR_SUFFIX ${SAMBA_VERSION_VENDOR_SUFFIX}" >> $OUTPUT_FILE
166     SAMBA_VERSION_STRING="${SAMBA_VERSION_STRING}-${SAMBA_VERSION_VENDOR_SUFFIX}"
167     if test -n "${SAMBA_VERSION_VENDOR_PATCH}";then
168         echo "#define SAMBA_VERSION_VENDOR_PATCH ${SAMBA_VERSION_VENDOR_PATCH}" >> $OUTPUT_FILE
169         SAMBA_VERSION_STRING="${SAMBA_VERSION_STRING}-${SAMBA_VERSION_VENDOR_PATCH}"
170     fi
171 fi
172
173 ##
174 ## Add a release nickname
175 ##
176 if test -n "${SAMBA_VERSION_RELEASE_NICKNAME}";then
177     echo "#define SAMBA_VERSION_RELEASE_NICKNAME ${SAMBA_VERSION_RELEASE_NICKNAME}" >> $OUTPUT_FILE
178     SAMBA_VERSION_STRING="${SAMBA_VERSION_STRING} (${SAMBA_VERSION_RELEASE_NICKNAME})"
179 fi
180
181 echo "#define SAMBA_VERSION_STRING \"${SAMBA_VERSION_STRING}\"" >> $OUTPUT_FILE
182
183 echo "$0: '$OUTPUT_FILE' created for Samba(\"${SAMBA_VERSION_STRING}\")"
184
185 exit 0