#!/bin/bash

LOCKFILE=/var/lock/mirrormanager/update-static-content.lock
PUBLICLIST_DIR=/var/lib/mirrormanager/mirrorlists

[ -e ${LOCKFILE} ] && kill -0 $(cat ${LOCKFILE}) && exit 2
mkdir -p $(dirname ${LOCKFILE})
echo $$ > ${LOCKFILE}
trap "cleanup" QUIT TERM INT HUP EXIT

function cleanup()
{
    [ -n "$temp_dir_name" ] && rm -rf $temp_dir_name
    rm -f ${LOCKFILE}
}    

mkdir -p "${PUBLICLIST_DIR}"
temp_dir_name=`mktemp -d /tmp/mirrorlists.XXXXXX`

pushd $temp_dir_name > /dev/null 2>&1
/usr/share/mirrormanager/server/generate-publiclist -c /etc/mirrormanager/prod.cfg -o ${temp_dir_name}/publiclist

if [ $(find . -type f -name \*.html | wc -l) -lt 5 ]; then
    # touch the existing files so our proxy caches refresh
    find "${PUBLICLIST_DIR}" -type f -exec touch \{\} \;
    exit 1
fi

rsync -a /usr/share/mirrormanager/server/mirrormanager/static .
# remove /mirrormanager in the URLs
find . -name index.html -type f -print0 | xargs -0 -n 20 sed -i -e 's:href=\"/mirrormanager/:href=\"/:g'
find . -name \*.css -type f -print0 | xargs -0 -n 20 sed -i -e 's:/mirrormanager/:/:g'
popd > /dev/null 2>&1
mkdir -m 0775 -p "${PUBLICLIST_DIR}"
# because the arg list is too long for cp
find $temp_dir_name -type d -exec chmod 0755 \{\} \;
find $temp_dir_name -type f -exec chmod 0644 \{\} \;
rsync -a --delete --delete-after --delay-updates $temp_dir_name/ "${PUBLICLIST_DIR}"/
