#!/usr/bin/perl -w

# written by D. Constantinescu (d.constantinescu@tu-bs.de)

chomp($ifile=shift);
$ofile=$ifile;
$ofile =~ s/cup/kml/;

#$ifile='Waypoints_Matei.cup';#'Pct21Mai.cup';
#$ofile='tst2.kml';

$fileheader='<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>';

# define the airport icon:

$fileheader.='
    <Style id="sh_airports">
            <IconStyle>
                    <scale>1.4</scale>
                    <Icon>
                            <href>http://maps.google.com/mapfiles/kml/shapes/airports.png</href>
                    </Icon>
                    <hotSpot x="0.5" y="0" xunits="fraction" yunits="fraction"/>
            </IconStyle>
            <ListStyle>
            </ListStyle>
    </Style>
    <StyleMap id="msn_airports">
            <Pair>
                    <key>normal</key>
                    <styleUrl>#sn_airports</styleUrl>
            </Pair>
            <Pair>
                    <key>highlight</key>
                    <styleUrl>#sh_airports</styleUrl>
            </Pair>
    </StyleMap>
    <Style id="sn_airports">
            <IconStyle>
                    <scale>1.0</scale>
                    <Icon>
                            <href>http://maps.google.com/mapfiles/kml/shapes/airports.png</href>
                    </Icon>
                    <hotSpot x="0.5" y="0" xunits="fraction" yunits="fraction"/>
            </IconStyle>
            <ListStyle>
            </ListStyle>
    </Style>

    <Style id="sh_donut">
            <IconStyle>
                    <scale>1.4</scale>
                    <Icon>
                            <href>http://maps.google.com/mapfiles/kml/shapes/donut.png</href>
                    </Icon>
                    <hotSpot x="0.5" y="0" xunits="fraction" yunits="fraction"/>
            </IconStyle>
            <ListStyle>
            </ListStyle>
    </Style>
    <StyleMap id="msn_donut">
            <Pair>
                    <key>normal</key>
                    <styleUrl>#sn_donut</styleUrl>
            </Pair>
            <Pair>
                    <key>highlight</key>
                    <styleUrl>#sh_donut</styleUrl>
            </Pair>
    </StyleMap>
    <Style id="sn_donut">
            <IconStyle>
                    <scale>1.0</scale>
                    <Icon>
                            <href>http://maps.google.com/mapfiles/kml/shapes/donut.png</href>
                    </Icon>
                    <hotSpot x="0.5" y="0" xunits="fraction" yunits="fraction"/>
            </IconStyle>
            <ListStyle>
            </ListStyle>
    </Style>


';

open IF, "<$ifile";
open OF, ">$ofile";

print OF "$fileheader\n";
while (<IF>) {
  chomp($line=$_);
  next if $line=~/^#/;
  @wp_arr=split /,/, $line;
  #print @wp_arr;
  #print "\n$#wp_arr\n";
  
  $name     =$wp_arr[0];
  $code     =$wp_arr[1];
  $country  =$wp_arr[2];
  $lat      =$wp_arr[3];
  $lon      =$wp_arr[4];
  $elev     =$wp_arr[5];
  $style    =$wp_arr[6];
  $rwdir    =$wp_arr[7];
  $frq      =$wp_arr[8];
  $desc     =$wp_arr[9];
  $userdata =$wp_arr[10];
  $pics     =$wp_arr[11];
  
print $code,"\t", $#wp_arr;
print "\n";

  # transform from (d)ddmm.mmm to decimal degrees dddd.dddddd
  $lat=~/(\d\d)(\d\d.\d\d\d)(N|S)/;
  $latdeg=$1; $latmin=$2; $latsgn=$3;
  if ($latsgn eq 'N') {$latsgn=1} else {$latsgn=-1};
  $latdec=$latsgn*($latdeg+$latmin/60.);
  
  $lon=~/(\d\d\d)(\d\d.\d\d\d)(E|W)/;
  $londeg=$1; $lonmin=$2; $lonsgn=$3;
  if ($lonsgn eq 'E') {$lonsgn=1} else {$lonsgn=-1};
  $londec=$lonsgn*($londeg+$lonmin/60.);

  print OF '    <Placemark>'."\n";
  print OF '      <name>'.$name.'</name>'."\n";
  print OF '      <description>'.$code.' '.$desc.'</description>'."\n";
  print OF '      <styleUrl>#msn_airports</styleUrl>'."\n" if $name=~/^"AD /;
  print OF '      <styleUrl>#msn_donut</styleUrl>'."\n" if $name=~/^"F - /;
  print OF '      <Point>'."\n";
  print OF '        <coordinates>'.$londec.','.$latdec.'</coordinates>'."\n";
  print OF '      </Point>'."\n";
  print OF '    </Placemark>'."\n";
}
print OF   '  </Document></kml>'."\n";

close IF;
close OF;














