#!/usr/bin/python
#
# striplog -- strip leading lines from logs
#
# striplog -j strips JSON leader sentences.
# striplog with no option strips all leading lines beginning with #
#
# This file is Copyright (c) 2010 by the GPSD project
# BSD terms apply: see the file COPYING in the distribution root for details.
#
import getopt, sys

secondline = firstline = stripjson = False
stripval = 0
(options, arguments) = getopt.getopt(sys.argv[1:], "12n:j")
for (switch, val) in options:
    if (switch == '-1'):
        firstline = True
    if (switch == '-2'):
        secondline = True
    if (switch == '-n'):
        stripval = int(val)
    if (switch == '-j'):
        stripjson = True

try:
    if firstline:
        sys.stdin.readline()
    elif secondline:
        sys.stdin.readline()
        sys.stdin.readline()
    elif stripval:
        for _dummy in range(stripval):
            sys.stdin.readline()
    elif stripjson:
        while True:
            line = sys.stdin.readline()
            if line.startswith('{"class":"VERSION"') \
                   or line.startswith('{"class":"DEVICE"') \
                   or line.startswith('{"class":"DEVICES"') \
                   or line.startswith('{"class":"WATCH"'):
                continue
            else:
                break
        sys.stdout.write(line)
    else:
        while True:
            line = sys.stdin.readline()
            if line[0] != '#':
                break
        sys.stdout.write(line)

    sys.stdout.write(sys.stdin.read())
except KeyboardInterrupt:
    pass
