#!/usr/bin/env bash

set -e

# location of this script
script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

rm -r ${script_dir}/../libs/hdf5/api-client/src/lib/api/
rm -r ${script_dir}/../libs/hdf5/api-client/src/lib/model/

# Unfortunately, I updated the API spec to 3.1.0 without checking if this was supported by the generator, so we need to disable the validation
npx @openapitools/openapi-generator-cli generate \
    -g typescript-nestjs \
    -i ${script_dir}/openapi.yml \
    -o ${script_dir}/../libs/hdf5/api-client/src/lib/ \
    --skip-validate-spec

# Correct a bug in the generation code that caused mismatched variable names
sed -i 's/obj_uuid/objUuid/'  ${script_dir}/../libs/hdf5/api-client/src/lib/api/*.service.ts

echo ${script_dir}
sed -i 's/public defaultHeaders = new Map()/public defaultHeaders: Record<string,string>= {};/' ${script_dir}/../libs/hdf5/api-client/src/lib/api/*.service.ts

sed -i 's/let queryParameters = {};/let queryParameters: any = {};/' ${script_dir}/../libs/hdf5/api-client/src/lib/api/*.service.ts

# Format the files
nx format --projects hdf5-api-client
