#!/bin/bash
#set -x

# Check for lock files from external scripts that touch TARGETDIR

for lock in render-blfs-{book,edguide}-in-progress render-lfs-book-in-progress render-hlfs-book-in-progress ; do
	while [ -f "/tmp/$lock" ]; do
		echo "/tmp/$lock external lock file present, sleeping for 10 minutes and trying again"
		sleep 600
	done
done

##########################################################
# Ensure there's only 1 instance of this script running  #

LOCKFILE="/tmp/update-website-in-progress"

while [ -f $LOCKFILE ]; do
	echo "Lock file present, sleeping for 10 minutes and trying again"
	sleep 600
done

touch $LOCKFILE

##########################################
# Set variables, create working dirs     #

SVN="svn://svn.linuxfromscratch.org"

# Temporary dir for assembling the site
TMPDIR=`mktemp -d` || exit 1

NEWSITE="$TMPDIR/NEWSITE"
svn -q export $SVN/www/html/trunk $NEWSITE || { rm -rf $TMPDIR; rm -f $LOCKFILE; exit 1; }

SCRIPTS="$NEWSITE/scripts"
MANAGE_NEWS="$SCRIPTS/manage_news.pl"
SVN2HTML="$SCRIPTS/svn2html.pl"
SVN2HTML2="$SCRIPTS/svn2html2.pl"
SVNLOG="svn log --verbose --xml"
TEMPLATES="$NEWSITE/templates"
YEAR=`date +%Y`

##############################################
# Generate all changelogs for the news pages #

cd $NEWSITE

# Generate all archives
# Add below the years to be archived
for y in 2003 2004 2005; do
    for i in . lfs alfs blfs hlfs hints; do
		if [ -e "$i/news-$y.txt" ]; then
			# "LC_ALL=C" garantees a sane localization setting
        	LC_ALL=C $MANAGE_NEWS -a news/ -t $TEMPLATES/$i/archive-top.html -b $TEMPLATES/$i/archive-bottom.html -i $i/news-$y.txt
		fi
    done
done

#############################################
# Now assemble all newspages                #

# Website.html
cp $TEMPLATES/website-top.html website.html
$SVNLOG $SVN/www 2>/dev/null | $SVN2HTML --with-branchname >> website.html
cat $TEMPLATES/website-bottom.html >> website.html 

# Patches/news.html
cd $NEWSITE/patches
cp $TEMPLATES/patches/news-top.html news.html
$SVNLOG $SVN/patches 2>/dev/null | $SVN2HTML --with-branchname >> news.html
cat $TEMPLATES/patches/news-bottom.html >> news.html

# LFS/news.html
cd $NEWSITE/lfs
cp $TEMPLATES/lfs/news-top.html news.html
$MANAGE_NEWS -i news-$YEAR.txt >> news.html
$MANAGE_NEWS -i ../news-$YEAR.txt >> news.html
$SVNLOG $SVN/LFS 2>/dev/null | $SVN2HTML --with-branchname >> news.html
cat $TEMPLATES/lfs/news-bottom.html >> news.html 

# BLFS/news.html
cd $NEWSITE/blfs
cp $TEMPLATES/blfs/news-top.html news.html
$MANAGE_NEWS -i news-$YEAR.txt >> news.html
$MANAGE_NEWS -i ../news-$YEAR.txt >> news.html
$SVNLOG $SVN/BLFS 2>/dev/null | $SVN2HTML --with-branchname >> news.html
cat $TEMPLATES/blfs/news-bottom.html >> news.html 

# Hints/news.html
cd $NEWSITE/hints
cp $TEMPLATES/hints/news-top.html news.html
$SVNLOG $SVN/hints 2>/dev/null | $SVN2HTML --with-branchname >> news.html
cat $TEMPLATES/hints/news-bottom.html >> news.html

# ALFS/news.html
cd $NEWSITE/alfs
cp $TEMPLATES/alfs/news-top.html news.html
$MANAGE_NEWS -i news-$YEAR.txt >> news.html
$MANAGE_NEWS -i ../news-$YEAR.txt >> news.html
$SVN2HTML2 --project ALFS --with-branchname >> news.html
cat $TEMPLATES/alfs/news-bottom.html >> news.html

# HLFS/news.html
cd $NEWSITE/hlfs
cp $TEMPLATES/hlfs/news-top.html news.html
$MANAGE_NEWS -i news-$YEAR.txt >> news.html
$MANAGE_NEWS -i ../news-$YEAR.txt >> news.html
$SVNLOG $SVN/HLFS 2>/dev/null | $SVN2HTML --with-branchname >> news.html
cat $TEMPLATES/hlfs/news-bottom.html >> news.html

##########################################
# Generate all RSS feeds                 #

cd $NEWSITE/lfs
$SCRIPTS/lfs2rss.pl -n news.html -r feed.rss
cd $NEWSITE/blfs
$SCRIPTS/lfs2rss.pl -n news.html -r feed.rss
cd $NEWSITE/alfs
$SCRIPTS/lfs2rss.pl -n news.html -r feed.rss
cd $NEWSITE/hlfs
$SCRIPTS/lfs2rss.pl -n news.html -r feed.rss

##########################################
# Create mirrors list for each section   #

cd $NEWSITE
cat $TEMPLATES/index-top.html > index.html
cat mirrorlist.html >> index.html
cat $TEMPLATES/index-bottom.html >> index.html
for i in alfs hlfs blfs hints lfs patches; do
    cat $TEMPLATES/$i/index-top.html > $i/index.html
    sed "s@/lfs/news.html@/$i/news.html@g" mirrorlist.html >> $i/index.html
    cat $TEMPLATES/$i/index-bottom.html >> $i/index.html
done
rm mirrorlist.html

# Copy these to the httpd error dirs
cp $NEWSITE/{403,404}.html /home/httpd/error/

#######################################################
# Add all other content 

# Fetch latest versions from patches 
svn -q export $SVN/patches/trunk $NEWSITE/patches/downloads

# Fetch current hints and create the hints tarball
mkdir -p $NEWSITE/hints/downloads/
cd $NEWSITE/hints/downloads/ && svn -q export $SVN/hints/trunk hints
tar cjf hints.tar.bz2 hints/
mv hints/ATTACHMENTS attachments
mv hints/ files/
# Generate list.html
cd $NEWSITE/hints/downloads/files &&
test -d HOLD && for i in $(ls HOLD/*.txt); do ln -sf $i; done
cd $NEWSITE/hints
$SCRIPTS/genlist.sh
cd $NEWSITE/hints/downloads/files &&
for i in $(ls PREVIOUS_FORMAT/*.txt); do ln -sf $i; done

# Remove all templates
rm -rf $TEMPLATES

# Validate all XHTML files 

for file in $(find $NEWSITE/ -name \*.html -maxdepth 3 -print); do
	xmllint --valid --noout $file
	if [ $? != 0 ]; then
		# Something is wrong, going to clean up our files in /tmp
		# so you can re-run the script after you fix the errors
		rm -r $TMPDIR $LOCKFILE
		exit 1
	fi
done

export TARGETDIR="/home/httpd/www.linuxfromscratch.org"
mkdir -p $TARGETDIR
cd $NEWSITE
cp -a $TARGETDIR/lfs/{view,downloads} lfs/
cp -a $TARGETDIR/blfs/{view,artwork,edguide,downloads} blfs/
cp -a $TARGETDIR/alfs/{downloads,view} alfs/
cp -a $TARGETDIR/patches/{lfs,blfs} patches/
cp -a $TARGETDIR/hlfs/{downloads,view} hlfs/

#################################################
# Everything is in place now, so replace TARGETDIR with NEWSITE

cd /var/tmp
tar cPf website-backup.tar $TARGETDIR && rm -rf $TARGETDIR

mkdir -p $TARGETDIR
tar -cC $NEWSITE . | tar -xC $TARGETDIR
rm -rf $TMPDIR

# Add the timestamp for mirrors
mkdir -p $TARGETDIR/timestamp
date +%s > $TARGETDIR/timestamp/timestamp

# Add some compatibility symlinks
cd $TARGETDIR
ln -sf lfs/view
ln -sf images/favicon.ico
ln -sf 2columns.css style/screen.css

# Set proper permissions
chmod -Rf g+w $TARGETDIR
chgrp -Rf lfswww $TARGETDIR

# Remove lock file
rm -f $LOCKFILE
