#!/usr/bin/env bash
set -euox pipefail

# use the same version with the driving repo's black validator
pip install black==19.3b0

project_name=$(basename $PWD)
code_path=${PWD}/${project_name}

# make temp folder
temp_dir=$(mktemp -d)

cp ${code_path}/api/api.yml $temp_dir

# generate
model_dir_name="swagger_models"

# delete the models
model_path=${code_path}/${model_dir_name}
rm -rf ${model_path}
cp -r ${code_path}/../../tools/openapi-generator/templates ${temp_dir}
docker run --rm -v ${temp_dir}:/local openapitools/openapi-generator-cli generate -i /local/api.yml \
-g python-flask --package-name $project_name -o /local/$project_name --model-package $model_dir_name -t /local/templates

tmp_model_path=${temp_dir}/${project_name}/${project_name}/${model_dir_name}

# copy the models back
cp -r ${tmp_model_path} ${code_path}

# post-processing, use line-length = 80 to align with driving repo python black validator
black -l 80 ${code_path}/${model_dir_name}
