#!/usr/bin/env python
import os
import sys
from graffito import telescope

# TODO: replace with argparse or something.
# DEVICE = (sys.argv[1] if (len(sys.argv)>=2) & (os.path.exists(sys.argv[1])) else None)
DEVICE = None
DEBUG  = ('debug' in sys.argv)


def main():
    try:
        with telescope.Telescope(device=DEVICE) as tele:
            while True:
                output = tele.get()
                if output:
                    print tele.niceprint(output, showcmd=True)
    
    except Exception as e:
        print 'Error!', e
        if DEBUG:
            raise
        
    except KeyboardInterrupt as e:
        print 'Goodbye!'
        return



if __name__ == '__main__':
    if DEBUG:
        from pysurvey import util
        util.setup_stop()
    
    main()