program Make_Deck

! Make the first two phases of the Fortran deck by putting the PARAM card
! BEFORE the clear-me overlay at the end of phase 1, and the program deck
! BEFORE the clear-me overlay at the end of phase 2. Phase 2 looks for
! a card with SCANNER in columns 1-7, but it needs to be AFTER the
! branch-to-this overlay card, and BEFORE the setup-for-next overlay
! cards, but Autocoder can't put it there, so we move it here.

! Command line arguments are Phase 1, Param card, Phase 2, Program file

! Output is on stdout

  use ISO_Fortran_Env, only: E => Error_Unit

  implicit NONE

  character(80) :: IOMsg
  integer :: IOStat, N
  character(80) :: Line, Param, Prog
  character(*), parameter :: Look = ',040040B' ! Overlay card

  ! Open Phase 1

  call get_command_argument ( 1, line )
  open ( 10, file=trim(line), status='old', iostat=iostat, iomsg=iomsg )
  if ( iostat /= 0 ) then
    write ( e, '(3a/i0,a/a)' ) 'Unable to open Phase 1 "', trim(line), '"', &
      & 'Status ', iostat, trim(iomsg)
    stop
  end if

  ! Copy phase 1 up to the first overlay card

  do
    read ( 10, '(a)', end=6661 ) line
    write ( *, '(a)' ) trim(line)
    if ( index(line, look) /= 0 ) exit
  end do

  ! Open the parameter card

  call get_command_argument ( 2, param )
  open ( 11, file=trim(param), status='old', iostat=iostat, iomsg=iomsg )
  if ( iostat /= 0 ) then
    write ( e, '(3a/a,i0/a)' ) 'Unable to open Param "', trim(param), '"', &
      & 'Status ', iostat, trim(iomsg)
    stop
  end if

  read ( 11, '(a)', end=6662 ) param
  write ( *, '(a)' ) trim(param)

  ! Copy the rest of phase 1

  do
    read ( 10, '(a)', end=1 ) line
    write ( *, '(a)' ) trim(line)
  end do
1 continue

  close ( 10 )
  close ( 11 )

  ! Open phase 2

  call get_command_argument ( 3, line )
  open ( 10, file=trim(line), status='old', iostat=iostat, iomsg=iomsg )
  if ( iostat /= 0 ) then
    write ( e, '(3a/i0,a/a)' ) 'Unable to open Phase 2 "', trim(line), '"', &
      & 'Status ', iostat, trim(iomsg)
    stop
  end if

  ! Copy phase 2 through the second overlay card

  do n = 1, 2
    do
      read ( 10, '(a)', end=6663 ) line
      write ( *, '(a)' ) trim(line)
      if ( index(line, look) /= 0 ) exit
    end do
  end do

  ! Open the program file

  call get_command_argument ( 4, prog )
  open ( 11, file=trim(prog), status='old', iostat=iostat, iomsg=iomsg )
  if ( iostat /= 0 ) then
    write ( e, '(3a/i0,a/a)' ) 'Unable to open Program "', trim(prog), '"', &
      & 'Status ', iostat, trim(iomsg)
    stop
  end if

  ! Copy the program file

  do
    read ( 11, '(a)', end=2 ) prog
    write ( *, '(a)' ) trim(prog)
  end do
2 continue

  ! Copy the rest of phase 2

  do
    read ( 10, '(a)', end=3 ) line
    write ( *, '(a)' ) trim(line)
  end do

3 continue
  stop

6661 write ( e, '(a)' ) 'Phase 1 ended before the Clear-Me overlay'
  stop 1

6662 write ( e, '(a)' ) 'PARAM card file is empty'
  stop 2

6663 write ( e, '(a)' ) 'Phase 1 ended before the signal before the Clear-Me overlay'
  stop 3

6664 write ( e, '(a)' ) 'Phase 1 ended before the Clear-Me overlay'
  stop 4

end program Make_Deck
