package/hostapd: enhance compile-options a bit, eases testing
[openadk.git] / scripts / ipkg-build
blob47158c4094f21dc4bc34da08ab4e21a8166ef1c4
1 #!/usr/bin/env bash
2 # ipkg-build -- construct a .ipk from a directory
3 # Waldemar Brodkorb <wbx@openadk.org>
4 # use cpio instead of tar for uid/gid handling
5 # Carl Worth <cworth@east.isi.edu>
6 # based on a script by Steve Redler IV <steve@sr-tech.com>
7 set -e
9 version=1.0
11 ipkg_extract_value() {
12 sed -e "s/^[^:]*:[[:space:]]*//"
15 required_field() {
16 field=$1
18 value=`grep "^$field:" < $CONTROL/control | ipkg_extract_value`
19 if [ -z "$value" ]; then
20 echo "*** Error: $CONTROL/control is missing field $field" >&2
21 return 1
23 echo $value
24 return 0
27 disallowed_field() {
28 field=$1
30 value=`grep "^$field:" < $CONTROL/control | ipkg_extract_value`
31 if [ -n "$value" ]; then
32 echo "*** Error: $CONTROL/control contains disallowed field $field" >&2
33 return 1
35 echo $value
36 return 0
39 pkg_appears_sane() {
40 local pkg_dir=$1
42 local owd=$PWD
43 cd $pkg_dir
45 PKG_ERROR=0
47 tilde_files=`find . -name '*~'`
48 if [ -n "$tilde_files" ]; then
49 if [ "$noclean" = "1" ]; then
50 echo "*** Warning: The following files have names ending in '~'.
51 You probably want to remove them: " >&2
52 ls -ld $tilde_files
53 echo >&2
54 else
55 echo "*** Removing the following files: $tilde_files"
56 rm -f "$tilde_files"
60 if [ ! -f "$CONTROL/control" ]; then
61 echo "*** Error: Control file $pkg_dir/$CONTROL/control not found." >&2
62 cd $owd
63 return 1
66 pkg=`required_field Package`
67 [ "$?" -ne 0 ] && PKG_ERROR=1
69 version=`required_field Version | sed 's/Version://; s/^.://g;'`
70 [ "$?" -ne 0 ] && PKG_ERROR=1
72 arch=`required_field Architecture`
73 [ "$?" -ne 0 ] && PKG_ERROR=1
75 required_field Maintainer >/dev/null
76 [ "$?" -ne 0 ] && PKG_ERROR=1
78 required_field Description >/dev/null
79 [ "$?" -ne 0 ] && PKG_ERROR=1
81 section=`required_field Section`
82 [ "$?" -ne 0 ] && PKG_ERROR=1
83 if [ -z "$section" ]; then
84 echo "The Section field should have one of the following values:" >&2
85 echo "admin, base, comm, editors, extras, games, graphics, kernel, lang, libs, misc, net, scm, text, web, x11" >&2
88 priority=`required_field Priority`
89 [ "$?" -ne 0 ] && PKG_ERROR=1
90 if [ -z "$priority" ]; then
91 echo "The Priority field should have one of the following values:" >&2
92 echo "required, important, standard, optional, extra." >&2
93 echo "If you don't know which priority value you should be using, then use \`optional'" >&2
96 source=`required_field Source`
97 [ "$?" -ne 0 ] && PKG_ERROR=1
98 if [ -z "$source" ]; then
99 echo "The Source field contain the URL's or filenames of the source code and any patches"
100 echo "used to build this package. Either gnu-style tarballs or Debian source packages "
101 echo "are acceptable. Relative filenames may be used if they are distributed in the same"
102 echo "directory as the .ipk file."
105 disallowed_filename=`disallowed_field Filename`
106 [ "$?" -ne 0 ] && PKG_ERROR=1
108 if echo $pkg | grep '[^a-z0-9.+-]'; then
109 echo "*** Error: Package name $name contains illegal characters, (other than [a-z0-9.+-])" >&2
110 PKG_ERROR=1;
113 local bad_fields=`sed -ne 's/^\([^[:space:]][^:[:space:]]\+[[:space:]]\+\)[^:].*/\1/p' < $CONTROL/control | sed -e 's/\\n//'`
114 if [ -n "$bad_fields" ]; then
115 bad_fields=`echo $bad_fields`
116 echo "*** Error: The following fields in $CONTROL/control are missing a ':'" >&2
117 echo " $bad_fields" >&2
118 echo "ipkg-build: This may be due to a missing initial space for a multi-line field value" >&2
119 PKG_ERROR=1
122 for script in $CONTROL/preinst $CONTROL/postinst $CONTROL/prerm $CONTROL/postrm; do
123 if [ -f $script -a ! -x $script ]; then
124 if [ "$noclean" = "1" ]; then
125 echo "*** Error: package script $script is not executable" >&2
126 PKG_ERROR=1
127 else
128 chmod a+x $script
131 done
133 if [ -f $CONTROL/conffiles ]; then
134 for cf in `cat $CONTROL/conffiles`; do
135 if [ ! -f ./$cf ]; then
136 echo "*** Error: $CONTROL/conffiles mentions conffile $cf which does not exist" >&2
137 PKG_ERROR=1
139 done
142 cd $owd
143 return $PKG_ERROR
147 # ipkg-build "main"
149 noclean=0
150 usage="Usage: $0 [-C] <pkg_directory> [<destination_directory>]"
151 while getopts ":h:v" opt; do
152 case $opt in
153 C ) noclean=1
155 v ) echo $version
156 exit 0
158 h ) echo $usage >&2 ;;
159 \? ) echo $usage >&2
160 esac
161 done
163 shift $(($OPTIND - 1))
165 # continue on to process additional arguments
167 case $# in
169 dest_dir=$PWD
172 dest_dir=$2
173 if [ "$dest_dir" = "." -o "$dest_dir" = "./" ] ; then
174 dest_dir=$PWD
178 echo $usage >&2
179 exit 1
181 esac
183 pkg_dir=$1
185 if [ ! -d $pkg_dir ]; then
186 echo "*** Error: Directory $pkg_dir does not exist" >&2
187 exit 1
190 # CONTROL is second so that it takes precedence
191 CONTROL=
192 [ -d $pkg_dir/DEBIAN ] && CONTROL=DEBIAN
193 [ -d $pkg_dir/CONTROL ] && CONTROL=CONTROL
194 if [ -z "$CONTROL" ]; then
195 echo "*** Error: Directory $pkg_dir has no CONTROL subdirectory." >&2
196 exit 1
199 if ! pkg_appears_sane $pkg_dir; then
200 echo >&2
201 echo "ipkg-build: Please fix the above errors and try again." >&2
202 exit 1
205 tmp_dir=$dest_dir/IPKG_BUILD.$$
206 mkdir $tmp_dir
208 ( cd $pkg_dir && find . | grep -v $CONTROL | \
209 sed "s#\(.*\)#:0:0::::::\1#" | \
210 sort | cpio -o -Hustar -P | gzip -n9 > $tmp_dir/data.tar.gz )
212 ( cd $pkg_dir/$CONTROL && find . | \
213 sed "s#\(.*\)#:0:0::::::\1#" | \
214 sort | cpio -o -Hustar -P | gzip -n9 > $tmp_dir/control.tar.gz )
216 echo "2.0" > $tmp_dir/debian-binary
217 pkg_file=$dest_dir/${pkg}_${version}_${arch}.ipk
218 rm -f $pkg_file
219 ( cd $tmp_dir && tar -zcf $pkg_file ./debian-binary ./data.tar.gz ./control.tar.gz )
220 rm $tmp_dir/debian-binary $tmp_dir/data.tar.gz $tmp_dir/control.tar.gz
221 rmdir $tmp_dir
222 echo "Packaged contents of $pkg_dir into $pkg_file"