newapkbuild.in 5.69 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 104 105 106 107 108 109
	fi
	if [ -e "$pn"/APKBUILD ] && [ -z "$force" ]; then
		error "$pn/APKBUILD already exist"
		return 1
	fi
	mkdir -p "$pn"
	cd "$pn"
Natanael Copa's avatar
Natanael Copa committed
110

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

115 116 117 118 119 120 121 122 123 124 125
	if [ -z "$depends" ] &&[ "$buildtype" == "python" ]; then
		depends="python"
	fi

	if [ -z "$makedepends" ] &&[ "$buildtype" == "python" ]; then
		makedepends="python-dev"
	else
        makedepends="\$depends_dev"
	fi

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

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

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

__EOF__

	abuild -f fetch unpack
164
	# Figure out the _builddir
Natanael Copa's avatar
Natanael Copa committed
165 166 167 168 169 170 171 172 173
	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

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


192
	# Create the prepare() template
Natanael Copa's avatar
Natanael Copa committed
193 194 195 196 197 198 199 200 201 202 203 204 205
	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__

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

	case "$buildtype" in
213 214
	make)
		build_make;;
215
	autotools)
216
		build_autotools;;
217
	perl)
218
		build_perl;;
219
	python)
220
		build_python;;
221 222
	esac

Natanael Copa's avatar
Natanael Copa committed
223 224 225 226 227
	cat >>APKBUILD<<__EOF__
}

__EOF__

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

	case "$buildtype" in
235 236
	make)
		package_make;;
237 238 239 240 241 242
	autotools)
		package_autotools;;
	perl)
		package_perl;;
	python)
		package_python;;
243 244
	esac

245 246 247 248 249 250 251 252 253
	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
254 255 256 257
	cat >>APKBUILD<<__EOF__
}

__EOF__
258 259 260 261
}

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

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

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