newapkbuild.in 5.89 KB
Newer Older
1
#!/bin/sh
2 3
#
# Script to generate a new APKBUILD
4 5 6 7 8 9 10 11 12 13 14 15 16
# Copyright (c) 2009 Natanael Copa <natanael.copa@gmail.com>
#
# Distributed under GPL-2
#
# Depends on: busybox utilities, fakeroot, 
#

version=@VERSION@
sysconfdir=@sysconfdir@
datadir=@datadir@

prog=${0##*/}

17
# Source $PACKAGER
Natanael Copa's avatar
Natanael Copa committed
18
for i in $sysconfdir/abuild.conf $HOME/.abuild/abuild.conf; do
19 20 21 22 23
	if [ -f "$i" ]; then
		. $i
	fi
done

24 25 26 27
error() {
	echo "$@" >&2
}

28 29 30 31
is_url() {
	case "$1" in
	http://*|ftp://*) return 0;;
	esac
32
	return 1
33 34
}

35
# Build sections
36 37 38 39 40 41
build_make() {
	cat >>APKBUILD<<__EOF__
	make || return 1
__EOF__
}

42
build_autotools() {
43 44 45 46 47 48 49
	cat >>APKBUILD<<__EOF__
	./configure --prefix=/usr \\
		--sysconfdir=/etc \\
		--mandir=/usr/share/man \\
		--infodir=/usr/share/info \\
		--localstatedir=/var \\
		|| return 1
50
	make || return 1
51 52 53
__EOF__
}

54
build_perl() {
55 56
	cat >>APKBUILD<<__EOF__
	PERL_MM_USE_DEFAULT=1 perl Makefile.PL INSTALLDIRS=vendor || return 1
57 58 59 60
	make || return 1
__EOF__
}

61
build_python() {
62 63
	cat >>APKBUILD<<__EOF__
	python setup.py build || return 1
64 65 66
__EOF__
}

67
# Package sections
68
package_make() {
69 70 71 72 73 74
	cat >>APKBUILD<<__EOF__
	make DESTDIR="\$pkgdir" install || return 1
	rm -f "\$pkgdir"/usr/lib/*.la
__EOF__
}

75 76 77 78
package_autotools() {
	package_make
}

79
package_perl() {
80
	cat >>APKBUILD<<__EOF__
81
	make DESTDIR="\$pkgdir" install || return 1
82 83 84 85
	find "\$pkgdir" \\( -name perllocal.pod -o -name .packlist \\) -delete
__EOF__
}

86 87 88 89 90 91 92
package_python() {
	cat >>APKBUILD<<__EOF__
	python setup.py install --prefix=/usr --root="\$pkgdir" || return 1
__EOF__
}

# Create new aport from templates
93
newaport() {
94
	local newname="${1##*/}"
95 96
	local pn=${newname%-[0-9]*}
	local pv
97 98 99
	local source=
	is_url "$1" && source="$1"

100 101
	if [ "$pn" != "$newname" ]; then
		pv=${newname#$pn-}
102
		pv=${pv%.t*} #strip .tar.gz .tgz .tar.bz2 etc
103
	fi
104 105 106 107 108
	if [ -z "$pkgname" ]; then
		pkgname=$pn
	fi
	if [ -e "$pkgname"/APKBUILD ] && [ -z "$force" ]; then
		error "$pkgname/APKBUILD already exist"
109 110
		return 1
	fi
111 112
	mkdir -p "$pkgname"
	cd "$pkgname"
Natanael Copa's avatar
Natanael Copa committed
113

114 115 116 117
	if [ -z "$source" ] && [ -n "$sourceforge" ]; then
		source="http://downloads.sourceforge.net/$pn/$pn-$pv.tar.gz"
	fi

118
	if [ -z "$depends" ] &&[ "$buildtype" = "python" ]; then
119 120 121
		depends="python"
	fi

122
	if [ -z "$makedepends" ] &&[ "$buildtype" = "python" ]; then
123 124 125 126 127 128
		makedepends="python-dev"
	else
        makedepends="\$depends_dev"
	fi

	# Replace pkgver in $source
129
	if [ -n "$source" ]; then
130
		source=$(echo "$source" | sed "s/$pv/\$pkgver/g")
131
	fi
Natanael Copa's avatar
Natanael Copa committed
132

133
	# Copy init.d scripts if requested
134
	if [ -n "$cpinitd" ]; then
135 136 137 138
		cp "$datadir"/sample.initd $pkgname.initd
		cp "$datadir"/sample.confd $pkgname.confd
		cp "$datadir"/sample.pre-install $pkgname.pre-install
		cp "$datadir"/sample.post-install $pkgname.post-install
Natanael Copa's avatar
Natanael Copa committed
139 140
		install="\$pkgname.pre-install \$pkgname.post-install"
		source="$source
141 142
	$pkgname.initd
	$pkgname.confd
Natanael Copa's avatar
Natanael Copa committed
143 144 145
	"
	fi

146
	# Generate header with standard variables
Natanael Copa's avatar
Natanael Copa committed
147 148 149
	cat >APKBUILD<<__EOF__
# Contributor:${PACKAGER:+" "}${PACKAGER}
# Maintainer:${MAINTAINER:+" "}${MAINTAINER}
150
pkgname=$pkgname
Natanael Copa's avatar
Natanael Copa committed
151 152 153 154 155 156
pkgver=$pv
pkgrel=0
pkgdesc="$pkgdesc"
url="$url"
arch="all"
license="$license"
157 158 159
depends="$depends"
depends_dev=""
makedepends="$makedepends"
Natanael Copa's avatar
Natanael Copa committed
160 161 162 163 164 165 166
install="$install"
subpackages="\$pkgname-dev \$pkgname-doc"
source="$source"

__EOF__

	abuild -f fetch unpack
167
	# Figure out the _builddir
Natanael Copa's avatar
Natanael Copa committed
168 169 170 171 172 173 174 175 176
	for i in src/*; do
		if [ -d "$i" ]; then
			sdir=$i
			_builddir=$(echo ${i#*/} | sed "s/$pv/\$pkgver/g")
			_builddir="\"\$srcdir\"/$_builddir"
		fi
	done
	echo "_builddir=$_builddir" >> APKBUILD

177
	# Check if its autotools
178 179 180
	if [ -z "$buildtype" ]; then
		if [ -x "$sdir"/configure ]; then
			buildtype="autotools"
181
		elif [ -r "$sdir"/Makefile.PL ] || [ "${pn#perl-}" != "$pn" ]; then
182 183 184 185 186 187 188
			buildtype="perl"
		elif [ -r "$sdir"/waf ]; then
			buildtype="waf"
		elif [ -d "$sdir"/cmake ]; then
			buildtype="cmake"
		elif [ -r "$sdir"/Makefile ]; then
			buildtype="make"
189 190
		elif [ -r "$sdir"/setup.py ]; then
			buildtype="python"
191 192 193 194
		fi
	fi


195
	# Create the prepare() template
Natanael Copa's avatar
Natanael Copa committed
196 197 198 199 200 201 202 203 204 205 206 207 208
	cat >>APKBUILD<<__EOF__
prepare() {
	local i
	cd "\$_builddir"
	for i in \$source; do
		case \$i in
		*.patch) msg \$i; patch -p1 -i "\$srcdir"/\$i || return 1;;
		esac
	done
}

__EOF__

209
	# Create build() function
Natanael Copa's avatar
Natanael Copa committed
210 211 212 213
	cat >>APKBUILD<<__EOF__
build() {
	cd "\$_builddir"
__EOF__
214 215

	case "$buildtype" in
216 217
	make)
		build_make;;
218
	autotools)
219
		build_autotools;;
220
	perl)
221
		build_perl;;
222
	python)
223
		build_python;;
224 225
	esac

Natanael Copa's avatar
Natanael Copa committed
226 227 228 229 230
	cat >>APKBUILD<<__EOF__
}

__EOF__

231
	# Create package() function
Natanael Copa's avatar
Natanael Copa committed
232 233
	cat >>APKBUILD<<__EOF__
package() {
234
	cd "\$_builddir"
Natanael Copa's avatar
Natanael Copa committed
235
__EOF__
236 237

	case "$buildtype" in
238 239
	make)
		package_make;;
240 241 242 243 244 245
	autotools)
		package_autotools;;
	perl)
		package_perl;;
	python)
		package_python;;
246 247
	esac

248 249 250 251 252 253 254 255 256
	if [ -n "$cpinitd" ]; then
		cat >>APKBUILD<<__EOF__

	install -m755 -D "\$srcdir"/\$pkgname.initd \\
		"\$pkgdir"/etc/init.d/\$pkgname || return 1
	install -m644 -D "\$srcdir"/\$pkgname.confd \\
		"\$pkgdir"/etc/conf.d/\$pkgname || return 1
__EOF__
	fi
Natanael Copa's avatar
Natanael Copa committed
257 258 259 260
	cat >>APKBUILD<<__EOF__
}

__EOF__
261
	abuild -f checksum
262 263 264 265
}

usage() {
	echo "$prog $version"
266
	echo "usage: $prog [-cfh] [-d DESC] [-l LICENSE] [-n NAME] [-u URL] PKGNAME[-PKGVER]|SRCURL"
267
	echo "Options:"
268
	echo " -a  Create autotools (use ./configure ...)"
269
	echo " -c  Copy a sample init.d, conf.d, and install script to new directory"
Natanael Copa's avatar
Natanael Copa committed
270
	echo " -d  Set package description (pkgdesc) to DESC"
271 272
	echo " -f  Force even if directory already exist"
	echo " -h  Show this help"
Natanael Copa's avatar
Natanael Copa committed
273
	echo " -l  Set package license to LICENSE"
274
	echo " -n  Set package name to NAME"
275
	echo " -p  Create perl package (Assume Makefile.PL is there)"
276
	echo " -y  Create python package (Assume setup.py is there)"
Natanael Copa's avatar
Natanael Copa committed
277
	echo " -u  Set package URL"
278
	echo " -s  Use sourceforge source URL"
279 280 281 282
	echo ""
	exit 0
}

283
while getopts "acd:fhl:n:pyu:s" opt; do
284
	case $opt in
285
		'a') buildtype="autotools";;
286
		'c') cpinitd=1;;
Natanael Copa's avatar
Natanael Copa committed
287
		'd') pkgdesc="$OPTARG";;
288 289
		'f') force=1;;
		'h') usage;;
Natanael Copa's avatar
Natanael Copa committed
290
		'l') license="$OPTARG";;
291
		'n') pkgname="$OPTARG";;
292
		'p') buildtype="perl";;
293
		'y') buildtype="python";;
Natanael Copa's avatar
Natanael Copa committed
294
		'u') url="$OPTARG";;
295
		's') sourceforge=1;;
296 297 298 299 300 301 302 303 304
	esac
done
shift $(( $OPTIND - 1 ))

while [ $# -gt 0 ]; do 
	newaport $1 || exit 1
	shift
done