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