#!/bin/bash

#set env
export PATH=$PATH:/bin:/sbin:/usr/sbin


function sortfile() {

logfile1=/home/work/semantic_platform_DAS/bottom_function/data
logfile1_1=${logfile1}/semantic/m1
logfile1_2=${logfile1}/semantic/m1_1
logfile2=/home/work/logfile/ctoc/unisound
logfile2_1=${logfile1}/semantic/m2
logfile3=${logfile1}/semantic/unorder/unisoundCtoC_logfile_`date +%Y%m%d`

logfile4=${logfile1}/semantic/order/semantic.txt
logfile5=${logfile1}/semantic/order/semantic_`date +%Y%m%d`

[ -d "${logfile1_1}" ] || mkdir -p "${logfile1_1}"
[ -d "${logfile1_2}" ] || mkdir -p "${logfile1_2}"
[ -d "${logfile2_1}" ] || mkdir -p "${logfile2_1}"
[ -d "${logfile1}/semantic/unorder" ] || mkdir -p "${logfile1}/semantic/unorder"
[ -d "${logfile1}/semantic/order" ] || mkdir -p "${logfile1}/semantic/order"

[ -f "${logfile3}" ] || touch "${logfile3}"
[ -f "${logfile4}" ] || touch "${logfile4}"
[ -f "${logfile5}" ] || touch "${logfile5}"

[ -f "${logfile1_2}/unisoundCtoC_logfile_`date +%Y%m%d`" ] || touch "${logfile1_2}/unisoundCtoC_logfile_`date +%Y%m%d`"
[ -f "${logfile2_1}/unisoundCtoC_logfile_`date +%Y%m%d`" ] || touch "${logfile2_1}/unisoundCtoC_logfile_`date +%Y%m%d`"


rsync -av root@10.7.19.130::testlogs ${logfile1_1} --password-file=/etc/rsync.password >/dev/null 
touch /tmp/temp
temp=/tmp/temp
diff ${logfile1_2}/unisoundCtoC_logfile_`date +%Y%m%d` ${logfile1_1}/unisoundCtoC_logfile_`date +%Y%m%d` | grep  ">"|awk -F "> " '{print $2}' > ${temp}
rsync -av ${logfile1_1}/unisoundCtoC_logfile_`date +%Y%m%d` ${logfile1_2}/unisoundCtoC_logfile_`date +%Y%m%d` >/dev/null
cat ${temp} >>${logfile1_2}/unisoundCtoC_logfile_`date +%Y%m%d`
cat /dev/null>${logfile3}
cat ${temp} >> ${logfile3}


diff ${logfile2_1}/unisoundCtoC_logfile_`date +%Y%m%d` ${logfile2}/unisoundCtoC_logfile_`date +%Y%m%d` | grep  ">"|awk -F "> " '{print $2}' > ${temp}
rsync -av ${logfile2}/unisoundCtoC_logfile_`date +%Y%m%d` ${logfile2_1}/unisoundCtoC_logfile_`date +%Y%m%d` >/dev/null
cat ${temp} >>${logfile2_1}/unisoundCtoC_logfile_`date +%Y%m%d`
cat ${temp} >> ${logfile3}
#sed 'N;s#\n#,#g' ${logfile3}


if [ -s ${logfile3} ]
then
	paste -sd ',\n' ${logfile3} | sort -u > ${logfile4}
else
	echo 0
	exit 1
fi	

lastEndTime=`tail -1 ${logfile5} |awk -F "(])|( request)" '{print $2} '`
newStartTime=`head -1 ${logfile4} |awk -F "(])|( request)" '{print $2} '`
t1=`date -d "${lastEndTime}" +%s`
t2=`date -d "${newStartTime}" +%s`

if [ ! -s ${logfile5} ] 
then
        cat ${logfile4}>>${logfile5}
else
	[ ${t1} -lt ${t2} ] && cat ${logfile4}>>${logfile5}||echo "no new logfiles"
fi
find ${logfile1}/semantic/order -name "unisoundCtoC_logfile_*" -type f -mtime +30|xargs rm -f
echo ${logfile4}
}
sortfile

