#!/bin/bash

. ./param_bash.ini

usr=$1
dir=$2
ftp_s=$3
TEMP=$4
FTP_TO_DIR=$5
FTP_PASSWD=$6
###############################################################################################
ftp_log_file=$IKIT_DIR/log/ftp_check.log
echo "`date` - check started $dir" >> $ftp_log_file || echo "cannot write to log file"
#download cksum_files_list file that exist on the target directory
rm -f ${TEMP}/cksum_files_list.* >& /dev/null
$WGET -c -o /dev/null -O ${TEMP}/cksum_files_list.$$ "ftp://${usr}:${FTP_PASSWD}@${ftp_s}/${dir}/cksum_files_list"  
for line in `cat ${TEMP}/cksum_files_list.$$`
do
    temp1=`echo $line | cut -d ";" -f1`
    block_size=`echo $line | cut -d ";" -f2`
    temp2=`echo $line | cut -d ";" -f3`
    if [ ! -e ${FTP_TO_DIR}/$temp2 ] ; then
        echo "$temp2 Does not exist" >> $ftp_log_file
        echo "$temp2 Does not exist" >> $gen_log
        exit 1 
    fi 
    if [ -e ${FTP_TO_DIR}/$temp2 ] ; then
        cur_block_size=`ls -l ${FTP_TO_DIR}/$temp2 | awk '{print $5}' `
        if [ "$block_size" != "$cur_block_size" ] ; then
            echo "$temp2 size dont match" >> $ftp_log_file
            echo "$temp2 size dont match" >> $gen_log
            exit 1
        else
            cur_cs=`cksum ${FTP_TO_DIR}/$temp2 | awk '{print $1}'`
            if [ "$temp1" != "$cur_cs" ] ; then
                echo "$cur_cs chksum failed" >> $ftp_log_file
                echo "$cur_cs chksum failed" >> $gen_log
                exit 1
            fi
        fi
    fi
done
echo "`date` - check success" >> $ftp_log_file
exit 0
