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