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