#! /bin/sh -e

# This is an example of how to build a local mirror containing only the
# index files, not the packages themselves. If you put something similar to
# this in a cron job, then you can run madison-lite locally. Be sure to
# change the various hostnames to appropriate mirrors, set MIRROR to a
# suitable directory, and fill the mirror directory into your madison-lite
# configuration file.
#
# If you are reading this after the release of sarge, then you may need to
# change the lists of distributions and architectures.

HOST_MAIN=${HOST_MAIN:-'ftp://ftp.debian.org/debian'}
HOST_NONUS=${HOST_NONUS:-'ftp://non-us.debian.org/debian-non-US'}
HOST_SECURITY=${HOST_SECURITY:-'ftp://security.debian.org/debian-security'}
HOST_UBUNTU=${HOST_UBUNTU:-'http://archive.ubuntu.com/ubuntu'}
HOST_UBUNTU_PORTS=${HOST_UBUNTU_PORTS:-'http://ports.ubuntu.com/ubuntu-ports'}
MIRROR=${MIRROR:-/tmp/mirror}

WGET_OPTS=${WGET_OPTS-'-q -N --passive-ftp'}

umask 002

mkdir -p "$MIRROR"
cd "$MIRROR"

mkdir -p dists
cd dists

# To mirror Ubuntu index files as well, run this script with:
#   ARCHIVES='main non-US security ubuntu ubuntu-ports'
# set in the environment.
archives=${ARCHIVES:-'main non-US security'}

for archive in $archives; do
    case $archive in
	main)
	    host="$HOST_MAIN"
	    suitesuffix=''
	    suites='stable proposed-updates testing testing-proposed-updates unstable experimental'
	    components='main contrib non-free'
	    ;;
	non-US)
	    host="$HOST_NONUS"
	    suitesuffix='/non-US'
	    suites='stable'
	    components='main contrib non-free'
	    ;;
	security)
	    host="$HOST_SECURITY"
	    suitesuffix='/updates'
	    suites='stable testing'
	    components='main contrib non-free'
	    ;;
	ubuntu)
	    host="$HOST_UBUNTU"
	    suitesuffix=''
	    suites='warty hoary breezy dapper'
	    components='main restricted universe multiverse'
	    ;;
	ubuntu-ports)
	    host="$HOST_UBUNTU_PORTS"
	    suitesuffix=''
	    suites='hoary breezy dapper'
	    components='main restricted universe multiverse'
	    ;;
	*)
	    echo "Internal error: archive '$archive'?" >&2
	    exit 1
    esac

    for suite in $suites; do
	case $archive in
	    main|ubuntu*)
		suitehere="$suite"
		;;
	    *)
		suitehere="$suite-$archive"
		;;
	esac

	mkdir -p "$suitehere"
	root=dists
	case $suite in
	    oldstable)
		arches='alpha arm i386 m68k powerpc sparc'
		;;
	    stable)
		arches='alpha arm hppa i386 ia64 m68k mips mipsel powerpc s390 sparc'
		;;
	    testing)
		arches='alpha amd64 arm hppa i386 ia64 m68k mips mipsel powerpc s390 sparc'
		;;
	    unstable|experimental)
		arches='alpha amd64 arm hppa hurd-i386 i386 ia64 m68k mips mipsel powerpc s390 sparc'
		;;
	    warty)
		arches='amd64 i386 powerpc'
		;;
	    hoary)
		case $archive in
		    ubuntu)
			arches='amd64 i386 powerpc'
			;;
		    ubuntu-ports)
			arches='ia64 sparc'
			;;
		esac
		;;
	    breezy|dapper)
		case $archive in
		    ubuntu)
			arches='amd64 i386 powerpc'
			;;
		    ubuntu-ports)
			arches='hppa ia64 sparc'
			;;
		esac
		;;
	esac
	for component in $components; do
	    mkdir -p "$suitehere/$component"
	    for arch in $arches; do
		mkdir -p "$suitehere/$component/binary-$arch"
		wget $WGET_OPTS -O "$suitehere/$component/binary-$arch/Packages.gz" "$host/$root/$suite$suitesuffix/$component/binary-$arch/Packages.gz"
	    done
	    mkdir -p "$suite/$component/source"
	    wget $WGET_OPTS -O "$suite/$component/source/Sources.gz" "$host/$root/$suite$suitesuffix/$component/source/Sources.gz"
	done
    done
done

exit 0
