How to duplicate automation when duplicating pattern content

hi, so, i’ve got this nice little @joule pattern doubler in use for eons now, or maybe just a decade or something. it’s really to die for.
but i just realized that the doubler does not also double automation, which is a shame, as the result is:
Näyttökuva 2022-6-19 kello 23.47.02

what would the process of modifying this function to both double the pattern data and the automation data be?

function joulepatterndoubler()
 local s=renoise.song()
 local old_patternlength = s.selected_pattern.number_of_lines
 local resultlength = nil

 resultlength = old_patternlength*2

if not (resultlength > 512) then
  s.selected_pattern.number_of_lines = resultlength

  for track_index, patterntrack in ipairs(s.selected_pattern.tracks) do
    if not patterntrack.is_empty then
      for line_index, line in ipairs(patterntrack.lines) do
        if line_index <= old_patternlength then
          if not line.is_empty then
            patterntrack:line(line_index+old_patternlength):copy_from(line)
          else
            patterntrack:line(line_index+old_patternlength):clear()
          end
        end
      end
    end
  end
else
  return
end

--Modification, cursor is placed to "start of "clone""
--renoise.song().selected_line_index = old_patternlength+1
 s.selected_line_index = old_patternlength+s.selected_line_index
-- s.transport.edit_step=0
end