commit 37472b4c6ff31dfe348390b49bef2353ddcc0ffb
parent fcafe43d1d41eb3cd52d5e22df8dc17a7dd0c91a
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date: Wed, 13 Mar 2024 20:27:30 -0700
Add asset allocater script
Diffstat:
A | alloc | | | 52 | ++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 52 insertions(+), 0 deletions(-)
diff --git a/alloc b/alloc
@@ -0,0 +1,52 @@
+#!/usr/bin/awk -f
+# Asset allocator
+#
+# Allocates the given number of assets between the list of assets
+# given using the percentages therein. An example file:
+#
+# foo 60
+# bar 30
+# baz 10
+#
+# If given 100, it will output an identical copy of the given input,
+# if given 200, all the numbers will be doubled.
+
+function die(msg) {
+ print "error: " msg > "/dev/stderr"
+ status = 1
+ exit 1
+}
+
+BEGIN {
+ IFS = " "
+ units = ARGV[1] ? ARGV[1] : 100
+ delete ARGV[1]
+ --ARGC
+}
+
+{
+ if (!NF || !/^ *[^#]/) {
+ print
+ next
+ }
+
+ if (NF <= 2 || $2 !~ /^[0-9]+$/)
+ die("Invalid entry")
+
+ match($0, IFS);
+ first = substr($0, 1, RSTART + RLENGTH - 1)
+ value = units * $2 / 100
+ last = substr($0, RSTART + RLENGTH + length($2))
+ print first, value, last
+ p += $2
+ next
+}
+
+END {
+ if (p > 100)
+ die(p "%: Invalid percentage")
+ if (p < 100) {
+ print "warning: Only " p "% of assets used" > "/dev/stderr"
+ exit 1
+ }
+}