/** @file ppArithVersion.c
 *
 *  @brief
 *
 *  @ingroup ppArith
 *
 *  @author IfA
 *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
 *  @date $Date: 2009-02-06 19:45:30 $
 *  Copyright 2009 Institute for Astronomy, University of Hawaii
 */

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

#include <stdio.h>
#include <pslib.h>
#include <psmodules.h>
#include <ppStats.h>

#include "ppArith.h"

psString ppArithVersion(void)
{
#ifndef PPARITH_VERSION
#error "PPARITH_VERSION is not set"
#endif
#ifndef PPARITH_BRANCH
#error "PPARITH_BRANCH is not set"
#endif
    return psStringCopy(PPARITH_BRANCH "@" PPARITH_VERSION);
}

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

psString ppArithVersionLong(void)
{
    psString version = ppArithVersion();  // Version, to return
    psString source = ppArithSource();    // Source

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

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

    return version;
};

void ppArithVersionMetadata(psMetadata *metadata)
{
    PS_ASSERT_METADATA_NON_NULL(metadata,);

    psString pslib = psLibVersionLong();// psLib version
    psString psmodules = psModulesVersionLong(); // psModules version
    psString ppStats = ppStatsVersionLong(); // ppStats version
    psString ppArith = ppArithVersionLong(); // ppArith version

    psTime *time = psTimeGetNow(PS_TIME_TAI); // The time now
    psString timeString = psTimeToISO(time); // The time in an ISO string
    psFree(time);
    psString head = NULL;               // Head string
    psStringAppend(&head, "ppArith processing at %s. Component information:", timeString);
    psFree(timeString);

    psMetadataAddStr(metadata, PS_LIST_TAIL, "HISTORY", PS_META_DUPLICATE_OK, head, "");
    psMetadataAddStr(metadata, PS_LIST_TAIL, "HISTORY", PS_META_DUPLICATE_OK, pslib, "");
    psMetadataAddStr(metadata, PS_LIST_TAIL, "HISTORY", PS_META_DUPLICATE_OK, psmodules, "");
    psMetadataAddStr(metadata, PS_LIST_TAIL, "HISTORY", PS_META_DUPLICATE_OK, ppStats, "");
    psMetadataAddStr(metadata, PS_LIST_TAIL, "HISTORY", PS_META_DUPLICATE_OK, ppArith, "");

    psFree(head);
    psFree(pslib);
    psFree(psmodules);
    psFree(ppStats);
    psFree(ppArith);

    return;
}
