#!/bin/bash

if [ "$#" -lt 2 ]; then
    echo "Usage: heapdump <dump file prefix> <bearer token> <env :optional>"
    exit 1
fi

case ${3:-'prod'} in
    'sandbox' )
      env='http://core-management.sandbox.netfoundry.io' ;;
    'staging' )
      env='http://core-management.staging.netfoundry.io' ;;
    'prod' )
      env='http://core-management.production.netfoundry.io'
      ;;
esac


echo "Saving heapdumps from $env with prefix $1"

for i in {1..10}
do
  curl -H "Accept: application/octet-stream" -H "Authorization: Bearer $2" -o $1.$i $env/core/actuator/heapdump
done
