/** @file streaksVersion.c
 *
 *  @brief
 *
 *  @ingroup streaks
 *
 *  Copyright 2009 Institute for Astronomy, University of Hawaii
 */

#include "streaksremove.h"
#include "streaksVersionDefinitions.h"

#ifndef STREAKS_VERSION
#error "STREAKS_VERSION is not set"
#endif
#ifndef STREAKS_BRANCH
#error "STREAKS_BRANCH is not set"
#endif
#ifndef STREAKS_SOURCE
#error "STREAKS_SOURCE is not set"
#endif

psString streaksVersion(void)
{
    char *value = NULL;
    psStringAppend(&value, "%s@%s", STREAKS_BRANCH, STREAKS_VERSION);
    return value;
}

psString streaksSource(void)
{
  return psStringCopy(STREAKS_SOURCE);
}

psString streaksVersionLong(void)
{
    psString version = streaksVersion();  // Version, to return
    psString source = streaksSource();    // Source

    psStringPrepend(&version, "%s ", streaksProgram);
    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 streaksVersionHeader(psMetadata *header)
{
    PS_ASSERT_METADATA_NON_NULL(header, false);

    psString version = streaksVersion(); // Software version
    psString source = streaksSource();   // Software source

    psStringPrepend(&version, "%s version: ", streaksProgram);
    psStringPrepend(&source, "%s source: ", streaksProgram);

    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;
}

bool streaksVersionHeaderFull(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, "%s at %s", streaksProgram, timeString);
    psFree(timeString);
    psMetadataAddStr(header, PS_LIST_TAIL, "HISTORY", PS_META_DUPLICATE_OK, NULL, history);
    psFree(history);

    psLibVersionHeader(header);
    psModulesVersionHeader(header);
    streaksVersionHeader(header);

    return true;
}

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

    psString pslib = psLibVersionLong();// psLib version
    psString psmodules = psModulesVersionLong(); // psModules version
    psString streaks = streaksVersionLong(); // streaks version

    psLogMsg("streaks", PS_LOG_INFO, "%s", pslib);
    psLogMsg("streaks", PS_LOG_INFO, "%s", psmodules);
    psLogMsg("streaks", PS_LOG_INFO, "%s", streaks);

    psFree(pslib);
    psFree(psmodules);
    psFree(streaks);

    return;
}
