summaryrefslogblamecommitdiff
path: root/newapkbuild.in
blob: c4f5aa9ac8e641b8b8d9fb765a68b530dae08962 (plain) (tree)
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##*/}

# source $PACKAGER
for i in $sysconfdir/abuild.conf $HOME/.abuild/abuild.conf; do
	if [ -f "$i" ]; then
		. $i
	fi
done

error() {
	echo "$@" >&2
}

is_url() {
	case "$1" in
	http://*|ftp://*) return 0;;
	esac
	return 1
}

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__
}

# create new aport from templates
newaport() {
	local newname="${1##*/}"
	local pn=${newname%-[0-9]*}
	local pv
	local source=
	is_url "$1" && source="$1"

	if [ "$pn" != "$newname" ]; then
		pv=${newname#$pn-}
		pv=${pv%.t*} #strip .tar.gz .tgz .tar.bz2 etc
	fi
	if [ -e "$pn"/APKBUILD ] && [ -z "$force" ]; then
		error "$pn/APKBUILD already exist"
		return 1
	fi
	mkdir -p "$pn"
	cd "$pn"

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

	# replace pkgver in $source
	if [ -n "$source" ]; then
		source=$(echo "$source" | sed "s/$pv/\$pkgver/g")
	fi

	# copy init.d scripts if requested
	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
		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

	# check if its autotools
	if [ -z "$buildtype" ]; then
		if [ -x "$sdir"/configure ]; then
			buildtype="autotools"
		elif [ -r "$sdir"/Makefile.PL ] || [ "${pn#perl-}" != "$pn" ]; then
			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


	# 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__

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

	cat >>APKBUILD<<__EOF__
	make || return 1
}

__EOF__

	# create package() function
	cat >>APKBUILD<<__EOF__
package() {
	cd "\$_builddir"
	make DESTDIR="\$pkgdir" install || return 1
	rm -f "\$pkgdir"/usr/lib/*.la
__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

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

	cat >>APKBUILD<<__EOF__
}

__EOF__
}

usage() {
	echo "$prog $version"
	echo "usage: $prog [-cfh] [-d DESC] [-l LICENSE] [-u URL] PKGNAME[-PKGVER]"
	echo "Options:"
	echo " -a  Create autotools (use ./configure ...)"
	echo " -c  Copy a sample init.d, conf.d and install script to new directory"
	echo " -d  Set package description (pkgdesc) to DESC"
	echo " -f  Force even if directory already exist"
	echo " -h  Show this help"
	echo " -l  Set package license to LICENSE"
	echo " -p  Create perl package (Assume Makefile.PL is there)"
	echo " -u  Set package URL"
	echo " -s  Use sourceforge source url"
	echo ""
	exit 0
}

while getopts "acd:fhl:pu:s" opt; do
	case $opt in
		'a') buildtype="autotools";;
		'c') cpinitd=1;;
		'd') pkgdesc="$OPTARG";;
		'f') force=1;;
		'h') usage;;
		'l') license="$OPTARG";;
		'p') buildtype="perl";;
		'u') url="$OPTARG";;
		's') sourceforge=1;;
	esac
done
shift $(( $OPTIND - 1 ))

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