#!/bin/bash
#
# $Id: archive-quinn 68 2004-08-31 07:49:33Z guillem $
#
# Copyright (C) 2003, 2004 Guillem Jover <guillem@debian.org>
#
# Functions sort_by_priority and sort_by_section ripped from quinn-diff
# package's example scripts.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
#

#
# Requires: wget bzip2 quinn-diff
#

. archive-lib

sort_by_priority()
{
  local tmpfile=$(mktemp /tmp/$(basename $0).XXXXXX)
  trap 'rm -f $tmpfile ; exit 1' 1 2 3 13 15

  quinn-diff "$@" > $tmpfile 2> /dev/null

  egrep "\[required\:" $tmpfile | sort
  egrep "\[important\:" $tmpfile | sort
  egrep "\[standard\:" $tmpfile | sort
  egrep "\[optional\:" $tmpfile | sort
  egrep "\[extra\:" $tmpfile | sort
  egrep -v "\[extra\:|\[optional\:|\[standard\:|\[important\:|\[required\:" $tmpfile | sort

  rm -f $tmpfile
}

sort_by_section()
{
  local tmpfile=$(mktemp /tmp/$(basename $0).XXXXXX)
  trap 'rm -f $tmpfile ; exit 1' 1 2 3 13 15

  quinn-diff "$@" > $tmpfile 2> /dev/null

  local sections="base admin comm devel doc editors electronics embedded games gnome graphics hamradio interpreters kde libs libdevel mail math misc net news oldlibs otherosfs perl python shells sound tex text utils web x11"

  for i in $sections; do
    egrep "$i\/.*\[required\:" $tmpfile | sort
    egrep "$i\/.*\[important\:" $tmpfile | sort
    egrep "$i\/.*\[standard\:" $tmpfile | sort
    egrep "$i\/.*\[optional\:" $tmpfile | sort
    egrep "$i\/.*\[extra\:" $tmpfile | sort
  done
  egrep -v "\[extra\:|\[optional\:|\[standard\:|\[important\:|\[required\:" $tmpfile | sort

  rm -f $tmpfile
}

cd $cache_dir

echo "-> Generating Quinn-Diff stats"

echo " -> Collecting Sources files"
wget -N http://jane.uab.es/debian/dists/unstable/main/source/Sources.bz2
bunzip2 -c Sources.bz2 > Sources
echo " -> Collecting hurd-i386 binary files"
wget -N http://jane.uab.es/debian/dists/unstable/main/binary-hurd-i386/Packages.bz2 -O Packages-unstable.hurd-i386.bz2
bunzip2 -c Packages-unstable.hurd-i386.bz2 > Packages-unstable.hurd-i386

echo " -> Collecting Packages files"
for arch in $arch_list_unreleased; do
  if [ $arch != source ]; then
    echo "  -> Architecture: $arch"
    cat $dists_dir/unreleased/main/binary-$arch/Packages > Packages.$arch
    if [ $arch = hurd-i386 ]; then
      cat Packages-unstable.hurd-i386 >> Packages.$arch
    fi
    mkdir -p $quinn_dir/unstable
    sort_by_priority -A$arch -p Packages.$arch -i > $quinn_dir/unstable/by_priority-$arch.txt
    sort_by_section -A$arch -p Packages.$arch -i > $quinn_dir/unstable/by_section-$arch.txt
  fi
done

