#!/usr/bin/env python

# Given an encrypted file and (enough) shares of a distributed secret key,
# decrypt the file

import os
import splinterglyph.splinterglyph_tools as splinterglyph
import argparse

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Encrypt file and produce multiple crypto key shares",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    )
    parser.add_argument(
        "crypt", help="Path to (input) encrypted file; default is STDOUT"
    )
    parser.add_argument("plain", help="Path to (output) plaintext file.", nargs="?")
    sample = '"1-even,thermostat,hinted,easel 3-odd,them,harv,wut"'
    parser.add_argument(
        "--shares",
        help=f"Key shares for recovery.  These can be specified either "
        f"as a path to a file containing the samples, or as a list of the "
        f"samples themselves, e.g., --shares {sample}",
        required=True,
    )
    args = parser.parse_args()

    if os.path.exists(args.shares):
        with open(args.shares, "r") as fp:
            key_shares = fp.read().split()
    else:
        key_shares = (args.shares).split()
    splinterglyph.decrypt(
        plain_path=args.plain, crypt_path=args.crypt, key_shares=key_shares
    )
