#!C:\perl\bin -w -I./t
# PROGRAM modul : EDIREX6.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
#***************************************************

#*******************************************
# SUB exec receive
#################################  
sub exec_receive
{
my $retok2=1;
    @bestanden =();
    $retok=1;
    &grep_files;  #   on error return 400
    if ($retok != 1) { return; }
    if ($#bestanden > -1){
      foreach $vstup (@bestanden){
        &get_file ; # on error return 500
        if ($retok != 1) {
          if ($param{'StopOnInError'} == 1 ) { return; }
          $retok2=0;
          next;
        }
        &remote_move_file ;# on error return 600
        if ($retok != 1) {
          if ($param{'StopOnInError'} == 1 ) { return; }
          $retok2=0;
        }
      }; # for each
    } else {
      &Hlaska("---- No Data on VANCCFS");
    }; # if bestanden
    if ( $param{'edirex_biolit'} >= 1){
      &Hlaska("-- konec exec_receive.");
    };
    if($retok2 != 1 ) { $retok =0; }
    else { $retok = 1; }
}
#
#*********************************
sub grep_files
 {
my $soubor;
my $typ;
    &getdatum;
    print "* $datum --- Prohlizim schranku ... \n";
    $file="";
    $dfile =0;
    @file="";
    $retok=1;
    $ret= syswrite (WRITE,"ls out/*\n\n\n");
    &vycti400;
    @file=split(/\n/,$file);
    my $dfile=@file;
    &DbgPrint(" pole file -- delka : ->$dfile<---- \nobsah : @file\n--------\n");
    @bestanden=();
    $dfile=$dfile-1;
    if ($dfile gt 0){
      for ($i=0;$i<$dfile;$i++){
        if(substr($file[$i],0,1) ne '-') { next; }
        ##if((length($file[$i]) == 80) and (length($file[$i+1]) < 61)) {
        ##  $file[$i]=$file[$i] . $file[$i+1];
        ##  $file[$i+1]="";
        ##}
        ($soubor)= $file[$i] =~ / (\S*)$/;     # Najdi posledni sloupec=jmeno souboru
        if ( $param{'edirex_biolit'} >= 1) { print AUDIT "Nalezen soubor -> $soubor\n"; };
        if(@DownloadOnlyType) {
          foreach $typ (@DownloadOnlyType) {
            if($soubor =~ /$typ/) { push(@bestanden,$soubor); }
          }
        } else { push(@bestanden,$soubor); }
      }; #edn of for
    }; #end of if 
 ;}
# end of sub grep files 
#********************************************
#****************************
#***    vycti400  GREP DIR  ******
#****************************
sub vycti400
{ my $delka=80;
  my $vycteno="";
  sleep 1;
   while (( $vycteno !~ /ls out/) and ($delka)){
     $delka = sysread ( READ,$vycteno,80);
     $vycteno=~s/\r//g;
     if ( $param{'edirex_biolit'} >= 2){
       print AUDIT "4.1 vycti400 $delka = |$vycteno|\n";
     };
   };
  $vycteno="";
  $delka=80;
   while (( $vycteno !~ /sftp>/) and ($delka)){
     $delka = sysread ( READ,$vycteno,80);
     $vycteno=~s/\r//g;
     if ( $param{'edirex_biolit'} >= 2){
       print AUDIT "4.2 vycti400 $delka = |$vycteno|\n";
     };
     $file=$file.$vycteno;
   };
   if ( $param{'edirex_biolit'} >= 2){
     print AUDIT " v retezci je  : ---------\n $file \n--------\n ";
   };
};
#****************************

#*******************************************
# SUB get_file to  VANCCF 
#################################  
sub get_file
{ 
   &getdatum;
   &DbgPrint("get Out/$vstup \"$param{'edirex_in_dir'}/$vstup\" -o -b\n");
   print AUDIT "* $datum >>>> STAHUJI $vstup ... ";		# Zde nepouzivat &Hlaska!!!
   print       "* $datum >>>> STAHUJI $vstup ... ";
   if( -e "$param{'edirex_in_dir'}/$vstup" ) {
     print AUDIT "file already exists in $param{'edirex_in_dir'}, leave it.\n";
     print       "file already exists in $param{'edirex_in_dir'}, leave it.\n";
     $Chyba = "ERROR 501 - Soubor \"$vstup\" jiz existuje v in, nestahuji!" ; 
     &error_log;
     $retok=11;   # Signalizuj existenci souboru v in
     return;  
   }   
   $retok=0;
   $ret= syswrite (WRITE,"get Out/$vstup \"$param{'edirex_in_dir'}/$vstup\" -o -b\n\n");
   ##$ret= syswrite (WRITE,"500 \n");
   &VyctiVystup("get");
   if ($retok eq 1){   # if vse ok 
     print AUDIT "OK\n";
     print       "OK\n";
     &DbgPrint("* $datum ---> File : $vstup downloaded to $param{'edirex_in_dir'}/$vstup \n");
     chmod 0666, "$param{'edirex_in_dir'}/$vstup";
     &ZapisLastDownload();
   } else {
     print AUDIT "ERROR\n";
     print       "ERROR\n";
     $Chyba = "ERROR 500 - Chyba SSH download \"$vstup\" !" ; 
     if( -e "$param{'edirex_in_dir'}/$vstup" ) {
       $Chyba = $Chyba . "  mazu pravdepodobne nekompletne preneseny soubor.";
       chmod 0666, "$param{'edirex_in_dir'}/$vstup";
       unlink("$param{'edirex_in_dir'}/$vstup" );  #Delete possibly bad file after download.
     }
     &error_log; 
   } 
}# end of get_file

#****************************
#***    VyctiVystupLS  ******
#****************************
sub VyctiVystupLS($souborls)
{ 
  my $vycteno="";
  my ($souborls)=@_;
  $retok=0;
  sysread ( READ,$vycteno,120);
  sysread ( READ,$vycteno,120);
  sysread ( READ,$vycteno,120);
  if( $vycteno =~ /$souborls/) { $retok=1; }
  if ( $param{'edirex_biolit'} >= 1){ print( AUDIT "VyctiVystupLS:|$vycteno|\n"); }
} # end of sub VyctiVystupLS
#***********************************

#*******************************************
# sub remotemove_file 
#**********************************************
sub remote_move_file
{   
   &getdatum;
   $ret= syswrite (WRITE,"rename out/$vstup out/archiv/$vstup\n\n");
   &VyctiVystup("rename");
   if ($retok eq 1) {
     &DbgPrint("* $datum ---> File : $vstup remote move to ARCHIV OK\n");
   } else {
     $ret= syswrite (WRITE,"rename out/$vstup out/archiv/$vstup$cas\n\n");
     &VyctiVystup("rename");
     if ($retok eq 1) {
       &Hlaska("! V remote archivu je soubor stejneho jmena, archivuji jako $vstup$cas");
     } else {
       chmod 0666, "$param{'edirex_in_dir'}/$vstup";
	   $ret= syswrite (WRITE,"ls out/$vstup\*\n\n");
	   &VyctiVystupLS($vstup);
       if ($retok eq 1) {
		 unlink("$param{'edirex_in_dir'}/$vstup" );   # Delete downloaded file.
         $Chyba = "ERROR 600 - Chyba SSH remote move to Archiv \"$vstup\" Delete $param{'edirex_in_dir'}/$vstup!" ;   
		 &error_log;
	   }
     }
   }
}# end of sub remote rename 
#*************************************

sub DbgPrint(){
my ($text)=@_;
  if( $param{'edirex_biolit'} >= 1){
    print("$text");
    print(AUDIT "$text");
  }
}
#*************************************

sub JeNecoVIn(){
  opendir (DIRDIR,"$param{'edirex_in_dir'}"); 
  @bestanden = readdir(DIRDIR);
  closedir(DIRDIR);
  if ($#bestanden > -1)  { return(1); }
  return(0);
}
#################################  
1; 
 
 
