#!/usr/bin/awk -f
#
#   http://code.google.com/media-translate/
#   Copyright (C) 2010  Serge A. Timchenko
#
#   This program 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 3 of the License, or
#   (at your option) any later version.
#
#   This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
#

function escape(str)
{
  gsub(/&/, "&amp;", str);
  gsub(/>/, "&gt;", str);
  gsub(/</, "&lt;", str);
  return str
}

function urlencode(str)
{
  gsub(/(^\s+|\s+$)/, "", str);
  gsub(/%/, "%25", str);
  gsub(/#/, "%23", str);
  gsub(/\$/, "%24", str);
  gsub(/&/, "%26", str);
  gsub(/\+/, "%2B", str);
  gsub(/,/, "%2C", str);
  gsub(/:/, "%3A", str);
  gsub(/;/, "%3B", str);
  gsub(/=/, "%3D", str);
  gsub(/\?/, "%3F", str);
  gsub(/@/, "%40", str);
  gsub(/ /, "%20", str);
  return str
}

BEGIN {
  title = ""
  track = 0 
  print "<?xml version='1.0' encoding='UTF-8'?>"
  print "<playlist version='1' xmlns='http://xspf.org/ns/0/'>"
  if( name != "" )
  {
    print "<title><![CDATA[" name "]]></title>"
    fn = (match(name, /(.+)\.[^\.]*$/, arr) != 0) ? arr[1] : name;
    print "<image><![CDATA[" path "/" fn ".jpg]]></image>"
  }
  print "<trackList>"
}

/^[ \t]*#EXTINF:/ { 
    gsub(/\r/, "");
    match($0,/^[ \t]*#EXTINF: *(-?[0-9]+) *,(.*)$/, arr);
    duration = arr[1]*1000;
    title = arr[2];
}

$0 != "" && $0 != "\r" && /^[ \t]*[^#]/ {
  gsub(/\r/, "");

  if($0 !~ /^[a-z]+:\/\// && $0 !~ /^\//)
  {
    gsub(/\\/, "/");
    location = path "/" $0;
  }
  else
  {
    location = $0;
  }
  match(location, /^(.+):\/\//, arr);
  protocol = tolower(arr[1]);
 
  isdir = location ~ /^(ftp:\/)?\/.*\/$/;

  print "<track>"
  if( title == "" )
  {
    match($0, /.*\/([^\/]+$)/, arr);
    if(arr[1] != "")
    {
      title = arr[1];
      if(isdir)
        title = "/" title "/";
    }
    else
    {
      title = $0;
    }
  }
  
  print "<title><![CDATA[" title "]]></title>"
  if( duration != "" && duration > 0 )
  {
    print "<duration>" duration "</duration>"
    duration = ""
  }
  
  if( title ~ / +TV$/ )
    print "<meta rel=\"translate\">Content-type:video/x-msvideo</meta>" 

  if( title ~ / +\((win-?|CP)1251\)$/ )
    print "<meta rel=\"translate\">Charset:CP1251</meta>" 
  
  match(location, /.*\.([^.\/\?]+).*$/, arr);
  ext = tolower(arr[1]);
  
  if(ext != "")
    print "<meta rel=\"ext\"><![CDATA[" ext "]]></meta>"

  if(isdir)
    print "<meta rel=\"class\">directory</meta>"
  
  if(protocol == "")
    protocol = "file";

  print "<meta rel=\"protocol\"><![CDATA[" protocol "]]></meta>"
  
  print "<location><![CDATA[" location "]]></location>"
  print "</track>"
  title = ""
}

END {
  print "</trackList>"
  print "</playlist>"
}