#!/usr/bin/ruby

# ChangeLog
#   ? Sat, 26 Aug 2006 10:12:59 -0600
#   : Fixed a major hydration bug. Releasing as 0.2
#   ? Sun Jun 12 18:59:52 MDT 2005
#   : added conventional volume equivalents for flour and salt
#   ? Tue, 14 Sep 2004 11:38:39 -0600 
#   : initial release (0.1)

# sourdough calculator
# Copyright © 2004-2006, Hans Fugal
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# The algorithms used are of course simple enough, and the defaults were chosen
# to match my tastes.

# defaults
dough = 500		# grams
hydration = 0.68	# baker's
start = 0.20		# percent
start_hydration = 1.00	# baker's
salt = 0.018		# baker's relative to flour
yaml = false

# options
require 'optparse'
opts = OptionParser.new do |opts|
  opts.banner = 'usage: sourdough [options]'

  opts.on('-d',"--dough-weight=#{dough}",Float,'grams (before salt)'){|dough|}
  opts.on('-w',"--hydration=#{hydration}",Float,"(baker's percentage)"){|hydration|}
  opts.on('-s',"--start=#{start}",Float,"(percentage)"){|start|}
  opts.on('-W',"--start-hydration=#{start_hydration}",Float,"(baker's percentage)"){|start_hydration|}
  opts.on('-n',"--salt=#{salt}",Float,"(percentage)"){|salt|}
  opts.separator ''
  opts.on('-y',"--yaml","Output YAML"){|yaml|}

  opts.on_tail('-h','--help','This message'){puts opts; exit}
end
opts.parse!(ARGV)

# calculations
start_weight = dough*start
total_flour = dough / (1.0 + hydration)
start_flour = start_weight / (1.0 + start_hydration)
flour = total_flour - start_flour
start_water = start_flour * start_hydration
water = total_flour * hydration - start_water
total_water = water + start_water
salt_weight = total_flour * salt
total_weight = dough + salt_weight

# formatting
recipe = {}
recipe['flour'] = sprintf("%dg (%0.2fc)",flour, flour/141.74762)
recipe['water'] = sprintf("%dml\t",water)
recipe['start flour'] = sprintf("%dg (%0.2fc)",start_flour, start_flour/141.74762)
recipe['start water'] = sprintf("%dml\t",start_water)
recipe['salt'] = sprintf("%dg (%0.2ftsp)",salt_weight,salt_weight/6.0)

recipe['total flour'] = sprintf("%dg (%0.2fc)",total_flour, total_flour/141.74762)
recipe['total water'] = sprintf("%dml\t",total_water)
recipe['total dough'] = sprintf("%dg\t",total_weight)

# output
if yaml
  require 'yaml'
  puts recipe.to_yaml
else
  puts "Sourdough Bread"
  ['flour','water','start flour','start water','salt'].each do |i|
    puts "  #{recipe[i]}\t#{i}"
  end
  puts
  ['total flour','total water','total dough'].each do |i|
    puts "  #{recipe[i]}\t#{i}"
  end
end

# vim:nowrap
