-- Switch the Tcl dispatcher from the legacy constructor to the current one.
do $$
declare
    legacy_args constant text := 'bug_id,bug_number,package_id,component_id,found_in_version,summary,user_agent,comment_content,comment_formt,creation_date,creation_user,creation_ip,item_subtype;bt_bug,content_type;bt_bug_revision';
    current_args constant text := 'bug_id,bug_number,package_id,component_id,found_in_version,summary,user_agent,comment_content,comment_format,creation_date,creation_user,creation_ip,fix_for_version,item_subtype;bt_bug,content_type;bt_bug_revision';
    registered_args text;
    target record;
    definition text;
    signature text;
begin
    select string_agg(lower(arg_name) || coalesce(';' || arg_default, ''),
                      ',' order by arg_seq)
      into registered_args
      from acs_function_args
     where function = 'BT_BUG__NEW';

    if registered_args is distinct from legacy_args
       and registered_args is distinct from current_args then
        return;
    end if;

    select p.*, n.nspname into target
      from pg_proc p
      join pg_namespace n on n.oid = p.pronamespace
     where p.oid = to_regprocedure(format(
               '%I.bt_bug__new(integer,integer,integer,integer,integer,varchar,varchar,text,varchar,timestamptz,integer,varchar,integer,varchar,varchar)',
               current_schema()))
       and p.prokind = 'f'
       and p.proargmodes is null
       and p.pronargdefaults = 0
       and pg_function_is_visible(p.oid);

    if not found or exists (
        select 1 from pg_proc p
         where p.proname = 'bt_bug__new' and p.pronargs = 15
           and p.oid <> target.oid
    ) then
        raise warning 'BT_BUG__NEW: expected a unique current 15-argument constructor; registration unchanged';
        return;
    end if;

    if target.proargnames is null then
        select string_agg(
                   format('%I %s', 'arg_' || split_part(a.spec, ';', 1),
                          format_type(a.type_oid, null)),
                   ', ' order by a.position)
          into signature
          from unnest(target.proargtypes::oid[], string_to_array(current_args, ','))
               with ordinality as a(type_oid, spec, position);

        definition := pg_get_functiondef(target.oid);
        -- The historical 1.3a8 body has a trailing comma in the revision call.
        definition := regexp_replace(
            definition,
            '(p_creation_ip),([ \t]*-- creation_ip[ \t]*\n[ \t]*\);)',
            '\1\2'
        );
        execute format('CREATE OR REPLACE FUNCTION %I.bt_bug__new(%s)', target.nspname, signature)
                || substr(definition, strpos(definition, chr(10)));
    elsif exists (
        select 1
          from unnest(target.proargnames, string_to_array(current_args, ',')) as a(name, spec)
         where a.name is distinct from split_part(a.spec, ';', 1)
           and right(a.name, length(split_part(a.spec, ';', 1)) + 1)
               is distinct from '_' || split_part(a.spec, ';', 1)
    ) then
        raise warning 'BT_BUG__NEW: current constructor has unexpected parameter names; registration unchanged';
        return;
    end if;

    if registered_args = legacy_args then
        perform define_function_args('bt_bug__new', current_args);
    end if;
end;
$$;