#!/usr/bin/perl

use Getopt::Std;

#---------------------------------------------------------------------------
# main

$Usage = qq{
Usage: $0 [options]

Options: -c filename           channels.conf
         -h	               Show this help text
};

die $Usage if (!getopts('c:h') || $opt_h || !$opt_c);
$channelsfile = $opt_c;

# channels.conf lesen
open(CHANNELS, "$channelsfile") || die "cannot open $channelsfile file: $!";
while(<CHANNELS>) {
	next if (/^:/);
	chomp;
	my($name, $frequency, $polarization, $source, $symbolrate, $vpid, $apid,
	 $tpid, $ca, $service_id, $nid, $tid, $rid) = split(/\:/, $_);

	push(@chan, {
		name         => $name,
		frequency    => $frequency,
		polarization => $polarization,
		source       => $source,
		symbolrate   => $symbolrate,
		vpid         => $vpid,
		apid         => $apid,
		tpid         => $tpid,
		ca           => $ca,
		service_id   => $service_id,
		nid          => $nid,
		tid          => $tid,
		rid          => $rid
	}); 
}
close (CHANNELS);


# sortieren
my($last) = 0;
my(@temp);
for(sort({ $a->{service_id} <=> $b->{service_id} } @chan)) {
#	next if($last == $_->{service_id});
	push(@temp, $_); 
#	$last = $_->{service_id};
}

foreach $c (@temp) {
	print "$c->{name}:$c->{frequency}:$c->{polarization}:$c->{source}:$c->{symbolrate}:$c->{vpid}:$c->{apid}:$c->{tpid}:$c->{ca}:$c->{service_id}:$c->{nid}:$c->{tid}:$c->{rid}\n";
}
exit;
