#!/bin/bash rm -f ./developers.sqlite source ./common.sh RECIPES=./recipes FILES=$(mktemp) SUPPLY=$(realpath ./supply.csv) mkdir -p recipes hasRecipe () { query="SELECT recipe_id FROM recipes WHERE (name = '${1}') LIMIT 1;" db "${query}" } createRecipe () { id=$(createUUID) name="${1}" query="INSERT OR IGNORE INTO recipes (recipe_id, name) VALUES ('${id}', '${name}');" db "${query}" echo "${id}" } hasChemical () { query="SELECT chemical_id FROM chemicals WHERE (name = '${1}') LIMIT 1;" db "${query}" } createChemical () { chemical_id="$(createUUID)" query="INSERT OR IGNORE INTO chemicals (chemical_id, name) VALUES ('${chemical_id}', '${1}');" db "${query}" } chemical () { chemical_id=$(hasChemical "${1}") if [[ "${chemical_id}" == "" ]]; then chemical_id=$(createChemical "${1}"); fi echo "${chemical_id}" } hasSupply () { query="SELECT supply_id FROM supply WHERE (url = '${1}') LIMIT 1;" db "${query}" } component () { cols="${1}" IFS=',' read -ra VALS <<< "${2}" recipe_id="${3}" id=$(createUUID) query="INSERT OR IGNORE INTO components (component_id,recipe_id,${cols}) VALUES ('${id}','${recipe_id}'," before="" first="" for i in "${VALS[@]}"; do val=NULL if [[ "${first}" == "" ]]; then chemical_id=$(chemical "${i}") val="'${chemical_id}'" first="completed" elif [[ "${i}" != "" ]]; then val="'${i}'" fi query="${query}${before}${val}" before="," done query="${query});" echo "${query}" db "${query}" } import () { name=$(basename "${1}") name=${name%.*} name=$(echo ${name} | sed 's/_/ /g') recipe_id=$(hasRecipe "${name}") if [[ "${recipe_id}" != "" ]]; then echo "${name} recipe already exists." return else recipe_id=$(createRecipe "${name}") fi cols="chemical,grams,milliliters,makes" while read line; do line=$(echo "${line}" | xargs) if [[ "${line}" == "chemical"* ]]; then cols="${line}" cols="${cols/chemical,/chemical_id,}" continue fi component "${cols}" "${line}" "${recipe_id}" done < "${1}" echo "Imported ${name}." } list () { echo "RECIPES:" db "SELECT name FROM recipes ORDER BY name DESC;" echo "CHEMICALS:" db "SELECT name FROM chemicals ORDER BY name DESC" } supply () { line=$(echo "${1}" | xargs) first="" before="" if [[ "${line}" == "chemical,"* ]]; then return fi chemical=$(echo "${line}" | awk -F',' '{print $1}') url=$(echo "${line}" | awk -F',' '{print $2}') chemical_id=$(chemical "${chemical}") has_supply=$(hasSupply "${url}") if [[ "${has_supply}" != "" ]]; then return fi supply_id=$(createUUID) query="INSERT OR IGNORE INTO supply (supply_id,chemical_id,url,grams,milliliters,price) VALUES ('${supply_id}','${chemical_id}'," IFS=',' read -ra VALS <<< "${line}" for i in "${VALS[@]}"; do if [[ "${first}" == "" ]]; then first="complete" continue fi val=NULL if [[ "${i}" != "" ]]; then val="'${i}'" fi query="${query}${before}${val}" before="," done query="${query});" #echo "${query}" db "${query}" ratio=$(db "SELECT round((price/100.0)/grams, 2) FROM supply WHERE (supply_id = '${supply_id}');") echo "${chemical}: ${ratio} $/g" } priceRecipe () { recipesList=$(mktemp) query="SELECT r.recipe_id FROM recipes AS r ORDER BY r.name;" db "${query}" > "${recipesList}" while read id; do db "SELECT name FROM recipes WHERE recipe_id = '${id}' LIMIT 1;" echo "---" db "SELECT COUNT(*) FROM components WHERE recipe_id = '${id}';" query="SELECT ch.name FROM components AS c INNER JOIN chemicals AS ch ON ch.chemical_id = c.chemical_id WHERE c.recipe_id = '${id}';" db "${query}" echo " " done < "${recipesList}" } ls -1 "${RECIPES}/"*.csv > "${FILES}" while read file; do import "${file}" done < "${FILES}" echo "---" while read line; do supply "${line}" done < "${SUPPLY}" echo "---" priceRecipe #list