summaryrefslogblamecommitdiff
path: root/newapkbuild.in
blob: 952980fd2591f7a22706f95cb4e968910af088bf (plain) (tree)
1
2
3
4
5
6
7
8
9
         
 
                                       



                                                            
 
                         

                 




                                                   
 
 

                    
                                              
            
                

 






                                                           
                





                               
                   
                               



                                   




                                            
                        


       
              

                                                                             



                        
                

                                         


       
                  
                



                                                   

 



                     
                
                               
                                                   



                                                                              






                                                                           
            
                                

                                   


                                  








                                                                  
                                  
                                                             
          




                                                              

                        

                           
 



                                                                            
                                                                


                                
                                                                    





                                        
                                 
                                                                 
          
 
                                          
                                  



                                                                       

                                                                      

                      


          
                                                 


                                            
                





                  


                          






                                         
                                  








                                                                          
                                


                                                 
                                                                                   






                                                  

                                                  



                  
                                       












                                                                           
                                 



                               

                            

                            
                  
                                 
             
                            
               
                              

            
                               



       
                                   

                               
                        
       

                            

                              





                                   

            








                                                           



                               
                          


         
                         
                                                   

                                                                













                                                                       

 
                                       
                    
                                            
                                
                                        
                              
                                  
                                        
                                        
                                       
                                         
                                    
                                    



                        
                      



                             
#!/bin/sh
#
# newapkbuild - generate a new APKBUILD
# Copyright (c) 2009 Natanael Copa <natanael.copa@gmail.com>
#
# Distributed under GPL-2
#

program_version=@VERSION@
datadir=@datadir@

if ! [ -f "$datadir/functions.sh" ]; then
	echo "$datadir/functions.sh: not found" >&2
	exit 1
fi
. "$datadir/functions.sh"


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

is_github_url() {
	case $1 in
	https://github.com/*/*/archive/*.tar.gz) return 0;;
	esac
	return 1
}

# Build sections
build_make() {
	cat >>APKBUILD<<__EOF__
	make || return 1
__EOF__
}

build_autotools() {
	cat >>APKBUILD<<__EOF__
	./configure \\
		--build=\$CBUILD \\
		--host=\$CHOST \\
		--prefix=/usr \\
		--sysconfdir=/etc \\
		--mandir=/usr/share/man \\
		--infodir=/usr/share/info \\
		--localstatedir=/var \\
		|| return 1
	make || return 1
__EOF__
}

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

build_python() {
	cat >>APKBUILD<<__EOF__
	python setup.py build || return 1
__EOF__
}

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

package_autotools() {
	package_make
}

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

package_python() {
	cat >>APKBUILD<<__EOF__
	python setup.py install --prefix=/usr --root="\$pkgdir" || return 1
__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 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
		pv=${newname#$pn-}
		pv=${pv%.t*} #strip .tar.gz .tgz .tar.bz2 etc
	fi
	if [ -z "$pkgname" ]; then
		pkgname=$pn
	fi
	if [ -e "$pkgname"/APKBUILD ] && [ -z "$force" ]; then
		error "$pkgname/APKBUILD already exist"
		return 1
	fi
	mkdir -p "$pkgname"
	cd "$pkgname"

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

	if [ -z "$depends" ] &&[ "$buildtype" = "python" ]; then
		depends="python"
	fi

	if [ -z "$makedepends" ] &&[ "$buildtype" = "python" ]; then
		makedepends="python-dev"
	else
        makedepends="\$depends_dev"
	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 $pkgname.initd
		cp "$datadir"/sample.confd $pkgname.confd
		cp "$datadir"/sample.pre-install $pkgname.pre-install
		cp "$datadir"/sample.post-install $pkgname.post-install
		install="\$pkgname.pre-install \$pkgname.post-install"
		source="$source
	$pkgname.initd
	$pkgname.confd
	"
	fi

	# Generate header with standard variables
	cat >APKBUILD<<__EOF__
# Contributor:${PACKAGER:+" "}${PACKAGER}
# Maintainer:${MAINTAINER:+" "}${MAINTAINER}
pkgname=$pkgname
pkgver=$pv
pkgrel=0
pkgdesc="$pkgdesc"
url="$url"
arch="all"
license="$license"
depends="$depends"
depends_dev=""
makedepends="$makedepends"
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"
		elif [ -r "$sdir"/setup.py ]; then
			buildtype="python"
		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() function
	cat >>APKBUILD<<__EOF__
build() {
	cd "\$_builddir"
__EOF__

	case "$buildtype" in
	make)
		build_make;;
	autotools)
		build_autotools;;
	perl)
		build_perl;;
	python)
		build_python;;
	esac

	cat >>APKBUILD<<__EOF__
}

__EOF__

	# Create package() function
	cat >>APKBUILD<<__EOF__
package() {
	cd "\$_builddir"
__EOF__

	case "$buildtype" in
	make)
		package_make;;
	autotools)
		package_autotools;;
	perl)
		package_perl;;
	python)
		package_python;;
	esac

	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
	cat >>APKBUILD<<__EOF__
}

__EOF__
	abuild -f checksum
}

usage() {
	cat >&2 <<__EOF__
$program $program_version - generate a new APKBUILD
Usage: $program [-cfh] [-d DESC] [-l LICENSE] [-n NAME] [-u URL]
		PKGNAME[-PKGVER]|SRCURL
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__
}

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

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