#ifndef COMMON_LIBRARY_DATA_HPP
#define COMMON_LIBRARY_DATA_HPP

/*
 * SPDX-FileCopyrightText: (c) 2009 RWTH Aachen University <rwth-aachen.de>
 * SPDX-FileCopyrightText: (c) 2009 Gesellschaft für numerische Simulation mbH Braunschweig <gns-mbh.com>
 * SPDX-FileCopyrightText: (c) 2009 Technische Universität Dresden <tu-dresden.de>
 * SPDX-FileCopyrightText: (c) 2009 University of Oregon <uoregon.edu>
 * SPDX-FileCopyrightText: (c) 2009 Forschungszentrum Jülich GmbH <fz-juelich.de>
 * SPDX-FileCopyrightText: (c) 2009 German Research School for Simulation Sciences GmbH
 * SPDX-FileCopyrightText: (c) 2009 Technische Universität München <tum.de>
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 */


/**
 * @file
 */

#include <string>
#include <deque>
#include <sstream>
#include <algorithm>

/**
 * Helper struct to represent an installed library as well as its
 * dependencies and required paths to use the library. Intended to be
 * used by generated code (config-external-libs.hpp, see
 * Makefile.rpaths.inc.am) and the packages' config tools. To make
 * usage easier, members are public.
 */
struct LibraryData
{
    /**
     * Empty constructor. Needed to allow copies of STL containers containing this
     * class
     */
    LibraryData( void )
    {
    }

    /**
     * Copy constructor.
     */
    LibraryData( const LibraryData& source )
        :
        m_lib_name( source.m_lib_name ),
        m_build_dir( source.m_build_dir ),
        m_install_dir( source.m_install_dir ),
        m_needs_libs( source.m_needs_libs ),
        m_needs_libdirs( source.m_needs_libdirs ),
        m_dependencies( source.m_dependencies )
    {
    }

    /**
     * Regular constructor for improved library dependency generation.
     * Intended to be called from generated config-external-libs.hpp.
     *
     * @param libName Unique library name with 'lib' prefix, e.g., 'libfoo'.
     * @param buildDir Absolute directory where libfoo has been built.
     * @param installDir Absolute directory where libfoo is installed.
     * @param needsLibs Space-separated libraries needed to use
     *        libfoo, with '-l' prefix, e.g., '-lbar -lfoobar'. Empty
     *        for shared builds. Converted to a container of separate
     *        libraries.
     * @param needsLibdirs Colon-separated absolute paths needed to
     *        use libfoo's @a needsLibs. Empty for shared builds.
     *        Converted to a container of separate paths.
     */
    LibraryData( const std::string& libName,
                 const std::string& buildDir,
                 const std::string& installDir,
                 const std::string& needsLibs,
                 const std::string& needsLibdirs )
        :
        m_lib_name( libName ),
        m_build_dir( buildDir ),
        m_install_dir( installDir ),
        m_needs_libs( gen_deque( needsLibs, '\0', "" ) ),
        m_needs_libdirs( gen_deque( needsLibdirs, ':', "" ) ),
        m_dependencies()
    {
    }

    /**
     * Destructor.
     */
    virtual
    ~LibraryData()
    {
    }

    /**
     * Member.
     */
    std::string             m_lib_name;
    std::string             m_build_dir;
    std::string             m_install_dir;
    std::deque<std::string> m_needs_libs;
    std::deque<std::string> m_needs_libdirs;
    /// Container of other m_lib_name that might be populated by
    /// packages' config tools. Initially empty.
    std::deque<std::string> m_dependencies;

private:
    // Helper method to populate container<string> members from
    // strings provided to the ctor from config-external-libs.hpp.
    static std::deque<std::string>
    gen_deque( std::string             input,
               std::string::value_type replace,
               std::string             prefix )
    {
        if ( replace != '\0' )
        {
            std::replace( input.begin(), input.end(), replace, ' ' );
        }
        std::stringstream       stream( input );
        std::deque<std::string> output;
        std::string             item;
        while ( stream >> item )
        {
            output.push_back( prefix + item );
        }
        return output;
    }
};

#endif /* COMMON_LIBRARY_DATA_HPP */
