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