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