#!/usr/bin/perl

# converting technosat transponder ini files into a transponder-list for VDR
# Usage: Get transponder inis from http://joshyfun.webzdarma.cz/transponders/index.html
#        Convert the .ini into a transponder-file for VDR: cat *.ini | TTini2VDR > channels.conf
#
# 15 Feb 2004: initial version from Stephan Martin <dvb@martin.de>
# 15 Feb 2004: small additions by Frank Scherthan <t.a.r.o.s@gmx.de>, now possible to use mor than one .ini
#              files and setting the start-channel-number
# 15 Feb 2004: removed plain stupid start-channel-number implementation and made a new one
# 15 Feb 2004: SM: improvement of readability, making the quickhack even more like a program, 
#                  fixed bug with more than 1 .ini-file, changed channelnames to 
#		   "<Freq> GHz on <SatName> (<Orbital Position>)"


print STDERR 
"usage: get transponder.ini-files for your satellites from 
         http://joshyfun.webzdarma.cz/transponders/index.html
         cat *ini | $0 >channels.conf

";
        
my $section ; # Flag what section we are parsing
my $orbit; # Orbital Position
my $source; # Sourcefield for vdr
my $sourcename ; # Name of the source (satellitename)
my $transpondername ; # Name for the transponder 
my $count = 0;
my $channelsstart = 5000;

print ":$channelsstart\@Transponders\n";

while (<>) {
  # Parse sectiontype
  if (/\[SATTYPE\]/) {
     $section = 'sat'; 
     $orbit = '?';
     $source = 'unknown';
     $sourcename = 'unknown';
     next;
  } elsif (/\[DVB\]/) {
     $section = 'transponder';
     next;
  }

  # Parse satellite description
  if ($section eq 'sat') {
     # 1= seems to contain the Orbital Position
     if (/1=(\d+)\s*$/) {
         $orbit = ($1 < 1800 ? $1/10 . 'E' : 180 - ($1 - 1800) / 10 .'W');
         $source = "S$orbit";
         next;
     } elsif (/2=(.*?)\s*$/) {
         $sourcename = $1; 
	 $sourcename =~ s/://; # Remove ':' in Satellite names
     }
  # Parse Transponders 
  } elsif ($section eq 'transponder') {
     if (/(\d+)=(\d+),([V|H]),(\d+),(\d+)\s*$/) {
        # Got a Transponder
        $transpondername = ($2/1000) . " GHz on $sourcename ($orbit)" ;
        print "$transpondername:$2:$3:$source:$4:8191:8191:8191:0:8191:8191:".$count++.":0\n"; 
     }
  }
}

