1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#! /bin/bash
#version 2.1
#auther simon@codewalker.me
LF=/var/log/statement/daily/statement.log

#current work directory
CW='/path/to/work/space'
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.someurl.com/files"
;;
N|n)
echo 'Choice no. exit' | tee -a $LF
;;
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.someurl.com/files"
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
fi
}

sendingFiles()
{
echo "Sending $YESTERDAY.Statement.zip to someone and simon" | tee -a $LF
echo "$YESTERDAY 对账单已发送" |mail -s "Statements for $YESTERDAY" -a Statement/zip/$YESTERDAY.Statement.zip -c someone@address.com me@email.com
}

sendingSingleFile()
{
targetFile=\`find Statement/utf8/${YESTERDAY}/ -name "${YESTERDAY}_jymx-zjtg*"\`
echo "$YESTERDAY 对账单已发送" |mail -s "Statements for $YESTERDAY" -a $targetFile -c someone@address.com me@email.com
}
errorReport(){
echo "对账单发送错误了快查看" |mail -s "ERROR:Statements sending error" -a /var/log/statement/daily/statement.log alert@email.com
echo "对账单发送错误" | tee -a $LF
echo '-------------------------------' >> $LF
exit 1
}
main()
{
echo 'Start... '\`date +%Y%m%d-%H:%M:%S\` | tee -a $LF
echo "Moving to $CW" | tee -a $LF
cd $CW
download
makedirs
unzipFiles
transaferFiles
zipFiles
if \[ ! -f "Statement/zip/$YESTERDAY.Statement.zip" \]; then
errorReport
else
#sendingFiles
sendingSingleFile
clearFiles
fi
echo 'Done...'\`date +%Y%m%d-%H:%M:%S\` >> $LF
echo '-------------------------------' >> $LF
}

main