--- /dev/null
+#!/bin/bash -l
+
+BASEDIR="/pub/nightly"
+
+MAX_AGE=$(( 7 * 24 * 3600 )) # 7 days
+
+NOW="$(date +"%s")"
+
+counter=0
+for build in $(find "${BASEDIR}" -mindepth 2 -maxdepth 2 -type d | sort -nr); do
+ time="$(basename "${build}")"
+ [ "${time}" = "latest" ] && continue
+
+ # Never delete the last build
+ if [ "${counter}" -lt 1 ]; 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
+
+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}")"
+
+ 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}"
+ ln -sf --relative "${release}" "${branch}/latest/${arch}"
+ done
+ done
+
+ return 0
+}
+
+main || exit $?