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

ROOTDIR=/mnt/i
if [ "$#" -eq 0 ] ; then
    WARGS="-xc -P $ROOTDIR/net/Http --tries=1"
  else
    WARGS="$@"
  fi
grep ^http | \
    sed -e 's@^\(https://distfiles.gentoo.org/distfiles/[a-f0-9][a-f0-9]/[^ ]*\) \(https://[^ ]*\) @\2 \1@' | \
    sed -e 's@ftp://[^ ]*@@' -e 's@^https://distfiles.gentoo.org/distfiles/[^ ]* https://pypi.python.org/@https://pypi.python.org/@' -e 's/http:/https:/' \
	-e 's@https*://mirror.leaseweb.com/gentoo/@https://gentoo.osuosl.org@g' \
	-e 's@https*://distfiles.gentoo.org@https://gentoo.osuosl.org@g' | \
    while read urls ; do
        url=`sed -e 's@ .*@@' <<< $urls`
        base=`basename "$url"`
	[ -e /usr/portage/distfiles/$base ] && echo distfiles/$base && continue
	for url in $urls ; do
            wget --restrict-file-names=windows --no-verbose $WARGS $url || continue
            break
	  done
      done
