birthday-reminder/main.sh
2021-09-09 12:24:22 +02:00

58 lines
1.5 KiB
Bash
Executable File

#!/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"