Skip to content
Snippets Groups Projects
buildrepo.in 3.39 KiB
Newer Older
Natanael Copa's avatar
Natanael Copa committed
#!/bin/sh

program=${0##*/}

aportsdir=${APORTSDIR:-$HOME/aports}
repodir=${REPODIR:-$HOME/packages}

if [ -f /etc/abuild.conf ]; then
	. /etc/abuild.conf
fi

if [ -z "$CARCH" ]; then
	machine=$(uname -m)
	case $machine in
	i[3-9]86) CARCH=x86;;
	*) CARCH=$machine;;
	esac
fi
Natanael Copa's avatar
Natanael Copa committed

usage() {
	echo "usage: $program [-a APORTSDIR] [-d REPODIR] [-hp] [-l LOGPREFIX ]"
	echo "                [-r DEPREPO] REPOSITORY..."
	
Natanael Copa's avatar
Natanael Copa committed
	echo "options:"
	echo " -a  Set the aports base dir to APORTSDIR instead of $aportsdir"
	echo " -d  Set destination repository base dir to REPODIR instead of $repodir"
	echo " -h  Show this help and exit"
	echo " -l  Send build to logfile, prefixed by LOGPREFIX"
	echo " -p  Purge obsolete packages from REPODIR after build"
	echo " -r  Dependencies are found in DEPREPO"
Natanael Copa's avatar
Natanael Copa committed
	exit 1
}


listpackages() {
	cd "$aportsdir/$1"
Natanael Copa's avatar
Natanael Copa committed
	for i in */APKBUILD; do
		APKBUILD=$i abuild listpkg
Natanael Copa's avatar
Natanael Copa committed
	done
}

all_exist() {
	while [ $# -gt 0 ]; do
		[ -e "$1" ] || return 1
		shift 1
	done
	return 0
}
	
Natanael Copa's avatar
Natanael Copa committed
build() {
	local repo="$1" i needbuild
Natanael Copa's avatar
Natanael Copa committed

	cd "$aportsdir/$repo" || return 0
Natanael Copa's avatar
Natanael Copa committed

	# first we try copy everything possible and find out which we need
	# to rebuild. By doing this we might save us for rebuilding 
	# needed when running 'abuild -R'
Natanael Copa's avatar
Natanael Copa committed
	for i in */APKBUILD; do
		[ -f "$aportsdir/$repo/$i" ] || continue
		export REPODEST="$repodir"
		cd "$aportsdir/$repo"/${i%/*} || return 1
		pkgname=
		pkgver=
		pkgrel=
		subpackages=
		. ./APKBUILD

		pkgs=
		for subpkg in $pkgname $subpackages; do
			pkgfile=${subpkg%:*}-$pkgver-r$pkgrel.apk
			if ! [ -f "$REPODEST/$repo/$CARCH/$pkgfile" ]; then
				pkgs="$pkgs $pkgfile"
			fi
		done
		if [ -z "$pkgs" ]; then
			continue
		fi

		# try link or copy the files if they are in the ports dir
			echo ">>> Copying " $pkgs
			cp -p -l $pkgs "$repodir/$repo/$CARCH"/ 2>/dev/null \
				|| cp -p $pkgs "$repodir/$repo/$CARCH"/ \
				|| needbuild="$needbuild $i"
		else
			needbuild="$needbuild $i"
		fi
Natanael Copa's avatar
Natanael Copa committed
	done

	# build the postponed packages if any
	if [ -n "$needbuild" ]; then
		for i in $needbuild; do
			cd "$aportsdir/$repo"/${i%/*} || return 1
			abuild -k -R || return 1
		done
	fi

	# kill old packages in repo
	if [ -n "$dopurge" ]; then
		echo ">>> Removing old packages from $repo..."
		local tmp=$(mktemp /tmp/$program-XXXXXX)
		local purgefiles
		cd "$repodir/$repo/$CARCH" || return 1
		trap 'rm -f "$tmp"; exit 1' INT
		( listpackages "$1") >$tmp
		purge=$(ls *.apk 2>/dev/null | grep -v -w -f $tmp)
		if [ -n "$purge" ]; then
			rm -f $purge
		fi
		rm -f "$tmp"
	fi
	# generate the repository index
	echo ">>> Generating Index for $repo..."
	cd "$repodir/$repo/$CARCH"
	local deps
	for i in $deprepo; do
		deps="--repo $repodir/$i"
	done
	oldindex=
	if [ -f APKINDEX.tar.gz ]; then
		oldindex="--index APKINDEX.tar.gz"
Natanael Copa's avatar
Natanael Copa committed
	fi
	tmpindex=$(mktemp).tar.gz
	apk index --rewrite-arch $CARCH $oldindex -o $tmpindex \
		--description "$repo $(cd $aportsdir && git describe)" \
	abuild-sign $tmpindex && mv $tmpindex APKINDEX.tar.gz
	chmod 644 APKINDEX.tar.gz
	rm -f tmp.*
while getopts "a:d:hl:pr:" opt; do
Natanael Copa's avatar
Natanael Copa committed
	case "$opt" in
		a) aportsdir=$OPTARG;;
		d) repodir=$OPTARG;;
		h) usage >&2;;
		l) logprefix=$OPTARG;;
		p) dopurge=1;;
		r) deprepo="$deprepo $OPTARG";;
Natanael Copa's avatar
Natanael Copa committed
	esac
done
shift $(($OPTIND - 1))

[ $# -eq 0 ] && usage >&2

while [ $# -gt 0 ]; do
	if [ -n "$logprefix" ]; then
		build $1  >$logprefix.$1.log 2>&1 || exit 1
	else
		build $1 || exit 1
	fi
	deprepo="$deprepo $1"
Natanael Copa's avatar
Natanael Copa committed
	shift
done