#!/usr/bin/perl -w

use File::Copy;

require "/etc/squid/redirector/dbl/dbl.conf";

# Write log function
 sub wlog
 {
  my ($sec, $min, $hour, $mday, $mon, $year) = localtime ;
  open (LOG,">>$logdir/update.fast.log") or die "Can't open $logdir/update.fast.log log: $!\n";
  printf LOG "%4d-%02d-%02d %2d:%02d:%02d %s\n", $year + 1900,$mon+1, $mday, $hour, $min, $sec,$_[0];
  close (LOG);
 }
 
 wlog("======= Start update ========");

 # Download new data
 wlog("Download new data");
 $ret=system("$WGET -t 1 -o $logdir/wget.log \"http://rejik.ru/cgi-bin/fast_get2.cgi?Login=$login\&Pass=$pass\" -O $dir/new.tmp");

 if ($ret!=0)
 {
  wlog("Can't download. See $logdir/wget.log for error\n");
  exit;
 }

 #Check to html
 open (FD,"<$dir/new.tmp") or die "Can't open $dir/new.tmp: $!\n";
 $line=<FD>;
 if ($line =~/\<\!DOCTYPE HTML PUBLIC/)
 {
  wlog("Download html file. See $dir/new.html\n");
  close(FD);
  move("$dir/new.tmp","$dir/new.html");
  exit;
 }

 # gzip new data to fast.dbl
  $ret=system("cat $dir/new.tmp | gzip -c >$dir/fast.dbl");
   if ($ret!=0)
    {
     wlog("Can\'t ungzip. Command \"cat $dir/new.tmp | gzip -c >$dir/fast.dbl\" return error.\n");
     exit;
    }
	       
 wlog("New data now aviable in $dir/fast.dbl");

 # Delete temp files
 unlink("$dir/new.tmp","$logdir/wget.log");
  
