phpide-d10
[projects.git] / tools / make / mconfigure
1 #!/bin/bash
2 set -e
3
4 function echo_usage {
5         echo "usage: mconfigure [options] <package>"
6         echo "  -a <arch>"
7         echo "  -c : configure"
8         echo "  -d : compile"
9         echo "  -p : pack default: do all"
10         echo "  -o : support old Debian 7 format"
11 }
12
13 function check_unversioned {
14         # has to be called in the projects base directory
15         # result: "M" in stdout or nothing
16         
17         set -e
18         project=$(pwd)
19         project=${project##*/}
20         pushd .. >/dev/null
21                 mCM $project -b -an > /dev/null
22                 wc=$(wc $project.batch)
23                 rm $project.batch
24                 if [ "${wc:0:5}" != "0 0 0" ]
25                 then
26                         echo "M"
27                 fi
28         popd >/dev/null
29 }
30
31 function set_build {
32         set -e
33         if [ -e .svn ]
34         then
35                 version="0.0"
36                 build=$(svnversion)
37                 build=${build/:/-}
38                 build="$build$(check_unversioned)"
39         elif [ -e .git ]
40         then
41                 tag=$(git describe --tags --match "${paket}_*" 2>/dev/null) || true
42                 if test -z "$tag"
43                 then
44                         tag="0.0-0TOP"
45                 fi
46                 changes=$(check_unversioned)
47                 tag="$tag$changes"
48                 version=${tag%-*}
49                 version=${version##*_}
50                 build=${tag##*-}
51         else
52                 echo "No configuration system found. Cannot determine version."
53                 exit 1
54         fi
55 }
56
57 function check_version {
58         set -e
59         
60         vline=$(head -1 debian/$paket.changelog)
61         vline=${vline%-*}
62         clversion=${vline#*(}
63         if [ $version = "0.0" ]
64         then
65                 version=$clversion
66                 return
67         fi
68
69         # do no check on a modified git repos
70         if [ "${build: -1}" == "M" ]
71         then
72                 return
73         fi
74
75         if [ "$clversion" != $version ]
76         then
77                 echo "version mismatch: git: $version, changelog: $clversion"
78                 exit 1
79         fi
80 }
81
82 if [ ! -d debian ]
83 then
84         echo "run mconfigure in the project base directory with a debian directory in it!"
85         exit 2
86 fi
87
88 configure=0
89 compile=0
90 pack=0
91 while getopts "a:cdpo" opt; do
92         case $opt in
93                 a) ARCH=$OPTARG
94                         ;;
95                 c) configure=1
96                         ;;
97                 d)      compile=1
98                         ;;
99                 p)      pack=1
100                         ;;
101                 o)      oldpack="-Zgzip"
102                         ;;
103                 \?) echo "Invalid option: -$OPTARG"
104                         echo_usage
105                         exit 1
106                         ;;
107                 :) echo "Option -$OPTARG requires an argument."
108                         echo_usage
109                         exit 1
110                         ;;
111         esac
112 done
113 shift $((OPTIND -1))
114
115 if [ $# -lt 1 ]
116 then
117         echo_usage
118         exit 1
119 fi
120
121 paket=$1
122
123 # if nothing is selected, select all
124 if [ $configure -eq 0 -a $compile -eq 0 -a $pack -eq 0 ]
125 then
126         configure=1
127         compile=1
128         pack=1
129 fi
130
131 if [ $configure -eq 1 ]
132 then
133         # delete changelog and control
134         rm debian/changelog debian/control 2>/dev/null || true
135         
136         # mconfigure builds 2 environment files: setenv.sh + rules.pre
137         
138         if [ -n "$ARCH" ]
139         then
140                 arch_opt="-a $ARCH"
141         fi
142         dpkg-architecture $arch_opt > debian/setenv.sh
143         echo "" > debian/rules.pre
144         
145         echo "paket=$paket" >> debian/setenv.sh
146         echo "oldpack=\"$oldpack\"" >> debian/setenv.sh
147
148         set_build
149         check_version
150         echo "version=$version" >> debian/setenv.sh
151         echo "build=$build" >> debian/setenv.sh
152
153         # get repository name
154 #       tmp=$(pwd)
155 #       pwd=${tmp##*/}
156 #       pwd=${pwd,,*}
157 #       echo "pwd=$pwd" >> debian/rules.pre
158
159         echo "building $paket with build $version-$build"
160
161         pushd debian >/dev/null
162         # ./debian -------------------------------------
163
164         ln -sf /usr/share/mbuild/rules .
165
166         if [ -e tmp ]
167         then
168                 rm -rf tmp
169         fi
170
171         # check for pre/post installation scripts
172         if [ -f $paket.preinst ]
173         then
174                 echo "add_inst_tgt += debian/tmp/DEBIAN/preinst" >> rules.pre
175         fi
176         if [ -f $paket.postinst ]
177         then
178                     echo "add_inst_tgt += debian/tmp/DEBIAN/postinst" >> rules.pre
179         fi
180         if [ -f $paket.prerm ]
181         then
182                     echo "add_inst_tgt += debian/tmp/DEBIAN/prerm" >> rules.pre
183         fi
184         if [ -f $paket.postrm ]
185         then
186                     echo "add_inst_tgt += debian/tmp/DEBIAN/postrm" >> rules.pre
187         fi
188         
189         mkdir -p tmp/DEBIAN
190
191         # add export to setenv.sh
192         sed -i "s/^/export /" setenv.sh
193
194         popd >/dev/null
195         # ./. ---------------------------------------------
196
197         # copy package control
198         if [ -f debian/$paket.control ]
199         then
200                 # proceed
201                 echo
202         else
203                 echo "warning: control file not found"
204                 echo "  this file is necessary for any package production."
205         fi
206         
207         # build prepare
208         if [ -x debian/$paket.prepare ]
209         then
210                 debian/$paket.prepare
211         fi
212         if [ -e debian/$paket.build ]
213         then
214                 if [ -e ../build ]
215                 then
216                         rm -rf ../build
217                         mkdir ../build
218                 fi
219                 if grep -- "-prepare" debian/$paket.build >/dev/null
220                 then
221                         debian/rules sync
222                         debian/$paket.build -prepare
223                 fi
224                 # sync build dir
225                 . debian/setenv.sh
226                 debian/rules sync
227                 
228                 # executing prebuild
229                 if [ -x debian/$paket.prebuild ]
230                 then
231                         pushd ~/build >/dev/null
232                         debian/$paket.prebuild
233                         popd >/dev/null
234                 fi
235         fi
236         
237         # pack prepare
238         if [ -f debian/$paket.cp -a -f debian/$paket.control ]
239         then
240                 echo "PACK=binary" >> debian/rules.pre
241                 if grep "^Architecture: *all" debian/$paket.control >/dev/null
242                 then
243                         echo "arch = all" >> debian/rules.pre
244                         echo "BINARY_INDEP = copy ../${paket}_$version-${build}_all.deb" >> debian/rules.pre
245                 else
246                         echo 'arch = ${DEB_HOST_ARCH}' >> debian/rules.pre
247                         echo "BINARY_ARCH = copy ../${paket}_$version-${build}_"'$(arch).deb' >> debian/rules.pre
248                 fi
249         elif [ -f debian/$paket.cp ]
250         then
251                 echo "PACK=zip" >> debian/rules.pre
252         else
253                 echo "PACK=version" >> debian/rules.pre
254         fi
255 fi
256
257 if [ $compile -eq 1 -a ! -e debian/$paket.build ]
258 then
259         echo "no debian/$paket.build: skipping build step"
260         compile=0
261 fi
262 if [ $compile -eq 1 ]
263 then
264         # build artefacts
265         . debian/setenv.sh
266         debian/rules build
267 fi
268
269 if [ $pack -eq 1 ]
270 then
271         # build package
272         . debian/setenv.sh
273         debian/rules pack
274 fi
275