#!/bin/bash

set -e

cd $(dirname $0)
if [[ -f conf/ec2.yml && "$(python bin/is_aws_spot.py)" == "0" ]]; then
  python bin/spot_request.py --cancel
else
  vagrant destroy -f
fi

if grep -Fq "# Generated by Alluxio vagrant deploy script" /etc/hosts
then
  echo "Remove Alluxio IP Alias from /etc/hosts..."
  sudo "$BASH" -c "sed -i '/^# Generated by Alluxio vagrant deploy script/d' /etc/hosts"
  sudo "$BASH" -c "sed -i '/^Alluxio.*$/d' /etc/hosts"
fi

ALLUXIO_BOX=$(vagrant box list | grep alluxio-dev | cut -d ' ' -f1)
if [[ "$ALLUXIO_BOX" != '' ]]; then
  echo "Vagrant VMs are removed sucesfully."
  echo "If you want to remove base vm image $ALLUXIO_BOX, please run: vagrant box remove $ALLUXIO_BOX"
fi

