#!/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 ,
	"Talk show"							 => $TALKSHOW ,
    "Météo"								 => $WEATHER ,
    "Drame"								 => $MOVIE ,
    "Série fantastique"                  => $SF,
	"Magazine scientifique"              => $SCIENCE,
	"Magazine de télé-achat"             => $SHOPPING, 
	"Série réaliste"                     => $MOVIE,
	"Série hospitalière"                 => $MOVIE,
	"Jeu"               				 => $GAME ,
	"Magazine de la gastronomie"  		 => $COOKING, 
    "Journal"            				 => $NEWS ,
    "Feuilleton sentimental"      		 => $SOAP ,
    "Téléfilm romanesque"          		 => $SOAP ,
    "Feuilleton réaliste"         		 => $SOAP ,
    "Série sentimentale"          		 => $SOAP ,
    "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 ,
    "Série policière"                	 => $THRILLER ,
    "Comédie policière"                	 => $THRILLER ,
    "Magazine sportif"              	 => $SPORT_MAGAZINE,
    "Téléfilm dramatique"           	 => $MOVIE ,
    "Téléfilm humoristique"        		 => $COMEDY ,
    "Téléfilm sentimental"       	     => $SOAP ,
    "Téléfilm romanesque"          		 => $SOAP ,
    "Football"              		     => $FOOTBALL , 
    "Tennis"                		     => $TENNIS ,
    "Divertissement"       			     => $SHOW ,
	"Divertissement - humour"       	 => $SHOW ,
    "Multisports"                        => $SPORT ,
    "Sport"                              => $SPORT ,
    "Sports mécaniques"      		     => $SPORT_MOTOR ,
    "Comédie"                  			 => $COMEDY ,
    "Classique"                		     => $CLASSICAL,
    "Documentaire"     					 => $DOCUMENTARY ,
    "Magazine de la mode"  				 => $MAGAZINE ,
	"Animalier"             			 => $NATURE ,
    "Nature"              			     => $NATURE ,
    "Magazine animalier"  			     => $NATURE ,
    "Découvertes"          			     => $FOREIGN ,
    "Magazine religieux"   			     => $RELIGION ,
    "Sports"          					 => $SPORT ,
    "Voyage"           				   	 => $FOREIGN ,
    "Culture"           				 => $CULTURE ,
    "Magazine littéraire"		       	 => $LITERATURE ,
    "Magazine sportif"       		   	 => $SPORT_MAGAZINE ,
    "Téléfilm policier"    		         => $THRILLER ,
    "Magazine politique"   			     => $SOCIAL,
    "Série dramatique"				  	=> $MOVIE ,
    "Environnement"         		    => $NATURE ,
    "Opéra"             				=> $OPERA ,
    "Magazine de géopolitique"   		=> $DOCUMENTARY ,
    "Série de suspense"          		=> $THRILLER ,
    "Economie"                			=> $SOCIAL,
    "Film"                      		=> $FILM ,
    "Téléfilm catastrophe"      	    => $FILM ,
    "Magazine du cinéma"                => $FILM ,
    "Magazine du court métrage"         => $FILM ,
    "Magazine pornographique"           => $XXX ,
    "Magazine de charme"        	    => $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 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 ,
    "Clips"          					=> $MUSIC ,
    "Téléfilm musical"                  => $MUSIC ,
    "Téléfilm dramatique"               => $MOVIE ,
    "Concert"                           => $MUSIC ,
    "Théâtre"       					=> $CULTURE ,
    "Spectacle"                     	=> $SHOW ,
    "Fitness"           				=> $FITNESS ,
    "Magazine jeunesse"     			=> $KIDS ,
    "Emission jeunesse"    				=> $KIDS ,
	"Jeunesse"    						=> $KIDS ,
    "Série d'aventures"    				=> $ADVENTURE ,
    "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 ,
    "Téléfilm d'animation"              => $CARTOON ,
    "Court métrage d'animation"         => $CARTOON ,
    "Film d'animation"                  => $CARTOON ,
    "Série d'action"         			=> $ADVENTURE ,
    "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 ,
	"Téléfilm d'action"					=> $THRILLER ,
	"Téléfilm"							=> $MOVIE ,
	"Série de science-fiction"			=> $SF ,
	"Série historique"					=> $HISTORICAL ,
 ) ; 

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;
} 

