DOWNLOAD FILES AND TRANSCODING THEN MAILING THEM ALL AUTOMATICALLY

#! /bin/bash
#version 2.0
#auther simon@codewalker.me
LF=/var/log/statement/daily/statement.log

YESTERDAY=`date +%Y%m%d -d 'yesterday'`
if [ $1 ]; then
	YESTERDAY=$1
fi

download ()
{
	if [ -f "Statement/zip/$YESTERDAY.Statement.zip" ]; then
		echo "Already sending $YESTERDAY.statement" | tee -a $LF
		read -n1 -p "Do you want force to send mail [Y|N]? " current_input
		case $current_input in
		Y|y)
			echo "Downloading ${YESTERDAY}_zhye-yh-cqg.zip file" | tee -a $LF
			curl "http://www.download.com/url"
		;;
		N|n)
			echo 'Choice no. exit' | tee -a $LF
			exit 0
		;;
		esac
			
	else
		if [ ! -f "download/$YESTERDAY/$YESTERDAY"_zhye-yh-cqg.zip ];then
			echo "Downloading ${YESTERDAY}_zhye-yh-cqg.zip file" | tee -a $LF
			curl "http://www.download.com/url"
		else
			echo "Found ${YESTERDAY}_*.zip zips will not download..." | tee -a $LF
		fi
	fi
}

makedirs()
{
	if [ ! -x "Statement/utf8/$YESTERDAY" ]; then  
		echo "Making dirs utf8/$YESTERDAY" | tee -a $LF
		mkdir -p "Statement/utf8/$YESTERDAY/" 
	else
		echo "dir Statement/utf8/$YESTERDAY existed." |tee -a $LF
	fi

	if [ ! -x "Statement/zip/" ]; then  
		echo "Making dir zip" | tee -a $LF
		mkdir -p "Statement/zip/" 
	else
		echo 'dir Statement/zip/ existed.' | tee -a $LF
	fi

	if [ ! -x "Statement/GBK/$YESTERDAY" ]; then  
		echo "Making dirs GBK/$YESTERDAY" | tee -a $LF
		mkdir -p "Statement/GBK/$YESTERDAY/" 
	else
		echo "dir Statement/GBK/$YESTERDAY existed." | tee -a $LF
	fi
}

unzipFiles()
{
	targetFile=`ls Statement/GBK/$YESTERDAY/ | wc -l` 
	if [ "$targetFile" -ge '1' ];then
        	echo "GBK/$YESTERDAY/*.csv found, will not unzip file"
	else
		if [ -f "download/$YESTERDAY/${YESTERDAY}_zhye-yh-cqg.zip" ];then
			echo "Finding *.zip then unzip them"| tee -a $LF
			find download -name "*.zip" -exec unzip -o -d Statement/GBK/$YESTERDAY {} \;
		else
       			echo "Zip file download/$YESTERDAY/${YESTERDAY}_zhye-yh-cqg.zip no found. Download failed" | tee -a $LF
			
		fi
	fi
}
clearFiles()
{
	echo "Clear up...*.zip"| tee -a $LF
	find download -name "*.zip" -exec rm -f {} \;
}

transaferFiles()
{
	targetFile=`ls Statement/GBK/$YESTERDAY/ | wc -l` 
	if [ "$targetFile" -ge '1' ];then
		echo "Encoding start..." | tee -a $LF
		files=`find Statement/GBK/$YESTERDAY -name "*.csv"`
		echo "iconf GBK to UTF8" | tee -a $LF
		for file in $files
		do
  			filename=`basename $file`
  			iconv -f GBK -t UTF-8 -c $file > Statement/utf8/$YESTERDAY/$filename
		done
	else
		echo "GBK/$YESTERDAY/$YESTERDAY/.csv files no found" | tee -a $LF
		errorReport
	fi

}

zipFiles()
{
	if [ ! -f "Statement/zip/$YESTERDAY.Statement.zip" ]; then
		echo "Zipping files into $YESTERDAY.Statement.zip" | tee -a $LF
		zip Statement/zip/$YESTERDAY.Statement.zip Statement/utf8/$YESTERDAY/*
	else
		echo "Found zip file will not zip them" | tee -a $LF
		errorReport
	fi
}

sendingFiles()
{
	echo "Sending $YESTERDAY.Statement.zip to 2406204350@qq.com and simon" | tee -a $LF
	echo "$YESTERDAY 对账单已发送" |mail -s "Statements for $YESTERDAY" -a Statement/zip/$YESTERDAY.Statement.zip -c mail@address.com  
}
errorReport(){
	echo "对账单发送错误了快查看" |mail -s "ERROR:Statements sending error" -a /var/log/statement/daily/statement.log  alert@mail.com  
	echo "对账单发送错误" | tee -a $LF
	echo '-------------------------------' >> $LF
	exit 1
}
main()
{
	echo 'Start... '`date +%Y%m%d-%H:%M:%S` | tee -a $LF
	download
	makedirs
	unzipFiles
	transaferFiles
	zipFiles
	if [ ! -f "Statement/zip/$YESTERDAY.Statement.zip" ]; then
		errorReport
	else
		sendingFiles
		clearFiles
	fi
	echo 'Done...'`date +%Y%m%d-%H:%M:%S` >> $LF
	echo '-------------------------------' >> $LF
}

main