#!C:\perl\bin -w -I./t
# PROGRAM modul : EDIREX4.pm  
# auther : LPO , M&F Hlinsko, Palackeho 1295, f.o. 
# GNU : garante jusqu a non changee  file.
# L'Original est depose chez auther
# Date de commence de creation  : 21.11.2002
# La dernier change             : 14.07.2003
#***************************************************
################################
# uklada do \unicom \export
# pouziva vlastni radu presess.id
#  ulozene souboru maji jmeno psxxxxx.edi 
#################################  

##################################  
#     ROZDELA vstup dle IEA
#################################
sub rozdelA
{ 
######################
#nacte se posledni platne cislo presession .id
   $poradi=0;
   $poradifile="$EDIREX_HOME/presess.id";
   &getdatum;
   $poradi=$cas;
   
 # print          "**** Spliting IEA ************************\n";
 # print AUDIT "* getdatum   Spliting IEA - PreSession reading  started: $poradifile *\n";
 # print          "*****************************************\n";
 # print          "*****************************************\n";
   #if (-e $poradifile)
   #{ open(PORADI, "<$poradifile") or die "Cannot open out presessionid file";
   #$poradi=<PORADI>;
   #close PORADI; }
   #$poradi=$poradi+1;
   # if ($poradi > 99999) { $poradi =1  } #  reset pocitadla at neleze za meze
   # $poradi=sprintf("%.5d",$poradi);
###########
$VYSTUPDIR=$param{'edirex_out_dir'};
#############################################
# smycka an cteni vsech edi filu koncicich na *.edi
#  je v conrol.pl na zacatku
#  vstupem je promena HOME, VERZE, vstup
########################################
###########
# zacatek deleni filu 
 open(VSTUP, "<$vstup") or die "Cannot open message file $vstup: $!,";
 #binmode(VSTUP);
 #  ($colon, $plus, $query, $quote)=("#", "*", "?", "~");    ASTERIX
 #  ($colon, $plus, $query, $quote)=("#", "~", "?",chr(13));  OBELIX
    ($colon, $plus, $query, $quote)=("#", "*", "?", "~");
    $lterm = "~";
 #  $lterm = "~"; ASTERIX
 #  $lterm = chr(13); OBELIX 
  $irs_save = $/;
   $/ = $lterm;      # input line separator
   $partial="";

################# vystup s unikatnim  id 
 $vystup = "$VYSTUPDIR/p".$poradi.".edi";
 $otevri =1;        ## prikaz k otevreni prvniho vystupu
    while ( <VSTUP> ) 
  {#print VYSTUP "\n\n   odtud ->  \n\n";
   chomp;
  if ($otevri ==1)   # otevreni povoleno tak otevri 
    { open(VYSTUP, ">$vystup") or die "Cannot open message file $vystup: $!,";
      $otevri =0;   
    }
 ### otevrel vystup a zakazal otevirat dalsi
#********************************************************
    if ( $query && m/[$query]$/ ) 
      {                              #if line ends with escape, then
      $partial .= "$_$quote" ;          #  we don't have the whole segment
      $.--;                             #  don't count this "line"
      next;                             #  go get some more
      }
    if ($partial) 
      {                              #prepend previous partial results to
      $_ = "$partial$_" ;               #   current line
      $partial="";
      }

#    print VYSTUP "\n\n dolar obsahoval :    odtud ->$_\n\n";      
    print VYSTUP "$_".$quote; # na printuj to do vystupu 
#*****************************************************

 if( $_ =~ /IEA\*/ ) {
    #  nasel jsi IEA tak zavri a otevri novy  ############
    $poradi=$poradi+1;
    close VYSTUP;
    $vystup = "$VYSTUPDIR/p".$poradi.".edi";
    $otevri =1; ## zavreno prikaz k otevreni dalsiho      
     }  
##
 }    ############### end while  ##########
 close VYSTUP;
 close VSTUP; 

 my $archporadi=$poradi-1; 
 rename ($vstup,"$param{'edirex_outarchiv'}/O_".$archporadi."_$vstup");

   $/ = $irs_save;      # input line separator

# pozor na konci probehnuvsi rutiny se musi ulozit
# posledni platne
   #open(PORADI, ">$poradifile") or die "Cannot open out sessionid file";
   #print PORADI "$poradi";
   #close PORADI;

} ########### end sub rozdel;
#
##################################  
#     ROZDEL vstup dle UNZ
#################################
sub rozdel
{ 
###################  
#nacte se posledni platne cislo presession .id
   $poradi=0;
   $poradifile="$EDIREX_HOME/presess.id";
   &getdatum;
   $poradi=$cas;
# print          "*****Spliting UNZ**********************\n";
# print AUDIT "* $datum  Spliting UNZ - PreSession reading  started: $poradifile *\n";
# print          "*****************************************\n";
#   if (-e $poradifile)
#   {
#   open(PORADI, "<$poradifile") or die "Cannot open out presessionid file";
#   $poradi=<PORADI>;
#   close PORADI;
#   }
#
#   $poradi=$poradi+1;
#   
#  if ($poradi > 99999) { $poradi =1  } #  reset pocitadla at neleze za meze
#   $poradi=sprintf("%.5d",$poradi);
#############
#$VYSTUPDIR="$EDIREX_HOME/OUTBOUND";   
    $VYSTUPDIR=$param{'edirex_out_dir'};
#############################################
# #############################################
# zacatek deleni filu 
 open(VSTUP, "<$vstup") or die "Cannot open message file $vstup: $!,";
 #binmode(VSTUP);
   ($colon, $plus, $query, $quote)=(":", "+", "?", "'");
   $lterm="'";
   $irs_save = $/;
   $/ = $lterm;      # input line separator
   $partial="";

################ 
 $vystup = "$VYSTUPDIR/p".$poradi.".edi";
 $otevri =1;        ## prikaz k otevreni prvniho vystupu
    while ( <VSTUP> ) 
  {
   chomp;
   #while(($_=~/\x0d$/) or ($_=~/\x0a$/)) { chop($_); } # Vymaz vsechny 0D,0A
  if ($otevri ==1)   # otevreni povoleno tak otevri 
    { open(VYSTUP, ">$vystup") or die "Cannot open message file $vystup: $!,";
      $otevri =0;   
    }
 ### otevrel vystup a zakazal otevirat dalsi
#********************************************************
    if ( $query && m/[$query]$/ ) 
      {                                 #if line ends with escape, then
      $partial .= "$_$quote" ;          #  we don't have the whole segment
      $.--;                             #  don't count this "line"
      next;                             #  go get some more
      }
    if ($partial) 
      {                                 #prepend previous partial results to
      $_ = "$partial$_" ;               #   current line
      $partial="";
      }
    print VYSTUP "$_$quote"; # na printuj to do vystupu 
#*****************************************************

  if( $_ =~ /UNZ\+/ ) {
    #  nasel jsi UNZ tak zavri a otevri novy  ############
    $poradi=$poradi+1;
    close VYSTUP;
    $vystup = "$VYSTUPDIR/p".$poradi.".edi";
    $otevri =1; ## zavreno prikaz k otevreni dalsiho      
     }  
##
 }    ############### end while  ##########
 close VYSTUP;
 close VSTUP;
 my $archporadi = $poradi-1; 
 #print "poradi archivu $archporadi -> $poradi \n";
 rename ($vstup,"$param{'edirex_outarchiv'}/O_".$archporadi."_$vstup");
 
 $/ = $irs_save;      # input line separator

# pozor na konci probehnuvsi rutini se musi ulozit
# posledni platne
  # open(PORADI, ">$poradifile") or die "Cannot open out sessionid file";
  # print PORADI "$poradi";
  # close PORADI;

} ########### end sub rozdel;
#
1;
