%EXTERNALROUTINE ftan %ALIAS "c#ftan"(%STRING (255) s)

   %ROUTINE define(%STRING (255) s)
      %EXTERNALROUTINESPEC emas3(%STRINGNAME command,params, %INTEGERNAME flag)
      %INTEGER flag
      emas3("DEFINE",s,flag)
   %END;                                 ! Of %ROUTINE define.

%EXTERNALROUTINESPEC prompt %ALIAS "S#PROMPT"(%STRING (255) s)
   %STRING (255) t,u,v,s2,t2,v2
   %INTEGER i,j,k,l
   %OWNINTEGER known= 0, days, state, total

   %RECORDFORMAT nf(%STRING (21) name, %INTEGERARRAY fig(1:27))

   %RECORD (nf) %NAME n, nt
   %OWNRECORD (nf) %ARRAY na(0:200)
   %integerarray order(0:200)

   %integerfn stoi(%string (255) s)
      %integer i,j,n, sum
      sum = 0
      %if length(s) = 0 %then %result = 0
      j=0
      %cycle i = 1, 1, length(s)
         n = charno(s, i)-'0'
          %if n = 32-'0' %then %continue
         %if n < 0 %or n > 9 %start
            %result = sum
         %finish
         sum = sum*10+n
      %repeat
      %result = sum
   %end

   %routine read line(%string (*) %name s)
      %integer i,j
      s = ""
      %cycle
         readsymbol(i); %return %if i = nl
         s = s.tostring(i)
      %repeat
   %end

   %INTEGERFN get fig(%INTEGER n)
      %STRING (23) s
      %INTEGER i,j
      s = ""
      %CYCLE i = 1,1,n
         readsymbol(j); s = s.tostring(j)
      %REPEAT
      %RESULT = stoi(s)
   %END

   %ROUTINE skip past nl
      %INTEGER i,j
      %CYCLE
         readsymbol(i)
         %IF i=nl %start
            %if nextsymbol = nl %then %continue
            %return
         %finish
      %REPEAT
   %END

   %STRING (21) %FN get name(%INTEGER n)
      %INTEGER i,j,k
      %STRING (21) s
      s = ""
      k = 0
      %CYCLE k = 1,1,n
         readsymbol(i)
         %RESULT = "err" %IF i=nl %or i='^'
         %UNLESS i=' ' %THEN s = s.tostring(i)
      %REPEAT
      %RESULT = s
   %END

   %routine sort(%integer n)
      %integer i,j,k,hit,z

      order(i) = i %for i = 1, 1, 200
      %cycle i = known-1, -1, 2
         hit = 0
         %cycle j = 1, 1, i-1
            %if na(order(j))_fig(n) < na(order(j+1))_fig(n) %start
               hit = 1
               z = order(j); order(j) = order(j+1); order(j+1) = z
            %finish
         %repeat
         %exit %if hit = 0
      %repeat
   %end

   %INTEGERFN get spaces(%INTEGER n)
      %INTEGER i,j,k
      %CYCLE i = 1,1,n
         readsymbol(k)
         %IF k#' ' %THENRESULT = 0
      %REPEAT
      %RESULT = 1
   %END

   %on 9 %start
     select input(0)
     close stream(1)
     -> eof
   %finish

   prompt("Number of days covered?")
   read(days)

   known = 0

   %if s -> s.("/").t %start
      define("2,".t)
      select output(2)
   %finish

   s = s.","
eof:
   %CYCLE
      %UNLESS s->t.(",").s %THENEXIT
      define("1,".t)
      select input(1)
      state = 0

      %CYCLE
         %if nextsymbol = '^' %start
            read line(v)
            %if v -> s2.("P st").t2 %then state = 9
            %if v -> s2.("Q st").t2 %then state = 18
            %cycle
               %exit %if nextsymbol # nl
               skipsymbol
            %repeat
            %continue
         %finish
         %IF get spaces(9)=1 %START
            v = get name(12)
            %if v = "err" %or v = "" %then -> skip

            %IF known#0 %START
               %CYCLE j = 1,1,known
                  n == na(j)
                  %IF n_name=v %THEN ->got name already
               %REPEAT
            %FINISH
            known = known+1
            n == na(known)

got name already:

            n_name = v;                  ! redundant if already known

            %CYCLE i = 1,1,9
               k = get fig(10)
               n_fig(i+state) = n_fig(i+state)+k;    ! adds to existing or accumulate
            %REPEAT

         %FINISH
skip:    skip past nl
      %REPEAT

   %REPEAT

! read all files, now print results

   total = known+1; known = known+1
   na(total)_name = "Totals"
   nt == na(total)

   sort(8);                   ! sort on total kb transferred
   
   %cycle i = 1, 1, known
      n == na(order(i))
      printstring(n_name); spaces(14-length(n_name))
      %cycle j = 2, 3, 26
            print(n_fig(j)/days, 5,1)
      %repeat
      newline
      %if i # known %start
         %cycle j = 1, 1, 27
            nt_fig(j) = nt_fig(j)+n_fig(j)
         %repeat
      %finish
   %repeat

   newpage
   printstring("Total Megabytes per day - FTP ")
   print((nt_fig(2)+nt_fig(5)+nt_fig(8))/(days*1000), 4, 1)
   printstring("
Total Megabytes per day - MAIL")
   print(nt_fig(2)/(days*1000), 4, 1)
   printstring("


   Host      Mb/day out              Mb/day in            TYPE
===============================================================
")
  %cycle i = 1, 1, known-1
     n == na(order(i))
     %if n_fig(8) # 0 %start
        printstring(n_name); spaces(15-length(n_name))
        print(n_fig(17)/(days*1000), 3, 3); spaces(15)
        print(n_fig(26)/(days*1000), 3, 3); spaces(15)
        printstring("FTP
")
      %finish
   %repeat
   newlines(3)
   sort(2)
   %cycle i = 1, 1, known-1
     n == na(order(i))
     %if n_fig(2) > days %start
        printstring(n_name); spaces(15-length(n_name))
        print(n_fig(11)/(days*1000), 3, 3); spaces(20)
        print(n_fig(20)/(days*1000), 3, 3); spaces(20)
        printstring("MAIL
")
     %finish
   %repeat
    
%END
%ENDOFFILE