#!/bin/bash
function DownLoad()
{
local url=$1
local dirpath=$2
wget --ftp-user=gsics  --ftp-password=gsics \
-c -r -q -e  robots=off --reject="index.html*"  \
--tries=5 \
--timeout=20 \
--waitretry=10 \
--wait=3 \
--no-parent \
-nd -nH --dont-remove-listing \
--directory-prefix=${dirpath} \
${url}
}
psname=$0
psNum=`ps aux |grep ${psname}|wc -l`
if [ $psNum -gt 3 ]
then
cdir=`pwd`
echo 'Process is Running .' >${cdir}/tmp.log
echo `date` >>${cdir}/tmp.log
exit
fi
DATADIR=/nas03/v6/AQUA/MODIS/L1/ORBIT
thread=5
if [ $# -eq 2 ];then
stime=$1
etime=$2
else
stime=`date --date="-16 day"  +%Y%m%d`
etime=`date --date="-1 day" +%Y%m%d`
fi
let Flag=0
while :
do
if [ ${Flag} -eq 1 ];then
                stime=$(date -d "$stime 1day"  +%Y%m%d)
        fi
        let Flag=1
stime=$(date -d "$stime 1day"  +%Y%m%d)
        if [[ $stime -gt $etime ]]; then
                break
        fi
cat > ftpawx33.tmp << EOF
user gsics gsics
as
cd /AQUA/MODIS/L1/ORBIT/${stime:0:6}
nlist 
quit
EOF
echo ${stime}
jjj=`date -d "${stime}" +%j`
        ftp -v -n -i  10.0.66.33 < ftpawx33.tmp > ftpnamelist33.info
cat ftpnamelist33.info | grep "^MYD" | grep ".A${stime:0:4}${jjj}" | grep ".006." >namelist33.txt
if [[ ! -s namelist33.txt ]]
        then
                continue;
        fi
if [ ! -d  $DATADIR/${stime:0:6} ]
then
mkdir -p $DATADIR/${stime:0:6}
fi
DirPath=$DATADIR/${stime:0:6}/
declare -a order_array
let jj=0
for Name in `cat namelist33.txt`
do
url=ftp://10.0.66.33:/AQUA/MODIS/L1/ORBIT/${stime:0:6}/$Name
order_array[$jj]=${url}
let jj=${jj}+1
done
if [ ${#order_array[@]} -eq 0 ];then
unset ${order_array}
continue
fi
tmp_fifofile="/tmp/$$.fifo"
mkfifo  $tmp_fifofile
exec 6<> $tmp_fifofile
rm  $tmp_fifofile
for  ((i=0 ;i<$thread ;i++ ));
do
    echo 
done >&6
for  ((i = 0 ;i < ${#order_array[@]};i++ ));
do
    read -u6
    {
        DownLoad ${order_array[$i]} ${DirPath}
        echo >&6
    }&
done
wait
exec  6>&-
unset ${order_array}
done