@echo off
setlocal enableextensions enabledelayedexpansion

set binkdlog=d:\fido\logs\binkd.log
set tpldir=d:\fido\logs

rem ˆ¬ï ¨á¯®«­ï¥¬®£® ä ©«  HPT.
rem Šà ©­¥ à¥ª®¬¥­¤®¢ ­® ãª §ë¢ âì ¯®«­®¥ ¨¬ï ä ©«  (¤¨áª, ¯ãâì, ¨¬ï, à è¨à¥­¨¥).
set hptexe=D:\Fido\hpt\bin\hpt.exe

rem ˆ¬ï ä ©«  ª®­ä¨£ãà æ¨¨ HPT (fidoconfig).
rem Šà ©­¥ à¥ª®¬¥­¤®¢ ­® ãª §ë¢ âì ¯®«­®¥ ¨¬ï ä ©«  (¤¨áª, ¯ãâì, ¨¬ï, à è¨à¥­¨¥).
set hptcfg=D:\Fido\hpt\hpt.cfg

rem Call :initchars

rem Call :ReadLog


rem Call :BuildNodeStatTable

rem Call :BuildNodeSessionsGraph

rem Call :BuildTotalSessionsGraph

rem Call :BuildSentRecivedGraph

rem for /l %%a in (1,1,%subj.0%) do %hptexe% -c %hptcfg% post -nf "Evil Robot" -s "!subj.%%a! [!startperiod! - !endperiod!]" -e "CRIMEA.ROBOTS" -z "!startperiod! - !endperiod!" -o "Lame Users Breading" -f loc,npd %tpldir%\%%a.tpl

%hptexe% -c %hptcfg% post -nf "Evil Robot" -s "Nodes Stat. Table" -e "CRIMEA.ROBOTS" -z "2:460/58" -o "Lame Users Breading" -f loc,npd D:\Fido\logs\sess.tpl
%hptexe% -c %hptcfg% post -nf "Evil Robot" -s "‘â â¨áâ¨ª  á¥áá¨© ¯® ç á ¬ ã§«®¢ R46 á 2:460/58" -e "R46.ALIVE" -z "2:460/58" -o "Lame Users Breading" -f loc,npd D:\Fido\logs\r46tab.tpl
%hptexe% -c %hptcfg% post -nf "Evil Robot" -s "‘â â¨áâ¨ª  á¥áá¨© ¯® ç á ¬ ­  2:460/58" -e "CRIMEA.ROBOTS" -z "2:460/58" -o "Lame Users Breading" -f loc,npd D:\Fido\logs\all.tpl

cmd /c D:\Fido\hpt\hptstat.cmd

rar m -y -m5 -cfg- -ed -ep -ilogd:\fido\logs\rar.log -ag.yyyy.mm.dd D:\Fido\logs\arc\logs D:\Fido\logs\*.log

exit

rem ---------------------------------------------------------------------------
rem ---------------------------------------------------------------------------

:BuildSentRecivedGraph

echo %time:~0,-3% Building In-Out Bytes Graph.

echo RealName: Evil Robot>%tpldir%\4.tpl
echo.>>%tpldir%\4.tpl
echo hour>>%tpldir%\4.tpl

set /a tg=0
if !traff.total.max.s! lss !traff.total.max.r! (
  set /a step=!traff.total.max.s! / !rmarg!
) else (
  set /a step=!traff.total.max.r! / !rmarg!
)
:STrR
   set str= 0!tg!
   set tgf=!str:~-2!
   set str=  !str:~-2!³
   set marg=0
   if !traff.total.%tgf%.r! lss !traff.total.%tgf%.s! (
      for /l %%a in (0,!step!,!traff.total.%tgf%.r!) do set str=!str!%char.2%&set /a marg+=1
      set /a ends=!traff.total.%tgf%.s!-!traff.total.%tgf%.r!
      for /l %%a in (0,!step!,!ends!) do if !marg! lss 65 set str=!str!%char.3%&set /a marg+=1
   ) else (
      for /l %%a in (0,!step!,!traff.total.%tgf%.s!) do set str=!str!%char.3%&set /a marg+=1
      set /a ends=!traff.total.%tgf%.r!-!traff.total.%tgf%.s!
      for /l %%a in (0,!step!,!ends!) do if !marg! lss 65 set str=!str!%char.2%&set /a marg+=1
   )
 echo !str!>>%tpldir%\4.tpl
   set /a tg+=1
   if !tg! lss 24 goto STrR
   
:ETrR

set /a lim=!rmarg!-1
set str=    À
set /a m1=!rmarg! / 4
set /a m2=!rmarg! / 2
set /a m3=!m1! + !m2!
for /l %%a in (1,1,!lim!) do if %%a==!m1! (
                               set str=!str!Â
                             ) else if %%a==!m2! (
                               set str=!str!Â
                             ) else if %%a==!m3! (
                               set str=!str!Â
                             ) else set str=!str!Ä
set str=!str!ÂÄÄÄÄÄÄÄ
echo !str!>>%tpldir%\4.tpl

set str=    0
for /l %%a in (1,1,!lim!) do set str=!str!ÿ
set /a _kb=!step!*!rmarg!/1024
set str=!str!!_kb! kb.
echo !str!>>%tpldir%\4.tpl
echo.>>%tpldir%\4.tpl
set /a max.sent=!traff.total.max.s!/1024
set /a  max.res=!traff.total.max.r!/1024
set /a _kb=!step!/1024
echo         %char.2% - !step! bytes in.>>%tpldir%\4.tpl
echo         %char.3% - !step! bytes out.>>%tpldir%\4.tpl
echo.>>%tpldir%\4.tpl
echo         Maximum out per hour: !max.sent! kb.>>%tpldir%\4.tpl
echo         Maximum in  per hour: !max.res! kb.>>%tpldir%\4.tpl
echo.>>%tpldir%\4.tpl
echo.>>%tpldir%\4.tpl

exit /b

:BuildTotalSessionsGraph

echo %time:~0,-3% Building Total Sessions Graph.

echo RealName: Evil Robot>%tpldir%\3.tpl
 echo.>>%tpldir%\3.tpl
rem  echo       From !startperiod! til !endperiod!>>%tpldir%\3.tpl
 echo hour>>%tpldir%\3.tpl
 set /a tg=0
 set /a flag=1
:STotal
   set str= 0!tg!
   set str=  !str:~-2!³
      set /a stth=0
      :STT
       if !stth!==!totalsessions.%tg%! goto ETT
       if !stth!==!rmarg! goto ETT
       if !totalsessions.%tg%! gtr !rmarg! (
          set str=!str!!char.4!
       ) ELSE (
          set str=!str!!bchar.%flag%!
       )
       set /a stth+=1
       goto STT
      :ETT
       if !totalsessions.%tg%! gtr !rmarg! (
          set str=!str!!char.4!
       ) else (
          set str=!str!!char.1!
       )
        set /a stth+=1
      if !stth! lss !rmarg! goto ETT

   set /a flag=1-!flag!
   echo !str!>>%tpldir%\3.tpl
   if !totalsessions.%tg%! gtr !maxsess! set /a maxsess=!totalsessions.%tg%!
   if !totalsessions.%tg%! lss !minsess! set /a minsess=!totalsessions.%tg%!
   set /a tg+=1
   if !tg! lss 24 goto STotal

set /a lim=!rmarg!-1
set str=    À
set /a m1=!rmarg! / 4
set /a m2=!rmarg! / 2
set /a m3=!m1! + !m2!
for /l %%a in (1,1,!lim!) do if %%a==!m1! (
                               set str=!str!Â
                             ) else if %%a==!m2! (
                               set str=!str!Â
                             ) else if %%a==!m3! (
                               set str=!str!Â
                             ) else set str=!str!Ä
set str=!str!ÂÄÄÄÄÄÄÄ
echo !str!>>%tpldir%\3.tpl

set str=    0
for /l %%a in (1,1,!lim!) do set str=!str!ÿ
set str=!str!!rmarg! sess.
echo !str!>>%tpldir%\3.tpl
echo.>>%tpldir%\3.tpl
echo         Minimum sessions per hour: !minsess!.>>%tpldir%\3.tpl
echo         Maximum sessions per hour: !maxsess!.>>%tpldir%\3.tpl
echo.>>%tpldir%\3.tpl
echo.>>%tpldir%\3.tpl

exit/b


:BuildNodeSessionsGraph
echo %time:~0,-3% Building Nodes Sessions Graph.

echo RealName: Evil Robot>%tpldir%\2.tpl
echo.>>%tpldir%\2.tpl
rem echo   From !startperiod! til !endperiod!>>%tpldir%\2.tpl
echo ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿>>%tpldir%\2.tpl
echo   Node address        000102030405060708091011121314151617181920212223³>>%tpldir%\2.tpl
echo ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄÂÄ´>>%tpldir%\2.tpl

set /a ad=0
set /a maxsess=0
set /a minsess=3600
:SGraph
 if !ad!==!add.0! goto EGraph
 set /a ad+=1
 set address=!add.%ad%!
 set addrstr=!add.%ad%.o:~0,-8!                   ³
 set addrstr=!addrstr:~0,22!
 set /a tg=0
 :S24
   if !tg! lss 10 (
    if defined graph.!address!.0!tg! (
      set ch=!graph.%address%.0%tg%!
    ) else (
      set ch=0
    )
   ) else (
    if defined graph.!address!.!tg! (
      set ch=!graph.%address%.%tg%!
    ) else (
      set ch=0
    )
   )

   if not defined totalsessions.!tg! (
      set /a totalsessions.!tg!=!ch!
   ) else (
      set /a totalsessions.!tg!+=!ch!
   )

   if !ch! gtr 4 set ch=4
   set addrstr=!addrstr!³!char.%ch%!
   set /a tg+=1
   if !tg! lss 24 goto S24

 echo !addrstr!³>>%tpldir%\2.tpl
 goto SGraph
:EGraph

 echo.>>%tpldir%\2.tpl
 echo " " - 0 sessions, ° - 1 session, ± - 2 sessions,>>%tpldir%\2.tpl
 echo ² - 3 sessions, Û - 4 or more sessions.>>%tpldir%\2.tpl
 echo.>>%tpldir%\2.tpl
 echo.>>%tpldir%\2.tpl

exit /b

:BuildNodeStatTable
echo %time:~0,-3% Building Nodes Stat Table.

echo RealName: Evil Robot>%tpldir%\1.tpl
echo.>>%tpldir%\1.tpl
rem echo      From !startperiod! til !endperiod!>>%tpldir%\1.tpl
echo ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿>>%tpldir%\1.tpl
echo ³    Node address       ³   Sent bytes    ³ Received bytes  ³   In   Out  Ses.³>>%tpldir%\1.tpl
echo ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´>>%tpldir%\1.tpl

set /a ad=0
set /a totalR=0
set /a totalS=0
set /a totalSes=0
:Start

 if !ad!==!add.0! goto End
 set /a ad+=1
 set address=!add.%ad%!
 set num=  !ad!
 set addrstr=!add.%ad%.o:~0,-8!                   ³
 set RStr=                        !traff.%address%.r!
 set SStr=                        !traff.%address%.s!
 set SesStr=          !sess.%address%!
 set SesStrFrom=          !sess.%address%.from!
 set SesStrTo=          !sess.%address%.to!
 set /a totalR+=!traff.%address%.r!
 set /a totalS+=!traff.%address%.s!
 set /a totalSes+=!sess.%address%!
 
 echo ³!num:~-2!³!addrstr:~0,20!³!SStr:~-17!³!RStr:~-17!³!SesStrFrom:~-5! !SesStrTo:~-5! !SesStr:~-5!³>>%tpldir%\1.tpl
 goto Start

:End
 set RStr=                        !totalR!
 set SStr=                        !totalS!
 set SesStr=                        !totalSes!
 echo ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´>>%tpldir%\1.tpl
 echo ³                Total: ³!SStr:~-17!³!RStr:~-17!³!SesStr:~-17!³>>%tpldir%\1.tpl
 echo.>>%tpldir%\1.tpl
 echo.>>%tpldir%\1.tpl

exit /b

:ReadLog
echo %time:~0,-3% Reading binkd log.

set /a ad=0
rem                  a b c d e f g h i j  k  l  m 
FOR /F "eol=; tokens=1,2,3,4,5,6,7,8,9,10,11,12,* delims=(, " %%a in (%binkdlog%) do if /i _%%f_==_done_ (
    set address=%%h&set address=!address:~0,-8!&set address=!address:/=.!&set address=!address::=.!
    set sr=%%l&set sr=!sr:/= !
    if /i not %%h==failed (
     if not defined traff.!address!.s set /a ad+=1&set add.!ad!=!address!&set add.!ad!.o=%%h&set /a add.0=!ad!&set /a sess.!address!.from=0&set /a sess.!address!.to=0
     Call :CountTraff !sr! %%g %%d
    )
    if not defined startperiod set startperiod=%%b %%c %%d
    set endperiod=%%b %%c %%d
   )

exit /b

:CountTraff

  if not defined traff.!address!.s (
     set /a traff.!address!.s=%~1
     ) else set /a traff.!address!.s+=%~1
  if not defined traff.!address!.r (
     set /a traff.!address!.r=%~2
     ) else set /a traff.!address!.r+=%~2
  if not defined sess.!address! (
     set /a sess.!address!=1
     ) else set /a sess.!address!+=1
  if not defined sess.!address!.%~3 (
     set /a sess.!address!.%~3=1
     ) else set /a sess.!address!.%~3+=1

  set _tg=%~4
  set _tg=!_tg:~0,2!
  if not defined graph.!address!.!_tg! (
     set /a graph.!address!.!_tg!=1
     ) else set /a graph.!address!.!_tg!+=1

rem  if not defined totalTraff.!_tg! (
rem     set /a totalTraff.!_tg!=%~1+%~2
rem     ) else set /a totalTraff.!_tg!+=%~1+%~2
rem  if !totaltraff.max! lss !totalTraff.%_tg%! set totaltraff.max=!totalTraff.%_tg%!

  if not defined traff.total.!_tg!.s (
     set /a traff.total.!_tg!.s=%~1
     ) else set /a traff.total.!_tg!.s+=%~1
  if not defined traff.total.!_tg!.r (
     set /a traff.total.!_tg!.r=%~2
     ) else set /a traff.total.!_tg!.r+=%~2
  if !traff.total.max.s! lss !traff.total.%_tg%.s! set traff.total.max.s=!traff.total.%_tg%.s!
  if !traff.total.max.r! lss !traff.total.%_tg%.r! set traff.total.max.r=!traff.total.%_tg%.r!

exit /b

:initchars
rem  echo  °²±ÛÜ³º

rem  set char.1=³
rem   set char.2=º
  set char.0= 
  set char.1=°
  set char.2=±
  set char.3=²
  set char.4=Û

  set bchar.0=±
  set bchar.1=²

  set /a rmarg=64
  set /a totaltraff.max=0
  set /a traff.total.max.s=0
  set /a traff.total.max.r=0

  set subj.0=1
  set subj.1=Nodes Stat. Table
  set subj.2=Nodes Sessions Graph.
  set subj.3=Total Sessions Graph.
  set subj.4=In-Out Bytes Graph.


exit /b

rem ---------------------------------------------------------------------------
rem ---------------------------------------------------------------------------

:SortStem
  set stem=%~1
  set /a ss=0
  set tmpval=Î
  :StartSort

     if !ss!==!%stem%.0! goto EndSort
     set /a ss+=1
     set /a st=!ss!+1
     if !st! GEQ !%stem%.0! goto EndSort
     if /I !%stem%.%ss%! GTR !%stem%.%st%! (
        set tmpval=!%stem%.%ss%!
        set !stem!.!ss!=!%stem%.%st%!
        set !stem!.!st!=!tmpval!
     )
     goto StartSort
  :EndSort
   if not !tmpval!==Î Call :SortStem !stem!
exit /b
