Mathematics in beermeter
[mirrors/Programs.git] / c / beermeter / beermeter.sh
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 calc() {
15 echo 'scale=2; '"$@" | bc
16 }
17
18 beer_stat() {
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')'
24 }
25
26 add_account() {
27 "$dialog" --inputbox "New account name" 0 0 2>"$tmp"
28 [ $? = 0 ] && touch "$accounts/$(cat "$tmp")";
29 }
30
31 servis_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)
40 clear
41 echo "=== Totals ($totals) ==="
42 echo
43 cat "$totals"
44 echo
45 exit
46 ;;
47 esac
48 }
49
50 beer_menu() {
51 echo -n > "$totals"
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"
55 echo -e "$i\t$(beer_stat "$i")" >> "$totals"
56 done | xargs -0 $dialog --menu "$title stamgasti" 0 0 0
57 }
58
59 main_menu() {
60 while true; do
61 beer_menu 2>"$tmp"
62 [ "$?" = "0" ] && {
63 stamgast="$(cat "$tmp")"
64 clear
65 echo == Cepuje stamgast $stamgast, ukonci ctrl+c ==
66 "$backend" | tee -a "$accounts/$stamgast"
67 true
68 } || servis_menu
69 done
70 }
71
72 main_menu
This page took 0.285212 seconds and 4 git commands to generate.