refactor and add comments

This commit is contained in:
finga 2020-01-07 17:08:18 +01:00
parent 9143df0017
commit fc00f7696b

View file

@ -1,19 +1,31 @@
#!/bin/bash
set -e
conf_file="$HOME/.birthdays"
days_left=30
# read file into line seperated string array (could become sorted)
readarray lines < "$conf_file"
# iterate over each field
for i in "${lines[@]}"; do
days=$(( 10#$(date -d "$( cut -f 1 <<< "$i" )" +%j 2>/dev/null || echo 59) - 10#$(date +%j) ))
# get date
date=$(cut -d" " -f1 <<< "$i")
# calculate days from now until $date if date fails it could be 2/29
days=$(( 10#$(date -d "$date" +%j 2>/dev/null || echo 059) - 10#$(date +%j) ))
# if $days is in range of $days_left
if [ 0 -le "$days" ] && [ "$days" -le "$days_left" ];then
if [ "$days" -le $((days_left / 2)) ] && [ ! "$days" -le 7 ];then
echo -e "\\033[33m$( cut -f 2- <<< "$i" ) in $days days\\033[m"
elif [ "$days" -le 7 ];then
echo -e "\\033[31m$( cut -f 2- <<< "$i" ) in $days days\\033[m"
# if $days is between less than 8: output red counter
if [ "$days" -le 7 ];then
echo -e "\\033[31m$(cut -f2- <<< "$i") in $days days\\033[m"
# if $days is between 8-15: output yellow counter
elif [ "$days" -le $((days_left / 2)) ];then
echo -e "\\033[33m$(cut -f2- <<< "$i") in $days days\\033[m"
# else print green counter
else
echo -e "\\033[32m$( cut -f 2- <<< "$i" ) in $days days\\033[m"
echo -e "\\033[32m$(cut -f2- <<< "$i") in $days days\\033[m"
fi
fi
done