#!/bin/sh

if [ -z "$1" ]; then
  echo "Missing provider as the first argument"
  exit 1
fi

workdir="/home/jchaloup/Projects/lab/watchers"

provider=$1
mkdir -p ${workdir}/${provider}

IFS=', ' read -r -a ids <<< "$(echo $(cat ${workdir}/${provider}.jobs))"

pullData() {
  id=$1
  mkdir -p ${workdir}/${provider}/${id}
  must_gather_tarball=""
  case ${provider} in
    aws)
      #must_gather_tarball="https://gcsweb-ci.apps.ci.l2s4.p1.openshiftapps.com/gcs/origin-ci-test/logs/periodic-ci-openshift-release-master-ci-4.8-e2e-aws/${id}/artifacts/e2e-aws/gather-must-gather/artifacts/must-gather.tar"
      must_gather_tarball="https://gcsweb-ci.apps.ci.l2s4.p1.openshiftapps.com/gcs/origin-ci-test/logs/periodic-ci-openshift-release-master-nightly-4.8-e2e-aws/${id}/artifacts/e2e-aws/gather-must-gather/artifacts/must-gather.tar"
      ;;
    azure)
      must_gather_tarball="https://gcsweb-ci.apps.ci.l2s4.p1.openshiftapps.com/gcs/origin-ci-test/logs/periodic-ci-openshift-release-master-ci-4.8-e2e-azure/${id}/artifacts/e2e-azure/gather-must-gather/artifacts/must-gather.tar"
      ;;
    gcp)
      #must_gather_tarball="https://gcsweb-ci.apps.ci.l2s4.p1.openshiftapps.com/gcs/origin-ci-test/logs/periodic-ci-openshift-release-master-ci-4.8-e2e-gcp-serial/${id}/artifacts/e2e-gcp-serial/gather-must-gather/artifacts/must-gather.tar"
      must_gather_tarball="https://gcsweb-ci.apps.ci.l2s4.p1.openshiftapps.com/gcs/origin-ci-test/logs/periodic-ci-openshift-release-master-ci-4.8-e2e-gcp/${id}/artifacts/e2e-gcp/gather-must-gather/artifacts/must-gather.tar"
      ;;
    metal)
      #must_gather_tarball="https://gcsweb-ci.apps.ci.l2s4.p1.openshiftapps.com/gcs/origin-ci-test/logs/release-openshift-ocp-installer-e2e-metal-4.8/${id}/artifacts/e2e-metal/must-gather.tar"
      must_gather_tarball="https://gcsweb-ci.apps.ci.l2s4.p1.openshiftapps.com/gcs/origin-ci-test/logs/periodic-ci-openshift-release-master-nightly-4.8-e2e-metal-ipi/${id}/artifacts/e2e-metal-ipi/gather-must-gather/artifacts/must-gather.tar"
      ;;
    vsphere)
      must_gather_tarball="https://gcsweb-ci.apps.ci.l2s4.p1.openshiftapps.com/gcs/origin-ci-test/logs/periodic-ci-openshift-release-master-nightly-4.8-e2e-vsphere-upi/${id}/artifacts/e2e-vsphere-upi/gather-must-gather/artifacts/must-gather.tar"
      ;;
    openstack)
      must_gather_tarball="https://gcsweb-ci.apps.ci.l2s4.p1.openshiftapps.com/gcs/origin-ci-test/logs/release-openshift-ocp-installer-e2e-openstack-4.8/${id}/artifacts/e2e-openstack/must-gather.tar"
      ;;
    ppc64le)
      must_gather_tarball="https://gcsweb-ci.apps.ci.l2s4.p1.openshiftapps.com/gcs/origin-ci-test/logs/release-openshift-ocp-installer-e2e-remote-libvirt-ppc64le-4.8/${id}/artifacts/e2e-remote-libvirt/must-gather.tar"
      ;;
    s390x)
      must_gather_tarball="https://gcsweb-ci.apps.ci.l2s4.p1.openshiftapps.com/gcs/origin-ci-test/logs/release-openshift-ocp-installer-e2e-remote-libvirt-s390x-4.8/${id}/artifacts/e2e-remote-libvirt/must-gather.tar"
      ;;
    *)
      echo "Unknown provider ${provider}"
      exit 1
      ;;
  esac

  if [ ! -f "${workdir}/${provider}/${id}/must-gather.tar" ]; then
    echo "Pulling $must_gather_tarball"
    wget -O ${workdir}/${provider}/${id}/must-gather.tar -q $must_gather_tarball
  fi
  if [ ! -f "${workdir}/${provider}/${id}/must-gather.tar" ]; then
    echo "Failed to pull $must_gather_tarball"
    continue
  fi

  apirequestcountsdir=$(tar -tf ${workdir}/${provider}/${id}/must-gather.tar | grep "cluster-scoped-resources/apiserver.openshift.io/apirequestcounts/$")
  if [ -z "$apirequestcountsdir" ]; then
    apirequestcountsdir=$(tar -tf ${workdir}/${provider}/${id}/must-gather.tar | grep "cluster-scoped-resources/apiserver.openshift.io/apirequestcounts$")
  fi

  mkdir -p ${workdir}/${provider}/${id}/$apirequestcountsdir
  tar -C ${workdir}/${provider}/${id} -xf ${workdir}/${provider}/${id}/must-gather.tar $apirequestcountsdir
  rm ${workdir}/${provider}/${id}/must-gather.tar
  /home/jchaloup/Projects/src/github.com/openshift/installer/compute-apirequestsmax.py ${workdir}/${provider}/${id}/$apirequestcountsdir > ${workdir}/${provider}/${id}/requests.json
}

i=0
for id in ${ids[@]}; do
  if [ $i -le 20 ]; then
    i=$(($i + 1))
    pullData $id &
  else
    i=0
    wait
  fi
done

wait
