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
	cat >>APKBUILD<<__EOF__
35 36 37 38
	./configure \\
		--build=\$CBUILD \\
		--host=\$CHOST \\
		--prefix=/usr \\
39 40 41 42 43
		--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
# Package sections
62
package_make() {
63 64 65 66 67 68
	cat >>APKBUILD<<__EOF__
	make DESTDIR="\$pkgdir" install || return 1
	rm -f "\$pkgdir"/usr/lib/*.la
__EOF__
}

69 70 71 72
package_autotools() {
	package_make
}

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

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

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

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

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

112
	if [ -z "$depends" ] &&[ "$buildtype" = "python" ]; then
113 114 115
		depends="python"
	fi

116
	if [ -z "$makedepends" ] &&[ "$buildtype" = "python" ]; then
117 118 119 120 121 122
		makedepends="python-dev"
	else
        makedepends="\$depends_dev"
	fi

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

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

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

__EOF__

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

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


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

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

	case "$buildtype" in
210 211
	make)
		build_make;;
212
	autotools)
213
		build_autotools;;
214
	perl)
215
		build_perl;;
216
	python)
217
		build_python;;
218 219
	esac

Natanael Copa's avatar
Natanael Copa committed
220 221 222 223 224
	cat >>APKBUILD<<__EOF__
}

__EOF__

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

	case "$buildtype" in
232 233
	make)
		package_make;;
234 235 236 237 238 239
	autotools)
		package_autotools;;
	perl)
		package_perl;;
	python)
		package_python;;
240 241
	esac

242 243 244 245 246 247 248 249 250
	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
251 252 253 254
	cat >>APKBUILD<<__EOF__
}

__EOF__
255
	abuild -f checksum
256 257 258
}

usage() {
259
	cat >&2 <<__EOF__
260 261
$program $abuild_ver - generate a new APKBUILD
Usage: $program [-cfh] [-d DESC] [-l LICENSE] [-n NAME] [-u URL] PKGNAME[-PKGVER]|SRCURL
262 263 264 265 266 267 268 269 270 271 272 273 274 275
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__
276 277
}

278
while getopts "acd:fhl:n: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
		'f') force=1;;
284
		'h') usage; exit;;
Natanael Copa's avatar
Natanael Copa committed
285
		'l') license="$OPTARG";;
286
		'n') pkgname="$OPTARG";;
287
		'p') buildtype="perl";;
288
		'y') buildtype="python";;
Natanael Copa's avatar
Natanael Copa committed
289
		'u') url="$OPTARG";;
290
		's') sourceforge=1;;
291 292 293 294
	esac
done
shift $(( $OPTIND - 1 ))

295
while [ $# -gt 0 ]; do
296 297 298 299
	newaport $1 || exit 1
	shift
done