#include "psphotInternal.h"

#ifdef HAVE_KAPA
#include <kapa.h>
#endif

psString psphotVersion(void)
{
#ifndef PSPHOT_VERSION
#error "PSPHOT_VERSION is not set"
#endif
#ifndef PSPHOT_BRANCH
#error "PSPHOT_BRANCH is not set"
#endif
    return psStringCopy(PSPHOT_BRANCH "@" PSPHOT_VERSION);
}

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

psString psphotVersionLong(void)
{
    psString version = psLibVersion();  // Version, to return
    psString source = psLibSource();    // Source

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

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

#ifdef HAVE_KAPA
#if 0
    // XXX Need to get ohana and libdvo versions
    psString ohanaVersion = psStringStripCVS(ohana_version(), "Name");
    psString libdvoVersion = psStringStripCVS(libdvo_version(), "Name");
    psStringAppend(&version, " with libkapa (ohana %s, libdvo: %s)", ohanaVersion, libdvoVersion);
    psFree(ohanaVersion);
    psFree(libdvoVersion);
#else
    psStringAppend(&version, " with libkapa");
#endif

#else
    psStringAppend (&version, " without libkapa");
#endif

    return version;
}

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

    psString version = psphotVersion(); // Software version
    psString source = psphotSource();   // Software source

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

    psFree(version);
    psFree(source);

    return true;
}
