#!/bin/bash # What you charge an hour RATE=50 short_help() { echo "$0 [output file]" echo "--help for help" } long_help() { short_help echo "" echo "The csv file should be setup as:" echo "" echo "Job , Hours " echo "" echo "With no empty line at the end." echo "Paying company name is any string" echo "The output argument is optional and will by default output to invoice-.pdf" } CSV="" if [[ -z $1 ]]; then short_help exit elif [[ $1 = "--help" ]]; then long_help exit else CSV=$1 fi CSV2="" if [[ -z $2 ]]; then short_help exit else CSV2=$2 fi COMPANY="" if [[ -z $3 ]]; then short_help exit else COMPANY=$3 fi # Increment a global invoice number INV_FILE="last_invoice_number" INV_NUM=0 if [[ -f $INV_FILE ]]; then INV_NUM=$(expr $(cat $INV_FILE) + 1) else INV_NUM=1 fi echo $INV_NUM > $INV_FILE # 3rd argument is output location (optional) OUTPUT="invoice-${INV_NUM}.pdf" if [[ ! -z $4 ]]; then OUTPUT=$4 fi # Parse the CSV into: Label;Hrs;Cost HRS_TMP=$(mktemp) cat $CSV | RATE=$RATE awk -F, '{ sum += $2 }; { print $1 ";" $2 ";" $2 * ENVIRON["RATE"] } END { print "Sub-Total" ";" sum ";" sum * ENVIRON["RATE"]}' > $HRS_TMP CONSUME_TMP=$(mktemp) cat $CSV2 | awk -F, '{ sum += $2 }; { print $1 ";" $2 } END { print "Sub-Total" ";" sum }' > $CONSUME_TMP HRS_COST=$(cat $HRS_TMP | tail -n 1 | awk -F";" '{ print $3 }') CONSUME_COST=$(cat $CONSUME_TMP | tail -n 1 | awk -F";" '{ print $2 }') TOTAL_COST=$(A=$HRS_COST B=$CONSUME_COST awk 'BEGIN{ print ENVIRON["A"] + ENVIRON["B"] }') # TOTAL = Last line # BODY = Everything but the last line COMPANY=$COMPANY \ INVOICE_NUM=$INV_NUM \ envsubst < start.mm > 1.tmp HRS_TOTAL=$(cat $HRS_TMP | tail -n 1) \ HRS_BODY=$(cat $HRS_TMP | head -n -1) \ envsubst < hours.tbl > 2.tmp ITEM_TOTAL=$(cat $CONSUME_TMP | tail -n 1) \ ITEM_BODY=$(cat $CONSUME_TMP | head -n -1) \ envsubst < item.tbl > 3.tmp TOTAL=$TOTAL_COST \ RATE=$RATE \ envsubst < end.mm > 4.tmp cat 1.tmp 2.tmp 3.tmp 4.tmp | groff -t -mm -T pdf > $OUTPUT rm $HRS_TMP rm $CONSUME_TMP