Mathematics in beermeter
[mirrors/Programs.git] / c / beermeter / beermeter.sh
CommitLineData
1e697494 1#!/bin/sh
1c6c46fe
TM
2pulses_per_liter='4380'
3beer_liters='0.5'
4beer_price='23'
5currency='Kč'
6
1e697494
TM
7title='Beer-O-Meter'
8accounts='./accounts'
2f17eaa1 9totals="$accounts/.totals"
1e697494
TM
10backend='./audio.sh'
11tmp="/tmp/beertmp-$$";
12dialog=$(which dialog);
13
1c6c46fe
TM
14calc() {
15 echo 'scale=2; '"$@" | bc
16}
17
1e697494 18beer_stat() {
1c6c46fe
TM
19 pulses=$(wc -c "$accounts/$1" | cut -d ' ' -f 1)
20 litres=$(calc $pulses/$pulses_per_liter)
21 beers=$(calc $litres/$beer_liters)
22 price=$(calc $beers*$beer_price)
23 echo $beers piv '('$price $currency, $litres l, $pulses pulses')'
1e697494
TM
24}
25
26add_account() {
27 "$dialog" --inputbox "New account name" 0 0 2>"$tmp"
28 [ $? = 0 ] && touch "$accounts/$(cat "$tmp")";
29}
30
31servis_menu() {
32 $dialog --menu "$title servis" 0 0 0 new "Novy stamgast" exit "Konec party" 2>"$tmp"
33 option="$(cat "$tmp")"
34
35 case $option in
36 new)
37 add_account
38 ;;
39 exit)
2f17eaa1
TM
40 clear
41 echo "=== Totals ($totals) ==="
42 echo
43 cat "$totals"
44 echo
1e697494
TM
45 exit
46 ;;
47 esac
48}
49
50beer_menu() {
2f17eaa1 51 echo -n > "$totals"
1e697494
TM
52 ls -1 --group-directories-first "$accounts" | while read i; do
53 echo -n "$i"; echo -ne "\x00";
54 echo -n $(beer_stat "$i"); echo -ne "\x00"
2f17eaa1 55 echo -e "$i\t$(beer_stat "$i")" >> "$totals"
1e697494
TM
56 done | xargs -0 $dialog --menu "$title stamgasti" 0 0 0
57}
58
59main_menu() {
60 while true; do
61 beer_menu 2>"$tmp"
62 [ "$?" = "0" ] && {
63 stamgast="$(cat "$tmp")"
2f17eaa1 64 clear
1e697494
TM
65 echo == Cepuje stamgast $stamgast, ukonci ctrl+c ==
66 "$backend" | tee -a "$accounts/$stamgast"
67 true
68 } || servis_menu
69 done
70}
71
72main_menu
This page took 0.189188 seconds and 4 git commands to generate.