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