d38ba20af30cee4b0843c64b40f5f101e218d411
[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 # 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 mv $CONTROL/conffiles.resolved $CONTROL/conffiles
68 chmod 0644 $CONTROL/conffiles
69 fi
70
71 cd $owd
72 return $PKG_ERROR
73 }
74
75 ###
76 # ipkg-build "main"
77 ###
78 ogargs=""
79 noclean=0
80 usage="Usage: $0 [-c] [-C] [-o owner] [-g group] <pkg_directory> [<destination_directory>]"
81 while getopts "cg:ho:v" opt; do
82 case $opt in
83 o ) owner=$OPTARG
84 ogargs="--owner=$owner"
85 ;;
86 g ) group=$OPTARG
87 ogargs="$ogargs --group=$group"
88 ;;
89 c ) ;;
90 C ) noclean=1;;
91 v ) echo $version
92 exit 0
93 ;;
94 h ) echo $usage >&2 ;;
95 \? ) echo $usage >&2
96 esac
97 done
98
99
100 shift $(($OPTIND - 1))
101
102 # continue on to process additional arguments
103
104 case $# in
105 1)
106 dest_dir=$PWD
107 ;;
108 2)
109 dest_dir=$2
110 if [ "$dest_dir" = "." -o "$dest_dir" = "./" ] ; then
111 dest_dir=$PWD
112 fi
113 ;;
114 *)
115 echo $usage >&2
116 exit 1
117 ;;
118 esac
119
120 pkg_dir=$1
121
122 if [ ! -d $pkg_dir ]; then
123 echo "*** Error: Directory $pkg_dir does not exist" >&2
124 exit 1
125 fi
126
127 # CONTROL is second so that it takes precedence
128 CONTROL=
129 [ -d $pkg_dir/CONTROL ] && CONTROL=CONTROL
130 if [ -z "$CONTROL" ]; then
131 echo "*** Error: Directory $pkg_dir has no CONTROL subdirectory." >&2
132 exit 1
133 fi
134
135 if ! pkg_appears_sane $pkg_dir; then
136 echo >&2
137 echo "ipkg-build: Please fix the above errors and try again." >&2
138 exit 1
139 fi
140
141 tmp_dir=$dest_dir/IPKG_BUILD.$$
142 mkdir $tmp_dir
143
144 echo $CONTROL > $tmp_dir/tarX
145 # Preserve permissions (-p) when creating data.tar.gz as non-root user
146 ( cd $pkg_dir && $TAR $ogargs -X $tmp_dir/tarX --format=gnu --sort=name -cpf - --mtime="$TIMESTAMP" . | $GZIP -n - > $tmp_dir/data.tar.gz )
147
148 installed_size=`stat -c "%s" $tmp_dir/data.tar.gz`
149 sed -i -e "s/^Installed-Size: .*/Installed-Size: $installed_size/" \
150 $pkg_dir/$CONTROL/control
151
152 ( cd $pkg_dir/$CONTROL && $TAR $ogargs --format=gnu --sort=name -cf - --mtime="$TIMESTAMP" . | $GZIP -n - > $tmp_dir/control.tar.gz )
153 rm $tmp_dir/tarX
154
155 echo "2.0" > $tmp_dir/debian-binary
156
157 pkg_file=$dest_dir/${pkg}_${version}_${arch}.ipk
158 rm -f $pkg_file
159 ( cd $tmp_dir && $TAR $ogargs --format=gnu --sort=name -cf - --mtime="$TIMESTAMP" ./debian-binary ./data.tar.gz ./control.tar.gz | $GZIP -n - > $pkg_file )
160
161 rm $tmp_dir/debian-binary $tmp_dir/data.tar.gz $tmp_dir/control.tar.gz
162 rmdir $tmp_dir
163
164 echo "Packaged contents of $pkg_dir into $pkg_file"