#!/bin/bash -xe
# Working variables
MIRROR_ROOT=$1
# A temporary file to which to write the new index
TMP_INDEX_FILE=$(mktemp)
trap "rm -f -- '$TMP_INDEX_FILE'" EXIT
# And the final location
INDEX_FILE=${MIRROR_ROOT}/index.html
# Start building our file
echo -e "\n
\n Wheel Index\n " > $TMP_INDEX_FILE
echo -e " \n " >> $TMP_INDEX_FILE
# Get a list of files
FILES=`find $MIRROR_ROOT -maxdepth 2 -type d`
REGEX="([^/])\/(\1[^/]+)$"
for f in $FILES; do
if [[ $f =~ $REGEX ]]; then
echo " - ${BASH_REMATCH[2]}
" >> $TMP_INDEX_FILE
fi
done
echo -e "
\n \n" >> $TMP_INDEX_FILE
if ! cmp $TMP_INDEX_FILE $INDEX_FILE >/dev/null 2>&1; then
# Atomically replace the index file
mv $TMP_INDEX_FILE $INDEX_FILE
else
rm $TMP_INDEX_FILE
fi
# The tempfile is gone so we don't need the trap
trap - EXIT