#!/usr/local/bin/bash
#
# generate_index_html
#
# Generates index.html files with a link to each file in the directory, and
# recursively for its sub-directories.
#
# Note: This script recursively calls itself, so it had better be in its
# own path!
#
CURRENT_RELEASE=0.11.0
BETA_RELEASE=0.11.1
# We need bash's extended globbing for the case patterns
# matching the release numbers.
shopt -s extglob
INDEX=index.html.$HOST.$$
LOCKDIR=generate_index_html.lock
handled_files=
# Lock the directory (this script may be invoked by test_mercury
# on multiple machines at once).
timeout=20
retries=0
until mkdir $LOCKDIR; do
case $retries in
$timeout)
echo "** $0: creating lock directory `pwd`/$LOCKDIR failed" 1>&2
echo "** $0: remove it manually if it is stale" 1>&2
exit 1
;;
esac
sleep 5
retries=`expr $retries + 1`
done
trap 'rm -rf $INDEX $LOCKDIR; exit 1' 1 2 3 13 15
#
# These are the standard locations for apache's icons
#
img_txt="
"
img_dir="
"
img_bak="
"
echo "" \
> $INDEX
echo "" >> $INDEX
echo "
" >> $INDEX
echo "The Mercury Project: Directory Listing" >> $INDEX
echo "" >> $INDEX
echo "" >> $INDEX
echo "Directory listing
" >> $INDEX
echo "
" >> $INDEX
# link_to_file prefix filename description.
#
# Generate a line with the prefix followed by a link to the given file.
# Record that the file has been handled.
link_to_file () {
prefix=$1
this_file=$2
this_file_descr=$3
this_file_size=`ls -Ll $this_file | awk '{ print $5; }'`
this_file_sizekb=`expr $this_file_size / 1024`
handled_files="$this_file $handled_files"
echo "$prefix $this_file_descr ($this_file_sizekb kilobytes)" >> $INDEX
}
# link_to_file filename description
#
# Generate a link to a file if it exists.
# This should be called within a `' element.
link_to_existing_file () {
if [ -f $1 ]; then
link_to_file "- " $1 "$2"
fi
}
# handle_release release_file
#
# Given the name of a source or binary distribution file in a release,
# generate an entry describing all the files in the release.
handle_release () {
file=$1
if [ -f $file ]
then
case "$handled_files" in
*$file*) ;;
*) do_handle_release $file ;;
esac
fi
}
do_handle_release () {
file=$1
date=`expr "$file" : '.*\([0-9]\{4\}-[0-9][0-9]-[0-9][0-9]\).*'`
case $file in
*.rpm|*.deb)
# XXX Currently we only produce RPMs and Debian
# packages for major releases.
version=`expr "$file" : ".*-\([0-9.]*[0-9]\)-[0-9].*"`
release_name="Release $version"
release_id="$version"
;;
*-rotd*-unstable*)
release_name="Unstable snapshot $date"
release_id="rotd-$date-unstable"
;;
*-rotd*)
release_name="Stable snapshot $date"
release_id="rotd-$date"
;;
*-*([0-9.])-beta*-unstable*)
version=`expr "$file" : ".*-\([0-9.]*\)-beta.*-unstable.*"`
release_name="Unstable $version beta $date"
release_id="$version-beta-$date-unstable"
;;
*-*([0-9.])-beta*)
version=`expr "$file" : ".*-\([0-9.]*\)-beta.*"`
release_name="Stable $version beta $date"
release_id="$version-beta-$date"
;;
*-*([0-9.])*)
version=`expr "$file" : ".*-\([0-9.]*[0-9]\).*"`
release_name="Release $version"
release_id="$version"
;;
*)
# Unknown release, this will be put in the list of
# other files after all known releases.
return 0
;;
esac
echo "
- $release_name" >> $INDEX
echo '
' >> $INDEX
source_dist_news="mercury-NEWS-$release_id.txt"
link_to_existing_file "$source_dist_news" "News"
link_to_existing_file mercury-INSTALL-$release_id.txt \
"Installation instructions"
link_to_existing_file mercury-compiler-$release_id.tar.gz \
"Source distribution"
link_to_existing_file mercury-compiler-$release_id-1.src.rpm \
"Source RPM"
link_to_existing_file mercury-compiler-$release_id-1.i386.rpm \
"RPM (x86)"
link_to_existing_file mercury_$release_id-1_i386.deb "Debian (x86)"
bindists=`echo mercury-$release_id.[a-z]*.tar.gz`
case "$bindists" in
*'*'.tar.gz)
;;
*)
echo "- Binary distributions" >> $INDEX
echo "
" >> $INDEX
for bindist in $bindists
do
bindist_name=`expr \
"$bindist" : "mercury-$release_id.\(.*\).tar.gz"`
link_to_file "- " $bindist $bindist_name
echo "
" >> $INDEX
bindist_news="mercury-NEWS-$release_id.txt"
# If the news file for the source distribution
# doesn't exist, or doesn't match the news file for
# the binary distribution, generate a link to the
# news file for the binary distribution.
# XXX We should probably generate a separate release
# entry if the news files for the source and binary
# distributions don't match, but that should be rare.
if cmp $bindist_news $source_dist_news >& /dev/null
then
:
else
link_to_existing_file \
mercury-NEWS-$release_id.$bindist_name.txt \
"News"
fi
link_to_existing_file \
mercury-INSTALL-$release_id.$bindist_name.txt \
"Installation instructions"
link_to_existing_file \
mercury-test-failures-$release_id.$bindist_name.txt \
"Test failures"
echo "
" >> $INDEX
done
echo "
" >> $INDEX
;;
esac
link_to_existing_file mercury-extras-$release_id.tar.gz "Extras"
link_to_existing_file mercury-tests-$release_id.tar.gz "Test suite"
link_to_existing_file mercury-test-failures-$release_id.txt \
"Test failures"
echo '
' >> $INDEX
}
echo "$img_bak Parent Directory
" >> $INDEX
#
# Directories.
#
for file in *
do
if [ -d $file -a $file != $LOCKDIR ]
then
(cd $file && generate_index_html)
echo "$img_dir $file/
" >> $INDEX
fi
done
echo "
" >> $INDEX
echo "
" >> $INDEX
#
# Handle releases. Note that handle_release checks for files
# which have been handled before, so the cases below need not
# be mutually exclusive.
#
#
# Current release.
#
handle_release mercury-compiler-${CURRENT_RELEASE}.tar.gz
#
# Stable beta for current release + bug fixes.
# There may be an unstable beta, but there's no point using it.
#
for file in mercury-compiler-${BETA_RELEASE}-beta*.tar.gz \
mercury-${BETA_RELEASE}-beta*.tar.gz
do
case $file in
*-unstable*) ;;
*) handle_release $file ;;
esac
done
#
# Stable release-of-the-day.
#
for file in mercury-compiler-rotd*.tar.gz mercury-rotd-*
do
case $file in
*-unstable*) ;;
*) handle_release $file ;;
esac
done
#
# Unstable release-of-the-day
#
for file in mercury-compiler-rotd-*-unstable.tar.gz
do
handle_release $file
done
#
# Other assorted releases.
#
for file in mercury-compiler-* mercury-rotd-*
do
handle_release $file
done
#
# Other stuff.
#
for file in *
do
case $file in
#
# Don't include the README or any of the index files
#
README|index.html*)
;;
*)
if [ ! -d $file ]
then
case $handled_files in
*$file*) ;;
*) link_to_file "- " $file $file ;;
esac
fi
;;
esac
done
echo "
" >> $INDEX
echo "
" >> $INDEX
if [ -f README ]
then
echo "" >> $INDEX
cat README >> $INDEX
echo "" >> $INDEX
fi
echo "" >> $INDEX
echo "" >> $INDEX
chmod a+r,g+w $INDEX
chgrp mercury $INDEX
mv $INDEX index.html
rmdir $LOCKDIR || { echo "** $0: error removing $LOCKDIR" 1>&2; exit 1; }