#include <iostream>
#include <string>
#include <sstream>
#include <unistd.h>

#include <iocsh.h>
#include <registryFunction.h>
#include <epicsExport.h>
#include <epicsThread.h>
#include <aSubRecord.h>
#include <dbFldTypes.h>
#include <dbAddr.h>
//#include <dbAccess.h>
#include "save_restore.h"

using namespace     std;

#ifndef		MAX_PATH
#define		MAX_PATH	100
#endif	//	MAX_PATH

///	Default initialization directory path
///	Leave this global so we can change it from the cmd line if desired
char	*		pszConfigNameInitDir	= "initBAT";
char			acStartupDir[MAX_PATH] = "";


///	Debug variable for Config functions
/// Set non-zero to enable diag output to console
/// Larger values generate more output
int		DEBUG_GMD_CONFIG		= 2;


extern "C" long ConfigNameInit(	aSubRecord	*	pSub	)
{
	static	bool	isInitialized	= false;

	// No need to do this more than once if we have
	// more than one sub record
	if ( isInitialized )
		return 0;

	// Set our init directory
//	string		strInitDir		= pszConfigNameInitDir;
//	if ( DEBUG_GMD_CONFIG >= 1 )
//		cout	<< endl << endl
//				<< "ConfigNameInit: Reading initial values from " << string( pszConfigNameInitDir )
//				<<	endl << endl;

	getcwd( acStartupDir, MAX_PATH );
	isInitialized	= true;
	return 0;
}


extern "C" long ConfigNameChange( aSubRecord	*	pSub	)
{
#if 0
	//
	//	Get PV values
	//

	//	Get configuration name
	string	strConfigName	= static_cast<char *>( pSub->a );
	strConfigName	+= ".sav";

	//	Change the config file
	unsigned long	status	= 0;
//	status	= request_manual_save( strConfigName.c_str(), NULL );
	if ( DEBUG_GMD_CONFIG >= 1 )
	{
		cout	<<	"Change config savefile to " << strConfigName << endl << endl;
	}

	if ( status != 0 )
	{
		cerr << "ConfigNameChange: Error " << endl;
		return status;
	}

	//	Gather the output
//	*static_cast<long *>(	pSub->vala )	= status;
#endif
	return 0;
}


extern "C" long ConfigNameRecall( aSubRecord	*	pSub	)
{
	//
	//	Get PV values
	//

	//	Get configuration name
	string	strConfigName	= static_cast<char *>( pSub->a );
	strConfigName	+= ".sav";

	//	Recall the config file
	unsigned long	status	= 0;
	status	= fdbrestore( const_cast<char *>( strConfigName.c_str() ) );
	if ( status != 0 )
	{
		cerr	<< "ConfigNameRecall: Error for filename "	<< strConfigName << endl;
		return status;
	}
	else if ( DEBUG_GMD_CONFIG >= 1 )
	{
		cout	<<	"Recall config from " << strConfigName << endl << endl;
	}

	//	Gather the output
//	*static_cast<long *>(	pSub->vala )	= status;

	return 0;
}


extern "C" long ConfigNameSave( aSubRecord	*	pSub	)
{
	unsigned long	status	= 0;
	//
	//	Get PV values
	//

	//	Get configuration name
	string	strConfigName	= static_cast<char *>( pSub->a );
	strConfigName	+= ".sav";

	//	Get name of request file with list of PV's to save
	string	strListName	= static_cast<char *>( pSub->b );

	//	Set the manual config autosave set to save to the configuration name
	status	= set_savefile_name(const_cast<char *>( strListName.c_str()	),
								const_cast<char *>( strConfigName.c_str()	) );

	//	Save the config set
	status	= request_manual_save( const_cast<char *>( strListName.c_str() ), NULL );

	if ( status != 0 )
	{
		cerr	<< "ConfigNameSave: Error: Unable to save " << strListName
				<< " configuration to " << strConfigName << endl;
		return status;
	}
	else if ( DEBUG_GMD_CONFIG >= 1 )
	{
		cout	<<	"Saved " << strListName << " configuration to " << strConfigName << endl << endl;
	}

	//	Gather the output
//	*static_cast<long *>(	pSub->vala )	= status;

	return 0;
}


// Register aSub functions
extern "C"
{

epicsRegisterFunction( ConfigNameInit );
epicsRegisterFunction( ConfigNameChange );
epicsRegisterFunction( ConfigNameRecall );
epicsRegisterFunction( ConfigNameSave );

epicsExportAddress( int, DEBUG_GMD_CONFIG );
}
