newapkbuild.in 5.55 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
build_autotools() {
37 38 39 40 41 42 43
	cat >>APKBUILD<<__EOF__
	./configure --prefix=/usr \\
		--sysconfdir=/etc \\
		--mandir=/usr/share/man \\
		--infodir=/usr/share/info \\
		--localstatedir=/var \\
		|| return 1
44
	make || return 1
45 46 47
__EOF__
}

48
build_perl() {
49 50
	cat >>APKBUILD<<__EOF__
	PERL_MM_USE_DEFAULT=1 perl Makefile.PL INSTALLDIRS=vendor || return 1
51 52 53 54
	make || return 1
__EOF__
}

55
build_python() {
56 57
	cat >>APKBUILD<<__EOF__
	python setup.py build || return 1
58 59 60
__EOF__
}

61 62 63 64 65 66 67 68 69
# Package sections
package_autotools() {
	cat >>APKBUILD<<__EOF__
	make DESTDIR="\$pkgdir" install || return 1
	rm -f "\$pkgdir"/usr/lib/*.la
__EOF__
}

package_perl() {
70
	cat >>APKBUILD<<__EOF__
71
	make DESTDIR="\$pkgdir" install || return 1
72 73 74 75
	find "\$pkgdir" \\( -name perllocal.pod -o -name .packlist \\) -delete
__EOF__
}

76 77 78 79 80 81 82
package_python() {
	cat >>APKBUILD<<__EOF__
	python setup.py install --prefix=/usr --root="\$pkgdir" || return 1
__EOF__
}

# Create new aport from templates
83
newaport() {
84
	local newname="${1##*/}"
85 86
	local pn=${newname%-[0-9]*}
	local pv
87 88 89
	local source=
	is_url "$1" && source="$1"

90 91
	if [ "$pn" != "$newname" ]; then
		pv=${newname#$pn-}
92
		pv=${pv%.t*} #strip .tar.gz .tgz .tar.bz2 etc
93 94 95 96 97 98 99
	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
100

101 102 103 104
	if [ -z "$source" ] && [ -n "$sourceforge" ]; then
		source="http://downloads.sourceforge.net/$pn/$pn-$pv.tar.gz"
	fi

105 106 107 108 109 110 111 112 113 114 115
	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
116
	if [ -n "$source" ]; then
117
		source=$(echo "$source" | sed "s/$pv/\$pkgver/g")
118
	fi
Natanael Copa's avatar
Natanael Copa committed
119

120
	# Copy init.d scripts if requested
121 122 123 124 125
	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
126 127 128 129 130 131 132
		install="\$pkgname.pre-install \$pkgname.post-install"
		source="$source
	$pn.initd
	$pn.confd
	"
	fi

133
	# Generate header with standard variables
Natanael Copa's avatar
Natanael Copa committed
134 135 136 137 138 139 140 141 142 143
	cat >APKBUILD<<__EOF__
# Contributor:${PACKAGER:+" "}${PACKAGER}
# Maintainer:${MAINTAINER:+" "}${MAINTAINER}
pkgname=$pn
pkgver=$pv
pkgrel=0
pkgdesc="$pkgdesc"
url="$url"
arch="all"
license="$license"
144 145 146
depends="$depends"
depends_dev=""
makedepends="$makedepends"
Natanael Copa's avatar
Natanael Copa committed
147 148 149 150 151 152 153
install="$install"
subpackages="\$pkgname-dev \$pkgname-doc"
source="$source"

__EOF__

	abuild -f fetch unpack
154
	# Figure out the _builddir
Natanael Copa's avatar
Natanael Copa committed
155 156 157 158 159 160 161 162 163
	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

164
	# Check if its autotools
165 166 167
	if [ -z "$buildtype" ]; then
		if [ -x "$sdir"/configure ]; then
			buildtype="autotools"
168
		elif [ -r "$sdir"/Makefile.PL ] || [ "${pn#perl-}" != "$pn" ]; then
169 170 171 172 173 174 175
			buildtype="perl"
		elif [ -r "$sdir"/waf ]; then
			buildtype="waf"
		elif [ -d "$sdir"/cmake ]; then
			buildtype="cmake"
		elif [ -r "$sdir"/Makefile ]; then
			buildtype="make"
176 177
		elif [ -r "$sdir"/setup.py ]; then
			buildtype="python"
178 179 180 181
		fi
	fi


182
	# Create the prepare() template
Natanael Copa's avatar
Natanael Copa committed
183 184 185 186 187 188 189 190 191 192 193 194 195
	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__

196
	# Create build() function
Natanael Copa's avatar
Natanael Copa committed
197 198 199 200
	cat >>APKBUILD<<__EOF__
build() {
	cd "\$_builddir"
__EOF__
201 202 203

	case "$buildtype" in
	autotools)
204
		build_autotools;;
205
	perl)
206
		build_perl;;
207
	python)
208
		build_python;;
209 210
	esac

Natanael Copa's avatar
Natanael Copa committed
211 212 213 214 215
	cat >>APKBUILD<<__EOF__
}

__EOF__

216
	# Create package() function
Natanael Copa's avatar
Natanael Copa committed
217 218
	cat >>APKBUILD<<__EOF__
package() {
219
	cd "\$_builddir"
Natanael Copa's avatar
Natanael Copa committed
220
__EOF__
221 222

	case "$buildtype" in
223 224 225 226 227 228
	autotools)
		package_autotools;;
	perl)
		package_perl;;
	python)
		package_python;;
229 230
	esac

231 232 233 234 235 236 237 238 239
	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
240 241 242 243
	cat >>APKBUILD<<__EOF__
}

__EOF__
244 245 246 247
}

usage() {
	echo "$prog $version"
Natanael Copa's avatar
Natanael Copa committed
248
	echo "usage: $prog [-cfh] [-d DESC] [-l LICENSE] [-u URL] PKGNAME[-PKGVER]"
249
	echo "Options:"
250
	echo " -a  Create autotools (use ./configure ...)"
251
	echo " -c  Copy a sample init.d, conf.d, and install script to new directory"
Natanael Copa's avatar
Natanael Copa committed
252
	echo " -d  Set package description (pkgdesc) to DESC"
253 254
	echo " -f  Force even if directory already exist"
	echo " -h  Show this help"
Natanael Copa's avatar
Natanael Copa committed
255
	echo " -l  Set package license to LICENSE"
256
	echo " -p  Create perl package (Assume Makefile.PL is there)"
257
	echo " -q  Create python package (Assume setup.py is there)"
Natanael Copa's avatar
Natanael Copa committed
258
	echo " -u  Set package URL"
259
	echo " -s  Use sourceforge source URL"
260 261 262 263
	echo ""
	exit 0
}

264
while getopts "acd:fhl:pqu:s" opt; do
265
	case $opt in
266
		'a') buildtype="autotools";;
267
		'c') cpinitd=1;;
Natanael Copa's avatar
Natanael Copa committed
268
		'd') pkgdesc="$OPTARG";;
269 270
		'f') force=1;;
		'h') usage;;
Natanael Copa's avatar
Natanael Copa committed
271
		'l') license="$OPTARG";;
272
		'p') buildtype="perl";;
273
		'q') buildtype="python";;
Natanael Copa's avatar
Natanael Copa committed
274
		'u') url="$OPTARG";;
275
		's') sourceforge=1;;
276 277 278 279 280 281 282 283 284
	esac
done
shift $(( $OPTIND - 1 ))

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