#!/usr/bin/env python
#
# Copyright (C) 2014  Leo Singer
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
"""
Example VOEvent listener that saves all incoming VOEvents to disk.

The default server is "68.169.57.253:8099".
"""
__author__ = "Leo Singer <leo.singer@ligo.org>"

# Command line interface
DEFAULT_HOST = '68.169.57.253'
DEFAULT_PORT = 8099
from optparse import Option, OptionParser
parser = OptionParser(description=__doc__, usage='%prog [options] [HOSTNAME[:PORT]]')
opts, args = parser.parse_args()
if len(args) == 0:
    host = DEFAULT_HOST
    port = DEFAULT_PORT
elif len(args) == 1:
    host, _, port = args[0].partition(':')
    if port:
        try:
            port = int(port)
        except ValueError:
            parser.error('invalid hostname: "{0}"'.format(args[0]))
    else:
        port = DEFAULT_PORT
else:
    parser.error('too many command line arguments')

# Imports
import gcn
import gcn.handlers
import logging

# Set up logger
logging.basicConfig(level=logging.INFO)

# Listen for GCN notices (until interrupted or killed)
gcn.listen(host=host, port=port, handler=gcn.handlers.archive)
