#!/usr/bin/env python
# -*- python -*-
#
# Copyright 2002 Free Software Foundation, Inc.
# 
# This file is part of GNU Radio
# 
# GNU Radio 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, or (at your option)
# any later version.
# 
# GNU Radio 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 GNU Radio; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
# 

#
# Loopback the transmitter stub to the receiver stub and check that
# the output matches the input.  Note, that due to the 52 data segment
# end to end delay in the convolutional interleaver, the first 52
# data segements output are garbage.  We skip them, and compare
# against the relevant parts of the actual output
#

import os, os.path, math, sys

# set srcdir to the directory that contains Makefile.am

try:
    srcdir = os.environ['srcdir']
except KeyError, e:
    srcdir = "."
srcdir = srcdir + '/'


def system_chk (cmd):
    r = os.system (cmd)
    if (r != 0):
        # print "FAILED (%d): %s" % (r >> 8, cmd)
        if ((r & 0xff) != 0) :          # killed by signal
            sys.exit (127)
        else :
            sys.exit (r >> 8)
    return 0


def usage ():
    sys.stderr.write ("usage: %s [mpeg_transport_stream]\n" % sys.argv[0])
    sys.exit (1)


# if an mpeg transport stream is provided on the command line, use it,
# else use fake.ts, generating it if required.

nargs = len (sys.argv) - 1
if nargs == 0 :
    input_ts = "fake.ts"
    if not os.path.exists (input_ts) :
        system_chk (srcdir + "make_fake_ts.py > " + input_ts)
elif nargs == 1 :
    input_ts = sys.argv[1];
else :
    usage ()



intermediate = input_ts + ".txout"
output_ts = input_ts + ".rxout"
input_subset = input_ts + ".sub"
output_subset = output_ts + ".sub"

files_to_cleanup = [ intermediate, output_ts, input_subset, output_subset]


# run txstub and rxstub
system_chk ("./atsc_txstub -f %s -o %s" % (input_ts, intermediate))
system_chk ("./atsc_rxstub -f %s -o %s" % (intermediate, output_ts))


# extract relevant parts of input and output files

osize = math.floor (os.path.getsize (output_ts) / 188)

interleaver_delay = 52
viterbi_delay = 12
delay = interleaver_delay + viterbi_delay

system_chk ("dd if=%s of=%s bs=188 count=%d" % (input_ts, input_subset, osize - delay))
system_chk ("dd if=%s of=%s bs=188 skip=%d count=%d" % (output_ts, output_subset,
                                                        delay,
                                                        osize - delay))

# compare them

r = os.system ("cmp %s %s" % (input_subset, output_subset))

map (os.remove, files_to_cleanup)       # cleanup...

sys.exit (r >> 8)                       # return result code
