#!/usr/bin/env bash
<%
# NOTE for cookbook developer: If you edit this script, you must update opt/dcos-grid/cloud-config.yaml
-%>

PATH=.:/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin:`dirname $0`

help() {
  echo 'Usage: node_setup.sh {master,slave,slave_public}'
}

ARGS="$@"
ROLE=$1

# Validation
if [ $# != 1 ]; then
  help
  exit 1
elif [ $ROLE = '-h' ]; then
  help
  exit 0
fi

case "$ROLE" in
  'master' | 'slave' | 'slave_public' )
    echo "Node Role: $ROLE"
    ;;
  * )
    help
    exit 1
    ;;
esac

if [ -d /opt/mesosphere ]; then
  echo -e '[ERROR] DC/OS is already installed.'
  exit 1
fi

user=`whoami`
sudo='sudo'
if [ $user = 'root' ]; then
  sudo=''
fi

if [ ! -d /tmp/dcos ]; then
  mkdir /tmp/dcos
fi
cd /tmp/dcos
echo 'Downloading dcos_install.sh ...'
curl -O <%= node['dcos-grid']['bootstrap']['config']['bootstrap_url'] %>/dcos_install.sh
echo "Installing DC/OS $ROLE node ..."
$sudo bash dcos_install.sh $ROLE

