pizzacalc/pizza.sh

112 lines
4.1 KiB
Bash
Executable file

#!/usr/bin/env bash
INSTDIR="$(dirname "$(readlink -f "$0")")" # find the install directory
RECIPE="${RECIPE:-quick}"
VARIATION="${VARIATION:-standmixer}"
COUNT="${COUNT:-1}"
shopt -s extglob
function pizzahelp { # this only prints help text
echo "Pizza Calculator: The pizza script you never asked for"
echo
echo "Usage: $0 [ -f <N> ] [ -c <N> ] [ -r <recipe> ] [ <pan diameter>(in|inch|\") ]"
echo " $0 [ -r <recipe> ] [ <flour weight>g ]"
echo " $0 -h"
echo " $0 [ -v <variation> ] [ -r <recipe> ] -i"
echo "Options:"
echo " -h Print this help"
echo " -i Print the instructions"
echo " -f <N> Set flour per square inch (default: 2.122)"
echo " -c <N> Set the count (default: 1)"
echo " -v <variation> Set instruction variation (default: standmixer)"
echo " -r <recipe> Set recipe (default: quick)"
echo "Notes:"
echo " The dough can be kept overnight in the refidgerator if needed"
echo " Water may also be used in place of beer"
echo " When in interactive mode, -f will get overridden"
echo " Try adding your choice of spices and/or herbs into the dough for more flavor"
echo
}
if [ -a "${XDG_CONFIG_HOME}/pizzacalc/conf" ]; then
ALTPIZZA="${XDG_CONFIG_HOME}/pizzacalc/conf"
elif [ -a "$HOME/.config/pizzacalc/conf" ]; then
ALTPIZZA="${HOME}/.config/pizzacalc/conf"
else
ALTPIZZA=/dev/null
fi
function pizzarecipe {
echo "How to make the $RECIPE calculator pizza, $VARIATION edition:"
cat "$INSTDIR/pizza.txt" | cat - "$ALTPIZZA" | sed -n -E "/\[$RECIPE:instructions(:$VARIATION)?\]/,/^$/p" | sed '/\[*.\]/d'
}
while getopts ":hiv:r:f:c:" option; do # loop through options, casing them
case $option in
h)
pizzahelp
exit;;
i)
pizzarecipe
exit;;
f) # the end result of changing this is to alter crust thickness
FSI="$OPTARG";;
v) # change variation (usually between hand and standmixer)
VARIATION="$OPTARG";;
r) # change dough recipe
RECIPE="$OPTARG";;
c) # change the number of pizzas the dough can be split into
COUNT="$OPTARG";;
\?)
echo "Error: Invalid option"
exit;;
esac
done
shift $((OPTIND-1)) # shift options out from infront of the command to leave only arugment in $@
function panarea {
DFSI=$(cat "$INSTDIR/pizza.txt" | cat - "$ALTPIZZA" | grep -A 1 "\[$RECIPE:ratios\]" | tail -n -1 | cut -f 2-) # find recipe's default flour per squre inch
FSI="${FSI:-$DFSI}" # dont use default fsi if already set
AREA=$(echo "3.1415926535*($WIDTH/2)^2" | bc -l) # bc cant exponentiate non-integer bases without -l
SIZE=$(echo "$COUNT"*$AREA*"$FSI" | bc -l)
arraytio
}
function arraytio { # im funny portmantaus are never bad
[ -n SIZE ] || read -p "Grams of flour: " SIZE # test ([) will send a non-zero exit code on if $SIZE doesnt exist, tripping the or (the '||')
INDEX=0
for RATIO in $(cat "$INSTDIR/pizza.txt" | cat - "$ALTPIZZA" | sed -n "/\[$RECIPE:ratios\]/,/^$/p" | tail -n +3 | cut -f 2-); do # finding [ratios] until the next blank line, removing the first two lines.
declare -a INGREDIENT # make arrays
declare -a MEASURE
((INDEX++)) # increment the index
INGREDIENT[$INDEX]=$(cat "$INSTDIR/pizza.txt" | cat - "$ALTPIZZA" | sed -n "/\[$RECIPE:ratios\]/,/^$/p" | tail -n +3 | cut -f 1 | sed $INDEX'!d') # same deal but itterating through the INDEX
MEASURE[$INDEX]=$(echo "scale = 2; ($RATIO*$SIZE)/1" | bc -l) # we have to divide by 1 to actually apply the scale
echo ${INGREDIENT[$INDEX]}: ${MEASURE[$INDEX]}g # actually print the calculated number with label for each INDEX
done
# cleanup
unset INDEX
unset RATIO
unset SIZE
unset FSI
unset AREA
unset WIDTH
}
case $1 in # this parses the first argument (and actually calls our main functions), the units need to be removed before bc will accept it
+([0-9])?(.)*([0-9])g)
SIZE=$(echo $1 | sed s/g//g)
arraytio;;
+([0-9])?(.)*([0-9])\")
WIDTH=$(echo $1 | sed s/\"//g)
panarea;;
+([0-9])?(.)*([0-9])inch)
WIDTH=$(echo $1 | sed s/inch//g)
panarea;;
+([0-9])?(.)*([0-9])in)
WIDTH=$(echo $1 | sed s/in//g)
panarea;;
*)
read -p "Diameter of pan (in inches): " WIDTH
DFSI=$(cat "$INSTDIR/pizza.txt" | cat - "$ALTPIZZA" | grep -A 1 "\[$RECIPE:ratios\]" | tail -n -1 | cut -f 2-)
read -p "Flour per square inch [$DFSI]: " FSI
panarea;;
esac