#include <stdlib.h>
#include <stdio.h>
#include <math.h>


#define WATER_RATIO 2.5

void Usage(void){
	printf("Usage:\t./lyecalc [SAP1] [SAP2] [SAP3] [Grams1] [Grams2] [Grams3] [Superfat]\n");

}

//Solid Soap (NaOH): Divide the average SAP Value by 1402.50
//Liquid Soap (KOH): Divide the average SAP Value by 1000

//Coconut Oil (76 degree) 	250 - 264 .178 	.252
//Soybean Oil, Organic 	190 	0.134 	0.188

//These numbers are actual SAP values expressed in the number of mg of KOH required to saponify 1 g of oil/fat.
//water = lye*3

int main(int argc, char **argv){
	double sap1;
	double sap2, sap3, sap4;
	double grams1;
	double grams2, grams3, grams4;
	double grams_t = 0;
	double water; // grams/milliliters


//	double type = 1402.50; //1000;
	double type = 1000;

	double lye, lye1, lye2, lye3, lye4;
	// lye discount
	double superfat = 0;
	double lye_purity = 0.70;

	if(argc != 10){
		Usage();
		exit(1);
	}

	sap1 = atof(argv[1]) / type;
	sap2 = atof(argv[2]) / type;
	sap3 = atof(argv[3]) / type;
	sap4 = atof(argv[4]) / type;

	grams1 = atof(argv[5]);
	grams2 = atof(argv[6]);
	grams3 = atof(argv[7]);
	grams4 = atof(argv[8]);

	superfat = 1 - (atof(argv[9]) / 100);

	grams_t = grams1+grams2+grams3+grams4;
	printf("sap1: %.03lf sap2: %.03lf sap3: %.03lf sap4: %.03lf\n", sap1, sap2, sap3, sap4);

	printf("total oil: %.03lfg\n", grams_t);
	printf("grams1: %.03lf(%.03lf%%) grams2: %.03lf(%.03lf%%) grams3: %.03lf(%.03lf%%) grams4: %.03lf(%.03lf%%)\n",
		grams1, grams1/grams_t*100, grams2, grams2/grams_t*100, grams3, grams3/grams_t*100, grams4, grams4/grams_t*100);


	lye1 = (grams1*sap1*superfat)/lye_purity;
	lye2 = (grams2*sap2*superfat)/lye_purity;
	lye3 = (grams3*sap3*superfat)/lye_purity;
	lye4 = (grams4*sap4*superfat)/lye_purity;

	printf("lye1: %.03lf lye2: %.03lf lye3: %.03lf lye4: %.03lf\n", lye1, lye2, lye3, lye4);

	lye = lye1+lye2+lye3+lye4;

	water = lye*WATER_RATIO;
	printf("lye total: %.03lf\n", lye);
	printf("water: %.03lf\n", water);

	printf("lye concentration: %.03lf%%\n", lye/(water+lye)*100);
	printf("water as %% of oil weight: %.03lf%%\n", water/grams_t*100);
	printf("lye discount(superfat): %.03lf%%\n", (1-superfat)*100);

	return 0;
}
