#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <string.h>
#include <pslib.h>
#include "pmVersion.h"

psString psModulesVersion(void)
{
#ifndef PSMODULES_VERSION
#error "PSMODULES_VERSION is not set"
#endif
#ifndef PSMODULES_BRANCH
#error "PSMODULES_BRANCH is not set"
#endif
    return psStringCopy(PSMODULES_BRANCH "@" PSMODULES_VERSION);
}

psString psModulesSource(void)
{
#ifndef PSMODULES_SOURCE
#error "PSMODULES_SOURCE is not set"
#endif
    return psStringCopy(PSMODULES_SOURCE);
}


psString psModulesVersionLong(void)
{
    psString version = psModulesVersion();  // Version, to return
    psString source = psModulesSource();    // Source

    psStringPrepend(&version, "psModules ");
    psStringAppend(&version, " from %s, built %s, %s", source, __DATE__, __TIME__);
    psFree(source);

#ifdef __OPTIMIZE__
    psStringAppend(&version, " optimised");
#else
    psStringAppend(&version, " unoptimised");
#endif

    return version;
};


bool psModulesVersionHeader(psMetadata *header)
{
    PS_ASSERT_METADATA_NON_NULL(header, false);

    psString version = psModulesVersion(); // Software version
    psString source = psModulesSource();   // Software source

    psMetadataAddStr(header, PS_LIST_TAIL, "IPP.PSMODULES.VERSION", PS_META_REPLACE,
                     "psModules software version", version);
    psMetadataAddStr(header, PS_LIST_TAIL, "IPP.PSMODULES.SOURCE", PS_META_REPLACE,
                     "psModules software source", source);

    psFree(version);
    psFree(source);

    return true;
}
