#!/bin/sh

tar xf /dev/stdin

OUTDIR=src/python_pachyderm/
mkdir -p $OUTDIR

for i in $(find ./proto/pachyderm/src/client -name "*.proto"); do \
    # Make sure to remove the gogoproto line, as that is go specific
    sed -i s/import.*gogo.proto.*\;// $i
    sed -i 's/\[.*gogoproto.*\]//' $i
    # We need to do this in a pass before compilation because it's not guaranteed we
    # traverse the proto files in order of dependence
done

# skip pkg because protos in there aren't part of the public API
for i in $(find ./proto/pachyderm/src/client -name "*.proto" | grep -v "proto/pachyderm/src/client/pkg"); do \
    python3 -m grpc_tools.protoc -I./proto/pachyderm/src --python_out=$OUTDIR --grpc_python_out=$OUTDIR ${i}; \
done

for i in $(find $OUTDIR -regex ".*pb2.*py"); do
    # Fix the imports so they work once this is packaged by pypi
    sed -i 's/from client\./from python_pachyderm\.proto\./' $i
done


find $OUTDIR -regex ".*pb2.*py" | xargs tar cf -
