c   reform.f  converts a .sco file into a .csv file suitable for
c     uploading into Sakai
c   requires a *.sco file from grad and a roster file downloaded from
c     REGIS  (format of fall 2009)
c   By Jerry Sellwood, Nov 2009

      implicit none
c
c external function giving length excluding trailing blanks
      integer lnblnk
c
c local variables
c
      character name*23, sect*3, score*6, line*50, root*20
      character comma*1, text*100, rid*9, sid*9, uname*50
      integer i, j, l, k, n
      real x
c
      comma = ','
 1    print *, 'Enter root name for files (e.g. 109f09mt1)'
      read '( a )', root
      k = lnblnk( root )
c open .sco file and skip header
      open( 1, file = root( 1:k )//'.sco', form = 'formatted',
     +      status = 'old', iostat = i )
      if( i .ne. 0 )then
         print '( a, ''.sco file not found'' )', root( 1:k )
         go to 1
      end if
      read( 1, * )
c open registrar's file and skip headers
      print *, 'Enter full name for roster file from REGIS'
      read '( a )', name
      j = lnblnk( name )
      open( 3, file = name( 1:j ), form = 'formatted',
     +      status = 'old', iostat = i )
      if( i .ne. 0 )then
         print '( a, ''registrar file not found'' )', name( 1:j )
         stop
      end if
      do i = 1, 4
         read( 3, * )
      end do
      read( 3, '( a )' )text
      rid = text( 10:18 )
c open output file
      open( 2, file = root( 1:k )//'.csv', form = 'formatted',
     +      status = 'new', iostat = i )
      if( i .ne. 0 )then
         print *, '.csv file exists, do you want to overwrite it?'
         read '( a1 )', line( 1:1 )
         if( line( 1:1 ) .ne. 'y' )stop
         open( 2, file = root( 1:k )//'.csv', form = 'formatted',
     +         status = 'old' )
      end if
c create header line
      line = 'username,id,score'
      i = lnblnk( line )
      write( 2, '( a )' )line( 1:i )
c
c copy students
      n = 0
 2    read( 1, '( a )', end = 3 )line
      name = line( 2:24 )
      i = lnblnk( name )
      sid = line( 25:33 )
      if( sid( 1:3 ) .eq. '   ' )sid( 1:3 ) = '000'
      if( sid( 1:2 ) .eq. '  ' )sid( 1:2 ) = '00'
      if( sid( 1:1 ) .eq. ' ' )sid( 1:1 ) = '0'
c match ID #
      do while ( rid .ne. sid )
         read( 3, '( a )', iostat = j )text
         if( j .ne. 0 )then

            print *, rid, ' ', sid

            print *, 'student ', name( 1:i ),
     +               ' not found in registrars file'
            rewind 3
            do i = 1, 4
               read( 3, * )
            end do
            read( 3, '( a )' )text
            rid = text( 10:18 )
            go to 2
         end if
         rid = text( 10:18 )
      end do
      n = n + 1
c extract username, which is Sakai's keyword
      l = lnblnk( text )
      i = 0
      do j = 1, 9
         i = i + index( text( i+1:l ), comma )
      end do
      j = i + index( text( i+1:l ), '@' )
      uname = text( i+2:j-1 )
      i = lnblnk( uname )
c get score and record
      read( line( 37:42 ), * )x
      if( x .gt. 99.99 )then
         k = 6
         write( score( 1:k ), '( f6.2 )' )x
      else if( x .gt. 9.99 )then
         k = 5
         write( score( 1:k ), '( f5.2 )' )x
      else
         k = 4
         write( score( 1:k ), '( f4.2 )' )x
      end if
      write( 2, '( a, a1, a9, a1, a )' )uname( 1:i ), comma, rid, comma,
     +       score( 1:k )
      go to 2
 3    print *, n, ' students copied'
      end
