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