#!/usr/bin/perl

# written by Alan Edwards, <rush@xanadu.llnl.gov>
# See Homebrew Digest #1073 (February 09, 1993) for more information.

require "getopts.pl";

# set option variable defaults

$boil_gravity = 1.050;
$volume       = 5;

# read command line

if ((!&Getopts('iv:g:')) || ($#ARGV != 2))
  {
   printf STDERR qq/
Usage:
  ibu [-v volume] [-g boil_gravity] ounces percent_alpha minutes
      -v  Final volume in gallons.  The default is 5.
      -g  Gravity of boil if over 1.050.
or
  ibu -i [-v volume] [-g boil_gravity] target_ibu percent_alpha minutes
      -i  Inverse.  Return number of ounces required to hit target IBU's.

Example:
  ibu -g 1.082 1.5 8.3 30
/;
  exit 1;
  }

$inverse       = $opt_i;
$boil_gravity  = $opt_g if ($opt_g);
$volume        = $opt_v if ($opt_v);
$ounces        = $ARGV[0];
$ibu           = $ARGV[0];
$alpha         = $ARGV[1];
$minutes       = $ARGV[2];
 

if ($boil_gravity > 1.050)
  {$adjustment = ($boil_gravity - 1.050) / 0.2;}
else
  {$adjustment = 0;}
 

# calculate utilization from exponential equivalent of hyperbolic tangent
$exp         = ($minutes - 31.322749) / 18.267743;
$utilization = 0.18109069 + 0.13862039
             * (exp($exp) - exp(-$exp)) / (exp($exp) + exp(-$exp));
 

$ibu_per_oz  = $utilization * $alpha * 74.62 / ($volume * (1 + $adjustment));

unless ($inverse)
  {$ibu = $ounces * $ibu_per_oz;}
else
  {$ounces = $ibu / $ibu_per_oz;}
 

printf("%.2f ounces of hops with %.1f%% alpha acid, boiled for %d minutes",
       $ounces, $alpha, $minutes);
printf(" in %.3f wort", $boil_gravity) if ($opt_g);
printf("\nwill");
printf(" be required to") if ($inverse);
printf(" produce %.1f IBU's.\n", $ibu);
