mt76: update to the latest version
[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="$(which find)"
14 FIND="${FIND:-$(which gfind)}"
15 TAR="${TAR:-$(which tar)}"
16 GZIP="$(which gzip)"
17
18 # look up date of last commit
19 if [ -d "$TOPDIR/.git" ]; then
20 GIT="$(which git)"
21 TIMESTAMP=$(cd $TOPDIR; $GIT log -1 -s --format=%ci)
22 elif [ -d "$TOPDIR/.svn" ]; then
23 SVN="$(which svn)"
24 TIMESTAMP=$($SVN info "$TOPDIR" | sed -n "s/^Last Changed Date: \(.*\)/\1/p")
25 else
26 TIMESTAMP=$(date)
27 fi
28
29 ipkg_extract_value() {
30 sed -e "s/^[^:]*:[[:space:]]*//"
31 }
32
33 required_field() {
34 field=$1
35
36 grep "^$field:" < $CONTROL/control | ipkg_extract_value
37 }
38
39 pkg_appears_sane() {
40 local pkg_dir=$1
41
42 local owd=$PWD
43 cd $pkg_dir
44
45 PKG_ERROR=0
46 pkg=`required_field Package`
47 version=`required_field Version | sed 's/Version://; s/^.://g;'`
48 arch=`required_field Architecture`
49
50 if echo $pkg | grep '[^a-zA-Z0-9_.+-]'; then
51 echo "*** Error: Package name $name contains illegal characters, (other than [a-z0-9.+-])" >&2
52 PKG_ERROR=1;
53 fi
54
55 if [ -f $CONTROL/conffiles ]; then
56 rm -f $CONTROL/conffiles.resolved
57
58 for cf in `$FIND $(sed -e "s!^/!$pkg_dir/!" $CONTROL/conffiles) -type f`; do
59 echo "${cf#$pkg_dir}" >> $CONTROL/conffiles.resolved
60 done
61
62 rm $CONTROL/conffiles
63 if [ -f $CONTROL/conffiles.resolved ]; then
64 mv $CONTROL/conffiles.resolved $CONTROL/conffiles
65 chmod 0644 $CONTROL/conffiles
66 fi
67 fi
68
69 cd $owd
70 return $PKG_ERROR
71 }
72
73 ###
74 # ipkg-build "main"
75 ###
76 ogargs=""
77 noclean=0
78 usage="Usage: $0 [-c] [-C] [-o owner] [-g group] <pkg_directory> [<destination_directory>]"
79 while getopts "cg:ho:v" opt; do
80 case $opt in
81 o ) owner=$OPTARG
82 ogargs="--owner=$owner"
83 ;;
84 g ) group=$OPTARG
85 ogargs="$ogargs --group=$group"
86 ;;
87 c ) ;;
88 C ) noclean=1;;
89 v ) echo $version
90 exit 0
91 ;;
92 h ) echo $usage >&2 ;;
93 \? ) echo $usage >&2
94 esac
95 done
96
97
98 shift $(($OPTIND - 1))
99
100 # continue on to process additional arguments
101
102 case $# in
103 1)
104 dest_dir=$PWD
105 ;;
106 2)
107 dest_dir=$2
108 if [ "$dest_dir" = "." -o "$dest_dir" = "./" ] ; then
109 dest_dir=$PWD
110 fi
111 ;;
112 *)
113 echo $usage >&2
114 exit 1
115 ;;
116 esac
117
118 pkg_dir=$1
119
120 if [ ! -d $pkg_dir ]; then
121 echo "*** Error: Directory $pkg_dir does not exist" >&2
122 exit 1
123 fi
124
125 # CONTROL is second so that it takes precedence
126 CONTROL=
127 [ -d $pkg_dir/CONTROL ] && CONTROL=CONTROL
128 if [ -z "$CONTROL" ]; then
129 echo "*** Error: Directory $pkg_dir has no CONTROL subdirectory." >&2
130 exit 1
131 fi
132
133 if ! pkg_appears_sane $pkg_dir; then
134 echo >&2
135 echo "ipkg-build: Please fix the above errors and try again." >&2
136 exit 1
137 fi
138
139 tmp_dir=$dest_dir/IPKG_BUILD.$$
140 mkdir $tmp_dir
141
142 echo $CONTROL > $tmp_dir/tarX
143 # Preserve permissions (-p) when creating data.tar.gz as non-root user
144 ( cd $pkg_dir && $TAR $ogargs -X $tmp_dir/tarX --format=gnu --sort=name -cpf - --mtime="$TIMESTAMP" . | $GZIP -n - > $tmp_dir/data.tar.gz )
145
146 installed_size=`stat -c "%s" $tmp_dir/data.tar.gz`
147 sed -i -e "s/^Installed-Size: .*/Installed-Size: $installed_size/" \
148 $pkg_dir/$CONTROL/control
149
150 ( cd $pkg_dir/$CONTROL && $TAR $ogargs --format=gnu --sort=name -cf - --mtime="$TIMESTAMP" . | $GZIP -n - > $tmp_dir/control.tar.gz )
151 rm $tmp_dir/tarX
152
153 echo "2.0" > $tmp_dir/debian-binary
154
155 pkg_file=$dest_dir/${pkg}_${version}_${arch}.ipk
156 rm -f $pkg_file
157 ( cd $tmp_dir && $TAR $ogargs --format=gnu --sort=name -cf - --mtime="$TIMESTAMP" ./debian-binary ./data.tar.gz ./control.tar.gz | $GZIP -n - > $pkg_file )
158
159 rm $tmp_dir/debian-binary $tmp_dir/data.tar.gz $tmp_dir/control.tar.gz
160 rmdir $tmp_dir
161
162 echo "Packaged contents of $pkg_dir into $pkg_file"