program Resequence

! Resequence an Autocoder or SPS deck.  This program is a filter.

! The default is to start at 01010 and step by 10.

! Options: -f[ ]# => Specify the first count, default 1010
!          -p[ ]# => Specify the maximum count per page, default 999
!          -s[ ]# => Specify the step, default 10

  character(80) :: Card
  integer :: Count               ! Current count
  integer :: First = 1010        ! First count
  integer :: I
  integer :: P = 999             ! Maximum per page
  integer :: S = 10              ! Step

! Process command-line options

  i = 0
  do
    i = i + 1
    call get_command_argument ( i, card )
    select case ( card(1:2) )
    case ( '  ' )
      exit
    case ( '-f' )
      if ( card(3:3) == '' ) then
        i = i + 1
        call get_command_argument ( i, card(3:) )
      end if
      read ( card(3:), *, err=9 ) first
     case ( '-p' )
      if ( card(3:3) == '' ) then
        i = i + 1
        call get_command_argument ( i, card(3:) )
      end if
      read ( card(3:), *, err=9 ) p
     case ( '-s' )
      if ( card(3:3) == '' ) then
        i = i + 1
        call get_command_argument ( i, card(3:) )
      end if
      read ( card(3:), *, err=9 ) s
    case default
      go to 9
    end select
  end do

! Process the deck

  count = first
  do
    read ( *, '(a)', end=6 ) card
    write ( card(1:5), '(i5.4)' ) count
    count = count + s
    if ( mod(count,1000) > p ) count = 1000 * ( count / 1000 + 1 )
    write ( *, '(a)' ) trim(card)
  end do

6 stop

! Print usage

9 continue
  call get_command_argument ( 0, card )
  print '(a)', 'Usage: ' // trim(card) // ' [options] <input >output'
  print '(a)',    ' Options:'
  print '(a,i0)', '  -f[ ]# => Set first count, default ', first
  print '(a,i0)', '  -p[ ]# => Set maximum count per page, default ', p
  print '(a,i0)', '  -s[ ]# => Set step, default ', s

end program
