#!/bin/bash

set -eu
set -o pipefail

trap "echo Exited!; exit 1;" SIGINT SIGTERM

collocate_with_doppler=false
while getopts ":c" opt; do
  case $opt in
    c)
      collocate_with_doppler=true
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      ;;
  esac
done

PROJECT_DIR="$(cd "$(dirname "$0")/.."; pwd)"
cd $PROJECT_DIR

tmp_dir=`mktemp -d`
trap "rm -r $tmp_dir" EXIT

# standalone LogCache consumes from Loggregator. Replace it with CF.
cat <<EOF > $tmp_dir/consumes_cf.yml
- type: replace
  path: /instance_groups/name=log-cache/jobs/name=log-cache-nozzle/consumes/reverse_log_proxy/deployment?
  value: cf
EOF

bosh int manifests/log-cache.yml --ops-file=$tmp_dir/consumes_cf.yml --ops-file=scripts/operations/cf-auth-proxy.yml > $tmp_dir/adjusted.yml

# save the instance_groups
instance_groups_log_cache=`bosh int $tmp_dir/adjusted.yml --path /instance_groups/name=log-cache | sed 's/^/    /'`
jobs_scheduler=`bosh int $tmp_dir/adjusted.yml --path /instance_groups/name=log-cache-scheduler/jobs/0 | sed 's/^/    /'`

# save the variables
for v in $(bosh int $tmp_dir/adjusted.yml --path /variables | awk '/^. name: / {print $NF}')
do
cat <<EOF >> $tmp_dir/vars.yml
- type: replace
  path: /variables/-
  value:
$(bosh int $tmp_dir/adjusted.yml --path /variables/name=$v | sed 's/^/    /')
EOF
done

for j in $(bosh int $tmp_dir/adjusted.yml --path /instance_groups/name=log-cache/jobs | awk '/^. name: / {print $NF}')
do
cat <<EOF >> $tmp_dir/jobs.yml
- type: replace
  path: /instance_groups/name=doppler/jobs/-
  value:
$(bosh int $tmp_dir/adjusted.yml --path /instance_groups/name=log-cache/jobs/name=$j | sed 's/^/    /')
EOF
done

if [ "$collocate_with_doppler" = true ]; then
# write jobs to the doppler instance group
cat <<EOF > manifests/operations/deploy-in-cf-doppler-collocated.yml
# NOTE: This is generated code
# The following operations file has been generated by the
# scripts/generate_deploy_in_cf_ops script.
# It is generated from the standalone manifest (manifests/log-cache.yml) and
# altered to fit into the CF-Deployment manifest.

- type: replace
  path: /releases/-
  value:
    name: log-cache
    version: latest
$(cat $tmp_dir/jobs.yml)
$(cat $tmp_dir/vars.yml)

- type: replace
  path: /instance_groups/name=scheduler/jobs/name=log-cache-scheduler?
  value:
$jobs_scheduler
EOF
else
# write instance_groups and variables to new ops-file
cat <<EOF > manifests/operations/deploy-in-cf.yml
# NOTE: This is generated code
# The following operations file has been generated by the
# scripts/generate_deploy_in_cf_ops script.
# It is generated from the standalone manifest (manifests/log-cache.yml) and
# altered to fit into the CF-Deployment manifest.

- type: replace
  path: /releases/-
  value:
    name: log-cache
    version: latest
- type: replace
  path: /instance_groups/-
  value:
$instance_groups_log_cache

- type: replace
  path: /instance_groups/name=scheduler/jobs/name=log-cache-scheduler?
  value:
$jobs_scheduler
$(cat $tmp_dir/vars.yml)
EOF
fi
