#!/usr/bin/env python3
# summary: Prepare signed startup files and request physical certificate enrollment.
"""Usage: starchaser secure boot test-keys EMPTY-NEW-FOLDER
       starchaser secure boot build KERNEL INITRD CMDLINE KEY CERT OUTPUT
       starchaser secure boot enroll PUBLIC.der

These are explicit preparation tools, not an installer migration. Test keys never provide factory-key trust.
Enrollment requires an already trusted shim, a one-time password entered here and physical confirmation at startup.
"""
import sys
from pathlib import Path

for folder in (Path(__file__).resolve().parents[1] / 'system/usr/local/lib/starchaser', Path('/usr/local/lib/starchaser')):
    if (folder / 'secure_boot.py').is_file():
        sys.path.insert(0, str(folder))
        break
import secure_boot


def main(args):
    if args in (['--help'], ['-h']):
        print(__doc__)
        return 0
    try:
        if len(args) == 2 and args[0] == 'test-keys':
            secure_boot.test_keys(Path(args[1]))
        elif len(args) == 7 and args[0] == 'build':
            secure_boot.build_uki(*map(Path, args[1:]))
        elif len(args) == 2 and args[0] == 'enroll':
            secure_boot.enroll(Path(args[1]))
        else:
            print(__doc__, file=sys.stderr)
            return 2
        return 0
    except (OSError, ValueError, RuntimeError) as error:
        print(str(error), file=sys.stderr)
        return 1


if __name__ == '__main__':
    sys.exit(main(sys.argv[1:]))
