#!/bin/sh

# This script publishes the CCSL website. It takes the current version from
# the SVN repository, initializes, and then makes it available on the
# webserver. 
#
# The publication is performed by the "run" function.


# The machine the website runs on. Note that we compare this to the value of
# `hostname`, so this should be the unqualified name.
WEB_HOST="dante"

# The URL for the svn repository
SVN_URL="https://tagore/svn/ccsl_website"

# The temporary location to check the repository out to, and perform any
# necessary updates
STAGING="/tmp/ccsl_website"


# The directory that the website lives in
TARGET="/var/www/ccsl/test"


# Display help text
help () 
{
    cat <<HELP_END
$0 [-n]
Publishes the CCSL website. If you are locally logged in to $WEB_HOST, 
this will perform the copy and install, otherwise you will automatically be 
SSH'd there. Arguments:

 -n    Run the publish script as if an actual publication was to occur, but 
       don't modify the existing site. Used to check for bugs. 
HELP_END
}


# Do the publish. 
run() 
{
    # Assume we're already on dante
    h="dante"
    #if [[ $h == $WEB_HOST ]]
    if [  ]
    then
        cat <<ERROR_END
Must be logged into $WEB_HOST to run $0. Please ssh to $WEB_HOST and run 
it there. You are currently logged into $h.
ERROR_END
        exit
    fi
    
    # Check out the site
    if [ -e $STAGING ] 
    then
        cat <<ERROR_END
The staging directory $STAGING already exists on `hostname`. 
Delete it and run again.
ERROR_END
        exit
    else 
        svn co $SVN_URL $STAGING
    fi

    # Run the site initialization script. 
    AUTO=${STAGING}/www/static/auto/
    mkdir $AUTO
    cd ${STAGING}

    sh bin/initialize_site ${AUTO}

    cd -

    # Nuke the old directory
    rm -rf $TARGET

    # Perform the publish step
    mv -f ${STAGING}/www $TARGET

    # Do cleanup
    rm -rf $STAGING
}

case $1 in 
    "--help"    )  help cat;;
    ''          )  run;;
esac

