# Path to this plugin
DRIVING_DIR=$(readlink -f "${PWD}/../../../../..")
PROTOC_GEN_TS_PATH="${DRIVING_DIR}/vision/data/tools/3d_annotation_frontend/node_modules/.bin/protoc-gen-ts"

rm -f ../../3d_annotation_frontend/src/api_clients/*.js
rm -f ../../3d_annotation_frontend/src/api_clients/*.d.ts

protoc \
    --proto_path="${DRIVING_DIR}" \
    --plugin="protoc-gen-ts=${PROTOC_GEN_TS_PATH}" \
    --js_out="import_style=commonjs,binary:${DRIVING_DIR}" \
    --ts_out="${DRIVING_DIR}" \
    vision/data/tools/annotation_3d_service/proto/common.proto \
    vision/data/tools/annotation_3d_service/proto/api_v0.proto \
    vision/data/tools/annotation_3d_service/proto/task.proto \
    vision/data/tools/annotation_3d_service/proto/labeling.proto \
    vision/data/tools/annotation_3d_service/proto/input_output.proto \
    vision/data/proto/oriented_bounding_box.proto \
    vision/data/tools/annotation_3d_service/proto/performance_tracking.proto \

# Move all generated files into current dir
find ../../../../data/proto -name "*pb.js" -exec mv '{}' ./ \;
find ../../../../data/proto -name "*pb.d.ts" -exec mv '{}' ./ \;

# Replace import path in generated files
find . -name "*.js" -execdir sed -i 's/..\/..\/..\/..\/..\/vision\/data\/tools\/annotation_3d_service\/proto/./g' {} \;
find . -name "*.ts" -execdir sed -i 's/..\/..\/..\/..\/..\/vision\/data\/tools\/annotation_3d_service\/proto/./g' {} \;
find . -name "*.js" -execdir sed -i 's/..\/..\/..\/..\/..\/vision\/data\/proto/./g' {} \;
find . -name "*.ts" -execdir sed -i 's/..\/..\/..\/..\/..\/vision\/data\/proto/./g' {} \;

echo '/* eslint-disable */' | cat - api_v0_pb.d.ts > temp && mv temp api_v0_pb.d.ts
echo '/* eslint-disable */' | cat - api_v0_pb.js > temp && mv temp api_v0_pb.js
echo '/* eslint-disable */' | cat - common_pb.d.ts > temp && mv temp common_pb.d.ts
echo '/* eslint-disable */' | cat - common_pb.js > temp && mv temp common_pb.js
echo '/* eslint-disable */' | cat - labeling_pb.d.ts > temp && mv temp labeling_pb.d.ts
echo '/* eslint-disable */' | cat - labeling_pb.js > temp && mv temp labeling_pb.js
echo '/* eslint-disable */' | cat - task_pb.d.ts > temp && mv temp task_pb.d.ts
echo '/* eslint-disable */' | cat - task_pb.js > temp && mv temp task_pb.js
echo '/* eslint-disable */' | cat - input_output_pb.d.ts > temp && mv temp input_output_pb.d.ts
echo '/* eslint-disable */' | cat - input_output_pb.js > temp && mv temp input_output_pb.js
echo '/* eslint-disable */' | cat - oriented_bounding_box_pb.d.ts > temp && mv temp oriented_bounding_box_pb.d.ts
echo '/* eslint-disable */' | cat - oriented_bounding_box_pb.js > temp && mv temp oriented_bounding_box_pb.js
echo '/* eslint-disable */' | cat - performance_tracking_pb.d.ts > temp && mv temp performance_tracking_pb.d.ts
echo '/* eslint-disable */' | cat - performance_tracking_pb.js > temp && mv temp performance_tracking_pb.js

find . -name "*.ts" -execdir mv -f {} ../../3d_annotation_frontend/src/api_clients \;
find . -name "*.js" -execdir mv -f {} ../../3d_annotation_frontend/src/api_clients \;
