program ObjDiff

! Compare two 1401 Autocoder object deck files.

! Assume the first is intended to be a contiguous subset of the second.

  character(255) :: FileA, FileB
  integer :: I              ! Argument number
  character(80) :: LineA = '', LineB = ''
  integer :: nA = 0, nB = 0 ! card numbers
  integer :: startB = 0     ! Where to start in file B

  i = 0
  do
    i = i + 1
    call getarg ( i, fileA )
    if ( fileA(1:1) /= '-' ) exit
    read ( fileA(2:), * ) startB
  end do

  call getarg ( i+1, fileb )

  open ( 10, file=trim(filea), status='old', form='formatted' )
  open ( 11, file=trim(fileb), status='old', form='formatted' )

  ! Skip the boot loader cards from file A

  do while ( lineA(40:40) /= 'L' )
    read ( 10, '(a)', end=96 ) lineA
    na = na + 1
  end do

  if ( startb == 0 ) then

    ! Find the first card in file B that is equal to the first card
    ! after the boot cards in file A

    do while ( lineA(1:71) /= lineB(1:71) )
      read ( 11, '(a)', end=97 ) lineB
      nb = nb + 1
    end do

  else

    ! Start comparing from fileB at startB.

    do i = 1, startB
      read ( 11, '(a)', end=97 ) lineB
      nB = nB + 1
    end do

  end if

  ! Compare cards, report differences

  do
    if ( lineA(1:71) /= lineB(1:71) ) write ( *, '(a,i5,": ",a)' ) &
      & "A", na, trim(lineA), "B", nb, trim(lineB)
    read ( 10, '(a)', end=99 ) lineA
    nA = nA + 1
    read ( 11, '(a)', end=98 ) lineB
    nB = nB + 1
  end do

96 write ( *, '(a)' ) "No load cards found in " // trim(fileA)
   stop

97 write ( *, '(a)' ) "No card matching", trim(lineA), "found in " // trim(fileB)
   stop

98 write ( *, '(a)' ) "Ran out of cards on " // trim(fileB) // &
     & " before end of ", trim(fileA)

99 stop

end program ObjDiff
