lua: honor LDFLAGS
[openwrt.git] / scripts / ipkg-build
1 #!/bin/sh
2
3 # ipkg-build -- construct a .ipk from a directory
4 # Carl Worth <cworth@east.isi.edu>
5 # based on a script by Steve Redler IV, steve@sr-tech.com 5-21-2001
6 # 2003-04-25 rea@sr.unh.edu
7 #   Updated to work on Familiar Pre0.7rc1, with busybox tar.
8 #   Note it Requires: binutils-ar (since the busybox ar can't create)
9 #   For UID debugging it needs a better "find".
10 set -e
11
12 version=1.0
13 FIND="$(which find)"
14 FIND="${FIND:-$(which gfind)}"
15 TAR="${TAR:-$(which tar)}"
16 export GZIP="-n"
17
18 ipkg_extract_value() {
19         sed -e "s/^[^:]*:[[:space:]]*//"
20 }
21
22 required_field() {
23         field=$1
24
25         grep "^$field:" < $CONTROL/control | ipkg_extract_value
26 }
27
28 pkg_appears_sane() {
29         local pkg_dir=$1
30
31         local owd=$PWD
32         cd $pkg_dir
33
34         PKG_ERROR=0
35         pkg=`required_field Package`
36         version=`required_field Version | sed 's/Version://; s/^.://g;'`
37         arch=`required_field Architecture`
38
39         if echo $pkg | grep '[^a-zA-Z0-9_.+-]'; then
40                 echo "*** Error: Package name $name contains illegal characters, (other than [a-z0-9.+-])" >&2
41                 PKG_ERROR=1;
42         fi
43
44         if [ -f $CONTROL/conffiles ]; then
45                 rm -f $CONTROL/conffiles.resolved
46
47                 for cf in `$FIND $(sed -e "s!^/!$pkg_dir/!" $CONTROL/conffiles) -type f`; do
48                         echo "${cf#$pkg_dir}" >> $CONTROL/conffiles.resolved
49                 done
50
51                 rm $CONTROL/conffiles
52                 mv $CONTROL/conffiles.resolved $CONTROL/conffiles
53                 chmod 0644 $CONTROL/conffiles
54         fi
55
56         cd $owd
57         return $PKG_ERROR
58 }
59
60 ###
61 # ipkg-build "main"
62 ###
63 ogargs=""
64 noclean=0
65 usage="Usage: $0 [-c] [-C] [-o owner] [-g group] <pkg_directory> [<destination_directory>]"
66 while getopts "cg:ho:v" opt; do
67     case $opt in
68         o ) owner=$OPTARG
69             ogargs="--owner=$owner"
70             ;;
71         g ) group=$OPTARG
72             ogargs="$ogargs --group=$group"
73             ;;
74         c ) ;;
75         C ) noclean=1;;
76         v ) echo $version
77             exit 0
78             ;;
79         h )     echo $usage  >&2 ;;
80         \? )    echo $usage  >&2
81         esac
82 done
83
84
85 shift $(($OPTIND - 1))
86
87 # continue on to process additional arguments
88
89 case $# in
90 1)
91         dest_dir=$PWD
92         ;;
93 2)
94         dest_dir=$2
95         if [ "$dest_dir" = "." -o "$dest_dir" = "./" ] ; then
96             dest_dir=$PWD
97         fi
98         ;;
99 *)
100         echo $usage >&2
101         exit 1 
102         ;;
103 esac
104
105 pkg_dir=$1
106
107 if [ ! -d $pkg_dir ]; then
108         echo "*** Error: Directory $pkg_dir does not exist" >&2
109         exit 1
110 fi
111
112 # CONTROL is second so that it takes precedence
113 CONTROL=
114 [ -d $pkg_dir/CONTROL ] && CONTROL=CONTROL
115 if [ -z "$CONTROL" ]; then
116         echo "*** Error: Directory $pkg_dir has no CONTROL subdirectory." >&2
117         exit 1
118 fi
119
120 if ! pkg_appears_sane $pkg_dir; then
121         echo >&2
122         echo "ipkg-build: Please fix the above errors and try again." >&2
123         exit 1
124 fi
125
126 tmp_dir=$dest_dir/IPKG_BUILD.$$
127 mkdir $tmp_dir
128
129 echo $CONTROL > $tmp_dir/tarX
130 # Preserve permissions (-p) when creating data.tar.gz as non-root user
131 ( cd $pkg_dir && $TAR $ogargs -X $tmp_dir/tarX --format=gnu -czpf $tmp_dir/data.tar.gz . )
132
133 installed_size=`stat -c "%s" $tmp_dir/data.tar.gz`
134 sed -i -e "s/^Installed-Size: .*/Installed-Size: $installed_size/" \
135         $pkg_dir/$CONTROL/control
136
137 ( cd $pkg_dir/$CONTROL && $TAR $ogargs --format=gnu -czf $tmp_dir/control.tar.gz . )
138 rm $tmp_dir/tarX
139
140 echo "2.0" > $tmp_dir/debian-binary
141
142 pkg_file=$dest_dir/${pkg}_${version}_${arch}.ipk
143 rm -f $pkg_file
144 ( cd $tmp_dir && $TAR --format=gnu -zcf $pkg_file ./debian-binary ./data.tar.gz ./control.tar.gz )
145
146 rm $tmp_dir/debian-binary $tmp_dir/data.tar.gz $tmp_dir/control.tar.gz
147 rmdir $tmp_dir
148
149 echo "Packaged contents of $pkg_dir into $pkg_file"