#!/bin/bash
# -----------------------------------------------------------------------------
# Remotely pushes bits to a remote node and installs ONOS on it.
# -----------------------------------------------------------------------------

function _usage () {
cat << _EOF_
usage:
 $(basename $0) [-fn] [-m] <settings> [node]

options:
- [node] : remote node to install ONOS on.

summary:
 Downloads Atomix bits to a remote node and installs Atomix on it.

 The -u should be used on upstart-based systems.

 If [node] is not specified the default target is \$OCI.

_EOF_
}

[ "$1" = "-h" ] && _usage && exit 0

[ ! -d "$ONOS_ROOT" ] && echo "ONOS_ROOT is not defined" >&2 && exit 1
. $ONOS_ROOT/tools/build/envDefaults

while getopts fnvm: o; do
    case "$o" in
        f) uninstall=true;;
        n) nostart=true;;
    esac
done
let OPC=$OPTIND-1
shift $OPC

# If the -f was given, attempt uninstall first.
[ -n "$uninstall" ] && atomix-uninstall ${1:-$OCI}

node=${1:-$OCI}
remote=$ONOS_USER@$node

$(dirname $0)/atomix-push-bits $node

ssh -tt $remote "
    [ -f $ATOMIX_INSTALL_DIR/bin/atomix-agent ] && echo \"Atomix is already installed\" && exit 1

    sudo mkdir -p $ATOMIX_INSTALL_DIR && sudo chown ${ONOS_USER}:${ONOS_GROUP} $ATOMIX_INSTALL_DIR
    tar -xvf /tmp/atomix.tar.gz -C $ATOMIX_INSTALL_DIR
"

# Configure the ONOS installation
atomix-config $node

# Upload the shared cluster key if present
[ -f "$ONOS_CLUSTER_KEY_FILE" ] && onos-push-cluster-key $1

# Unless -n option was given, attempt to ignite the ONOS service.
[ -z "$nostart" ] && atomix-service $node start || true