#!/bin/bash

# Simple script to download binary releases, update, create, and upload source
# releases, generate deployment manifests and deploy releases to bosh-lite.
#
# We assume you have already cloned the necessary releases into ~/workspace.

set -eu

error() {
    echo "[ERROR]: $1" 2>&1
    exit 1
}

create_bosh_env() {
  ssh-keygen -R 192.168.50.6
  rm -f ~/workspace/cf-networking-deployments/environments/local/state.json
  bosh create-env ~/workspace/bosh-deployment/bosh.yml \
    --state ~/workspace/cf-networking-deployments/environments/local/state.json \
    -o ~/workspace/bosh-deployment/virtualbox/cpi.yml \
    -o ~/workspace/bosh-deployment/virtualbox/outbound-network.yml \
    -o ~/workspace/bosh-deployment/bosh-lite.yml \
    -o ~/workspace/bosh-deployment/bosh-lite-runc.yml \
    -o ~/workspace/bosh-deployment/jumpbox-user.yml \
    --vars-store ~/workspace/cf-networking-deployments/environments/local/creds.yml \
    -v director_name="Bosh Lite Director" \
    -v internal_ip=192.168.50.6 \
    -v internal_gw=192.168.50.1 \
    -v internal_cidr=192.168.50.0/24 \
    -v outbound_network_name="NatNetwork"
}

set_bosh_env() {
  bosh -e 192.168.50.6 --ca-cert <(bosh int ~/workspace/cf-networking-deployments/environments/local/creds.yml --path /director_ssl/ca) alias-env vbox
  export BOSH_CLIENT="admin"
  export BOSH_CLIENT_SECRET="$(bosh int ~/workspace/cf-networking-deployments/environments/local/creds.yml --path /admin_password)"
  export BOSH_ENVIRONMENT="vbox"
  export BOSH_DEPLOYMENT="cf"
  export BOSH_CA_CERT="/tmp/bosh-lite-ca-cert"
  bosh int ~/workspace/cf-networking-deployments/environments/local/creds.yml --path /director_ssl/ca > ${BOSH_CA_CERT}
}

upload_bosh_stemcell() {
  STEMCELL_VERSION="$(bosh int ~/workspace/cf-deployment/cf-deployment.yml --path=/stemcells/0/version)"
  echo "will upload stemcell ${STEMCELL_VERSION}"
  bosh -e vbox upload-stemcell "https://bosh.io/d/stemcells/bosh-warden-boshlite-ubuntu-trusty-go_agent?v=${STEMCELL_VERSION}"
}

upload_cloud_config() {
  bosh -e vbox -n update-cloud-config ~/workspace/cf-deployment/iaas-support/bosh-lite/cloud-config.yml
}

deploy_cf() {
  bosh deploy --no-redact -n ~/workspace/cf-deployment/cf-deployment.yml \
  -o ~/workspace/cf-networking-release/manifest-generation/opsfiles/cf-networking.yml \
  -o ~/workspace/cf-networking-release/manifest-generation/opsfiles/use-latest.yml \
  -o ~/workspace/cf-deployment/operations/bosh-lite.yml \
  -o ~/workspace/cf-networking-deployments/environments/local/instance-count-overrides.yml \
  --vars-store ~/workspace/cf-networking-deployments/environments/local/deployment-vars.yml \
  -v system_domain=bosh-lite.com
}

upload_local_release() {
    bosh create-release --force
    bosh upload-release
}

main() {
    if [ "$(uname)" == "Darwin" ]; then
      sudo route add -net "$ips" "$gw"
    fi
    create_bosh_env
    set_bosh_env

    if [ "$(uname)" == "Linux" ]; then
      sudo route add -net "$ips" gw "$gw"
    fi

    if [ "$upload_stemcell" == true ]; then
      upload_bosh_stemcell
    fi

    upload_cloud_config

    upload_local_release

    deploy_cf
}

ips="10.244.0.0/16"
gw="192.168.50.6"
upload_stemcell=true

main
