#!/usr/bin/env bash
set -e

# Default Values
MONIKER=${1:-"$DOCKER_IP"}
GREEN='\033[0;32m'
NO_COLOR='\033[0m'

confirm_stop() {
    if [ -z "$1" ]; then
        echo -e "Stop dock?"
        read -e -p "Type enter to Cancel, h for Help, y to Stop Instance: " RESPONSE
    fi

    if [ "$RESPONSE" == "h" ]; then print_help; fi
}

print_help() {
    echo "Stop dock - Help"
    echo
    echo "Description"
    echo "  This script uses the aws cli to stop an existing ec2 dock instance."
    echo "  Either the dock ip or the moniker must be provided"
    echo
    echo "Usage"
    echo "  $ stop-dock moniker|ip"
    echo

    exit 0
}

# Parse command line arguments in any order
while getopts 'h' flag; do    # if a character is followed by a colon, that argument is expected to have an argument.
  case "${flag}" in
    h) hflag='true';;
    *) error "Unexpected option ${flag}" ;;
  esac
done

# Help
if [ -n "$hflag" ] || [ "$RESPONSE" == "h" ]; then
    print_help
fi

# Look up IP from moniker
FOUND_MONIKER=false
for f in $HOME/.docker/*; do
    if [ -d $f ] && [ -f $f/connection_config.txt ]; then
      while read -r line; do declare $line; done < "$f/connection_config.txt"
      if [[ $DOCK_MONIKER = $MONIKER || $DOCK_IP = $MONIKER ]]; then
        FOUND_MONIKER=true
        break
      fi
    fi
done

if [ $FOUND_MONIKER = false ]; then
  echo "Can't find dock configuration for $MONIKER"
  exit -1
fi


get_instance_id() {
    aws ec2 describe-instances \
        --filters Name=private-ip-address,Values="$1" \
        --query 'Reservations[*].Instances[*].InstanceId' --output text
}

# stop dock
INSTANCE_ID=$(get_instance_id $DOCK_IP)

# Confirmation
confirm_stop
if [ "$RESPONSE" != "y" ] && [ "$RESPONSE" != "h" ]; then
    echo "Canceled"
    exit 0
fi

if [ -n "$INSTANCE_ID" ]; then
    aws ec2 stop-instances --instance-ids "${INSTANCE_ID}" --output text
fi