citadel

My dotfiles, scripts and nix configs
git clone git://jb55.com/citadel
Log | Files | Refs | README | LICENSE

nannypay (2291B)


      1 #!/usr/bin/env bash
      2 
      3 set -e
      4 
      5 if [ -z "$NANNY" ]; then
      6     printf "set NANNY (and make sure PAY is correct)\n"
      7     exit 1
      8 fi
      9 
     10 pay=20
     11 
     12 if [ -z $pay ]; then
     13     printf "unknown pay for %s\n" "$NANNY"
     14     exit 1
     15 fi
     16 
     17 
     18 CSV="${NANNYPAY_CSV:-$HOME/.local/share/nannypay/${NANNY}.tsv}"
     19 
     20 set -e
     21 mkdir -p "$(dirname "$CSV")"
     22 if [ ! -f "$CSV" ]; then
     23     touch "$CSV"
     24 fi
     25 
     26 deps() {
     27     printf "looks like 'ratio' is broken fam\n" 1>&2
     28     exit 3
     29 }
     30 
     31 ratio <<<"0" >/dev/null || deps
     32 
     33 cmd="$3"
     34 
     35 usage () {
     36     printf "usage: nannypay <start-time> <end-time> [r|p]\n" 1>&2
     37     show_entries
     38     exit 1
     39 }
     40 
     41 duplicate () {
     42     printf "there is a already an entry for %s\n" "$1" 1>&2
     43     show_entries
     44     exit 2
     45 }
     46 
     47 show_entries () {
     48     n=${1:-12}
     49     printf "\nlast %d entries:\n" "$n" 1>&2
     50     (printf "date\tamount\tstart\tend\tminutes\thour_rate\tpaid?\n"; tail -n "$n" "$CSV") | column -t -s $'\t' 1>&2
     51     printf "\n%s to be paid\n" "$(to_pay_amt)" 1>&2
     52 }
     53 
     54 
     55 do_pay () {
     56     sed -i"" "/\*$/! s,$,\t\*,g" "$CSV"
     57 }
     58 
     59 
     60 
     61 to_pay_amt () {
     62     dates=${1:-$(grep -v '\*$' "$CSV")}
     63     <<<"$dates" \
     64       cut -d $'\t' -f2 \
     65     | paste -sd+ \
     66     | ratio \
     67     | bc -l \
     68     | xargs printf "%.02f\n"
     69 }
     70 
     71 pay() {
     72     dates=$(grep -v '\*$' "$CSV")
     73     amt=$(to_pay_amt "$dates")
     74 
     75     if [ "$amt" = "0.00" ]; then
     76         printf "nothing to pay!\n" 1>&2
     77     else
     78         printf "paying amt %s from dates:\n%s\ncontinue? (y/n)\n" "$amt" "$dates" 1>&2
     79 
     80         read -r confirm
     81 
     82         if [ "$confirm" = "y" ]; then
     83             do_pay "$amt"
     84         else
     85             printf "cancelled.\n" 1>&2
     86         fi
     87     fi
     88 }
     89 
     90 if [ -z "$1" ] && [ -z "$2" ]
     91 then
     92     usage
     93 fi
     94 
     95 get_minutes() {
     96     datediff "$1" "$2" -f %M
     97 }
     98 
     99 get_amt() {
    100      <<<"$1" xargs printf "%s/(60/$pay)\n" | ratio
    101 }
    102 
    103 if [ "$cmd" = "r" ]; then
    104     start="$1"
    105     end="$2"
    106     minutes=$(get_minutes "$start" "$end")
    107     amt=$(get_amt "$minutes")
    108     day=${4:-$(date +%F)}
    109 
    110     printf "%s\n" "$amt"
    111     #grep "$day" "$CSV" >/dev/null && duplicate "$day"
    112     printf "%s\t%s\t%s\t%s\t%s\t%s\n" "$day" "$amt" "$start" "$end" "$minutes" "$pay" >> "$CSV"
    113 elif [ "$1" = "p" ] || [ "$cmd" = "p" ]; then
    114     pay
    115 elif [ "$1" = "e" ]; then
    116     "$EDITOR" "$CSV"
    117 else
    118     minutes=$(get_minutes "$1" "$2")
    119     amt=$(get_amt "$minutes")
    120     printf "%s\n" "$amt"
    121 fi
    122 
    123 show_entries