
#!/usr/bin/perl -w

#
# The categories recognized by tvheadend (see epg.c) 
#  

my $MOVIE             =    "Movie / Drama";
my $THRILLER          =    "Detective / Thriller";
my $ADVENTURE         =    "Adventure / Western / War";
my $SF                =    "Science fiction / Fantasy / Horror";
my $COMEDY            =    "Comedy";
my $SOAP              =    "Soap / Melodrama / Folkloric";
my $ROMANCE           =    "Romance";
my $HISTORICAL        =    "Serious / Classical / Religious / Historical movie / Drama";
my $XXX               =    "Adult movie / Drama";

my $NEWS              =    "News / Current affairs";
my $WEATHER           =    "News / Weather report";
my $NEWS_MAGAZINE     =    "News magazine";
my $DOCUMENTARY       =    "Documentary";
my $DEBATE            =    "Discussion / Interview / Debate";
my $INTERVIEW         =    $DEBATE ;

my $SHOW              =    "Show / Game show";
my $GAME              =    "Game show / Quiz / Contest";
my $VARIETY           =    "Variety show";
my $TALKSHOW          =    "Talk show";

my $SPORT             =    "Sports";
my $SPORT_SPECIAL     =    "Special events (Olympic Games; World Cup; etc.)";
my $SPORT_MAGAZINE    =    "Sports magazines";
my $FOOTBALL          =    "Football / Soccer";
my $TENNIS            =    "Tennis / Squash";
my $SPORT_TEAM        =    "Team sports (excluding football)";
my $ATHLETICS         =    "Athletics";
my $SPORT_MOTOR       =    "Motor sport";
my $SPORT_WATER       =    "Water sport";

my $KIDS              =    "Children's / Youth programmes";
my $KIDS_0_5          =    "Pre-school children's programmes";
my $KIDS_6_14         =    "Entertainment programmes for 6 to 14";
my $KIDS_10_16        =    "Entertainment programmes for 10 to 16";
my $EDUCATIONAL       =    "Informational / Educational / School programmes";
my $CARTOON           =    "Cartoons / Puppets";

my $MUSIC             =    "Music / Ballet / Dance";
my $ROCK_POP          =    "Rock / Pop";
my $CLASSICAL         =    "Serious music / Classical music";
my $FOLK              =    "Folk / Traditional music";
my $JAZZ              =    "Jazz";
my $OPERA             =    "Musical / Opera";

my $CULTURE           =    "Arts / Culture (without music)";
my $PERFORMING        =    "Performing arts";
my $FINE_ARTS         =    "Fine arts";
my $RELIGION          =    "Religion";
my $POPULAR_ART       =    "Popular culture / Traditional arts";
my $LITERATURE        =    "Literature";
my $FILM              =    "Film / Cinema";
my $EXPERIMENTAL_FILM =    "Experimental film / Video";
my $BROADCASTING      =    "Broadcasting / Press";

my $SOCIAL            =    "Social / Political issues / Economics";
my $MAGAZINE          =    "Magazines / Reports / Documentary";
my $ECONOMIC          =    "Economics / Social advisory";
my $VIP               =    "Remarkable people";

my $SCIENCE           =    "Education / Science / Factual topics";
my $NATURE            =    "Nature / Animals / Environment";
my $TECHNOLOGY        =    "Technology / Natural sciences";
my $DIOLOGY           =    $TECHNOLOGY;
my $MEDECINE          =    "Medicine / Physiology / Psychology";
my $FOREIGN           =    "Foreign countries / Expeditions";
my $SPIRITUAL         =    "Social / Spiritual sciences";
my $FURTHER_EDUCATION =    "Further education";
my $LANGUAGES         =    "Languages";
my $NOCLASS           =    "Unpublished";
my $HOBBIES           =    "Leisure hobbies";
my $TRAVEL            =    "Tourism / Travel";
my $HANDICRAF         =    "Handicraft";
my $MOTORING          =    "Motoring";
my $FITNESS           =    "Fitness and health";
my $COOKING           =    "Cooking";
my $SHOPPING          =    "Advertisement / Shopping";
my $GARDENING         =    "Gardening";

#
# This is the 
#
#
#

my %REPLACE=(

    "fr:Météo"              => $WEATHER ,
    "fr:Film"               => $MOVIE ,
    "fr:Théâtre"            => $PERFORMING,
    "fr:Ballet"             => $OPERA ,
    "fr:Clips"              => $MUSIC ,
    "fr:Concert"            => $MUSIC ,
    "fr:Court métrage"      => $EXPERIMENTAL_FILM,
    "fr:Débat"              => $SOCIAL ,
    "fr:Dessin animé"       => $CARTOON ,
    "fr:Divertissement"     => $VARIETY ,
    "fr:Documentaire"       => $DOCUMENTARY ,
    "fr:Drame"              => $SOAP ,
    "fr:Émission"           => 0,
    "fr:Feuilleton"         => $SOAP ,
    "fr:Fin"                => 0,
    "fr:Fin des programmes" => 0 ,
    "fr:Interview"          => $INTERVIEW ,
    "fr:Jeu"                => $GAME ,
    "fr:Jeunesse"           => $KIDS ,
    "fr:Journal"            => $NEWS ,
    "fr:Loterie"            => 0 ,
    "fr:Magazine"           => $MAGAZINE ,
    "fr:Opéra"              => $OPERA ,
    "fr:Série"              => $MOVIE  ,
    "fr:Spectacle"          => $PERFORMING ,
    "fr:Sport"              => $SPORT ,
    "fr:Talk show"          => $TALKSHOW ,
#    "fr:Téléfilm"           => $MOVIE ,
    "fr:Télé-réalité"       => $VARIETY ,
    "fr:Téléréalité"        => $VARIETY ,
    "fr:Tiercé"             => $SPORT ,
    "fr:Variétés"           => $VARIETY ,

    "es:Toros"                 => $POPULAR_ART ,
    "es:Drama"                 => $MOVIE ,
    "es:Deportes de invierno"  => $SPORT_SPECIAL ,
    "es:Otros deportes"        => $SPORT ,
    "es:Ciclismo"              => $SPORT ,
    "es:Automovilismo"         => $SPORT_MOTOR ,
    "es:Fútbol"                => $FOOTBALL ,
    "es:Champions League"      => $FOOTBALL ,
    "es:Cultural\/Educativo"   => $VARIETY ,
    "es:Telefilme"             => $MOVIE ,
    "es:Ocio y Aficiones"      => $HOBBIES,
    "es:Música"                => $MUSIC ,
    "es:Musicales"             => $OPERA,
    "es:Magazine"              => $MAGAZINE ,
    "es:Entretenimiento"       => $VARIETY ,
    "es:Concurso"              => $VARIETY ,
    "es:Concursos"             => $VARIETY ,
    "es:Informativo"           => $NEWS , 
    "es:Informativos"          => $NEWS ,
    "es:Noticias"              => $NEWS ,
    "es:Documentales"          => $DOCUMENTARY ,
    "es:Series"                => $SOAP ,
    "es:Serie"                 => $SOAP ,
    "es:Entrevistas"           => $INTERVIEW ,
    "es:Infantil"              => $KIDS ,
    "es:Información"           => $NEWS ,
    "es:Película para adultos" => $XXX ,
    "es:Cine"                  => $FILM ,
    "es:Espectáculo"           => $PERFORMING ,
    "es:Otros"                 => $VARIETY ,
    "es:Deportes"              => $SPORT ,
    "es:Deporte"               => $SPORT ,
    "es:Religion"              => $HISTORICAL ,
    "es:Fitness"               => $FITNESS ,
    "es:Animación"             => $CARTOON ,
    "es:Humor"                 => $COMEDY ,
    "es:Cortometraje"          => $MOVIE ,
    "es:Sin clasificar"        => $NOCLASS ,
    "es:Viajes"                => $DOCUMENTARY ,
    "es:Clásica"               => $CLASSICAL ,
    "es:Política"              => $SOCIAL ,
    "es:Debate"                => $DEBATE ,
    "es:Naturaleza y animales" => $NATURE ,
    "es:Reality Show"          => $VARIETY ,
    "es:Biográficos"           => $DOCUMENTARY ,
    "es:Ciencia y tecnología"  => $TECHNOLOGY ,
    "es:Actualidad"            => $NEWS ,
    "es:Videoclips"            => $MUSIC ,
    "es:Fantásticas"           => $SF ,
    "es:Programas"             => $SHOW ,
 ) ; 

my $PRE  = '<category( lang=\"([a-z]+)\"|)>' ;
my $POST = '</category>'  ;

sub myfilter {
  my ($lang,$name) = @_;
  $lang="en"  if ( $lang eq "" ) ;   # Default language is "en" when is no lang attribute
  my $a = "$lang:$name" ; 
  if ( exists $REPLACE{$a} ) {     
      return $REPLACE{$a} ;
  } elsif ( $lang eq "en" ) {    
      return $name ;   # For English, assume that missing entries are fine
  } else {
      print STDERR "Warning: Unmanaged category: '$a'\n" ;
      return $name ;
  }
}

while (<>) {
    my $line = $_ ;
    # Warning $PRE contains 2 hidden level of parenthesis
    $line =~ s/($PRE)([^<]*)($POST)/"$1".myfilter($3,$4)."$5"/ge ;
    print $line;
} 
