#!/bin/bash

set -e
. "$(dirname "$0")"/vars.sh

export BORG_PASSCOMMAND="cat ${BORG_DIR}/passphrase"
export BORG_BASE_DIR=${BORG_DIR}
export BORG_CACHE_DIR=${BORG_DIR}/cache
export BORG_CONFIG_DIR=${BORG_DIR}/config
if [ "$1" = "--rw" ] ; then
    if [ "$BORG_RW_KEY_ADDED" != "1" ] ; then
        echo "=== Need SSH key passphrase.  Check Bitwarden for:"
        echo "===   borg $HOSTNAME / read-write SSH key"
    fi
    export BORG_RSH="ssh -F $SSH/config -o BatchMode=no -o PreferredAuthentication=publickey -i $SSH/id_ecdsa"
    shift
else
    export BORG_RSH="ssh -F $SSH/config -i $SSH/id_ecdsa_appendonly"
fi

exec "${BORG_BIN}" "$@"
