summaryrefslogtreecommitdiff
path: root/bin/bibleplan
blob: 6d31667f306863617a45e9a7b2965bd8adc3a607 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/bin/sh
# Reads a delimited text file containing a daily Bible reading plan,
# finds the line corresponding to the current date, extracts each
# passage to be read, and sends them sequentially to the "kjv" program.
# If given a date on $1, finds the reading for that date instead.
# The -l option prints the passages listed for that date instead of
# sending them to kjv.

filedetails() {
  delim=";" # character that separates fields in reading plan file
  plandir="/usr/local/share/bibleplan" # path of plan directory
  planfile="plan.txt" # name of plan file
  case "$1" in
    show) echo $plandir/$planfile ;;
    edit) $EDITOR $plandir/$planfile ;;
    make) makeplan $2 ;;
  esac 
}

checkkjv() {
  [ -z $(which kjv) ] && echo "bibleplan: Couldn't find kjv(1). Is it installed and in your \$PATH?" && exit
}

printweekdays() {
  year="$1" && i=0
  dayone="Mon"
  daysperweek=5
  while [ "$day" != "$dayone" ]
  do
    i=$((i+1)) && day=$(date -d "$year-01-0$i" +%a)
  done
  day="$year-01-0$i" && i=0 && j=0
  while [ "$j" -lt 52 ]
  do
    i=0 && printf "\n"
    while [ "$i" -lt $daysperweek ]
    do
      k=$((i+j*7)) && echo "$(date -d "$day + $k days" +%d%^b);" && i=$((i+1))
    done
    j=$((j+1)) && printf "\n"
  done
}

makeplan() {
  printweekdays $1 | paste - $plandir/5daytemplate.txt -d\  | sed -e 's/^ //g'
}

makeuppercase() {
  echo "$1" | tr [:lower:] [:upper:]
}

todaydtg() {
  month=`date +%^b` # current month in the form "JAN" "FEB" etc
  day=`date +%d`    # day of month in the form "01" "31" etc
  dtg=$day$month
  echo $dtg
}

findpassages() {
  # to do: implement regex handling for proper dates
  dtg=$(makeuppercase $dtg)
  todaysreading=$(cat $plandir/$planfile | grep $dtg)
  [ -z "$todaysreading" ] && echo "No passages for $(echo $dtg | cut -c1,2) $(echo $dtg | cut -c3,4,5)." && exit 1
}

listpassages() {
  filedetails
  [ $# -eq 1 ] && dtg=$(paddtg $1) || dtg=$(todaydtg)
  dtg=$(makeuppercase $dtg)
  findpassages
  echo "$todaysreading"
  exit 0
}

paddtg() {
  [ $(echo "$1" | wc -m ) = 5 ] && echo 0$1 || echo $1
}

readpassages() {
  checkkjv
  filedetails
  [ $# -eq 1 ] && dtg=$(paddtg $1) || dtg=$(todaydtg)
  dtg=$(makeuppercase $dtg)
  [ $(echo -n "$dtg" | wc -c) = 4 ] && dtg=0$dtg
  findpassages
  i=1
  numberofpassages=$(echo "$todaysreading" | grep -o $delim | wc -l)
  while [ $i -lt $((numberofpassages+1)) ]
  do
    passage=$(echo $todaysreading | cut -f$((i+1)) -d$delim) # read j-th field
    kjv $passage
    i=$((i+1)) 
  done
}

help() {
  cat << EOF
bibleplan - Follow a Bible-reading plan with kjv(1).
Written 2020 by Ray Patrick.

Usage:
        $ bibleplan [date]
                If assigned reading exists for [date], opens it in kjv(1).
                If [date] is not given, looks up today's reading by
                default. Dates may be of the form: 1jan, 01jan, 1JAN,
                01JAN, etc.

        $ bibleplan -l, --list [date]
                Lists the assigned reading for [date]. If [date] is not
                given, looks up today's reading by default.

        $ bibleplan -f, --file
                Shows the plan file that bibleplan is using.

        $ bibleplan -e, --edit
                Opens the current plan file in \$EDITOR.

        $ bibleplan -m, --make [year]
                Will make a new 5-day-per-week plan for [year].
                The new plan will be emitted to stdout. You can do
                something like:
                          $ bibleplan -m 2021 > 2021plan.txt
                to write the plan to a text file.

        $ bibleplan -h, --help
                Print this help and exit. See 'man bibleplan' for more
                in-depth documentation.

EOF
}

case "$1" in
  -l|--list) listpassages $2 ;;
  -f|--file) filedetails show ;; 
  -e|--edit) filedetails edit ;;
  -m|--make) filedetails make $2 ;;
  -h|--help) help ;;
  #*) readpassages $1 ;;
  *) case "$2" in
    -l|--list) listpassages $1 ;;
    *) readpassages $1 ;;
  esac
esac