#!/bin/bash
milvusvers=$(awk '/Version:/ {print $2}' *spec)

cat <<EOF
Creates the vendor.tar.xz for milvus-$milvusvers.
Needs podman to be installed.

EOF
#read -t 10 -p "Are you sure you want to continue? <y/N>" prompt
#if [[ $prompt =~ [yY](es)* ]] ; then
#  echo "Starting to create vendoring"
#else
#  exit 0
#fi


create_vendor=`mktemp create_vendor-XXXXXXXX`
cat >  $create_vendor  <<EOF
FROM opensuse/leap:latest

COPY milvus-${milvusvers}.tar milvus-proto-${milvusvers}.tar.gz /root


RUN zypper ar https://download.opensuse.org/repositories/devel:/languages:/go/openSUSE_Factory/devel:languages:go.repo && \
    zypper ar https://download.opensuse.org/repositories/science:/machinelearning:/milvus/15.6/science:machinelearning:milvus.repo 
RUN cat > devel-go-key <<EOFKEY
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.4.5 (GNU/Linux)

mQGiBEyvJq8RBAD64FtKbU3VFsZ4PAzcSi9rETc1qXZLVQYtrE38niriIJUuT/tC
kxGjkduD7ozP6rxL+M8fk9UdbbmzhHhXzQT2blyYy2iwpZP5+AFeQawz+oyMRVze
x6IY5qO31TiRu+aEBptk6nco4DULlKABlvtjKI+gJ4buJm+TumyrPaxe9wCgpFwl
O0fEjpOfQd+6Du0sCnPX7+8D/3xYVj9q5ILmrJ9zehBKjRYEcaP0rZVqudH51FUS
I0LQNHsJdzKLzhT+Zj7TJU4KnCN4sX7amfYL3GT9qyZEBDx2alubvEsI36wtNq0i
ewU4XmyZ+Rd7Q4mAPNoBGe5DsUA8wn/7iHGg9aJHu+GXvH7T3DVoUxW5W2KqPw+h
jzqGBACWGuuCefa683uf6aKqpTS3JB+01BDPf+sG9qJ+k7xcJOsEiXIe1X5vH9ov
kEvTM12kzY7SGwlsFsTDT1xioDl62g5nPlZMXErUBVnjoMtjl9bDwUPJY/vP1S9M
VaONBMoJEON3HRui9jhdEK25LTdrcFlFpRzJI3AgdXwy8aQyiLRGZGV2ZWw6bGFu
Z3VhZ2VzO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UCGwMFCSIQQSsGCwkIBwMCBBUCCAMEFgIDAQIeAQIXgAAKCRAB23MClD2L
uHHbAKCFt4d4TotnM42E15Ozhf78+2HIFgCeNU+nX1RJ3e+XqU9t7mD1q2T7CauI
RgQTEQIABgUCR5ZbOgAKCRA7MBG3a51lI6bqAKChyrk96dWoOdFYRI9Lh3Dhk7RW
KwCfSlpN87ipcMW2KFKnxtn6TX0FCms=
=R8UM
-----END PGP PUBLIC KEY BLOCK-----
EOFKEY

RUN rpm -import devel-go-key  science-key && \ 
    zypper install -y go protoc-gen-go132 which protobuf-devel tar gzip
 
RUN <<EOFVEND
cd /root
tar xf milvus-${milvusvers}.tar
tar xzf milvus-proto-${milvusvers}.tar.gz
mkdir -p /root/milvus-${milvusvers}/cmake_build/bin/
ln -s $(which protoc) /root/milvus-${milvusvers}/cmake_build/bin/
mkdir -p /root/milvus-${milvusvers}/cmake_build/thirdparty
mv -v milvus-proto-${milvusvers} /root/milvus-${milvusvers}/cmake_build/thirdparty/milvus-proto
cd /root/milvus-${milvusvers}
./scripts/download_milvus_proto.sh
./scripts/generate_proto.sh
#sed -i 's@google.golang.org/grpc v1.59.0@google.golang.org/grpc v1.66.2@' go.mod
#sed -i 's@github.com/bytedance/sonic v1.9.1@github.com/bytedance/sonic v1.12.1@' go.mod
go mod vendor
#tar cf vendor.tar vendor/ go.mod
EOFVEND

EOF
podman build -f $create_vendor -t create_vendor
id=$(podman create create_vendor)
rm vendor.tar.xz
podman cp $id:/root/milvus-${milvusvers}/vendor -> ./vendor.tar
xz vendor.tar
podman rm -v $id
podman image rm create_vendor
rm $create_vendor
