costs

Tool to help you see the impact of recurring costs
git clone git://jacobedwards.org/costs
Log | Files | Refs | README

costs.awk (1021B)


      1 #!/usr/bin/awk -f
      2 # Copyright 2023 Jacob R. Edwards <jacob@jacobedwards.org>
      3 
      4 function error(msg) {
      5 	print("error: " msg) > "/dev/stderr";
      6 }
      7 
      8 function die(msg) {
      9 	status = msg;
     10 	error(status);
     11 	exit 1
     12 }
     13 
     14 BEGIN {
     15 	FS = "	";
     16 	OFS = FS;
     17 	OFMT = "%.2f";
     18 
     19 	period = 1;
     20 	amount = 2;
     21 	name = 3;
     22 	desc = 4;
     23 
     24 	t["daily"] = 1;
     25 	t["weekly"] = 7;
     26 	t["bi-weekly"] = 14;
     27 	t["monthly"] = 365 / 12 # 'spose 30 or 30.4 could produce easier numbers
     28 	t["quarterly"] = t["monthly"] * 3;
     29 	t["yearly"] = 365;
     30 
     31 	if (! ARGV[1]) {
     32 		output = "daily";
     33 	} else {
     34 		output = ARGV[1];
     35 		if (!(output in t))
     36 			die(output " not a valid period");
     37 		delete ARGV[1];
     38 		--ARGC;
     39 	}
     40 }
     41 
     42 /^[^# ]/ && ! /^$/ {
     43 	m = 1
     44 	n = split($period, ts, "/")
     45 	if (n > 2 || !(ts[1] in t) || (n > 2 && ts[2] == 0))
     46 		die($period ": Invalid period")
     47 
     48 	if (n > 1)
     49 		m = ts[2]
     50 	x = t[ts[1]] * m
     51 
     52 	daily = $amount / x
     53 	if (maketotal)
     54 		total += daily * t[output];
     55 	else
     56 		print($name, daily * t[output]);
     57 }
     58 
     59 END {
     60 	if (status)
     61 		exit 1
     62 	if (maketotal)
     63 		print("total", total);
     64 }