newapkbuild.in 4.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#!/bin/sh

# script to generate a new APKBUILD
# 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
config_autotools() {
	cat >>APKBUILD<<__EOF__
	./configure --prefix=/usr \\
		--sysconfdir=/etc \\
		--mandir=/usr/share/man \\
		--infodir=/usr/share/info \\
		--localstatedir=/var \\
		|| return 1
__EOF__
}

config_perl() {
	cat >>APKBUILD<<__EOF__
	PERL_MM_USE_DEFAULT=1 perl Makefile.PL INSTALLDIRS=vendor || return 1
__EOF__
}

cleanup_perl() {
	cat >>APKBUILD<<__EOF__
	find "\$pkgdir" \\( -name perllocal.pod -o -name .packlist \\) -delete
__EOF__
}

58 59
# create new aport from templates
newaport() {
60
	local newname="${1##*/}"
61 62
	local pn=${newname%-[0-9]*}
	local pv
63 64 65
	local source=
	is_url "$1" && source="$1"

66 67
	if [ "$pn" != "$newname" ]; then
		pv=${newname#$pn-}
68
		pv=${pv%.t*} #strip .tar.gz .tgz .tar.bz2 etc
69 70 71 72 73 74 75
	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
76

77 78 79 80
	if [ -z "$source" ] && [ -n "$sourceforge" ]; then
		source="http://downloads.sourceforge.net/$pn/$pn-$pv.tar.gz"
	fi

Natanael Copa's avatar
Natanael Copa committed
81
	# replace pkgver in $source
82
	if [ -n "$source" ]; then
83
		source=$(echo "$source" | sed "s/$pv/\$pkgver/g")
84
	fi
Natanael Copa's avatar
Natanael Copa committed
85 86

	# copy init.d scripts if requested
87 88 89 90 91
	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
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
		install="\$pkgname.pre-install \$pkgname.post-install"
		source="$source
	$pn.initd
	$pn.confd
	"
	fi

	# generate header with standard variables
	cat >APKBUILD<<__EOF__
# Contributor:${PACKAGER:+" "}${PACKAGER}
# Maintainer:${MAINTAINER:+" "}${MAINTAINER}
pkgname=$pn
pkgver=$pv
pkgrel=0
pkgdesc="$pkgdesc"
url="$url"
arch="all"
license="$license"
depends=
depends_dev=
makedepends="\$depends_dev"
install="$install"
subpackages="\$pkgname-dev \$pkgname-doc"
source="$source"

__EOF__

	abuild -f fetch unpack
	# figure out the _builddir
	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

130 131 132 133
	# check if its autotools
	if [ -z "$buildtype" ]; then
		if [ -x "$sdir"/configure ]; then
			buildtype="autotools"
134
		elif [ -r "$sdir"/Makefile.PL ] || [ "${pn#perl-}" != "$pn" ]; then
135 136 137 138 139 140 141 142 143 144 145
			buildtype="perl"
		elif [ -r "$sdir"/waf ]; then
			buildtype="waf"
		elif [ -d "$sdir"/cmake ]; then
			buildtype="cmake"
		elif [ -r "$sdir"/Makefile ]; then
			buildtype="make"
		fi
	fi


Natanael Copa's avatar
Natanael Copa committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	# create the prepare() template
	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__

	# create build()
	cat >>APKBUILD<<__EOF__
build() {
	cd "\$_builddir"
__EOF__
165 166 167 168 169 170 171 172

	case "$buildtype" in
	autotools)
		config_autotools;;
	perl)
		config_perl;;
	esac

Natanael Copa's avatar
Natanael Copa committed
173 174 175 176 177 178 179 180 181
	cat >>APKBUILD<<__EOF__
	make || return 1
}

__EOF__

	# create package() function
	cat >>APKBUILD<<__EOF__
package() {
182
	cd "\$_builddir"
Natanael Copa's avatar
Natanael Copa committed
183
	make DESTDIR="\$pkgdir" install || return 1
184
	rm -f "\$pkgdir"/usr/lib/*.la
Natanael Copa's avatar
Natanael Copa committed
185 186 187 188 189 190 191 192 193 194
__EOF__
	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
195 196 197 198 199

	case "$buildtype" in
	perl)	cleanup_perl;;
	esac

Natanael Copa's avatar
Natanael Copa committed
200 201 202 203
	cat >>APKBUILD<<__EOF__
}

__EOF__
204 205 206 207
}

usage() {
	echo "$prog $version"
Natanael Copa's avatar
Natanael Copa committed
208
	echo "usage: $prog [-cfh] [-d DESC] [-l LICENSE] [-u URL] PKGNAME[-PKGVER]"
209
	echo "Options:"
210
	echo " -a  Create autotools (use ./configure ...)"
211
	echo " -c  Copy a sample init.d, conf.d and install script to new directory"
Natanael Copa's avatar
Natanael Copa committed
212
	echo " -d  Set package description (pkgdesc) to DESC"
213 214
	echo " -f  Force even if directory already exist"
	echo " -h  Show this help"
Natanael Copa's avatar
Natanael Copa committed
215
	echo " -l  Set package license to LICENSE"
216
	echo " -p  Create perl package (Assume Makefile.PL is there)"
Natanael Copa's avatar
Natanael Copa committed
217
	echo " -u  Set package URL"
218
	echo " -s  Use sourceforge source url"
219 220 221 222
	echo ""
	exit 0
}

223
while getopts "acd:fhl:pu:s" opt; do
224
	case $opt in
225
		'a') buildtype="autotools";;
226
		'c') cpinitd=1;;
Natanael Copa's avatar
Natanael Copa committed
227
		'd') pkgdesc="$OPTARG";;
228 229
		'f') force=1;;
		'h') usage;;
Natanael Copa's avatar
Natanael Copa committed
230
		'l') license="$OPTARG";;
231
		'p') buildtype="perl";;
Natanael Copa's avatar
Natanael Copa committed
232
		'u') url="$OPTARG";;
233
		's') sourceforge=1;;
234 235 236 237 238 239 240 241 242
	esac
done
shift $(( $OPTIND - 1 ))

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