#!/bin/bash

USER=user
SERVER=host.example.com

LIVE_DIR=path/to/example.com/
DEV_DIR=path/to/dev.example.com/

CONTENT=content/

ERROR="Error. Please make sure you've indicated correct parameters"

if [ $# -eq 0 ]
then
    echo $ERROR;
    exit 1    
elif [ $1 == "dev" ]
then
    DIR=$DEV_DIR
elif [ $1 == "live" ]
then
    DIR=$LIVE_DIR
else
    echo $ERROR
    exit 1
fi

if [[ -z $2 ]]
then
    echo "Running App Sync (dry-run)"
    rsync --dry-run -az --force --delete --progress --exclude-from=.rsyncignore ./ $USER@$SERVER:$DIR;
elif [[ $2 == "ssh" ]]
then
    echo "entering server"
    ssh -t $USER@$SERVER "cd $LIVE_DIR; bash --login";
elif [[ $2 == "go" ]]
then
    echo "Running App Sync"
    rsync -az --force --delete --progress --exclude-from=.rsyncignore ./ $USER@$SERVER:$DIR;
elif [[ $2 == "content" ]]
then
    if [[ -z $3 ]]
    then
        echo "Fetching Content (dry-run)"
        rsync --dry-run -azv --force --delete --progress --exclude-from=.rsyncignore $USER@$SERVER:$DIR$CONTENT ./$CONTENT;
        echo "done"
    elif [[ $3 == "go" ]]
    then
        echo "Fetching Content"
        rsync -azv --force --delete --progress --exclude-from=.rsyncignore $USER@$SERVER:$DIR$CONTENT ./$CONTENT;
        echo "done"
    elif [[ $3 == "up" ]]
    then
        if [[ -z $4 ]]
        then
            echo "Uploading Content (dry-run)"
            rsync --dry-run -azv --progress --exclude-from=.rsyncignore ./$CONTENT $USER@$SERVER:$DIR$CONTENT;
            echo "done"
        elif [[ $4 == "go" ]]
        then
            echo "Uploading Content"
            rsync -azv --progress --exclude-from=.rsyncignore ./$CONTENT $USER@$SERVER:$DIR$CONTENT;
            echo "done"
        elif [[ $4 == "del" ]]
        then
            if [[ -z $5 ]]
            then
                echo "Uploading Content, deleting remotes that don't occur locally (dry-run)"
                rsync --dry-run -azv --progress --delete --exclude-from=.rsyncignore ./$CONTENT $USER@$SERVER:$DIR$CONTENT;
                echo "done"
            elif [[ $5 == "go" ]]
            then
                echo "Uploading Content, deleting remotes that don't occur locally"
                rsync -azv --progress --delete --exclude-from=.rsyncignore ./$CONTENT $USER@$SERVER:$DIR$CONTENT;
                echo "done"
            fi
        fi
    fi
fi