summaryrefslogtreecommitdiffstats
path: root/buildrepo
blob: b1f36cdd4d0e564d7fa86c8d45d64a78e9355c0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
#!/bin/sh

program=${0##*/}

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


usage() {
	echo "usage: $program [-a APORTSDIR] [-d REPODIR] [-hp] [-l LOGPREFIX ]"
	echo "                [-r DEPREPO] REPOSITORY..."
	
	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"
	exit 1
}


listpackages() {
	cd "$aportsdir/$1"
	for i in */APKBUILD; do
		APKBUILD=$i abuild listpkg
	done
}

# purge obsolete packages
purge() {
	[ -z "$dopurge" ] && return 0
	local tmp=$(mktemp /tmp/$program-XXXXXX)	
	cd "$repodir/$1" || return 1
	trap 'rm -f "$tmp"; exit 1' INT
	( listpackages "$1") >$tmp
	ls *.apk 2>/dev/null | grep -v -w -f $tmp | xargs rm 2>/dev/null
	return 0
}

build() {
	local repo="$1" i indexupdate needbuild

	cd "$aportsdir/$repo" || return 1
	mkdir -p "$repodir/$repo"
	if ! [ -f "$repodir/$repo"/APK_INDEX.gz ]; then
		indexupdate="APK_INDEX.gz"
	fi

	# 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'
	for i in */APKBUILD; do
		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)
		if cp -p -l $pkgs "$repodir/$repo"/ 2>/dev/null \
			|| cp -p $pkgs "$repodir/$repo"/ 2>/dev/null; then
			echo ">>> Copying " $pkgs
		else
			needbuild="$needbuild $i"
		fi
		indexupdate="$indexupdate $i"
	done

	if [ -z "$indexupdate" ] && [ -z "$needbuild" ]; then
		echo ">>> Repository $repo is up-to-date."
		return 0
	fi

	# 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
	purge "$repo"

	# generate the repository index
	cd "$repodir/$repo"
	echo ">>> Generating Index for $repo..."
	md5sum *.apk | gzip -9 > INDEX.md5.gz
	if which apk >/dev/null; then
		local deps
		for i in $deprepo; do
			deps="--repo $repodir/$i"
		done
		apk $deps index *.apk | gzip -9 > APK_INDEX.gz
	fi
}

while getopts "a:d:hl:pr:" opt; do
	case "$opt" in
		a) aportsdir=$OPTARG;;
		d) repodir=$OPTARG;;
		h) usage >&2;;
		l) logprefix=$OPTARG;;
		p) dopurge=1;;
		r) deprepo="$deprepo $OPTARG";;
	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"
	shift
done