| 1 | #!/bin/sh |
| 2 | pulses_per_liter='4380' |
| 3 | beer_liters='0.5' |
| 4 | beer_price='23' |
| 5 | currency='Kč' |
| 6 | |
| 7 | title='Beer-O-Meter' |
| 8 | accounts='./accounts' |
| 9 | totals="$accounts/.totals" |
| 10 | backend='./audio.sh' |
| 11 | tmp="/tmp/beertmp-$$"; |
| 12 | dialog=$(which dialog); |
| 13 | |
| 14 | #set +bm #job controll messages were messing up dialog's output!!! |
| 15 | |
| 16 | rst() { echo -ne "\033[0;0H"; } |
| 17 | |
| 18 | calc() { |
| 19 | precise="$(echo 'scale=10; '"$@" | bc)" |
| 20 | echo 'scale=2; '"$precise/1" | bc |
| 21 | } |
| 22 | |
| 23 | beer_calc() { |
| 24 | pulses="$1" |
| 25 | litres=$(calc "$pulses/$pulses_per_liter") |
| 26 | beers=$(calc "($pulses/$pulses_per_liter)/$beer_liters") |
| 27 | price=$(calc "(($pulses/$pulses_per_liter)/$beer_liters)*$beer_price") |
| 28 | echo $beers beers '('$price $currency, $litres l, $pulses pulses')' |
| 29 | } |
| 30 | |
| 31 | beer_pulses() { |
| 32 | echo $(wc -c "$accounts/$1" | cut -d ' ' -f 1) |
| 33 | } |
| 34 | |
| 35 | beer_stat() { |
| 36 | beer_calc "$(beer_pulses "$1")" |
| 37 | } |
| 38 | |
| 39 | add_account() { |
| 40 | "$dialog" --inputbox "New account name" 0 0 2>"$tmp" |
| 41 | stamgast=$(cat "$tmp") |
| 42 | [ $? = 0 ] && [ -n "$stamgast"] && touch "$accounts/$stamgast"; |
| 43 | } |
| 44 | |
| 45 | servis_menu() { |
| 46 | $dialog --menu "$title servis" 0 0 0 \ |
| 47 | tap "Seznam stamgastu" \ |
| 48 | new "Novy stamgast" \ |
| 49 | exit "Konec party" \ |
| 50 | 2>"$tmp" |
| 51 | option="$(cat "$tmp")" |
| 52 | |
| 53 | case $option in |
| 54 | new) |
| 55 | add_account |
| 56 | ;; |
| 57 | exit) |
| 58 | clear |
| 59 | echo "=== Totals ($totals) ===" |
| 60 | echo |
| 61 | cat "$totals" |
| 62 | echo |
| 63 | exit |
| 64 | ;; |
| 65 | esac |
| 66 | } |
| 67 | |
| 68 | beer_menu() { |
| 69 | echo -n > "$totals" |
| 70 | ls -1 --group-directories-first "$accounts" | while read i; do |
| 71 | echo -n "$i"; echo -ne "\x00"; |
| 72 | echo -n $(beer_stat "$i"); echo -ne "\x00" |
| 73 | echo -e "$i\t$(beer_stat "$i")" >> "$totals" |
| 74 | done | xargs -0 $dialog --menu "$title stamgasti" 0 0 0 2>"$1" |
| 75 | } |
| 76 | |
| 77 | tap() { |
| 78 | stamgast="$1" |
| 79 | |
| 80 | pulses_start=$(beer_pulses "$stamgast") |
| 81 | clear |
| 82 | "$backend" >> "$accounts/$stamgast" & |
| 83 | pid="$!" |
| 84 | while kill -0 "$pid" 2>/dev/null; do |
| 85 | #echo "$stamgast" > /tmp/testo |
| 86 | pulses_current=$(beer_pulses "$stamgast") |
| 87 | pulses_diff=$[ $pulses_current - $pulses_start ] |
| 88 | |
| 89 | rst |
| 90 | echo == Cepuje stamgast "$stamgast", ukonci stisknutim [ENTER] == |
| 91 | echo "CURRE: $(beer_calc $pulses_diff) "; |
| 92 | echo "TOTAL: $(beer_calc $pulses_current) "; |
| 93 | sleep 0.3; |
| 94 | done & |
| 95 | read -n 1 |
| 96 | pkill -TERM -P "$pid" 2>/dev/null |
| 97 | kill -TERM "$pid" 2>/dev/null |
| 98 | (sleep 3; kill -KILL "$pid" 2>/dev/null) & |
| 99 | } |
| 100 | |
| 101 | main_menu() { |
| 102 | while true; do |
| 103 | beer_menu "$tmp" |
| 104 | [ "$?" = "0" ] && { |
| 105 | tap "$(cat "$tmp")" |
| 106 | true |
| 107 | } || servis_menu |
| 108 | done |
| 109 | } |
| 110 | |
| 111 | main_menu |