#!/usr/bin/env bash
#
# Copyright (c) Cognitect, Inc.
# All rights reserved.

if [ -f /opt/datomic.app.deploy/bin/export-environment ]; then
  . /opt/datomic.app.deploy/bin/export-environment
fi

SHUTDOWN_PORT=${DATOMIC_SHUTDOWN_PORT:-8190}

cd $(dirname $(realpath $0))/..

. $(dirname $0)/common-aws-functions

set +e
start_sec=$(/bin/date +%s.%N)
msg "Started $(basename $0) at $(/bin/date "+%F %T")"

nc --version 2>/dev/null
if [ $? -ne 0 ] ; then
  # Old version, -w is always seconds
  echo "prepare-for-shutdown" | nc -w 120 localhost ${SHUTDOWN_PORT}
else
  # New version, specify -w time and -i
  echo "prepare-for-shutdown" | nc -w 120s -i 120s localhost ${SHUTDOWN_PORT}
fi

finish_msg
exit 0