dotfiles/bin/gpg-key-to-datamatrix
2024-08-02 11:21:47 -05:00

40 lines
1.1 KiB
Text
Executable file

#! /usr/bin/env nix-shell
#! nix-shell -p typst dmtx-utils nushell -i nu
def get-chunked-key [keyid?: string] {
let flags = [
"--export-secret-key", "--export-options", "export-minimal"
] | do { if keyid == null { $in } else { $in | append $keyid}}
let key = (gpg ...$flags)
let length = ($key | bytes length)
# 1555 is the maximum length in bytes split data evenly
let n = ($length / (($length / 1555)| math ceil)) | math ceil
0..$n..$length
| each {|i| $key | bytes at $i..<($i + $n)}
}
def generate-typst-doc [tmp: string, length: int] {
0..<$length
| each { |i|
let path = $tmp
| path join $"dmtx-($i).svg"
| path relative-to $env.PWD
['#image("', $path, '")'] | str join
}
| str join "\n"
}
def main [keyid?: string, outfile = "dmtxdata.pdf"] {
let tmpdir = mktemp -d -p . -t tmp.key-2-matrix-XXX
let byte_chunks = get-chunked-key $keyid
$byte_chunks | enumerate | each {|it|
$it.item | dmtxwrite -e 8 -o ($tmpdir | path join $"dmtx-($it.index).svg")
}
let doc = generate-typst-doc $tmpdir ($byte_chunks | length)
$doc | typst compile - $outfile
rm -r $tmpdir
}