#!/bin/sh

# Copyright: © 2002-2008 Raphaël Hertzog
# Copyright: © 2007-2009 Stefano Zacchiroli <zack@debian.org>

# This file is distributed under the terms of the General Public License
# version 2 or (at your option) any later version.

#mirror="http://gluck.debian.org/debian"
mirror="http://cdn.debian.net/debian"
security="http://security.debian.org"
piuparts="http://piuparts.debian.org"

official_distros="oldstable stable testing unstable experimental"
pu_distros="stable-proposed-updates testing-proposed-updates"
security_distros="oldstable stable testing"

umask 002
set -e

if [ -d "../incoming" ]; then
    root=$PWD/..
elif [ -d "incoming" ]; then
    root=$PWD
else
    root=/org/packages.qa.debian.org/www
fi

cd $root/incoming

nice_wget() {
    # $1: url
    # $2: filename
    # For timestamping to work, copy to .new (not link, wget will not break
    # the link)
    cp -a $2 $2.new || true
    # Beware that -N conflicts with -O (#88176, #202911)
    if wget -U pts -q -O $2.new "$1" ; then
        mv -f $2.new $2
    else
        echo "Downloading $1 failed, $2 is stale now"
        rm -f $2.new
    fi
}

http_head() {
    # $1: url
    # return 0 if the URL exists (HEAD was OK), non-0 otherwise
    curl --head --fail --silent "$1" > /dev/null
    return $?
}

nice_redirect_to() {
  # $1 : target file
  # $2-... : shell command
  dest="$1"
  shift
  if [ -f "$dest" ] ; then
    mv -f "$dest" "$dest.bak"
  fi
  $* > "$dest" || (echo "Failure while executing $* . Continuing ..." ; cp "$dest.bak" "$dest")
}

# Download all Sources.gz
for comp in main contrib non-free
do
    for dist in $official_distros ; do
      nice_wget $mirror/dists/$dist/$comp/source/Sources.gz \
	Sources-${dist}_$comp.gz
    done
    for dist in $pu_distros ; do
      nice_wget $mirror/dists/$dist/$comp/source/Sources.gz \
	Sources-${dist}_$comp.gz
    done
    for dist in $security_distros ; do
      nice_wget $security/dists/$dist/updates/$comp/source/Sources.gz \
	Sources-security-${dist}_$comp.gz
    done
done

# Download mentors.d.n Sources
for comp in main contrib non-free
do
  nice_wget http://mentors.debian.net/debian/dists/unstable/$comp/source/Sources.gz \
    Sources-mentors_$comp.gz
done

# Download update_excuses.html
nice_wget http://ftp-master.debian.org/testing/update_excuses.html.gz \
    update_excuses.html.gz

# Download PTS subscription count
nice_wget http://packages.qa.debian.org/data/pts-subscription-count.txt \
  count.txt

# Download override disparities
nice_wget http://qa.debian.org/data/ftp/override-disparities.unstable \
    override-disparities.unstable
nice_wget http://qa.debian.org/data/ftp/override-disparities.experimental \
    override-disparities.experimental

# Download lintian.d.o run info
#nice_wget http://lintian.debian.org/lintian.log lintian.log
nice_wget http://lintian.debian.org/qa-list.txt lintian.qa-list.txt

# Download bugs summary
nice_wget http://udd.debian.org/cgi-bin/bugs-binpkgs-pts.cgi bugs.txt
nice_wget http://udd.debian.org/cgi-bin/ddpo-bugs.cgi bugs-src.txt
rsync rsync://bugs-mirror.debian.org/bts-spool-index/sources ./
cut -f1,3 sources >sources.map.new
touch -r sources sources.map.new
mv -f sources.map.new sources.map
nice_redirect_to bugs.help.txt $root/bin/tagged_bugs.py "help"
nice_redirect_to bugs.gift.txt \
  $root/bin/tagged_bugs.py "gift" "debian-qa@lists.debian.org"
piuparts_distros="sid lenny2squeeze"
for distro in $piuparts_distros ; do
  nice_wget $piuparts/$distro/sources.txt piuparts-$distro.txt
done

# Download debcheck lists
DCROOT=http://qa.debian.org/data/debcheck/result
#nice_wget $DCROOT/oldstable/lists/ALL-pkglist debcheck-oldstable
nice_wget $DCROOT/stable/lists/ALL-pkglist debcheck-stable
nice_wget $DCROOT/testing/lists/ALL-pkglist debcheck-testing
nice_wget $DCROOT/unstable/lists/ALL-pkglist debcheck-unstable

# Download the translation status of packages
nice_wget http://i18n.debian.net/l10n-pkg-status/pkglist l10n-status.txt

# Get wnpp information
nice_wget http://qa.debian.org/data/bts/wnpp_rm wnpp_rm

# get patches from ubuntu
nice_wget http://patches.ubuntu.com/PATCHES patches.ubuntu
# get packages version in Ubuntu
nice_wget http://udd.debian.org/cgi-bin/ubuntupackages.cgi versions.ubuntu
# get bugs in Ubuntu
nice_wget http://udd.debian.org/cgi-bin/ubuntubugs.cgi bugs.ubuntu

# download LowThresholdNmu list
nice_wget 'http://wiki.debian.org/LowThresholdNmu?action=raw' \
    low_threshold_nmu.txt

# ongoing transitions
nice_wget http://ftp-master.debian.org/transitions.yaml \
    transitions.yaml

# download the list of packages indexed by svnbuildstat
#nice_wget http://svnbuildstat.debian.net/packages/flatlist \
#    svnbuildstat_list.txt

# download DEHS (http://dehs.alioth.debian.org)
nice_wget http://dehs.alioth.debian.org/no_updated.txt \
    dehs_out_of_date.txt
nice_wget http://dehs.alioth.debian.org/no_upstream.txt \
    dehs_error.txt

# download list of security issues
nice_wget http://security-tracker.debian.org/tracker/data/pts/1 \
    security_issues.txt

# download NEW queue info
nice_wget http://ftp-master.debian.org/new.822 new.822

# retrieve package descriptions from UDD
nice_wget http://qa.debian.org/data/pts/shortdesc.txt shortdesc.txt

# download the Debian fonts review
nice_wget http://pkg-fonts.alioth.debian.org/review/debian-font-review.yaml debian-font-review.yaml

# What more ?

# Decompress all files
for file in *.gz; do
    gzip -d -c $file > ${file%%.gz}.new
    touch -r $file ${file%%.gz}.new
    mv -f ${file%%.gz}.new ${file%%.gz}
done

# vim: ts=8
