From 5a47198e8101e0cd9664e67dee82eca13b2a604d Mon Sep 17 00:00:00 2001 From: Adrian Amaglio Date: Thu, 9 Sep 2021 12:24:22 +0200 Subject: [PATCH] first script --- main.sh | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100755 main.sh diff --git a/main.sh b/main.sh new file mode 100755 index 0000000..915574c --- /dev/null +++ b/main.sh @@ -0,0 +1,57 @@ +#!/bin/sh + +VCF_DIR=~/.contacts/contacts +DELTAD=10 + +today="$(date '+%Y %m %d')" +ty="${today:0:4}" +tm="${today:5:2}" +td="${today:8:2}" +now="$(date +%s)" + +display='' + +for vcf in "$VCF_DIR"/*.vcf ; do + bday_raw="$(cat $vcf | grep BDAY)" + if [ -z "$bday_raw" ] ; then continue ; fi + bday_raw="$(echo $bday_raw | cut -d ':' -f 2)" + bday_raw=${bday_raw:0:-1} + echo $bday_raw | grep '-' &>/dev/null + # format yyyy-mm-dd + if [ $? -eq 0 ] ; then + year="${bday_raw:0:4}" + month="${bday_raw:5:2}" + day="${bday_raw:8:2}" + # format yyyymmdd + elif [ ${#bday_raw} -eq 8 ] ; then + year="${bday_raw:0:4}" + month="${bday_raw:4:2}" + day="${bday_raw:6:2}" + # unknown format + else + echo "Impossible to determine date format for: $bday_raw" + continue + fi + + # Is the bday after or before today in civil year? + # This comparison can make dalta negative. Be careful with next instructions. + if [ "$month$day" -lt "$tm$td" ] ; then + ((nextbday_year=$ty+1)) + else + nextbday_year=$ty + fi + + # How many days to next bday + nextbday_stamp=$(date --date="$nextbday_year$month$day" +%s) + ((delta_s=$nextbday_stamp-$now)) + ((delta_d=$delta_s/3600/24)) + + # Get all bdays + if [ "$delta_d" -le "$DELTAD" ] ; then + name="$(cat $vcf | grep 'FN:' | cut -d ':' -f 2)" + display="$display${name:0:-1} : $year-$month-$day\n" + fi + +done +# Send notification if date is close +notify-send "Anniversaires" "$display"