#!/bin/sh
#author:Wos
#a script to booster github resource 
function logo()
{
    echo -e "\033[96;40m"
    base64 -d <<<"H4sIAAAAAAAAA61QsRHEIAzrmUIdnWsNkAHS0vm49Gzg2d82n4TLfxkXSLFlQYSCg2RHQrIkXQA5Jl5EciQP6bJSyIrmDTUjzSpIQLlBuMMncpOQou2r1Bte50oJ8q1JyTFGq9fXTZR9yI90jMTuK2kWjUVhZl3+mPn1bf74KjVz1EgqzARGWxVxbNCZZL3J0edkkcr5Tg1ePA1GKMg44ow095rPSLyIxqTVh9SX07p5Znix3jX7AGO02cJqAgAA"|gunzip
    echo -e "\033[0m"
}

function usage()
{
    logo
    echo "github resource mirror booster"
    echo ""
    echo "      `basename $0` -h (help infomation)"
    echo "      `basename $0` -t (test mirror server available)"
    exit  -1
}

#if you have new mirror server,you can append to blow variable
glist="https://ghfast.top/ https://bgithub.xyz/ https://gitclone.com/ https://github.ur1.fun/ https://gh-proxy.com/ https://ghproxy.net/ https://ghproxy.homeboyc.cn/"

if [ $# -eq 1 ] && [ $1 == '-t' ]; then
    >/tmp/glist.txt
    echo -e "current available mirror server"
    echo "${glist}"|tr ' ' '\n'|xargs -P3 -I {} sh -c 'curl --connect-timeout 3 -I -f -s {} > /dev/null && echo {}'|sed '/^$/d'|tee -a /tmp/glist.txt
    exit -1
elif [ $# -eq 1 ] && [ $1 == '-h' ]; then
    usage
fi
if [ ! -s /tmp/glist.txt ]; then
    echo -e "run \"`basename $0` -t\" first!"
    exit -1
fi

#read link from clipboard,so you don't need provide extra argument in command line 
LINK=$(xclip -selection clipboard -o 2>/dev/null)
if [[ "$LINK" =~ ^https:// ]] && [[ "$LINK" == *"github"* ]]; then
    logo
    cat /tmp/glist.txt|awk '{print $0"'${LINK}'"}'|xargs -P3 -I {} sh -c 'curl --connect-timeout 3 -I -f -s {} >/dev/null && echo {}'
else
    echo -e "error github resource link!,please check it again!"
fi