| 1 | #!/bin/sh |
| 2 | |
| 3 | program=${0##*/} |
| 4 | |
| 5 | aportsdir=${APORTSDIR:-$HOME/aports} |
| 6 | repodir=${REPODIR:-$HOME/packages} |
| 7 | |
| 8 | |
| 9 | usage() { |
| 10 | echo "usage: $program [-a APORTSDIR] [-d REPODIR] [-hp] [-l LOGPREFIX ]" |
| 11 | echo " [-r DEPREPO] REPOSITORY..." |
| 12 | |
| 13 | echo "options:" |
| 14 | echo " -a Set the aports base dir to APORTSDIR instead of $aportsdir" |
| 15 | echo " -d Set destination repository base dir to REPODIR instead of $repodir" |
| 16 | echo " -h Show this help and exit" |
| 17 | echo " -l Send build to logfile, prefixed by LOGPREFIX" |
| 18 | echo " -p Purge obsolete packages from REPODIR after build" |
| 19 | echo " -r Dependencies are found in DEPREPO" |
| 20 | exit 1 |
| 21 | } |
| 22 | |
| 23 | |
| 24 | listpackages() { |
| 25 | cd "$aportsdir/$1" |
| 26 | for i in */APKBUILD; do |
| 27 | APKBUILD=$i abuild listpkg |
| 28 | done |
| 29 | } |
| 30 | |
| 31 | all_exist() { |
| 32 | while [ $# -gt 0 ]; do |
| 33 | [ -e "$1" ] || return 1 |
| 34 | shift 1 |
| 35 | done |
| 36 | return 0 |
| 37 | } |
| 38 | |
| 39 | build() { |
| 40 | local repo="$1" i indexupdate needbuild |
| 41 | |
| 42 | cd "$aportsdir/$repo" || return 1 |
| 43 | mkdir -p "$repodir/$repo" |
| 44 | if ! [ -f "$repodir/$repo"/APK_INDEX.gz ]; then |
| 45 | indexupdate="APK_INDEX.gz" |
| 46 | fi |
| 47 | |
| 48 | # first we try copy everything possible and find out which we need |
| 49 | # to rebuild. By doing this we might save us for rebuilding |
| 50 | # needed when running 'abuild -R' |
| 51 | for i in */APKBUILD; do |
| 52 | export REPODEST="$repodir" |
| 53 | cd "$aportsdir/$repo"/${i%/*} || return 1 |
| 54 | if abuild -k -q up2date 2>/dev/null; then |
| 55 | continue |
| 56 | fi |
| 57 | |
| 58 | indexupdate="$indexupdate $i" |
| 59 | |
| 60 | # try link or copy the files if they are in the ports dir |
| 61 | pkgs=$(abuild listpkg) |
| 62 | if all_exist $pkgs; then |
| 63 | echo ">>> Copying " $pkgs |
| 64 | cp -p -l $pkgs "$repodir/$repo"/ 2>/dev/null \ |
| 65 | || cp -p $pkgs "$repodir/$repo"/ \ |
| 66 | || needbuild="$needbuild $i" |
| 67 | else |
| 68 | needbuild="$needbuild $i" |
| 69 | fi |
| 70 | done |
| 71 | |
| 72 | # build the postponed packages if any |
| 73 | if [ -n "$needbuild" ]; then |
| 74 | for i in $needbuild; do |
| 75 | cd "$aportsdir/$repo"/${i%/*} || return 1 |
| 76 | abuild -k -R || return 1 |
| 77 | done |
| 78 | fi |
| 79 | |
| 80 | # kill old packages in repo |
| 81 | if [ -n "$dopurge" ]; then |
| 82 | local tmp=$(mktemp /tmp/$program-XXXXXX) |
| 83 | local purgefiles |
| 84 | cd "$repodir/$1" || return 1 |
| 85 | trap 'rm -f "$tmp"; exit 1' INT |
| 86 | ( listpackages "$1") >$tmp |
| 87 | purge=$(ls *.apk 2>/dev/null | grep -v -w -f $tmp) |
| 88 | if [ -n "$purge" ]; then |
| 89 | rm -f $purge |
| 90 | indexupdate="$indexupdate $purge" |
| 91 | fi |
| 92 | rm -f "$tmp" |
| 93 | fi |
| 94 | |
| 95 | # check if we have any .apk newer than our index |
| 96 | cd "$repodir/$repo" |
| 97 | if [ -z "$indexupdate" ]; then |
| 98 | for i in *.apk; do |
| 99 | if [ $i -nt APK_INDEX.gz ]; then |
| 100 | indexupdate=1 |
| 101 | break; |
| 102 | fi |
| 103 | done |
| 104 | fi |
| 105 | |
| 106 | # generate the repository index if needed |
| 107 | if [ -z "$indexupdate" ]; then |
| 108 | echo ">>> Index for $repo is up-to-date" |
| 109 | else |
| 110 | echo ">>> Generating Index for $repo..." |
| 111 | if which apk >/dev/null; then |
| 112 | local deps |
| 113 | for i in $deprepo; do |
| 114 | deps="--repo $repodir/$i" |
| 115 | done |
| 116 | apk $deps index *.apk | gzip -9 > APK_INDEX.gz |
| 117 | fi |
| 118 | fi |
| 119 | } |
| 120 | |
| 121 | while getopts "a:d:hl:pr:" opt; do |
| 122 | case "$opt" in |
| 123 | a) aportsdir=$OPTARG;; |
| 124 | d) repodir=$OPTARG;; |
| 125 | h) usage >&2;; |
| 126 | l) logprefix=$OPTARG;; |
| 127 | p) dopurge=1;; |
| 128 | r) deprepo="$deprepo $OPTARG";; |
| 129 | esac |
| 130 | done |
| 131 | shift $(($OPTIND - 1)) |
| 132 | |
| 133 | [ $# -eq 0 ] && usage >&2 |
| 134 | |
| 135 | while [ $# -gt 0 ]; do |
| 136 | if [ -n "$logprefix" ]; then |
| 137 | build $1 >$logprefix.$1.log 2>&1 || exit 1 |
| 138 | else |
| 139 | build $1 || exit 1 |
| 140 | fi |
| 141 | deprepo="$deprepo $1" |
| 142 | shift |
| 143 | done |
| 144 | |