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

9
abuild_ver=@VERSION@
10 11
datadir=@datadir@

12 13 14 15 16
if ! [ -f "$datadir/functions.sh" ]; then
	echo "$datadir/functions.sh: not found" >&2
	exit 1
fi
. "$datadir/functions.sh"
17 18


19 20
is_url() {
	case "$1" in
21
	http://*|https://*|ftp://*) return 0;;
22
	esac
23
	return 1
24 25
}

26
# Build sections
27 28 29 30 31 32
build_make() {
	cat >>APKBUILD<<__EOF__
	make || return 1
__EOF__
}

33
build_autotools() {
34 35 36 37 38 39 40
	cat >>APKBUILD<<__EOF__
	./configure --prefix=/usr \\
		--sysconfdir=/etc \\
		--mandir=/usr/share/man \\
		--infodir=/usr/share/info \\
		--localstatedir=/var \\
		|| return 1
41
	make || return 1
42 43 44
__EOF__
}

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

52
build_python() {
53 54
	cat >>APKBUILD<<__EOF__
	python setup.py build || return 1
55 56 57
__EOF__
}

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

66 67 68 69
package_autotools() {
	package_make
}

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

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

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

91 92
	if [ "$pn" != "$newname" ]; then
		pv=${newname#$pn-}
93
		pv=${pv%.t*} #strip .tar.gz .tgz .tar.bz2 etc
94
	fi
95 96 97 98 99
	if [ -z "$pkgname" ]; then
		pkgname=$pn
	fi
	if [ -e "$pkgname"/APKBUILD ] && [ -z "$force" ]; then
		error "$pkgname/APKBUILD already exist"
100 101
		return 1
	fi
102 103
	mkdir -p "$pkgname"
	cd "$pkgname"
Natanael Copa's avatar
Natanael Copa committed
104

105 106 107 108
	if [ -z "$source" ] && [ -n "$sourceforge" ]; then
		source="http://downloads.sourceforge.net/$pn/$pn-$pv.tar.gz"
	fi

109
	if [ -z "$depends" ] &&[ "$buildtype" = "python" ]; then
110 111 112
		depends="python"
	fi

113
	if [ -z "$makedepends" ] &&[ "$buildtype" = "python" ]; then
114 115 116 117 118 119
		makedepends="python-dev"
	else
        makedepends="\$depends_dev"
	fi

	# Replace pkgver in $source
120
	if [ -n "$source" ]; then
121
		source=$(echo "$source" | sed "s/$pv/\$pkgver/g")
122
	fi
Natanael Copa's avatar
Natanael Copa committed
123

124
	# Copy init.d scripts if requested
125
	if [ -n "$cpinitd" ]; then
126 127 128 129
		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
130 131
		install="\$pkgname.pre-install \$pkgname.post-install"
		source="$source
132 133
	$pkgname.initd
	$pkgname.confd
Natanael Copa's avatar
Natanael Copa committed
134 135 136
	"
	fi

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

__EOF__

	abuild -f fetch unpack
158
	# Figure out the _builddir
Natanael Copa's avatar
Natanael Copa committed
159 160 161 162 163 164 165 166 167
	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

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


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

200
	# Create build() function
Natanael Copa's avatar
Natanael Copa committed
201 202 203 204
	cat >>APKBUILD<<__EOF__
build() {
	cd "\$_builddir"
__EOF__
205 206

	case "$buildtype" in
207 208
	make)
		build_make;;
209
	autotools)
210
		build_autotools;;
211
	perl)
212
		build_perl;;
213
	python)
214
		build_python;;
215 216
	esac

Natanael Copa's avatar
Natanael Copa committed
217 218 219 220 221
	cat >>APKBUILD<<__EOF__
}

__EOF__

222
	# Create package() function
Natanael Copa's avatar
Natanael Copa committed
223 224
	cat >>APKBUILD<<__EOF__
package() {
225
	cd "\$_builddir"
Natanael Copa's avatar
Natanael Copa committed
226
__EOF__
227 228

	case "$buildtype" in
229 230
	make)
		package_make;;
231 232 233 234 235 236
	autotools)
		package_autotools;;
	perl)
		package_perl;;
	python)
		package_python;;
237 238
	esac

239 240 241 242 243 244 245 246 247
	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
248 249 250 251
	cat >>APKBUILD<<__EOF__
}

__EOF__
252
	abuild -f checksum
253 254 255
}

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

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

291
while [ $# -gt 0 ]; do
292 293 294 295
	newaport $1 || exit 1
	shift
done