newapkbuild.in 5.86 KB
Newer Older
1
#!/bin/sh
2
#
3
# newapkbuild - generate a new APKBUILD
4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2009 Natanael Copa <natanael.copa@gmail.com>
#
# Distributed under GPL-2
#

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

prog=${0##*/}

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

22 23 24 25
error() {
	echo "$@" >&2
}

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

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

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

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

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

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

73 74 75 76
package_autotools() {
	package_make
}

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

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

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

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

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

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

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

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

131
	# Copy init.d scripts if requested
132
	if [ -n "$cpinitd" ]; then
133 134 135 136
		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
137 138
		install="\$pkgname.pre-install \$pkgname.post-install"
		source="$source
139 140
	$pkgname.initd
	$pkgname.confd
Natanael Copa's avatar
Natanael Copa committed
141 142 143
	"
	fi

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

__EOF__

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

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


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

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

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

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

__EOF__

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

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

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

__EOF__
259
	abuild -f checksum
260 261 262 263
}

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

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

298
while [ $# -gt 0 ]; do
299 300 301 302
	newaport $1 || exit 1
	shift
done