BR -- it is very custom, unfortunately.
It relies on bash, so it won't work on Windows, and uses hardcoded assumptions about what a filename must look like. I'd like to fix that, but I don't know how to deal with named capturing groups purely in bash+universal utilities.
It shouldn't be too hard though, to simply chain together calibredb add $file ==> $added_book_id ==> calibredb set_metadata -f "field1:contents1" $added_bookid
The trick is to extract the right information from the path, and especially what to do if some of it is optional.