#!/usr/bin/perl
use strict ;
use Pod::Usage ;
use Getopt::Long qw/:config no_ignore_case/ ;

++$! ;

use Linux::DVB::DVBT ;

	my ($help, $man, $DEBUG, $VERBOSE, $config, $check) ;
	GetOptions('v|verbose=s' => \$VERBOSE,
			   'debug=s' => \$DEBUG,
			   'h|help' => \$help,
			   'man' => \$man,
			   'cfg=s' => \$config,
			   'check' => \$check,
			   ) or pod2usage(2) ;


    pod2usage(1) if $help;
    pod2usage(-verbose => 2) if $man;

	Linux::DVB::DVBT->debug($DEBUG) ;
	Linux::DVB::DVBT->dvb_debug($DEBUG) ;
	Linux::DVB::DVBT->verbose($VERBOSE) ;

	## Create dvb (use first found adapter). 
	## NOTE: With default object settings, the application will
	## die on *any* error, so there is no error checking in this script
	##
	my $dvb = Linux::DVB::DVBT->new() ;
	
	$dvb->config_path($config) if $config ;
	
	# read config
	my $tuning_href = $dvb->get_tuning_info() ;
	my $channels_aref = $dvb->get_channel_list() ;
	
	my $ok = 1 ;
	print "Chans\n" ;
	my ($tv, $radio)=(0,0) ;
	foreach my $ch_href (@$channels_aref)
	{
		my $chan = $ch_href->{'channel'} ;
		my $checkstr = "" ;
		if ($check)
		{
			$checkstr .= " ..." ;
			my $tsid = $tuning_href->{'pr'}{$chan}{'tsid'} ;
			if (exists($tuning_href->{'ts'}{$tsid}))
			{
				$checkstr .= "ok" ;
			}
			else
			{
				$checkstr .= "FAILED" ;
				$ok = 0 ;
			}
		}
		printf "%3d : %-40s %5d-%-5d $ch_href->{type} $checkstr\n", 
			$ch_href->{'channel_num'},
			$chan,
			$tuning_href->{'pr'}{$chan}{'tsid'},
			$tuning_href->{'pr'}{$chan}{'pnr'} ;
		
		if ($ch_href->{type} eq 'tv')
		{
			++$tv ;
		}
		else
		{
			++$radio ;
		}
	}
	printf "Found %d channels (%d tv, %d radio)\n", $tv+$radio, $tv, $radio ;	
	if ($check)
	{
		print $ok ? "Passed checks\n" : "FAILED checks\n" ;
	}
	

#=================================================================================
# END
#=================================================================================
__END__

=head1 NAME

dvbt-chans - Show DVBT channels

=head1 SYNOPSIS

dvbt-chans [options]

Options:

       -debug level         set debug level
       -verbose level       set verbosity level
       -help                brief help message
       -man                 full documentation
       -check               check validity of channels
       
=head1 OPTIONS

=over 8

=item B<-help>

Print a brief help message and exits.

=item B<-man>

Prints the manual page and exits.

=item B<-verbose>

Set verbosity level. Higher values show more information.

=item B<-debug>

Set debug level. Higher levels show more debugging information (only really of any interest to developers!)

=item B<-check>

Ensures that all channels have a valid set of frequency parameters (Note: does not check that these will actually
record the correct channel!)


=back

=head1 DESCRIPTION

Script that uses the perl Linux::DVB::DVBT package to provide DVB-T adapter functions.
 
Reads the current config files and displays the list of known channels.

For full details of the DVBT functions, please see:

   perldoc Linux::DVB::DVBT
 
=cut

	
