prayertimes-sh/prayertime.sh

100 lines
3.4 KiB
Bash

PRAYERTIMES_CSV="prayertimes.csv"
# PT = prayer time
# ESec = Epoch second
# Human = Human readable
function convertPTToESec() {
local arg="$(( $1 / 60 )):$(( $1 % 60 )) today"
echo $(date -d "$arg" +%s)
}
function convertPTToESecTomorrow() {
local arg="$(( $1 / 60 )):$(( $1 % 60 )) tomorrow"
echo $(date -d "$arg" +%s)
}
function convertPTToHuman() {
# echo $1
HOUR="$(( $1 / 60 ))"
if (( $(( $1 / 60 )) < 10 )) ; then
HOUR="0$(( $1 / 60 ))"
fi
MINUTE="$(( $1 % 60 ))"
if (( $(( $1 % 60 )) < 10 )) ; then
MINUTE="0$(( $1 % 60 ))"
fi
echo "$HOUR:$MINUTE"
# echo "$(( $1 / 60 )):$(( $1 % 60 ))"
}
# thanks https://devhints.io/bash#getting-options
while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in
-h | --help )
echo "Usage: $0 [OPTIONS] [FILE]"
echo "Options:"
echo " -h, --help Show this help message and exit"
echo " -f, --file Use FILE as the prayer times CSV file"
exit 0
;;
-f | --file )
shift; PRAYERTIMES_CSV=$1
;;
esac; shift; done
if [[ "$1" == '--' ]]; then shift; fi
NOW=$(date +%s)
DAY_OF_YEAR=$(($(date -d "@$NOW" +%j) - 1))
PRAYERTIMES_CSV=$(realpath $PRAYERTIMES_CSV)
if [[ ! -s $PRAYERTIMES_CSV ]]; then
echo "Prayertimes db is missing"
echo "Attempting to fetch it from github..."
echo "Enter the Island ID (more info in https://github.com/WovenCoast/prayertimes-db/blob/main/island-mapping.md) :"
read ISLAND_ID
if [[ -z $ISLAND_ID ]]; then
echo "No Island ID entered. Defaulting to 102"
ISLAND_ID=102
fi
echo "Fetching prayertimes db from github..."
wget -O $PRAYERTIMES_CSV "https://raw.githubusercontent.com/WovenCoast/prayertimes-db/main/out/$ISLAND_ID.csv"
if [[ ! -s $PRAYERTIMES_CSV ]]; then
echo "Failed using wget, trying curl..."
curl -o $PRAYERTIMES_CSV "https://raw.githubusercontent.com/WovenCoast/prayertimes-db/main/out/$ISLAND_ID.csv"
if [[ ! -s $PRAYERTIMES_CSV ]]; then
echo "Failed to fetch prayertimes db from github"
exit 1
fi
fi
echo ""
echo "Prayertimes db fetched successfully"
echo "To change this, delete the file $PRAYERTIMES_CSV and run this script again to choose another island id"
fi
# this really isn't necessary but
# if you do have a better way, feel free to contribute
today_Fajr=$(grep "^$DAY_OF_YEAR," $PRAYERTIMES_CSV | cut -d, -f2)
today_Sunrise=$(grep "^$DAY_OF_YEAR," $PRAYERTIMES_CSV | cut -d, -f3)
today_Dhuhr=$(grep "^$DAY_OF_YEAR," $PRAYERTIMES_CSV | cut -d, -f4)
today_Asr=$(grep "^$DAY_OF_YEAR," $PRAYERTIMES_CSV | cut -d, -f5)
today_Maghrib=$(grep "^$DAY_OF_YEAR," $PRAYERTIMES_CSV | cut -d, -f6)
today_Isha=$(grep "^$DAY_OF_YEAR," $PRAYERTIMES_CSV | cut -d, -f7)
tomorrow_Fajr=$(grep "^$((($DAY_OF_YEAR + 1) % 366))," $PRAYERTIMES_CSV | cut -d, -f2)
if [[ $(convertPTToESec $today_Fajr) -ge $NOW ]]; then
echo "Fajr $(convertPTToHuman $today_Fajr)"
# elif [[ $(convertPTToESec $today_Sunrise) -ge $NOW ]]; then
# echo "Sunrise $(convertPTToHuman $today_Sunrise)"
elif [[ $(convertPTToESec $today_Dhuhr) -ge $NOW ]]; then
echo "Dhuhr $(convertPTToHuman $today_Dhuhr)"
elif [[ $(convertPTToESec $today_Asr) -ge $NOW ]]; then
echo "Asr $(convertPTToHuman $today_Asr)"
elif [[ $(convertPTToESec $today_Maghrib) -ge $NOW ]]; then
echo "Maghrib $(convertPTToHuman $today_Maghrib)"
elif [[ $(convertPTToESec $today_Isha) -ge $NOW ]]; then
echo "Isha $(convertPTToHuman $today_Isha)"
elif [[ $(convertPTToESecTomorrow $tomorrow_Fajr) -ge $NOW ]]; then
echo "Fajr $(convertPTToHuman $tomorrow_Fajr)"
fi