#!/bin/sh
##############################################################
# Install or merge in the SAGE documention.
##############################################################

CUR=`pwd`

DEVEL="$SAGE_ROOT"/devel

if [ ! -d $DEVEL ]; then
    mkdir $DEVEL
fi

if [ $? -ne 0 ]; then
  exit 1
fi

TARGET="$DEVEL"/doc-main

if [ ! -d $TARGET ]; then
    # Brand new install -- we just copy everything there.
    mkdir "$TARGET"
    cd "$CUR"
    cp -r * .hg* $TARGET/
    if [ $? -ne 0 ]; then
        echo "Error copying in new docs."
        exit 1
    fi
else
    # There is an existing doc directory. We do a merge. 
    cd "$TARGET"
    hg ci -m "Check-in during upgrade of Sage."
    hg pull "$CUR"
    hg merge tip
    hg ci -m "Check-in during upgrade of Sage."
    hg update
    if [ $? -ne 0 ]; then
        echo "Error pulling in new docs."
        exit 1
    fi

    # We also copy over the ref/sage.* files, since they
    # aren't in the repo, and their existence is what determines
    # ref generation.   (They change constantly, and are autogenerate
    # so should not be in the repo.)
    cd "$TARGET"
    cp "$CUR"/ref/sage.*tex ref/
fi

cd "$DEVEL"
rm -f doc
ln -sf doc-main doc

cd "$SAGE_ROOT"
rm doc 2>/dev/null
ln -sf devel/doc/html doc
