# Sanity checks.
if [ ! -e ref.pdf ]; then
	echo "'ref.pdf' missing.  Run 'make release', not this script directly."
	exit 1
fi

if [ ! -d ref.html ]; then
	echo "'ref.html' not a directory.  Run 'make release', not this script directly."
	exit 1
fi

# Which version of HTCondor are we releasing for?
VERSION=`grep set\(VERSION ../CMakeLists.txt | awk '{print $2}' \
	| sed -e's/\"//g' | sed -e's/)//g'`
VERSION_STRING=V`echo ${VERSION} | sed -e's/\./_/g'`

# Miscellaneous constants
# ROOT=/tmp/install-test
ROOT=/p/condor/public/html/htcondor/manual
INSTALL_DIR=${ROOT}/v${VERSION}
STAGING_DIR=${INSTALL_DIR}.new

mkdir -p ${INSTALL_DIR}
mkdir -p ${STAGING_DIR}

# Stage the PDF.
cp ref.pdf ${STAGING_DIR}/condor-${VERSION_STRING}-Manual.pdf

# Patch the HTML.  We do this to a temporary so that we don't patch the user's
# HTML more than once.  (Patching may not be necessary with htlatex.)
cp -a ref.html condor-${VERSION_STRING}-Manual; sync
cd condor-${VERSION_STRING}-Manual
perl -p -i -e's,\<HEAD\>,<HEAD>\n<link rel="canonical" href="http://research.cs.wisc.edu/htcondor/manual/current/$ARGV">\n,' condor_*.html
cd ..

# Create HTML tarballs for download.
tar -z -c -f condor-${VERSION_STRING}-Manual.tar.gz condor-${VERSION_STRING}-Manual
tar -j -c -f condor-${VERSION_STRING}-Manual.tar.bz2 condor-${VERSION_STRING}-Manual

# Stage the HTML and its tarballs.
mv condor-${VERSION_STRING}-Manual/* ${STAGING_DIR}
mv condor-${VERSION_STRING}-Manual.tar.gz ${STAGING_DIR}
mv condor-${VERSION_STRING}-Manual.tar.bz2 ${STAGING_DIR}
rmdir condor-${VERSION_STRING}-Manual

# Install the new manual after backing up the old one.
if [ -e ${INSTALL_DIR}.old ]; then
	rm -fr ${INSTALL_DIR}.old
fi
mv -f ${INSTALL_DIR} ${INSTALL_DIR}.old
mv -f ${STAGING_DIR} ${INSTALL_DIR}

# Success.
exit 0
