#!/bin/bash

set -e

templates=$(dirname $0)/../templates

infrastructure=$1
cf_release=$2

shift
shift

function usage() {
  echo "    Usage:
    $0 <aws|bosh-lite> /path/to/cf-release [stubs...]

    Ex:
    $0 aws \\
      ~/workspace/cf-release \\
      ~/workspace/deployments-aws/ketchup/cf-aws-stub.yml \\
      ~/workspace/deployments-aws/ketchup/cf-shared-secrets.yml
"
  exit 1
}

if [ "$infrastructure" != "aws" ] && [ "$infrastructure" != "bosh-lite" ]; then
  usage
fi

if [ -z "$cf_release" ]; then
  usage
fi

shared_stub=/tmp/cf-diego-shared.yml
cf_infrastructure=$infrastructure
if [ "$infrastructure" == "bosh-lite" ]; then
  cf_infrastructure="warden"
fi

spiff merge \
  $templates/cf-shared.yml \
  $templates/diego-cf-stub.yml \
  $cf_release/templates/cf-properties.yml \
  $cf_release/templates/cf-infrastructure-${cf_infrastructure}.yml \
  "$@" \
  > $shared_stub

spiff merge \
  $templates/deployment.yml \
  $templates/jobs.yml \
  $templates/infrastructure-${infrastructure}.yml \
  $shared_stub \
  "$@"
