#!/usr/bin/env python3
"""Usage: starchaser-boot-system status | default ENTRY | restart-windows"""
import json
import os
import sys
import boot_system

try:
    args = sys.argv[1:]
    if args in (['--help'], ['-h']):
        print(__doc__)
        sys.exit(0)
    if args == ['status']:
        # /boot and /efi stay private. The read-only polkit action lets the active
        # desktop discover the same entry IDs that the authenticated writer sees.
        if os.geteuid() != 0:
            os.execv('/usr/bin/pkexec', ['pkexec', '/usr/local/lib/starchaser/starchaser-boot-system', 'status'])
        result = boot_system.status()
    elif os.geteuid() == 0 and (args == ['restart-windows'] or len(args) == 2 and args[0] == 'default'):
        result = boot_system.change(*args)
    else:
        raise ValueError(__doc__)
except (ValueError, OSError) as error:
    print(json.dumps(dict(ok=False, error=str(error))))
    sys.exit(1)
print(json.dumps(result))
