2024-11-06 01:13:36 +00:00
#!/bin/bash
if [ [ " ${ 1 } " = = "" ] ] ; then
echo "Please use a .scad file as first argument"
exit 1
fi
FILENAME = $( basename " ${ 1 } " | tr '[:upper:]' '[:lower:]' )
EXTENSION = " ${ FILENAME ##*. } "
if [ [ " ${ EXTENSION } " != "scad" ] ] ; then
echo " Please use a .scad file as first argument, not . ${ EXTENSION } "
exit 2
fi
if [ [ ! -f " ${ 1 } " ] ] ; then
echo " File ${ 1 } does not exist "
exit 3
fi
mkdir -p ./hardware/bom
NAME = " ${ FILENAME %.* } "
DESTINATION = " ./hardware/bom/ ${ NAME } _BOM.csv "
TOTAL = " ./hardware/bom/ ${ NAME } _BOM_total.csv "
touch " ${ DESTINATION } "
touch " ${ TOTAL } "
DESTINATION = $( realpath " ${ DESTINATION } " )
TOTAL = $( realpath " ${ TOTAL } " )
PRICES = $( realpath "./hardware/parts/prices.csv" )
MODULE = ""
2024-11-06 01:50:42 +00:00
echo "module,quantity,part,part_id,description" > " ${ DESTINATION } "
2024-11-06 01:13:36 +00:00
tac " ${ 1 } " | while read line; do
module = $( echo " ${ line } " | grep 'module' )
if [ [ " ${ module } " != "" ] ] ; then
MODULE = $( echo " ${ module } " | xargs | awk '{print $2}' | awk -F'{' '{print $1}' )
fi
bom = $( echo " ${ line } " | grep '//' | grep 'BOM' | awk -F'BOM:' '{print $2}' | xargs)
if [ [ " ${ bom } " != "" ] ] ; then
2024-11-06 01:50:42 +00:00
QUANTITY = $( echo " ${ bom } " | awk -F',' '{print $1}' | xargs)
2024-11-06 01:13:36 +00:00
PART = $( echo " ${ bom } " | awk -F',' '{print $2}' | xargs)
ID = $( echo " ${ bom } " | awk -F',' '{print $3}' | xargs)
DESCRIPTION = $( echo " ${ bom } " | awk -F',' '{print $4}' | xargs)
2024-11-06 01:50:42 +00:00
echo " [ ${ MODULE } ] ${ QUANTITY } x ${ PART } ( ${ ID } ) "
echo " ${ MODULE } , ${ QUANTITY } , ${ PART } , ${ ID } , ${ DESCRIPTION } " >> " ${ DESTINATION } "
2024-11-06 01:13:36 +00:00
fi
done
2024-11-06 01:50:42 +00:00
echo "quantity,part_id,part,price" > " ${ TOTAL } "
2024-11-06 01:13:36 +00:00
sqlite3 :memory: -cmd '.mode csv' -cmd " .import ${ DESTINATION } bom " -cmd " .import ${ PRICES } prices " \
2024-11-06 01:50:42 +00:00
'SELECT SUM(quantity),part,part_id, SUM(quantity) * (COALESCE((SELECT prices.price FROM prices WHERE prices.part = bom.part LIMIT 1), 0)) as price FROM bom GROUP BY part ORDER BY part DESC;' >> " ${ TOTAL } "
sqlite3 :memory: -cmd '.mode csv' -cmd " .import ${ TOTAL } bom " \
2024-11-06 17:32:54 +00:00
'SELECT SUM(quantity),"N/A","TOTALS", SUM(price) FROM bom;' | tr -d '"' >> " ${ TOTAL } "