//============================================================================
// Name        : execTest.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <string>
#include <cstring>
#include <unistd.h>
#include <limits.h>

using namespace std;

int main(int argc, char* argv[])
{
	if (argc != 2)
	{
		cout << "usage: " << argv[0] << " path" << endl;
		return 0;
	}
	const char* strPath = getenv("LD_LIBRARY_PATH");
	if (strPath == NULL)
	{
		char buffer[PATH_MAX];
		snprintf(buffer, PATH_MAX, "LD_LIBRARY_PATH=%s", argv[1]);


		char *env[] = {buffer, NULL};
		char result[ PATH_MAX ];
		if (readlink( "/proc/self/exe", result, PATH_MAX )> 0)
		{
			execvpe(result, argv, env);
			cout << "I will never reach here." << endl;
		}
		else
		{
			cout << "readlink returns error " << endl;
			return 0;
		}
	}
	else
	{
		if (strcmp(strPath, argv[1]) != 0)
		{
			cout << "LD_LIBRARY_PATH must be set as: " << argv[1] << endl;
			return 0;
		}
		else
		{
			cout << "successfully setup LD_LIBRARY_PATH as: " << strPath << endl;
			return 0;
		}
	}


	return 0;
}
