#!/bin/bash

yxml=0;
nxml=0;
gxml=0;
dxml=0;

for i in `find /var/git/meta-repo/kits/*/*/* -maxdepth 0 -type d`; do
  # Filter out files, metadata and profiles directories
  if [ $(basename -- ${i%.*}) != "files" ] && [ `echo $i | grep -v profiles` ] && [ `echo $i | grep -v metadata` ]; then
    if [ `ls $i/metadata.xml 2>/dev/null` ]; then
      # Count packages with existing metadata.xml
      let yxml=$yxml+1;
    else
      # Count packages with missing metadata.xml
      echo "$i has no metadata";
      let nxml=$nxml+1;

      base=`basename -- ${i%.*}`;
      #calculate base directory
      u=${i%$base*}
      v=${u%/*}
      dir=${v##*/}
      # set url of gentoo path
      url="https://gitweb.gentoo.org/repo/gentoo.git/tree/$dir/$base";

      # check if package exists in the gentoo tree
      if [[ `wget -S --spider curl $url  2>&1 | grep 'HTTP/1.1 200 OK'` ]]; then
        let dxml=$dxml+1;

        # check if we have a metadata.xml file in the gentoo tree
        if [[ `wget -S --spider curl "$url/metadata.xml"  2>&1 | grep 'HTTP/1.1 200 OK'` ]]; then
          echo "metadata.xml file exists at $url";
          let gxml=$gxml+1;
        fi
      fi
    fi
  fi
done

echo "Packages with metadata.xml: $yxml
Packages without metadata.xml: $nxml
Packages in the gentoo tree: $dxml
Packages with metadata in gentoo: $gxml";
