buildrepo.in 3.04 KB
Newer Older
Natanael Copa's avatar
Natanael Copa committed
1 2 3 4 5 6 7
#!/bin/sh

program=${0##*/}

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

8 9 10 11 12
machine=$(uname -m)
case $machine in
	i[3-9]86) arch=x86;;
	*) arch=$machine;;
esac
Natanael Copa's avatar
Natanael Copa committed
13 14

usage() {
15 16 17
	echo "usage: $program [-a APORTSDIR] [-d REPODIR] [-hp] [-l LOGPREFIX ]"
	echo "                [-r DEPREPO] REPOSITORY..."
	
Natanael Copa's avatar
Natanael Copa committed
18 19 20 21 22 23
	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"
24
	echo " -r  Dependencies are found in DEPREPO"
Natanael Copa's avatar
Natanael Copa committed
25 26 27 28 29
	exit 1
}


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

36 37 38 39 40 41 42 43
all_exist() {
	while [ $# -gt 0 ]; do
		[ -e "$1" ] || return 1
		shift 1
	done
	return 0
}
	
Natanael Copa's avatar
Natanael Copa committed
44
build() {
45
	local repo="$1" i needbuild
Natanael Copa's avatar
Natanael Copa committed
46

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

49 50 51
	# 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
52
	for i in */APKBUILD; do
Natanael Copa's avatar
Natanael Copa committed
53
		[ -f "$aportsdir/$repo/$i" ] || continue
54 55 56 57 58 59 60 61
		export REPODEST="$repodir"
		cd "$aportsdir/$repo"/${i%/*} || return 1
		if abuild -k -q up2date 2>/dev/null; then
			continue
		fi

		# try link or copy the files if they are in the ports dir
		pkgs=$(abuild listpkg)
62
		if all_exist $pkgs; then
63
			echo ">>> Copying " $pkgs
64 65
			cp -p -l $pkgs "$repodir/$repo/$CARCH"/ 2>/dev/null \
				|| cp -p $pkgs "$repodir/$repo/$CARCH"/ \
66
				|| needbuild="$needbuild $i"
67 68 69
		else
			needbuild="$needbuild $i"
		fi
Natanael Copa's avatar
Natanael Copa committed
70
	done
71 72 73 74 75 76 77 78 79

	# 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

80
	# kill old packages in repo
81 82 83 84 85 86 87 88 89 90 91 92
	if [ -n "$dopurge" ]; then
		local tmp=$(mktemp /tmp/$program-XXXXXX)
		local purgefiles
		cd "$repodir/$1" || 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
93

94 95
	# generate the repository index
	echo ">>> Generating Index for $repo..."
96
	cd "$repodir/$repo/$arch"
97 98 99 100 101 102 103
	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
104
	fi
105
	tmpindex=$(mktemp).tar.gz
106
	apk index --rewrite-arch $arch $oldindex -o $tmpindex \
107 108
		--description "$repo $(cd $aportsdir && git describe)" \
		*.apk
109
	abuild-sign $tmpindex && mv $tmpindex APKINDEX.tar.gz
110 111
	chmod 644 APKINDEX.tar.gz
	rm -f tmp.*
Natanael Copa's avatar
Natanael Copa committed
112 113
}

114
while getopts "a:d:hl:pr:" opt; do
Natanael Copa's avatar
Natanael Copa committed
115 116 117 118 119 120
	case "$opt" in
		a) aportsdir=$OPTARG;;
		d) repodir=$OPTARG;;
		h) usage >&2;;
		l) logprefix=$OPTARG;;
		p) dopurge=1;;
121
		r) deprepo="$deprepo $OPTARG";;
Natanael Copa's avatar
Natanael Copa committed
122 123 124 125 126 127 128 129 130 131 132 133
	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
134
	deprepo="$deprepo $1"
Natanael Copa's avatar
Natanael Copa committed
135 136 137
	shift
done