//*****************************************************************************
// Copyright 1986, 2016 NVIDIA Corporation. All rights reserved.
//*****************************************************************************

// This file is a placeholder for a real authentication key. It is used to
// allow easy integration of the authentication code in the examples. If
// your variant of the neuray library requires an authentication key you need
// to replace this file with a file that contains a valid authentication key.
//
// Alternatively, you can keep this file and put the key into a file named
// "examples.lic" (two lines, first line contains the vendor key, second line
// contains the secret key).

#ifndef MI_NEURAYLIB_AUTHENTICATION_H
#define MI_NEURAYLIB_AUTHENTICATION_H

#include <fstream>
#include <string>

#include <mi/neuraylib.h>

void fix_line_ending( std::string& s)
{
    size_t length = s.length();
    if( length > 0 && s[length-1] == '\r')
       s.erase( length-1, 1);
}

inline mi::Sint32 authenticate( mi::neuraylib::INeuray* neuray)
{
    std::ifstream file( "examples.lic");
    if( !file.is_open())
        return 0;

    std::string vendor_key;
    std::string secret_key;
    getline( file, vendor_key);
    getline( file, secret_key);
    fix_line_ending( vendor_key);
    fix_line_ending( secret_key);

    return mi::neuraylib::ILibrary_authenticator::authenticate(
        neuray,
        vendor_key.c_str(),
        vendor_key.size(),
        secret_key.c_str(),
        secret_key.size());
}

#endif // MI_NEURAYLIB_AUTHENTICATION_H