##########################################################
#                #                                       #
#	 ManyPage    #    See habille.pl / Voir habille.pl   #
#                #                                       #
##########################################################
#
# Cette fonction lit le fichier d'habillage
#
# Pour chaque nouvelle lecture, on remplace l'objet existant
#
###################
#
#  Copyright (C) 2000 Pierre Cordani - Pascal Vuylsteker
#
# 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 2
# of the License, or (at your option) any later version.
#
# Ce programme est un logiciel libre ; vous pouvez le redistribuer et/ou le modifier conformŽment
# aux dispositions de la Licence Publique GŽnŽrale GNU, telle que publiŽe par la Free Software
# Foundation ; version 2 de la licence, ou encore (ˆ votre choix) toute version ultŽrieure.
#
###################

sub lecture_habillage
{
 local ($Habille,*habillage_brut) = @_;

 $sauve = $/;
 undef $/;
 open (HAB, $Habille);
 $cont = <HAB>;
 $/ = $sauve;
 
 $Objet = '';
 $Nb_Objets = 0;

 local %Objet_html = %habillage_brut;
 local ($i) = 0;
 
 $cont =~ s/\s*<!--[^-].*?-->\s*//gis; 
 if($cont =~ s/<\s*$Tag_Langages\s*>(.*?)<\s*\/$Tag_Langages\s*>//is)
  {
   $Objet_html{$Tag_Langages} = $1;
  }

 local(@Les_Langues_Suf) = [];
 local(%Les_Langues_H) = ();
 (*Les_Langues_Suf,*Les_Langues_H) = &make_langue(%Objet_html);

 local ($i_obj) = 0;

 $cont =~ s/<\s*(MDP(_|\.))(.*?)\s*>/<MP\.$3>/isg;
 $cont =~ s/<\s*(\/MDP(_|\.))(.*?)\s*>/<\/MP\.$3>/isg;
 while($Les_Langues_Suf[$i_obj])
  {
   local ($ouverture) = $SAPI_LT."SAPI.$Les_Langues_Suf[$i_obj]$SAPI_GT";
   local ($fermeture) = "$SAPI_LT/SAPI.$Les_Langues_Suf[$i_obj]$SAPI_GT";
   $cont =~ s/<\s*(MP\.)?$Les_Langues_Suf[$i_obj]\s*>/$ouverture/isg;
   $cont =~ s/<\s*\/(MP\.)?$Les_Langues_Suf[$i_obj]\s*>/$fermeture/isg;
   $i_obj++;
  }
# while($cont =~ s/(.*)<\s*((?:(?:MP)|(?:PM))\.(.*?))( .*?)?>(.*?)<\s*\/(\2)\s*>/$1/si)
# while($cont =~ s/(.*)<\s*((?:(?:MP))\.(.*?))( .*?)?>(.*?)<\s*\/(\2)\s*>/$1/si)
 while($cont =~ s/(.*?)<\s*((?:(?:MP)|(?:PM))\.(.*?))( .*?)?>(.*?)<\s*\/(\2)\s*>(.*)/$7/si)
  {
#   print "==>$2 $6\n";
   local ($open,$val_def,$obj,$close) = ($2,$4,$5,$6);

   $obj =~ s/^\s*//s;
   $obj =~ s/\s*$//s;
   if($val_def)
    {
	 local ($Op_temp) = $val_def;
     while($Op_temp =~ /(?:(.*)\s+|)(.*)\s*=\s*"(.*?)"\s*/s)
	  {
	   $Op_temp = $1;
	   $Attr = $2;
	   $Val_attr = $3;
	   if(!($obj =~ s/(PM\.ATTRIBUT\.$Attr)/$1<SAPI.DEF>$Val_attr<\/SAPI.DEF>/i))
		{
		 print "L'attribut $Attr n'existe pas dans l'objet $Expr\n";
		}
#	   print "$open--> $Attr => $Val_attr\n";
	  }
#	 print "$val_def => ($obj)\n";
	}
#   print "==>$open\n";
#   $obj =~ s/\s*$//s;
   $Objet_html{uc($open)} = $obj;
#   print "($open => $obj)\n";
  }
 $cont =~ s/\s*//gs;
 if($cont)
  {
   if($cont =~ /<(.*?)>/s)
    {
	 print "L'objet $1 dans $Habille n'est pas correct\n";exit;
	}
  }
# print "c'est ok\n";
 return ({%Objet_html},[@Les_Langues_Suf],{%Les_Langues_H});
}


###################
#
# Cette fonction prepare l'habillage
#
# Habillage brut -> Habillage net
#
###################

sub dispersion_habillage
{
 local (*habillage_brut) = @_;
 local (%Objet_html) = %habillage_brut;
 local ($i) = 0;local ($t) = 0;

 @Nom_Objets = keys(%Objet_html);

 $i = 0;
 while($Nom_Objets[$i])
  {
#   print "==>$Nom_Objets[$i]\n";
   $nb_lignes = @lignes = split (/\n/,$Objet_html{$Nom_Objets[$i]});
   $nb_rec = 0;

     for(local ($k) = 0;$k < $nb_lignes; $k++)
      {
       $au_debut = $info_val = $lignes[$k];
#       print "$k : $Nom_Objets[$i]======$info_val\n";

#       print "$nb_rec\n";
       while($info_val =~ /(.*)<(MP)(.*?)( (.*?))?>(.*)/is)
        {
         $Expr = uc("$2$3");
         $Option = $5;
         if(!$Option){$Option = ""}
#        $info_val = $1;
#     	 print "($Nom_Objets[$i]) ($au_debut) $info_val -->$Expr ($5) $Objet_html{$Expr}\n";
		 $nb_rec++; 
         if($nb_rec > $Nb_rec_max)
		  {
		   print "Le nombre maximum de remplacement ($Nb_rec_max) dans un objet a ete depasse\n";
		   print "L'objet $Nom_Objets[$i] peut etre la source d'une boucle infinie\n";
           exit(1);
		  }
		 $Expr_e = $Objet_html{$Expr};

		 local ($Op_temp) = $Option;
		 while($Op_temp =~ /(?:(.*)\s+|)(.*)\s*=\s*"(.*?)"\s*/is)
		  {
		   $Op_temp = $1;
		   $Attr = $2;
		   $Val_attr = $3;
		   if(!($Expr_e =~ s/PM\.ATTRIBUT\.$Attr(<SAPI.DEF>(.*?)<\/SAPI.DEF>)?/$Val_attr/i))
		    {
			 print "L'attribut $Attr n'existe pas dans l'objet $Expr\n";
			}
#		   print "--> $Attr => $Val_attr\n";
		  }

#
# On est oblige de garder une trace des objets MDP.UP, MDP.NEXT, MDP.BACK
# MDP.FLAG.FR , MDP.FLAG.EN
# MDP.AVERTISSEMENT.FR , MDP.AVERTISSEMENT.EN
# Pour apres pouvoir placer les liens correspondants
#

         local($i_tags_t) = 0;
         local($val) = '';
		 while($Tags_traiter[$i_tags_t])
		  {
           if($Expr =~ /.*$Tags_traiter[$i_tags_t](.*)/)
		    {
			 $val = $1;
		     $lignes[$k] =~ s/<$Expr( ?$Option)?>/<$Mon_Pref$Tags_traduir[$i_tags_t]$val>$Expr_e<\/$Mon_Pref$Tags_traduir[$i_tags_t]$val>/isg if($Objet_html{$Expr});
		     $info_val =~ s/<$Expr( ?$Option)?>/<$Mon_Pref$Tags_traduir[$i_tags_t]$val>$Expr_e<\/$Mon_Pref$Tags_traduir[$i_tags_t]$val>/isg;
		     last;
		  	}
		   $i_tags_t++;
		  }

		 if($val eq '')
		  {

#
# Je dois garder une trace de la nationalite des objets
#

		   local ($t_n,$t_l) = $Expr =~ /MD?P\.(.*)\.(.*)$/;
		   if($t_l && $Les_Langues_H{lc($t_l)})
		    {
			 $lignes[$k] =~ s/<$Expr( .*)?>/$SAPI_LT$t_n.$t_l$SAPI_GT$Expr_e$SAPI_LT\/$t_n.$t_l$SAPI_GT/isg if($Objet_html{$Expr});
			 $info_val =~ s/<$Expr( .*)?>/$SAPI_LT$t_n.$t_l$SAPI_GT$Expr_e$SAPI_LT\/$t_n.$t_l$SAPI_GT/isg;
			}
		   else
		    {
			 $lignes[$k] =~ s/<$Expr( .*)?>/$Expr_e/isg if($Objet_html{$Expr});
			 $info_val =~ s/<$Expr( .*)?>/$Expr_e/isg;
		  	}
          }
#print "($Expr => $Expr_e)\n";
		}
      }
   $Objet_html{$Nom_Objets[$i]} = '';
   for(local ($k) = 0;$k < $nb_lignes; $k++)
    {
     $Objet_html{$Nom_Objets[$i]} .= $lignes[$k]."\n";
    }
     $Objet_html{$Nom_Objets[$i]} =~ s/\n$//;
   $i++;
  }
 
 return ({%Objet_html});

}

sub lecture_config
{
 open (Config, "$Home_Path/config");
 $sauve = $/;
 undef $/;
 $config = <Config>;
 $/ = $sauve;
 
 local($Source) = $config;
 $Source =~ s/.*<Source>(.*)<\/Source>.*/$1/s;
 $Source =~ s/\n//sg;

 local($Dest) = $config;
 $Dest =~ s/.*<Destination>(.*)<\/Destination>.*/$1/s;
 $Dest =~ s/\n//sg;

 return ($Source, $Dest);
 
}

return 1;
