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

#include "ippToPsps.h"
#include "ippToPspsVersionDefinitions.h"

#ifndef IPPTOPSPS_VERSION
#error "IPPTOPSPS_VERSION is not set"
#endif
#ifndef IPPTOPSPS_BRANCH
#error "IPPTOPSPS_BRANCH is not set"
#endif
#ifndef IPPTOPSPS_SOURCE
#error "IPPTOPSPS_SOURCE is not set"
#endif

psString ippToPsps_Version(void)
{
    char *value = NULL;
    psStringAppend(&value, "%s@%s", IPPTOPSPS_BRANCH, IPPTOPSPS_VERSION);
    return value;
}

psString ippToPsps_Source(void)
{
  return psStringCopy(IPPTOPSPS_SOURCE);
}

psString ippToPsps_VersionLong(void)
{
    psString version = ippToPsps_Version();  // Version, to return
    psString source = ippToPsps_Source();    // Source

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

    psString version = ippToPsps_Version(); // Software version
    psString source = ippToPsps_Source();   // Software source

    psMetadataAddStr(header, PS_LIST_TAIL, "IPP2PS_V", PS_META_REPLACE, NULL, IPPTOPSPS_VERSION);
    
    psStringPrepend(&version, "ippToPsps version: ");
    psStringPrepend(&source, "ippToPsps 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;
}

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

    psLibVersionHeader(header);
    psModulesVersionHeader(header);
    ippToPsps_VersionHeader(header);

    return true;
}

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

    psString pslib = psLibVersionLong();// psLib version
    psString psmodules = psModulesVersionLong(); // psModules version
    psString ippToPsps = ippToPsps_VersionLong(); // ippToPsps version

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

    psFree(pslib);
    psFree(psmodules);
    psFree(ippToPsps);

    return;
}
