!*************************************************************************
! TRKANA.BAS    -       TRACK ANALYSIS for TRACK.LIT output
!
! by Dave Heyliger - AMUS Staff
!
! Usage: .RUN TRKANA    (program will prompt for *.TRK file to process)
!
! TRKANA will process a *.TRK file and provide a cumulative report for
! each unique activity within the track file.
!
! Last Update 10-20-87: fixed "next day air" problem!
!*************************************************************************

MAP1    in'file,S,10,""                         !input file
MAP1    out'file,S,10,""                        !output file
MAP1    in'line,S,82                            !input string from file
MAP1    temp'string,S,10                        !temporary string value
MAP1    temp'string2,S,10                       !another bogus string variable
MAP1    temp'time,F                             !temporary time value
MAP1    activity(100)                           !up to 100 unique activities:
MAP2      action,S,10                           !  action
MAP2      input'time,F                          !  time of input
MAP2      total'time,F                          !  total time spent on activ.
MAP1    last'activity,F                         !pointer to "last activity"
MAP1    line'count,F                            !total lines read in
MAP1    activity'pointer,F                      !pointer to activity array
MAP1    cum'time,F                              !total time tracked
MAP1    am'or'pm,S,2,""                         !10-20-87

! INITIALIZE ARRAY
!
       FOR I = 1 TO 100
         action(I) = ""
       NEXT I
       last'activity = 0
       line'count = 0
       cum'time = 0

! QUERY FOR FILE
!
       ? TAB(-1,0)
       ? TAB(5,20);
       INPUT "Enter track file to be processed: ",in'file
       IF in'file = "" THEN END
       ? TAB(7,20);
       INPUT "Enter output file to be created: ", out'file
       IF out'file = "" THEN END
       OPEN #1,in'file,INPUT
       OPEN #2,out'file,OUTPUT
       INPUT LINE #1,in'line
       INPUT LINE #1,in'line

! PROCESS
!   Read file line by line and process
!
       ? TAB(-1,0)
       ? "Processing " in'file ".";

REPEAT: INPUT LINE #1,in'line
       IF LEN(in'line) < 10 &
          THEN in'line = in'line + SPACES(10 - LEN(in'line))
       IF EOF(1) THEN CALL OUTPUT'ANALYSIS
       CALL SCAN'ARRAY
       CALL ISOLATE'TIME
       IF line'count <> 0 THEN CALL TIME'UPDATE
       last'activity = activity'pointer
       line'count = line'count + 1
       ? ".";
       GOTO REPEAT


! SCAN ARRAY Subroutine
!   On entry: a line from the track file has been read
!   On exit:  activity'pointer has location of unique activity
!
SCAN'ARRAY:
       temp'string = MID$(in'line,20,10)
       FOR I = 1 to 100
         IF action(I) = "" &
            THEN action(I) = temp'string : total'time(I) = 0 : &
                 activity'pointer = I : RETURN &
            ELSE IF action(I) = temp'string &
                    THEN activity'pointer = I : RETURN
       NEXT I


! ISOLATE'TIME Subroutine
!   On entry: line has been read in, activity pointer valid array location
!   On exit:  input'time has been inserted into array in "minutes" format
!
ISOLATE'TIME:
       FOR I = 1 TO 10
         IF MID$(in'line,I,1) <> " " THEN NEXT I
       temp'string = MID$(in'line,I+1,8)
       CALL CONVERT'TO'MINUTES
       input'time(activity'pointer) = temp'time
       RETURN


! CONVERT'TO'MINUTES Subroutine
!   On entry: temp'string holds input time in "xx:xx xM" format
!   On exit:  temp'time holds "minutes" format value of above format
!
CONVERT'TO'MINUTES:
       temp'time = 60 * (VAL(MID$(temp'string,1,2)))
       IF MID$(temp'string,7,2) = "PM" &
          THEN IF MID$(temp'string,1,2) <> "12" &
               THEN temp'time = temp'time + 60*12
       temp'time = temp'time + (VAL(MID$(temp'string,4,2)))
       RETURN


! TIME'UPDATE Subroutine
!   On entry: last'activity holds previous activity, temp'time holds time
!   On exit:  last'activity total time is updated in "minutes" format
!
TIME'UPDATE:
       !10-20-87
       IF am'or'pm = "" THEN am'or'pm = MID$(temp'string,7,2)
       IF am'or'pm = "PM" &
          THEN IF MID$(temp'string,7,2) = "AM" &
              THEN total'time(last'activity) = total'time(last'activity) &
                + (24*60 - input'time(last'activity) + temp'time) : &
                am'or'pm = MID$(temp'string,7,2) :&
                IF MID$(temp'string,1,2) = "12" &
                   THEN total'time(last'activity) = &
                       total'time(last'activity) - 12*60 : RETURN &
                ELSE RETURN
       !10-20-87

BYPASS: total'time(last'activity) = total'time(last'activity) +  &
       (temp'time - input'time(last'activity))
       am'or'pm = MID$(temp'string,7,2)
       RETURN


! OUTPUT'ANALYSIS Subroutine
!   On entry: analysis is complete
!   On exit:  analysis displayed
!
OUTPUT'ANALYSIS:
       ?
       FOR I = 1 TO 100
         IF action(I) = "" THEN GOTO FINAL
         ? #2,CHR(9);
         ? #2,action(I) "  ";
         IF total'time(I) < 10 THEN ? #2, " :0" ;: ? #2,STR(total'time(I)) : GOTO CUM
         IF total'time(I) < 59 &
            THEN ? #2, " :" ;: ? #2,STR(total'time(I)) &
            ELSE CALL MINUTES'TO'HOURS
CUM:      cum'time = cum'time + total'time(I)
       NEXT I

FINAL:  ? #2
       ? #2,"Total number of lines processed: " line'count
       ? #2,"Total number of unique activities: " I
       ? #2,"Cumulative time recorded: ";
       total'time(I) = cum'time
       CALL MINUTES'TO'HOURS
       CLOSE #2
       END


! MINUTES'TO'HOURS Subroutine
!   On entry: total'time(I) >= 60
!   On exit:  total'time converted to string of xx:xx and printed out
!
MINUTES'TO'HOURS:
       temp'string = STR(INT(total'time(I)/60)) + ":"
       temp'string2 = STR(total'time(I) - ((INT(total'time(I)/60)) * 60))
       IF LEN(temp'string2) = 1 &
          THEN temp'string = temp'string + "0" + temp'string2 &
          ELSE temp'string = temp'string + temp'string2
       ? #2,temp'string
       RETURN