#!/bin/sh

# if RSYNC_USER or RSYNC_PASS is not set, exit
if [ -z "${RSYNC_USER}" ] || [ -z "${RSYNC_PASS}" ] || [ -z "${RSYNC_HOST}" ] || [ -z "${RSYNC_PORT}" ]; then
    echo "RSYNC_ settings not complete, exiting"
    exit 1
fi

excludes=""
if [ "${BRANCH}" == "main" ]; then
    excludes='--exclude=src --exclude=*.map'
    echo "main deploy, excluding $excludes"
fi

# sync frontend
rsync -rlcgD --perms -i -u -v --stats --progress \
    --delete \
    -e "sshpass -p ${RSYNC_PASS} ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p ${RSYNC_PORT}" \
    $excludes \
    frontend/ \
    ${RSYNC_USER}@${RSYNC_HOST}:./frontend/ \

# sync api config
rsync -rlcgD --perms -i -u -v --stats --progress \
    --delete \
    -e "sshpass -p ${RSYNC_PASS} ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p ${RSYNC_PORT}" \
    api/ \
    ${RSYNC_USER}@${RSYNC_HOST}:./api/

# create media directory
mkdir media
chmod 770 media
rsync -rlcgD --perms -i -u -v --stats --progress \
    -e "sshpass -p ${RSYNC_PASS} ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p ${RSYNC_PORT}" \
    media \
    ${RSYNC_USER}@${RSYNC_HOST}:./

# tst