#!/usr/bin/env python3
# summary: Bring your files and check the first-day checklist.
"""Usage: starchaser firstday checklist|sources|copy
copy reads a selected source as JSON on standard input. Files go into a new folder in Documents.
"""
import json
import sys
from starchaser_firstday import checklist, sources, copy_source


def main():
    if sys.argv[1:] in (['--help'], ['-h'], ['help']):
        print(__doc__)
        return 0
    try:
        if sys.argv[1:] == ['checklist']:
            result = {'ok': True, 'items': checklist(), 'fleetPending': (__import__('pathlib').Path.home() / '.local/state/starchaser/fleet-install.json').is_file()}
        elif sys.argv[1:] == ['sources']:
            result = {'ok': True, 'sources': sources()}
        elif sys.argv[1:] == ['copy']:
            result = copy_source(json.load(sys.stdin))
        else:
            print(__doc__, file=sys.stderr)
            return 2
    except (OSError, ValueError, KeyError) as error:
        result = {'ok': False, 'error': str(error)}
    print(json.dumps(result))
    return 0 if result.get('ok') else 1


if __name__ == '__main__':
    sys.exit(main())
