#!/bin/bash

. ./param_bash.ini
file_name="sfx_version_4_linux_installer.run"
user="sfx4"
password="M0zart4"
best_speed="0"
best_ftp=""
echo ""
for ftp in ftp.exlibris-usa.com ftp.exlibris.co.il il-ftp01
do
    #speed=$(curl -o /dev/null -m 4 "ftp://sfx4:M0zart4@$ftp/$file_name" -w %{size_download} 2>test.log)
    if [ -f test.log ] ; then
        rm -rf test.log
    fi
    echo "Checking $ftp"
    ($WGET -o test.log "ftp://$user:$password@$ftp/$file_name" & 2>&1 > /dev/null echo $! > uidfile)
    uid=`cat uidfile`
    sleep 6
    $PS | grep -v grep | grep sfx4 | grep wget > /dev/null 2>&1
    if [ "$?" == "0" ] ; then
        kill $uid  2>&1 > /dev/null
        sleep 2  > /dev/null 2>&1
    fi
    speed=`cat test.log | wc -l`
    if [ "$speed" -gt "$best_speed" ] ; then
        best_speed=$speed
        best_ftp=$ftp
    fi
    if [ -f $file_name ] ; then
        rm $file_name > /dev/null
    fi
done
if [ -f test.log ] ; then
    rm -rf test.log
fi

Update_params "FTP_SERVER" "$best_ftp"
echo ""
echo "The selected FTP server is: $best_ftp"
