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