#!/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 programs";
my $KIDS_0_5          =    "Pre-school children's programs";
my $KIDS_6_14         =    "Entertainment programs for 6 to 14";
my $KIDS_10_16        =    "Entertainment programs for 10 to 16";
my $EDUCATIONAL       =    "Informational / Educational / School programs";
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 $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=(
    
    "Émission"          				 => 0,
    "Fin"               				 => 0,
    "Fin des programmes" 				 => 0 ,
    "Loterie"           				 => 0 ,
	"Programme indéterminé" 			 => 0 ,
    "Autre"                              => 0 ,
    "Série TV"                           => $MOVIE ,
    "Série Télé"                         => $MOVIE ,
    "Série"                              => $MOVIE ,  
    "Emission spéciale"                  => 0 ,
	"Talk show"							 => $TALKSHOW ,
    "Talk-show"							 => $TALKSHOW ,
    "Météo"								 => $WEATHER ,
    "Drame"								 => $MOVIE ,
    "Série fantastique"                  => $SF,
	"Magazine scientifique"              => $SCIENCE,
	"Magazine de télé-achat"             => $SHOPPING, 
    "Publicité"                          => $SHOPPING, 
	"Série réaliste"                     => $MOVIE,
	"Série hospitalière"                 => $MOVIE,
    "Mélodrame"                          => $MOVIE,
	"Jeu"               				 => $GAME ,
	"Magazine de la gastronomie"  		 => $COOKING, 
    "Journal"            				 => $NEWS ,
    "Actualité"            				 => $NEWS ,
    "Information"            			 => $NEWS ,
    "Feuilleton sentimental"      		 => $SOAP ,
    "Téléfilm romanesque"          		 => $ROMANCE  ,
    "Feuilleton réaliste"         		 => $SOAP ,
    "Série sentimentale"          		 => $ROMANCE  ,
    "Téléfilm de suspense"         		 => $THRILLER ,
    "Téléréalité"                		 => $VIP ,
    "Série de téléréalité"       		 => $VIP ,
    "Magazine du show-biz"      	     => $VIP ,
    "Série humoristique"         	     => $COMEDY ,
    "Téléfilm humoristique"       		 => $COMEDY ,
    "Comédie satirique"                  => $COMEDY , 
    "Humour"                             => $COMEDY , 
    "Série policière"                	 => $THRILLER ,
    "Comédie policière"                	 => $THRILLER ,
    "Magazine sportif"              	 => $SPORT_MAGAZINE,
    "JT sport"                           => $SPORT_MAGAZINE,
    "Téléfilm dramatique"           	 => $MOVIE ,
    "Téléfilm humoristique"        		 => $COMEDY ,
    "Série à sketches"                   => $COMEDY ,
    "Téléfilm sentimental"       	     => $ROMANCE ,
    "Téléfilm romanesque"          		 => $ROMANCE ,
    "Comédie romantique"                 => $ROMANCE ,
    "Feuilleton"                         => $SOAP ,
    "Football"              		     => $FOOTBALL , 
    "Tennis"                		     => $TENNIS ,
    "Divertissement"       			     => $SHOW ,
	"Divertissement - humour"       	 => $SHOW ,
    "Divertissement-humour"              => $SHOW ,
    "Gala"                               => $SHOW ,
    "One man show"                       => $SHOW ,
    "Multisports"                        => $SPORT ,
    "Sport"                              => $SPORT ,
    "e-sport"                            => $SPORT ,
    "Golf"                               => $SPORT ,
    "Basket-ball"                        => $SPORT ,
    "Equitation"                         => $SPORT ,
    "Boxe"                               => $SPORT ,
    "Hockey sur gazon"                   => $SPORT ,
    "Football américain"                 => $SPORT ,
    "Rugby à XIII"                       => $SPORT ,
    "Handball"                           => $SPORT ,
    "Baseball"                           => $SPORT ,
    "Gymnastique rythmique"              => $SPORT ,
    "Badminton"                          => $SPORT ,
    "Gymnastique artistique"             => $SPORT ,
    "BMX"                                => $SPORT ,
    "Skate-board"                        => $SPORT ,
    "VTT"                               => $SPORT ,
    "Voltige aérienne"                   => $SPORT ,
    "Sports extrêmes"                   => $SPORT ,
    "Roller"                            => $SPORT ,
    "Sports fun"                         => $SPORT ,
    "Escalade"                          => $SPORT ,
    "Kick-boxing"                       => $SPORT ,
    "Lutte"                              => $SPORT ,
    "Arts martiaux"                      => $SPORT ,
    "Boxe thaï"                         => $SPORT ,
    "Karaté"                            => $SPORT ,
    "MMA"                                => $SPORT ,
    "Ju jitsu"                          => $SPORT ,
    "Sports de combat"                  => $SPORT ,
    "Sumo"                              => $SPORT ,
    "Surf"                               => $SPORT_WATER ,
    "Plongeon"                          => $SPORT_WATER ,
    "Canoë-kayak"                       => $SPORT_WATER ,
    "Wakeboard"                         => $SPORT_WATER ,
    "Sports mécaniques"      		     => $SPORT_MOTOR ,
    "Motocross"                          => $SPORT_MOTOR ,
    "Magazine de la moto"               => $SPORT_MOTOR ,
    "Enduro"                             => $SPORT_MOTOR ,
    "Motocross freestyle"                => $SPORT_MOTOR ,
    "Speedway"                           => $SPORT_MOTOR ,
    "Comédie"                  			 => $COMEDY ,
    "Classique"                		     => $CLASSICAL,
    "Documentaire"     					 => $DOCUMENTARY ,
    "Magazine de la mode"  				 => $MAGAZINE ,
    "Emission"                           => $MAGAZINE ,
	"Animalier"             			 => $NATURE ,
    "Nature"              			     => $NATURE ,
    "Magazine animalier"  			     => $NATURE ,
    "Découvertes"          			     => $FOREIGN ,
    "Magazine du tourisme"               => $FOREIGN ,
    "Magazine religieux"   			     => $RELIGION ,
    "Sports"          					 => $SPORT ,
    "Voyage"           				   	 => $FOREIGN ,
    "Culture"           				 => $CULTURE ,
    "Pièce de théâtre"                  => $CULTURE ,
    "Magazine littéraire"		       	 => $LITERATURE ,
    "Magazine sportif"       		   	 => $SPORT_MAGAZINE ,
    "Téléfilm policier"    		         => $THRILLER ,
    "Magazine politique"   			     => $SOCIAL,
    "Série dramatique"				  	=> $MOVIE ,
    "Drame psychologique"               => $MOVIE ,
    "Environnement"         		    => $NATURE ,
    "Opéra"             				=> $OPERA ,
    "Magazine de géopolitique"   		=> $DOCUMENTARY ,
    "Film documentaire"                 => $DOCUMENTARY ,
    "Série documentaire"                => $DOCUMENTARY ,
    "Reportage"                          => $DOCUMENTARY ,
    "Making-of"                          => $DOCUMENTARY ,
    "Série de suspense"          		=> $THRILLER ,
    "Economie"                			=> $SOCIAL,
    "Programme court"                   => $EXPERIMENTAL_FILM ,
    "Court-métrage"                      => $EXPERIMENTAL_FILM ,
    "Court métrage"                      => $EXPERIMENTAL_FILM ,
    "Prog court"                         => $EXPERIMENTAL_FILM ,
    "Court métrage dramatique"          => $EXPERIMENTAL_FILM ,
    "Moyen-métrage"                     => $FILM ,
    "Film"                      		=> $FILM ,
    "Films"                      		=> $FILM ,
    "Téléfilm catastrophe"      	    => $FILM ,
    "Magazine du cinéma"                => $FILM ,
    "Magazine du court métrage"         => $FILM ,
    "Cinéma"                            => $FILM ,
    "Magazine pornographique"           => $XXX ,
    "Magazine de charme"        	    => $XXX ,
    "Film pornographique"               => $XXX ,
    "Film érotique"                     => $XXX ,
    "Série érotique"                    => $XXX ,
    "Téléfilm pornographique"           => $XXX ,
    "Téléfilm érotique"                 => $XXX ,
    "Magazine culturel"          		=> $CULTURE ,
    "Cérémonie"                   		=> $SPORT_SPECIAL ,
    "Magazine de découvertes"     		=> $TRAVEL ,
    "Magazine de reportages"          	=> $TRAVEL ,
    "Magazine régional"                 => $TRAVEL ,
    "Magazine du multimédia"         	=> $EXPERIMENTAL_FILM ,
    "Magazine de services"            	=> $NEWS_MAGAZINE ,
    "Magazine du jeu vidéo"             => $HOBBIES ,
    "Magazine de la mer"   				=> $MAGAZINE ,
    "Magazine de l'armée"               => $MAGAZINE ,
    "Magazine"                          => $MAGAZINE ,
    "Magazine de société"  				=> $MAGAZINE ,
    "Magazine historique" 			    => $MAGAZINE ,
    "Magazine du consommateur"  	    => $ECONOMIC ,
    "Dessin animé"                      => $CARTOON ,
    "Série jeunesse"            		=> $CARTOON ,
    "Politique"                         => $SOCIAL,
    "Magazine culinaire"                => $COOKING, 
    "Ballet"                         	=> $MUSIC ,
    "Magazine musical"                  => $MUSIC ,
    "Formule 1"                         => $SPORT_MOTOR ,
    "Comédie sentimentale"              => $SOAP ,
	"Comédie dramatique"                => $SOAP ,
    "Athlétisme"                        => $ATHLETICS,
    "Magazine du jardinage"             => $GARDENING ,
    "Interview"                         => $DEBATE ,
    "Rugby"                  			=> $SPORT_TEAM ,
    "Gastronomie"                       => $COOKING, 
    "Magazine de la décoration"    		=> $HOBBIES ,
    "Indycar"                           => $SPORT_MOTOR ,
    "Automobilisme"                     => $SPORT_MOTOR ,
    "Magazine de la santé"              => $MEDECINE ,
    "Magazine médical"                  => $MEDECINE ,
    "Santé"                             => $MEDECINE ,
    "Débat"                             => $DEBATE ,
    "Sciences et technique"             => $TECHNOLOGY ,
    "Clip"          					=> $MUSIC ,
    "Musique"          					=> $MUSIC ,
    "Clips"          					=> $MUSIC ,
    "Téléfilm musical"                  => $MUSIC ,
    "Série musicale"                    => $MUSIC ,
    "Téléfilm dramatique"               => $MOVIE ,
    "Concert"                           => $MUSIC ,
    "Emission musicale"                 => $MUSIC ,
    "Film musical"                      => $MUSIC ,
    "Comédie musicale"                   => $MUSIC ,
    "Pop & Rock"                        => $MUSIC ,
    "World Music"                       => $MUSIC ,
    "Emission sportive"                 => $SPORT_MAGAZINE ,
    "Théâtre"       					=> $CULTURE ,
    "Spectacle"                     	=> $SHOW ,
    "Fitness"           				=> $FITNESS ,
    "Magazine jeunesse"     			=> $KIDS ,
    "Enfants"                           => $KIDS ,
    "Emission jeunesse"    				=> $KIDS ,
	"Jeunesse"    						=> $KIDS ,
    "Film pour la jeunesse"             => $KIDS ,
    "Téléfilm pour la jeunesse"         => $KIDS ,
    "Série d'aventures"    				=> $ADVENTURE ,
    "Aventures"                         => $ADVENTURE ,
    "Péplum"                            => $HISTORICAL ,
    "Magazine d'actualité"              => $NEWS_MAGAZINE ,
    "Magazine d'information"            => $NEWS_MAGAZINE ,
    "Magazine de l'automobile"          => $MOTORING , 
    "Magazine de l'économie"            => $ECONOMIC ,
    "Série d'animation"                 => $CARTOON ,
    "Animation"                         => $CARTOON ,
    "Téléfilm d'animation"              => $CARTOON ,
    "Court métrage d'animation"         => $CARTOON ,
    "Film d'animation"                  => $CARTOON ,
    "Dessin animé manga"                => $CARTOON ,
    "Dessin animé jeunesse"             => $CARTOON ,
    "Dessin animé Dessin animé"         => $CARTOON ,
    "Série d'action"         			=> $ADVENTURE ,
    "Film de guerre"                    => $ADVENTURE ,
    "Film d'aventures"                  => $ADVENTURE ,
    "Film de suspense"                  => $THRILLER ,
    "Comédie noire"                     => $THRILLER ,
    "Thriller"                          => $THRILLER ,
    "Film policier"                     => $THRILLER ,
    "Thriller politique"                => $THRILLER ,
    "Magazine de l'art de vivre"        => $TRAVEL ,
    "Magazine de l'environnement"       => $NATURE ,
    "Téléfilm d'horreur"          		=> $SF ,
    "Série d'horreur"          			=> $SF ,
	"Téléfilm de science-fiction"		=> $SF ,
    "Film de science-fiction"           => $SF ,
    "Film fantastique"                  => $SF ,
    "Film d'horreur"                    => $SF ,
	"Téléfilm d'action"					=> $ADVENTURE ,
    "Film d'action"					    => $ADVENTURE ,
    "Western"                           => $ADVENTURE ,
	"Téléfilm"							=> $MOVIE ,
	"Série de science-fiction"			=> $SF ,
	"Série historique"					=> $HISTORICAL ,
    "Biographie"                        => $HISTORICAL ,
    "Civilisations"                     => $HISTORICAL ,
    "Histoire"                          => $HISTORICAL ,
    "Société"                           => $SOCIAL , 
    "Magazine de l'emploi"              => $SOCIAL , 
    "Justice"                           => $SOCIAL , 
    "Conférence"                        => $DEBATE ,
    "Variétés"                          => $VARIETY ,
    "Emission du bien-être"             => $FITNESS ,
    "Magazine de la nature"             => $NATURE , 
    "Magazine de la pêche"              => $NATURE , 
    "Magazine des loisirs"              => $HOBBIES ,
    "Voyance"                           => $SPIRITUAL ,
    "Médias"                            => $BROADCASTING ,          
 ) ; 

my $PRE  = '<category lang=\"fr\">' ;
my $POST = '</category>'  ;

sub myfilter {
  my ($a) = @_;
  if ( exists $REPLACE{$a} ) {     
      return $REPLACE{$a} ;
  } else {
      print STDERR "Warning: Unmanaged category: '$a'\n" ;
      return $a ;
  }
}

while (<>) {
    my $line = $_ ;
    $line =~ s/($PRE)(.*)($POST)/"$1".myfilter("$2")."$3"/ge ;
    print $line;
} 

