aboutsummaryrefslogtreecommitdiffhomepage
path: root/bin/mail/log.sh
diff options
context:
space:
mode:
Diffstat (limited to 'bin/mail/log.sh')
-rwxr-xr-xbin/mail/log.sh34
1 files changed, 34 insertions, 0 deletions
diff --git a/bin/mail/log.sh b/bin/mail/log.sh
new file mode 100755
index 0000000..e96c9b4
--- /dev/null
+++ b/bin/mail/log.sh
@@ -0,0 +1,34 @@
1#! /bin/bash
2
3# Restore mail in variable
4MAIL="$(cat)"
5
6# Only execute the following script when mail receiver is log@topo.tw
7grep -qE "^X-Original-To: .*log@topo.tw[>]?$" <<<"$MAIL" || exit 0
8# A little hacky way to check if mail is sent from me
9sed -nE '/^Received: /p;/^$/q' <<<"$(MAIL)" | wc -l | xargs -i test {} -lt 2 || exit 0
10
11# Write a log
12date >>~/Downloads/log.log
13echo $$ >>~/Downloads/log.log
14awk -v RS= 'NR>1' <<<"$MAIL" >>~/Downloads/log.log
15
16LOG=~/log/`date +%y.w%W.md`
17TODAY="`date '+%a %b.%d'`"
18
19# If header of today doesn't exist
20# Create it and separate with 2 empty lines
21grep -Eq "^## ${TODAY}$" ${LOG} || \
22cat <<EOF >>${LOG}
23
24
25## $TODAY
26EOF
27
28
29# Save content to log file of current week
30echo >>${LOG}
31awk -v RS= 'NR>1' <<<"$MAIL" >>${LOG}
32
33# git commit
34{ cd ~/log && git add `basename ${LOG}` && git commit -m "Update by mail"; } >>~/Downloads/log.log