3f9ee9
#!/bin/bash
f9b602
#set -e
f9b602
rm -f /tmp/pyxis*.json
3f9ee9
TOTAL=`curl -s --negotiate -u: -H 'Content-Type: application/json' -H 'Accept: application/json' -X GET "https://pyxis.engineering.redhat.com/v1/repositories?page_size=1" | jq .total`
f9b602
if [ "$TOTAL" == "null" ]; then
f9b602
  echo "Error comunicating with Pyxis API."
f9b602
  exit 1
f9b602
fi
3f9ee9
PAGES=$(($TOTAL/500))
3f9ee9
for P in `seq 0 $PAGES`; do
3f9ee9
  curl -s --negotiate -u: -H 'Content-Type: application/json' -H 'Accept: application/json' -X GET "https://pyxis.engineering.redhat.com/v1/repositories?page_size=500&page=$P" > /tmp/pyxis$P.json
3f9ee9
done
3f9ee9
cat /tmp/pyxis*.json > /tmp/pyx.json
3f9ee9
rm -f /tmp/rhel-shortnames.conf
3f9ee9
while read -r LINE; do
3f9ee9
  if [[ "$LINE" == *\"_id\":* ]] || [[ "$LINE" == *\"total\":* ]]; then
3f9ee9
    if [ -z $REGISTRY ] ||
3f9ee9
       [ -z $PUBLISHED ] ||
3f9ee9
       [ -z $REPOSITORY ] ||
3f9ee9
       [  $REPOSITORY == \"\" ] ||
3f9ee9
       [ "$AVAILABLE" != "Generally Available" ] ||
3f9ee9
       [[ $REPOSITORY == *[@:]* ]] ||
3f9ee9
       [[ "$REGISTRY" == *non_registry* ]] ||
3f9ee9
       [[ $REGISTRY != *.* ]]
3f9ee9
     then
3f9ee9
      continue
3f9ee9
    fi
3f9ee9
    if [[ $REGISTRY == *quay.io* ]] ||
3f9ee9
       [[ $REGISTRY == *redhat.com* ]]; then
3f9ee9
      if [ "$REQUIRES_TERMS" == "1" ]; then
3f9ee9
        REGISTRY=registry.redhat.io
3f9ee9
      fi
3f9ee9
      echo "\"$REPOSITORY\" = \"$REGISTRY/$REPOSITORY\""
3f9ee9
      echo "\"$REPOSITORY\" = \"$REGISTRY/$REPOSITORY\"" >> /tmp/rhel-shortnames.conf
3f9ee9
    fi
3f9ee9
    REGISTRY=""
3f9ee9
    PUBLISHED=""
3f9ee9
    AVAILABLE=""
3f9ee9
    REPOSITORY=""
3f9ee9
    REQUIRES_TERMS=""
3f9ee9
    continue
3f9ee9
  fi
3f9ee9
  if [[ "$LINE" == *\"published\":\ true,* ]]; then
3f9ee9
    PUBLISHED=1
3f9ee9
  fi
3f9ee9
  if [[ "$LINE" == *\"requires_terms\":\ true,* ]]; then
3f9ee9
    REQUIRES_TERMS=1
3f9ee9
  fi
3f9ee9
  if [[ "$LINE" == *\"repository\":\ * ]]; then
3f9ee9
    REPOSITORY=`echo $LINE | sed 's,^.* ",,' | sed 's;",$;;'`
3f9ee9
  fi
3f9ee9
  if [[ "$LINE" == *\"registry\":\ * ]]; then
3f9ee9
    REGISTRY=`echo $LINE | sed -e 's,^.*:\ ",,' -e 's,".*,,'`
3f9ee9
  fi
3f9ee9
  if [[ "$LINE" == *\"release_categories\":\ * ]]; then
3f9ee9
    read -r LINE
3f9ee9
    AVAILABLE=`echo $LINE | sed 's,",,g'`
3f9ee9
  fi
3f9ee9
done < /tmp/pyx.json
3f9ee9
3f9ee9
cp /tmp/rhel-shortnames.conf /tmp/r.conf
3f9ee9
for D in `cut -d\  -f1 /tmp/r.conf | sort | uniq -d`; do
3f9ee9
  echo $D
3f9ee9
  M=`grep ^$D /tmp/r.conf | grep 'redhat.com' | tail -n1`
3f9ee9
  [ -z "$M" ] && M=`grep ^$D /tmp/r.conf | tail -n1`
3f9ee9
  echo $M
3f9ee9
  if [ ! -z "$M" ]; then
3f9ee9
    echo "replacing $D with $M"
3f9ee9
    grep -v "^$D.*" /tmp/r.conf > /tmp/r2.conf
3f9ee9
    echo "$M" >> /tmp/r2.conf
3f9ee9
    mv /tmp/r2.conf /tmp/r.conf
3f9ee9
  fi
3f9ee9
done
3f9ee9
3f9ee9
echo "[aliases]" > 001-rhel-shortnames-pyxis.conf
3f9ee9
sort /tmp/r.conf >> 001-rhel-shortnames-pyxis.conf