#!/usr/bin/env bash

set -euox pipefail

current_directory="$(pwd)"
result_location="/tmp/result"
cleanup() {
# Shutdown the swagger generator image
docker stop $CID && docker rm $CID
}
trap cleanup EXIT

generate(){
  # ---------------------- Begin Swagger Documentation Recommeneded workflow
  # Start container and save the container id
  CID=$(docker run -d --network='host' -p 8188:8080 swaggerapi/swagger-generator)
  # allow for startup
  sleep 5
  # Get the IP of the running container
  GEN_IP=$(hostname -I | awk '{print $1}')
  # Execute an HTTP request and store the download link
  RESULT=$(curl --fail -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{
    "options": { "modelPropertyNaming": "original"},
    "swaggerUrl": '\""${API_JSON_URL}"\"'
  }' ''"http://${GEN_IP}:8080/api/gen/clients/python"'' | jq '.link' | tr -d '"')
  # Download the generated zip and redirect to a file
  RESULT=$(echo ${RESULT} | sed  -e 's/https:\/\/generator.swaggerhub.com\/api\/swagger.json/http:\/\/'${GEN_IP}':8080/g')
  curl --fail $RESULT > /tmp/result.zip
  # ---------------------- End Swagger Documentation Recommeneded workflow
  unzip -o /tmp/result -d /tmp/unpacked
  rsync -a --ignore-times /tmp/unpacked/python-client/ ./
  rm -rf health_api_client
  mv swagger_client health_api_client
}

rename_validator_conflicts(){
  # rename conflicts
  rm -rf tests/ git_push.sh .swagger-codegen-ignore .travis.yml .swagger-codegen/
  rsync -a --ignore-times ./test/ tests/
  find . -name "platform.py" -exec rename -vf 's/platform.py/platform_.py/i' {} \;
  find . -name "token.py" -exec rename -vf 's/token.py/token_.py/i' {} \;

  # NOTE(@price) bug in grep that doesn't match __init__ ??
  # https://bugs.launchpad.net/bzr-grep/+bug/667443
  grep -rl "swagger_client.models.platform" \
    ./**/*.py health_api_client/models/__init__.py \
    health_api_client/__init__.py \
    health_api_client/api/__init__.py \
    health_api_client/api/default_api.py \
    | xargs sed -i 's/from swagger_client.models.platform /from swagger_client.models.platform_ /g'

  grep -rl "swagger_client.models.token" \
    ./**/*.py health_api_client/models/__init__.py \
      health_api_client/__init__.py \
      health_api_client/api/__init__.py \
      health_api_client/api/default_api.py \
      | xargs sed -i 's/from swagger_client.models.token /from swagger_client.models.token_ /g'
  grep -rl "swagger_client" \
    ./**/*.py health_api_client/models/__init__.py \
    health_api_client/__init__.py \
    health_api_client/api/__init__.py \
    health_api_client/api/default_api.py \
    | xargs sed -i 's/swagger_client/health_api_client/g'

  grep -rl "swagger_client" ./docs ./health_api_client README.md \
  | xargs sed -i 's/swagger_client/health_api_client/g'
  grep -rl "swagger-client" README.md | xargs sed -i 's/swagger-client/health_api_client/g'
  # cleanup
  rm -r test/
}

add_ci_data(){
  # remove trailing whitespace
  sed --in-place 's/[[:space:]]\+$//' ./**/*.md README.md

  # add ci variables
  sed -i '2s/^/__QUALIFIER__ = ""\n/' setup.py
  sed -i '/^VERSION = "1.0.0"/ s/$/ + __QUALIFIER__/' setup.py
  sed -i 's/swagger-client/health_api_client/g' setup.py
  sed -i 's/py27, py3/py36, py36-{sdist,wheel}/g' tox.ini
  echo "
[testenv:py36-sdist]
commands=python setup.py sdist

[testenv:py36-wheel]
commands=python setup.py bdist_wheel" >> tox.ini

  echo "[wheel]
universal = 1
dist-dir = dist" > setup.cfg
}

format(){
  black . --line-length=80
}

generate
rename_validator_conflicts
add_ci_data
format
