//
// LDASTools frameCPP - A library implementing the LIGO/Virgo frame
// specification
//
// Copyright (C) 2025 California Institute of Technology
//
// LDASTools frameCPP is free software; you may redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 (GPLv2) of the
// License or at your discretion, any later version.
//
// LDASTools frameCPP is distributed in the hope that it will be useful, but
// without any warranty or even the implied warranty of merchantability
// or fitness for a particular purpose. See the GNU General Public
// License (GPLv2) for more details.
//
// Neither the names of the California Institute of Technology (Caltech),
// The Massachusetts Institute of Technology (M.I.T), The Laser
// Interferometer Gravitational-Wave Observatory (LIGO), nor the names
// of its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// You should have received a copy of the licensing terms for this
// software included in the file LICENSE located in the top-level
// directory of this package. If you did not, you can view a copy at
// http://dcc.ligo.org/M1500244/LICENSE
//

#include "framecpp/Universal/VersionRegistry.hh"

// Forward declarations of version-specific init functions
namespace FrameCPP
{
    namespace Version_3
    {
        extern bool init_frame_spec();
    }
    namespace Version_4
    {
        extern bool init_frame_spec();
    }
    namespace Version_5
    {
        extern bool init_frame_spec();
    }
    namespace Version_6
    {
        extern bool init_frame_spec();
    }
    namespace Version_7
    {
        extern bool init_frame_spec();
    }
    namespace Version_8
    {
        extern bool init_frame_spec();
    }
    namespace Version_9
    {
        extern bool init_frame_spec();
    }
} // namespace FrameCPP

namespace FrameCPP
{
    namespace Universal
    {
        namespace VersionRegistry
        {
            bool
            init_all_versions()
            {
                static bool initialized = false;

                if( !initialized )
                {
                    // Initialize all frame specification versions
                    // Order matters: initialize from oldest to newest
                    // so that newer versions can reference previous ones
                    Version_3::init_frame_spec();
                    Version_4::init_frame_spec();
                    Version_5::init_frame_spec();
                    Version_6::init_frame_spec();
                    Version_7::init_frame_spec();
                    Version_8::init_frame_spec();
                    Version_9::init_frame_spec();

                    initialized = true;
                }

                return true;
            }

        } // namespace VersionRegistry
    }     // namespace Universal
} // namespace FrameCPP

// Automatic initialization via static initializer
// This ensures all frame specifications are initialized when the library loads
namespace
{
    struct AutoInitializer
    {
        AutoInitializer()
        {
            FrameCPP::Universal::VersionRegistry::init_all_versions();
        }
    };
    static AutoInitializer auto_init;
} // namespace

// Additional initialization hook using __attribute__((constructor))
// This ensures initialization even with static libraries
#if defined( __GNUC__ ) || defined( __clang__ )
static void __attribute__( ( constructor ) ) version_registry_init()
{
    FrameCPP::Universal::VersionRegistry::init_all_versions();
}
#endif
