#!/bin/bash

if [ "$#" -lt 2 ]; then
    echo "Usage: threaddump <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 thread dumps from $env with prefix $1"

for i in {1..10}
do
  curl -H "Accept: text/plain" -H "Authorization: Bearer $2" -o $1.$i ${env}/core/actuator/threaddump
done
