program LISTCB

  ! Read a column binary "deck" from Brian Knittel using READCB.  List
  ! it in hex.

  use GetImage_m, only: OpenImages, GetImage, CloseImages
  use ReadCB_m, only: Dump
  use Unpack_m, only: Unpack

  implicit NONE

  character(80) :: BUF, UBUF
  integer :: I, REC1 = 1, COL1 = 1

  character(127) :: FILE
  integer :: FLAG
  logical :: DoUnpack = .false.

  i = 0
  do
    i = i + 1
    call getarg ( i, file )
    if ( file(1:2) == '-c' ) then
      if ( file(3:3) == '' ) then
        i = i + 1
        call getarg ( i, file(3:) )
      end if
      read ( file(3:), * ) col1
    else if ( file(1:3) == '-d ' ) then
      dump = .true.
    else if ( file(1:2) == '-r' ) then
      if ( file(3:3) == '' ) then
        i = i + 1
        call getarg ( i, file(3:) )
      end if
      read ( file(3:), * ) rec1
    else if ( file(1:3) == '-u ' ) then
      doUnpack = .true.
    else if ( file(1:2) == '- ' ) then
      i = i + 1
      call getarg ( i, file )
      exit
    else
      exit
    end if
  end do

  if ( file == '' ) then
    print '(a)', 'Enter file name: '
    read ( *, '(a)' ) file
  end if
  call openImages ( trim(file), rec1=rec1, col1=col1 )

  do
    call getImage ( buf, flag )
    if ( flag == 0 ) exit
    if ( doUnpack ) then
      call unpack ( buf, ubuf )
      buf = ubuf
      flag = len_trim(buf)
    end if
    write ( *, '(a)' ) buf(:flag)
  end do

end program LISTCB
