#!/bin/bash

bail() {
    echo fail: $*
    exit 1
}

# test
if [[ "$1" = "-t" ]]; then
    prefix=echo
    shift
fi

[[ "$PUBLISH_HOST" != "" ]] && [[ "$publish_host" = "" ]] && publish_host="$PUBLISH_HOST"
[[ "$PUBLISH_DIR" != "" ]] && [[ "$publish_dir" = "" ]] && publish_dir="$PUBLISH_DIR"

orig_pwd="$PWD"
if [[ ! -f .publish ]]; then
    while ! [[ -f .publish ]]; do
        [[ "$PWD" = "/" ]] && bail "no .publish file found"
        cd ..
    done
    subdir="${orig_pwd#$PWD/}"
fi

source .publish

cd $orig_pwd
if [[ $# -eq 0 ]]; then
    $prefix rsync -aKv * "$publish_host":"${publish_dir}${subdir}"
else
    $prefix rsync -aKv "$@" "$publish_host":"${publish_dir}${subdir}"
fi
