newapkbuild.in 6.14 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
program_version=@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 27 28 29 30 31 32
is_github_url() {
	case $1 in
	https://github.com/*/*/archive/*.tar.gz) return 0;;
	esac
	return 1
}

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

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

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

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

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

76 77 78 79
package_autotools() {
	package_make
}

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

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

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

101 102 103 104 105 106 107 108 109
	if is_github_url $source; then
		if [ -z "$pkgname" ]; then
			pkgname=${source%/archive/*}
			pkgname=${pkgname##*/}
		fi
		pv=${newname%.t*} #strip .tar.gz .tgz .tar.bz2 etc
		pv=${pv#*[a-z]}
		source="$pkgname-$pv.tar.gz::$source"
	elif [ "$pn" != "$newname" ]; then
110
		pv=${newname#$pn-}
111
		pv=${pv%.t*} #strip .tar.gz .tgz .tar.bz2 etc
112
	fi
113 114 115 116 117
	if [ -z "$pkgname" ]; then
		pkgname=$pn
	fi
	if [ -e "$pkgname"/APKBUILD ] && [ -z "$force" ]; then
		error "$pkgname/APKBUILD already exist"
118 119
		return 1
	fi
120 121
	mkdir -p "$pkgname"
	cd "$pkgname"
Natanael Copa's avatar
Natanael Copa committed
122

123 124 125 126
	if [ -z "$source" ] && [ -n "$sourceforge" ]; then
		source="http://downloads.sourceforge.net/$pn/$pn-$pv.tar.gz"
	fi

127
	if [ -z "$depends" ] &&[ "$buildtype" = "python" ]; then
128 129 130
		depends="python"
	fi

131
	if [ -z "$makedepends" ] &&[ "$buildtype" = "python" ]; then
132 133 134 135 136 137
		makedepends="python-dev"
	else
        makedepends="\$depends_dev"
	fi

	# Replace pkgver in $source
138
	if [ -n "$source" ]; then
139
		source=$(echo "$source" | sed "s/$pv/\$pkgver/g")
140
	fi
Natanael Copa's avatar
Natanael Copa committed
141

142
	# Copy init.d scripts if requested
143
	if [ -n "$cpinitd" ]; then
144 145 146 147
		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
148 149
		install="\$pkgname.pre-install \$pkgname.post-install"
		source="$source
150 151
	$pkgname.initd
	$pkgname.confd
Natanael Copa's avatar
Natanael Copa committed
152 153 154
	"
	fi

155
	# Generate header with standard variables
Natanael Copa's avatar
Natanael Copa committed
156 157 158
	cat >APKBUILD<<__EOF__
# Contributor:${PACKAGER:+" "}${PACKAGER}
# Maintainer:${MAINTAINER:+" "}${MAINTAINER}
159
pkgname=$pkgname
Natanael Copa's avatar
Natanael Copa committed
160 161 162 163 164 165
pkgver=$pv
pkgrel=0
pkgdesc="$pkgdesc"
url="$url"
arch="all"
license="$license"
166 167 168
depends="$depends"
depends_dev=""
makedepends="$makedepends"
Natanael Copa's avatar
Natanael Copa committed
169 170 171 172 173 174 175
install="$install"
subpackages="\$pkgname-dev \$pkgname-doc"
source="$source"

__EOF__

	abuild -f fetch unpack
176
	# Figure out the _builddir
Natanael Copa's avatar
Natanael Copa committed
177 178 179 180 181 182 183 184 185
	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

186
	# Check if its autotools
187 188 189
	if [ -z "$buildtype" ]; then
		if [ -x "$sdir"/configure ]; then
			buildtype="autotools"
190
		elif [ -r "$sdir"/Makefile.PL ] || [ "${pn#perl-}" != "$pn" ]; then
191 192 193 194 195 196 197
			buildtype="perl"
		elif [ -r "$sdir"/waf ]; then
			buildtype="waf"
		elif [ -d "$sdir"/cmake ]; then
			buildtype="cmake"
		elif [ -r "$sdir"/Makefile ]; then
			buildtype="make"
198 199
		elif [ -r "$sdir"/setup.py ]; then
			buildtype="python"
200 201 202 203
		fi
	fi


204
	# Create the prepare() template
Natanael Copa's avatar
Natanael Copa committed
205 206 207 208 209 210 211 212 213 214 215 216 217
	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__

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

	case "$buildtype" in
225 226
	make)
		build_make;;
227
	autotools)
228
		build_autotools;;
229
	perl)
230
		build_perl;;
231
	python)
232
		build_python;;
233 234
	esac

Natanael Copa's avatar
Natanael Copa committed
235 236 237 238 239
	cat >>APKBUILD<<__EOF__
}

__EOF__

240
	# Create package() function
Natanael Copa's avatar
Natanael Copa committed
241 242
	cat >>APKBUILD<<__EOF__
package() {
243
	cd "\$_builddir"
Natanael Copa's avatar
Natanael Copa committed
244
__EOF__
245 246

	case "$buildtype" in
247 248
	make)
		package_make;;
249 250 251 252 253 254
	autotools)
		package_autotools;;
	perl)
		package_perl;;
	python)
		package_python;;
255 256
	esac

257 258 259 260 261 262 263 264 265
	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
266 267 268 269
	cat >>APKBUILD<<__EOF__
}

__EOF__
270
	abuild -f checksum
271 272 273
}

usage() {
274
	cat >&2 <<__EOF__
275
$program $program_version - generate a new APKBUILD
276 277
Usage: $program [-cfh] [-d DESC] [-l LICENSE] [-n NAME] [-u URL]
		PKGNAME[-PKGVER]|SRCURL
278 279 280 281 282 283 284 285 286 287 288 289 290 291
Options:
  -n  Set package name to NAME
  -d  Set package description (pkgdesc) to DESC
  -l  Set package license to LICENSE
  -u  Set package URL
  -a  Create autotools (use ./configure ...)
  -p  Create perl package (Assume Makefile.PL is there)
  -y  Create python package (Assume setup.py is there)
  -s  Use sourceforge source URL
  -c  Copy a sample init.d, conf.d, and install script to new directory
  -f  Force even if directory already exist
  -h  Show this help

__EOF__
292 293
}

294
while getopts "acd:fhl:n:pyu:s" opt; do
295
	case $opt in
296
		'a') buildtype="autotools";;
297
		'c') cpinitd=1;;
Natanael Copa's avatar
Natanael Copa committed
298
		'd') pkgdesc="$OPTARG";;
299
		'f') force=1;;
300
		'h') usage; exit;;
Natanael Copa's avatar
Natanael Copa committed
301
		'l') license="$OPTARG";;
302
		'n') pkgname="$OPTARG";;
303
		'p') buildtype="perl";;
304
		'y') buildtype="python";;
Natanael Copa's avatar
Natanael Copa committed
305
		'u') url="$OPTARG";;
306
		's') sourceforge=1;;
307 308 309 310
	esac
done
shift $(( $OPTIND - 1 ))

311
while [ $# -gt 0 ]; do
312 313 314 315
	newaport $1 || exit 1
	shift
done