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