--- /dev/null
+#!/bin/bash -l
+
+BASEDIR="/pub/nightly"
+declare -A ARCHES
+ARCHES["master"]="x86_64 i586"
+ARCHES["next"]="x86_64 i586 armv5tel"
+
+MAX_AGE=$(( 7 * 24 * 3600 )) # 7 days
+
+NOW="$(date +"%s")"
+
+all_successful() {
+ local branch="${1}"
+ local release="${2}"
+
+ local arches="${ARCHES[${branch}]}"
+ [ -z "${arches}" ] && return 0
+
+ local arch
+ for arch in ${arches}; do
+ if [ ! -e "${release}/${arch}/.success" ]; then
+ return 1
+ fi
+ done
+
+ return 0
+}
+
+for branch in $(find "${BASEDIR}" -mindepth 1 -maxdepth 1 -type d); do
+ counter=0
+
+ for build in $(find "${branch}" -mindepth 1 -maxdepth 1 -type d | sort -nr); do
+ time="$(basename "${build}")"
+ [ "${time}" = "latest" ] && continue
+
+ # Never delete the last two builds
+ if [ "${counter}" -lt 2 ] && all_successful "$(basename ${branch})" "${build}"; then
+ counter=$(( ${counter} + 1 ))
+ continue
+ fi
+
+ # Determine age of the build
+ change="$(stat --format="%Y" "${build}")"
+ age=$(( ${NOW} - ${change} ))
+
+ # If the build is old enough we will delete it
+ if [[ ${age} -ge ${MAX_AGE} ]]; then
+ rm -rf "${build}"
+ fi
+ done
+done
+
+exit 0
--- /dev/null
+#!/bin/bash -l
+
+BASEDIR="/pub/nightly"
+
+main() {
+ local branch
+ for branch in ${BASEDIR}/*; do
+ [ -d "${branch}" ] || continue
+
+ local -A successful_releases=()
+
+ local arch
+ local release
+ for release in ${branch}/*/*; do
+ arch="$(basename "${release}")"
+
+ # Remember that we have seen the architecture
+ if [ -z "${successful_releases[${arch}]}" ]; then
+ successful_releases["${arch}"]=""
+ fi
+
+ # Skip the "latest" symlink
+ local time="$(basename "$(dirname "${release}")")"
+ [ "${time}" = "latest" ] && continue
+
+ if [ -e "${release}/.success" ]; then
+ successful_releases["${arch}"]="${release}"
+ fi
+ done 2>/dev/null
+
+ for arch in ${!successful_releases[@]}; do
+ release="${successful_releases[${arch}]}"
+
+ mkdir -p "${branch}/latest"
+ rm -f "${branch}/latest/${arch}"
+
+ if [ -n "${release}" ]; then
+ ln -sf --relative "${release}" "${branch}/latest/${arch}"
+ fi
+ done
+ done
+
+ return 0
+}
+
+main || exit $?