/* app/builder/KMBuild.h
 *
 * Copyright (C) 2024 OpenKylin, Inc
 */

#ifndef __KMBUILD_H__
#define __KMBUILD_H__

#include "app/KMSubCommand.h"
#include "common/KMMetaData.h"

/**
 * @brief 	 组装沙箱运行环境及命令行参数信息，并在沙箱中运行命令
 * @note     供kaiming-builder构建流程中调用
 */
class KMBuild : public KMSubCommand
{
    DECLARE_DYNCREATE(KMBuild);

public:
    KMBuild();
    virtual ~KMBuild();

    int despose(int argc, char **argv) override;

private:
    void init(int argc, char **argv);
    int run();

    void setOptionEnvContext();
    void parseEnvFd();
    void parseEnvBlock(const char *data, std::size_t length);

    void mountOverlays();
    void setupAppInfo();
    void setProcess();
    void setupOverlayExtra();

    // TODO: 下面方法废弃，后面稳定后删除
    void computePermissions(const std::shared_ptr<KMMetaData> &appMetadata, const std::shared_ptr<KMMetaData> &runtimeMetadata);
    void parseOptionsToContext();
    void setOptionSharesContext();
    void setOptionSocketsContext();
    void setOptionDevicesContext();
    void setOptionFeaturesContext();
    void setOptionFilesystemContext();
    void setOptionOwnNameContext();
    bool verifyDbusName(const std::string &dbusName);
    void setOptionTalkNameContext();
    void setOptionSystemDbusContext();
    void setOptionGenericPolicyContext();
    void verifyPolicy(const std::string &policy, std::string &key, std::string &value);
    void setOptionPersistContext();
    void setRunFlags();

private:
    class Options;
    class Private;
    std::unique_ptr<Private> d;
};

#endif // !__KMBUILD_H__
