#!/bin/bash
# ===========================================================================
# Copyright (c) 2015, 2021 Eclipse Foundation and others.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html
#
# Contributors:
#    Christopher Guindon (Eclipse Foundation)
# ===========================================================================

LOCKFILE=/tmp/update-files-update
if [ ! -f $LOCKFILE ]; then
    touch $LOCKFILE;
    if [ $# -ne 0 ]; then

        # accounts and api (same directory for both sites)
        if [ $1 = "api" ]; then
            LOCAL_FILES_DIRECTORY="api.eclipse.org"
            REMOTE_HOST="api-vm1"
            REMOTE_FILES_DIRECTORY="/localsite/api.eclipse.org/sites/default"
        fi

        # Make sure the user specified a valid web site to build.
        if [ -z ${REMOTE_HOST+x} ]; then
            echo "You must enter a valid site name in order for this command to work.  Try viewing the contents of this file with an editor"
            exit
        fi

        # Create new files...
        echo "Compressing $REMOTE_FILES_DIRECTORY on $REMOTE_HOST server..."
        COMMAND="rm ~/webdev/files/$LOCAL_FILES_DIRECTORY-files.tar.gz 2>/dev/null; tar -czf ~/webdev/files/$LOCAL_FILES_DIRECTORY-files.tar.gz -C $REMOTE_FILES_DIRECTORY files"
        ssh $REMOTE_HOST $COMMAND

    else
        echo "USAGE: ./sync-drupal-files.sh SITENAME"
    fi
else
    echo "Another job is running.  Check the existence of $LOCKFILE"
    exit
fi

rm $LOCKFILE