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

prog=$( basename $0 .bash )
prog=ScurlU
ROOTDIR=/mnt/i/net/Http
ROLE=base
CACHE=/usr/portage/distfiles
[ -z "$SCURL_BG" ] && SCURL_BG=0
SSL_VER=2

. /usr/local/bin/proxy_curl_lib.bash
. /usr/local/bin/usr_local_tput.bash

route | grep -q ^def || { ERROR not connected ; exit -1 ; }

FETCHCOMMAND='/usr/local/bin/scurl.bash --force-directories --directory-prefix "\${DISTDIR}"  -- "\${URI}"'

# RARGS="--retry 1 --connect-timeout 10"
RARGS=" -S $SSL_VER"
if [ "$#" -eq 0 ] ; then
    LARGS="--force-directories --directory-prefix $ROOTDIR"
  else
    LARGS="$@"
fi
cp /dev/null /tmp/$prog$$.urls

# //www.simplesystems.org/users/bfriesen/public-key.txt no https:
# https://opencoder.net/WayneDavison.key cloudflare 403
# https://www.simplesystems.org/users/bfriesen/public-key.txt 503
# https://tiswww.case.edu/php/chet/gpgkey.asc 500 timeout
# https://botan.randombit.net/pgpkey.txt no tls1.3
# https://sourceware.org/elfutils/ftp/gpgkey-1AA44BE649DE760A.gpg no tls1.3
# https://gnutls.org/gnutls-release-keyring.gpg no tls1.3

declare -A IPS
retval=0
# NOT 1.3 	-e 's@^https://distfiles.gentoo.org/distfiles/[^ ]* https://pypi.python.org/@https://pypi.python.org/@'
grep ^http | \
    sed -e 's@ftp://[^ ]*@@' \
	-e 's/http:/https:/' \
	-e 's@^https://distfiles.gentoo.org/distfiles/openpgp-keys-[^ ]*.asc @@' \
	-e 's@https*://distfiles.gentoo.org@https://gentoo.osuosl.org@g' \
	-e 's@https://gentoo.osuosl.org@https://mirror.leaseweb.com/gentoo@g' \
    | \
    while read urls ; do
        url=`echo $urls|sed -e 's@ .*@@'`
        base=`basename "$url"`
	[ -e $CACHE/$base ] && echo $CACHE/$base && continue
        base=`echo $url | sed -e 's@ .*@@' -e 's@https*://@@'`
        [ -e $ROOTDIR/"$base" ] && echo $ROOTDIR/"$base" && continue
	for url in $urls ; do
	    url=`sed -e 's@http://@https://@g' <<< $url`
            domain=`sed -e 's@^https*://@@' -e 's@/.*@@' <<< $url`
	    a=`proxy_ami_nottlsv3 $domain`
	    ar=$?
	    [ $ar -eq 0 -a "$a" = True ] && \
		WARN $prog proxy_ami_nottlsv3 $domain $url && continue
	    if [ -z "${IPS[$domain]}" ] ; then
		ip=`tor-resolve $domain`
		ir=$?
		[ $ir -eq 0 ] && IPS[$domain]=$ip
	    else
		ip="${IPS[$domain]}"
		ir=0
	    fi
	    if [ $ir -eq 0 -a -n "$ip" ] ; then
		a=`proxy_ami_cloudflared $ip`
		[ $? -eq 0 -a "$a" = True ] && \
		    WARN $prog $url Cloudflared $ip && \
		    continue
	    fi

	    if [ "$SCURL_BG" = 1 ] ; then
		/usr/local/bin/scurl.bash $LARGS -- $RARGS $url &
		INFO $prog /usr/local/bin/scurl.bash $LARGS -- $RARGS $url
	    else
		/usr/local/bin/scurl.bash $LARGS -- $RARGS $url || {
		    retval=$?
		    WARN $prog retval=$retval /usr/local/bin/scurl.bash $LARGS -- $RARGS  $url
		    continue
		}
	    fi

            break
	  done
      done

exit $retval
