#!/bin/sh
# -*- mode: sh; tab-width: 8; coding: utf-8-unix -*-
# filter - arguments are to wget - quoted?

ROOTDIR=/mnt/i
ROLE=base

LARGS="-X -P $ROOTDIR/net/Http "
RARGS="--retry 1"

grep ^http | \
    sed -e 's@http://@https://@g' \
	-e 's@^\(https://distfiles.gentoo.org/distfiles/[a-f0-9][a-f0-9]/[^ ]*\) \(https*://[^ ]*\) @\2 \1@' \
        -e 's@^\(https://distfiles.gentoo.org/distfiles/[^ ]*\) \(https*://[^ ]*\) @\2 \1@' \
        -e 's@^\(https://dev.gentoo.org/~sam/distfiles/[^ ]*\) \(https*://[^ ]*\) @\2 \1@' \
    | \
    sed -e 's@ftp://[^ ]*@@' \
	-e 's@^https://distfiles.gentoo.org/distfiles/[^ ]* https://pypi.python.org/@https://pypi.python.org/@' \
	-e 's@https*://distfiles.gentoo.org@https://gentoo.osuosl.org@g' \
	-e 's@https*://gentoo.osuosl.org@https://mirror.leaseweb.com/gentoo@g' \
	-e 's@https*://download.sourceforge.net@https://download.sourceforge.net@g' | \
    while read urls ; do
        url=`echo $urls|sed -e 's@ .*@@'`
        base=`basename "$url"`
	[ -e /usr/portage/distfiles/$base ] && echo distfiles/$base && continue
	for url in $urls ; do
            /usr/local/bin/scurl.bash $LARGS -- $RARGS $url || continue
            break
	  done
      done
