pexes='[
          {"name":"messaging", "version":"2.8.2rc1"},
          {"name":"workspaces", "version":"2.8.2rc1"},
          {"name":"carta_envoy", "version":"2.8.2rc1"},
          {"name":"casa_envoy", "version":"2.8.2rc1"},
          {"name":"conveyor", "version":"2.8.2rc1"},
          {"name":"deliver", "version":"2.8.2rc1"},
          {"name":"ingest", "version":"2.8.2rc1"},
          {"name":"ingest_envoy", "version":"2.8.2rc1"},
          {"name":"mediator", "version":"2.8.2rc1"},
          {"name":"null", "version":"2.8.2rc1"},
          {"name":"productfetcher", "version":"2.8.2rc1"},
          {"name":"update_stage", "version":"2.8.2rc1"},
          {"name":"vela", "version":"2.8.2rc1"},
          {"name":"wf_inspector", "version":"2.8.2rc1"},
          {"name":"ws_annihilator", "version":"2.8.2rc1"},
          {"name":"ws_metrics", "version":"2.8.2rc1"}
        ]'

BUILD_DIR=/lustre/aoc/cluster/pipeline/dsoc-dev/workspaces/sbin
[[ -d $BUILD_DIR ]] ||  mkdir -p "$BUILD_DIR"

for row in $(echo "${pexes}" | jq -r '.[] | @base64'); do
    _jq() {
     echo "${row}" | base64 --decode | jq -r "${1}"
    }
   curl --header "PRIVATE-TOKEN: glpat-vPamXXk4PZPe8GQAzmY2" "https://gitlab.nrao.edu/api/v4/projects/621/packages/generic/$(_jq '.name')/$(_jq '.version')/$(_jq '.name')-$(_jq '.version').tar" --output "$(_jq '.name')-$(_jq '.version').tar"
   tar -zxf "$(_jq '.name')-$(_jq '.version').tar.gz"
   cd "$(_jq '.name')-$(_jq '.version')" || return
   if [ -e pyproject.toml ]; then
       until pex . -c "$(_jq '.name')" -o "$BUILD_DIR/$(_jq '.name')" --python-shebang /home/ssa/bin/python3.10 ; do
         echo "PEX build failed. Retrying."; sleep 2;
       done
     else
       echo "PEX build impossible in $PWD because there is no pyproject.toml file"
     fi
     cd ..
#   pip3 install "$(_jq '.name')-$(_jq '.version')-py3-none-any.whl"
done
