c9be18ec47f4f28d60cbf48f7319feaaec3e64f0
[openwrt/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="$(command -v find)"
14 FIND="${FIND:-$(command -v gfind)}"
15 TAR="${TAR:-$(command -v tar)}"
16 GZIP="$(command -v gzip)"
17
18 # try to use fixed source epoch
19 if [ -n "$PKG_SOURCE_DATE_EPOCH" ]; then
20 TIMESTAMP=$(date --date="@$PKG_SOURCE_DATE_EPOCH")
21 elif [ -n "$SOURCE_DATE_EPOCH" ]; then
22 TIMESTAMP=$(date --date="@$SOURCE_DATE_EPOCH")
23 else
24 TIMESTAMP=$(date)
25 fi
26
27 ipkg_extract_value() {
28 sed -e "s/^[^:]*:[[:space:]]*//"
29 }
30
31 required_field() {
32 field=$1
33
34 grep "^$field:" < $CONTROL/control | ipkg_extract_value
35 }
36
37 pkg_appears_sane() {
38 local pkg_dir=$1
39
40 local owd=$PWD
41 cd $pkg_dir
42
43 PKG_ERROR=0
44 pkg=`required_field Package`
45 version=`required_field Version | sed 's/Version://; s/^.://g;'`
46 arch=`required_field Architecture`
47
48 if echo $pkg | grep '[^a-zA-Z0-9_.+-]'; then
49 echo "*** Error: Package name $name contains illegal characters, (other than [a-z0-9.+-])" >&2
50 PKG_ERROR=1;
51 fi
52
53 if [ -f $CONTROL/conffiles ]; then
54 rm -f $CONTROL/conffiles.resolved
55
56 for cf in `$FIND $(sed -e "s!^/!$pkg_dir/!" $CONTROL/conffiles) -type f`; do
57 echo "${cf#$pkg_dir}" >> $CONTROL/conffiles.resolved
58 done
59
60 rm $CONTROL/conffiles
61 if [ -f $CONTROL/conffiles.resolved ]; then
62 mv $CONTROL/conffiles.resolved $CONTROL/conffiles
63 chmod 0644 $CONTROL/conffiles
64 fi
65 fi
66
67 cd $owd
68 return $PKG_ERROR
69 }
70
71 resolve_file_mode_id() {
72 type="$1"
73 name="$2"
74 position=1
75 if [ "$type" = "group" ]; then
76 position=2
77 fi
78
79 # root is always 0
80 if [ "$name" = "root" ]; then
81 echo 0
82 exit 0
83 fi
84
85 # return numeric names
86 if [ "$name" -eq "$name" 2>/dev/null ]; then
87 echo "$name"
88 exit 0
89 fi
90
91 ids=$(grep "$name" "$TOPDIR/tmp/userids")
92 for id in $ids; do
93 resolved_name=$(echo "$id" | cut -d ":" -f "$position" | cut -d "=" -f 1)
94 resolved_id=$(echo "$id" | cut -d ":" -f "$position" | cut -d "=" -f 2)
95 if [ "$resolved_name" = "$name" ]; then
96 echo "$resolved_id"
97 exit 0
98 fi
99 done
100
101 >&2 echo "No $type ID found for $name"
102 exit 1
103 }
104
105 ###
106 # ipkg-build "main"
107 ###
108 file_modes=""
109 usage="Usage: $0 [-v] [-h] [-m] <pkg_directory> [<destination_directory>]"
110 while getopts "hvm:" opt; do
111 case $opt in
112 v ) echo $version
113 exit 0
114 ;;
115 h ) echo $usage >&2 ;;
116 m ) file_modes=$OPTARG ;;
117 \? ) echo $usage >&2
118 esac
119 done
120
121
122 shift $(($OPTIND - 1))
123
124 # continue on to process additional arguments
125
126 case $# in
127 1)
128 dest_dir=$PWD
129 ;;
130 2)
131 dest_dir=$2
132 if [ "$dest_dir" = "." -o "$dest_dir" = "./" ] ; then
133 dest_dir=$PWD
134 fi
135 ;;
136 *)
137 echo $usage >&2
138 exit 1
139 ;;
140 esac
141
142 pkg_dir=$1
143
144 if [ ! -d $pkg_dir ]; then
145 echo "*** Error: Directory $pkg_dir does not exist" >&2
146 exit 1
147 fi
148
149 # CONTROL is second so that it takes precedence
150 CONTROL=
151 [ -d $pkg_dir/CONTROL ] && CONTROL=CONTROL
152 if [ -z "$CONTROL" ]; then
153 echo "*** Error: Directory $pkg_dir has no CONTROL subdirectory." >&2
154 exit 1
155 fi
156
157 if ! pkg_appears_sane $pkg_dir; then
158 echo >&2
159 echo "ipkg-build: Please fix the above errors and try again." >&2
160 exit 1
161 fi
162
163 tmp_dir=$dest_dir/IPKG_BUILD.$$
164 mkdir $tmp_dir
165
166 echo $CONTROL > $tmp_dir/tarX
167 cd $pkg_dir
168 for file_mode in $file_modes; do
169 case $file_mode in
170 /*:*:*:*)
171 ;;
172 *)
173 echo "ERROR: file modes must use absolute path and contain user:group:mode"
174 echo "$file_mode"
175 exit 1
176 ;;
177 esac
178 path=$(echo "$file_mode" | cut -d ':' -f 1)
179 user=$(echo "$file_mode" | cut -d ':' -f 2)
180 group=$(echo "$file_mode" | cut -d ':' -f 3)
181 mode=$(echo "$file_mode" | cut -d ':' -f 4)
182
183 uid=$(resolve_file_mode_id user "$user")
184 gid=$(resolve_file_mode_id group "$group")
185
186 chown "$uid:$gid" "$pkg_dir/$path"
187 chmod "$mode" "$pkg_dir/$path"
188 done
189 $TAR -X $tmp_dir/tarX --format=gnu --sort=name -cpf - --mtime="$TIMESTAMP" . | $GZIP -n - > $tmp_dir/data.tar.gz
190
191 installed_size=`stat -c "%s" $tmp_dir/data.tar.gz`
192 sed -i -e "s/^Installed-Size: .*/Installed-Size: $installed_size/" \
193 $pkg_dir/$CONTROL/control
194
195 ( cd $pkg_dir/$CONTROL && $TAR --format=gnu --sort=name -cf - --mtime="$TIMESTAMP" . | $GZIP -n - > $tmp_dir/control.tar.gz )
196 rm $tmp_dir/tarX
197
198 echo "2.0" > $tmp_dir/debian-binary
199
200 pkg_file=$dest_dir/${pkg}_${version}_${arch}.ipk
201 rm -f $pkg_file
202 ( cd $tmp_dir && $TAR --format=gnu --sort=name -cf - --mtime="$TIMESTAMP" ./debian-binary ./data.tar.gz ./control.tar.gz | $GZIP -n - > $pkg_file )
203
204 rm $tmp_dir/debian-binary $tmp_dir/data.tar.gz $tmp_dir/control.tar.gz
205 rmdir $tmp_dir
206
207 echo "Packaged contents of $pkg_dir into $pkg_file"