#!/usr/bin/env 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 2010 by the GPSD project
# SPDX-License-Identifier: BSD-2-clause
#
# This code runs compatibly under Python 2 and 3.x for x >= 2.
# Preserve this property!
from __future__ import absolute_import, print_function, division

import getopt
import 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
# vim: set expandtab shiftwidth=4
