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

#include "psvideophot.h"
#include "psvideophotVersionDefinitions.h"

#ifndef PSVIDEOPHOT_VERSION
#error "PSVIDEOPHOT_VERSION is not set"
#endif
#ifndef PSVIDEOPHOT_BRANCH
#error "PSVIDEOPHOT_BRANCH is not set"
#endif
#ifndef PSVIDEOPHOT_SOURCE
#error "PSVIDEOPHOT_SOURCE is not set"
#endif

psString psvideophotVersion(void)
{
    char *value = NULL;
    psStringAppend(&value, "%s@%s", PSVIDEOPHOT_BRANCH, PSVIDEOPHOT_VERSION);
    return value;
}

psString psvideophotSource(void)
{
    return psStringCopy(PSVIDEOPHOT_SOURCE);
}

psString psvideophotVersionLong(void)
{
    psString version = psvideophotVersion();  // Version, to return
    psString source = psvideophotSource(); // Source

    psStringPrepend(&version, "psvideophot ");
    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 psvideophotVersionHeader(psMetadata *header)
{
    PS_ASSERT_METADATA_NON_NULL(header, false);

    psTime *time = psTimeGetNow(PS_TIME_TAI); // The time now
    psString timeString = psTimeToISO(time); // The time in an ISO string
    psFree(time);
    psString history = NULL;               // History string
    psStringAppend(&history, "psvideophot at %s", timeString);
    psFree(timeString);
    psMetadataAddStr(header, PS_LIST_TAIL, "HISTORY", PS_META_DUPLICATE_OK, NULL, history);
    psFree(history);

    psLibVersionHeader(header);
    psModulesVersionHeader(header);
    psphotVersionHeader(header);
    psastroVersionHeader(header);
    ppStatsVersionHeader(header);

    psString version = psvideophotVersion(); // psvideophot software version
    psString source  = psvideophotSource();  // psvideophot software source

    psMetadataAddStr(header, PS_LIST_TAIL, "IMAGE_V", PS_META_REPLACE, NULL, PSVIDEOPHOT_VERSION);
    
    psStringPrepend(&version, "psvideophot version: ");
    psStringPrepend(&source, "psvideophot source: ");

    psMetadataAddStr(header, PS_LIST_TAIL, "HISTORY", PS_META_DUPLICATE_OK, NULL, version);
    psMetadataAddStr(header, PS_LIST_TAIL, "HISTORY", PS_META_DUPLICATE_OK, NULL, source);

    psFree(version);
    psFree(source);

    return true;
}


void psvideophotVersionPrint(void)
{
    psTime *time = psTimeGetNow(PS_TIME_TAI); // The time now
    psString timeString = psTimeToISO(time); // The time in an ISO string
    psFree(time);
    psLogMsg("psvideophot", PS_LOG_INFO, "psvideophot at %s", timeString);
    psFree(timeString);

    psString pslib = psLibVersionLong();// psLib version
    psString psmodules = psModulesVersionLong(); // psModules version
    psString psphot = psphotVersionLong(); // psphot version
    psString psastro = psastroVersionLong(); // psastro version
    psString ppStats = ppStatsVersionLong(); // ppStats version
    psString psvideophot = psvideophotVersionLong(); // psvideophot version

    psLogMsg("psvideophot", PS_LOG_INFO, "%s", pslib);
    psLogMsg("psvideophot", PS_LOG_INFO, "%s", psmodules);
    psLogMsg("psvideophot", PS_LOG_INFO, "%s", psphot);
    psLogMsg("psvideophot", PS_LOG_INFO, "%s", psastro);
    psLogMsg("psvideophot", PS_LOG_INFO, "%s", ppStats);
    psLogMsg("psvideophot", PS_LOG_INFO, "%s", psvideophot);

    psFree(pslib);
    psFree(psmodules);
    psFree(psphot);
    psFree(psastro);
    psFree(ppStats);
    psFree(psvideophot);

    return;
}
