#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#include "SHSSDK.h"
#include "SHCSDK.h"

#include "log.h"
#include "protocol.h"

using namespace std;
using namespace SDSocket;

HSHSDK access_sh(rcacct_t *rcinfo, char *errorMsg)
{
	char *serviceHost = NULL;
	char *authString  = NULL;
	HSHSDK hshsdk = NULL;
	int  errorCode;
	
	serviceHost = "demo.opendisk.com/dav";
	//serviceHost = "rc101.ktsh.co.kr/dav";
	authString = "ZGVtb0BkZW1vOjJhMzY2MTZiZjUxMjMxMzVkMTNjNjM5MTZlNGFkNDg2";
	//authString = "ZnJlZXpvbmV0ZXN0QGZyZWV6b25ldGVzdDo6/YBHEvD83rs4+oN8Axa5x/7vLY/CE/sjaj6wgUcyinDklTsjRrthzMV61zrosygAdkLyj9Ns1Otj8jngwcRhNzI0OTM=";
	
	//authString = (char *)sh_get_auth_string("kpost", "kpost", "kpost", "geniuspizon", "./kpost48.cert", time(NULL) + 150000);
	
	/* System Hosting Client-side SDK : 초기화 */

	fprintf(stderr, "serviceHost : %s\nauthString : %s\n", serviceHost, authString);
	hshsdk = sh_init(serviceHost, authString, NULL, NULL);
	fprintf(stderr, "------------------ 1 ------------------\n");
	if (!hshsdk) {
		errorCode = errno;
		fprintf(stderr, "System Hosting Client-side SDK 초기화에 실패하였습니다.");

		fprintf(stderr, "------------------ 2 ------------------\n");
		if (hshsdk)
			sh_free(hshsdk);
		if (serviceHost)
			free(serviceHost);
		if (authString)
			free(authString);
		fprintf(stderr, "------------------ 3 ------------------\n");

		return NULL;
	}

	fprintf(stderr, "------------------ 4 ------------------\n");
	fprintf(stderr, "------------------ 5 ------------------\n");
	return hshsdk;
}

int check_rc(char *filename, rcacct_t *rcinfo)
{
	HSHSDK hshsdk = NULL;
	char local_path[128], remote_path[128], local_temp[128];

	int  errorCode;
	char errorMsg[1024];

	strcpy(filename, "jin.avi");
	sprintf(local_path, "/tmp/%s", filename);
	sprintf(remote_path, "/%s", filename);
	sprintf(local_temp, "/user/ics10/gms/new_sh_statusd/test/%s", filename);
	
	unlink(local_temp);

	fprintf(stderr, "filename : %s\n", filename);
	hshsdk = access_sh(rcinfo, errorMsg);
	if (hshsdk) {
		errorCode = sh_upload(hshsdk, local_path, remote_path, SHWRITEPOLICY_OVERWRITE, -1);
		fprintf(stderr, "error: upload: %d:%s\n", errorCode, sh_get_error_message(hshsdk));
		fprintf(stderr, "debug: step 1 - %s\n", filename);
		if (errorCode == 0) {
			fprintf(stderr, "Error : %s\n", rcinfo->m_pszServiceName);
			sh_free(hshsdk);
			return -1;
		}
		
		fprintf(stderr, "debug: step 2\n");
		//usleep(500000);
		sleep(5);
		fprintf(stderr, "debug: step 3\n");

		errorCode = sh_download(hshsdk, remote_path, local_temp, SHWRITEPOLICY_OVERWRITE, -1);
		fprintf(stderr, "debug: step 4\n");		
		fprintf(stderr, "error: download: %d:%s\n", errorCode, sh_get_error_message(hshsdk));
		if (errorCode == 0) {
			sh_free(hshsdk);
			return -1;
		}
		fprintf(stderr, "debug: step 5\n");
		sh_delete(hshsdk, remote_path);
		fprintf(stderr, "debug: step 6\n");
		sh_free(hshsdk);
	} else {
		fprintf(stderr, "error: %s\n", errorMsg);
		return -1;
	}
	unlink(local_temp);
	fflush(stderr);
	return 0;
}

int check_rc_updown(rcacct_t *rcinfo)
{
	FILE *fp;
	struct timeval tv;
	char tagname[38];
	char tagfull[38];
	int retval = -1;

	gettimeofday(&tv, NULL);
	sprintf(tagname, "%d%06d", (int)tv.tv_sec, (int)tv.tv_usec);
	sprintf(tagfull, "/tmp/%d%06d", (int)tv.tv_sec, (int)tv.tv_usec);
#ifdef __DEBUG__
	fprintf(stderr, "tagname %s tagfull %s\n", tagname, tagfull);
#endif

	if ((fp = fopen(tagfull, "w"))) {
		//fprintf(fp, "%s", tagname);
		fclose(fp);

		/* start! */
		if (check_rc(tagname, rcinfo)) {
#ifdef __DEBUG__
			fprintf(stderr, "RC %s check failed\n", rcinfo->m_pszServiceName);
#endif
			retval = -1;
		} else {
#ifdef __DEBUG__
			fprintf(stderr, "RC %s check success\n", rcinfo->m_pszServiceName);
#endif
			retval = 0;
		}
		unlink(tagfull);
	}

	return retval;
}

void usage(void)
{
	fprintf(stderr, " usage : rc_updown_test <user> <passwd>\n");
	exit(2);
}

void HandleTimeOut(int signum)
{
	fprintf(stderr, "TIMEOUT!\n");
	exit(1);
	return;
}

int main(int argc, char *argv[])
{
	rcacct_t rcinfo;
	char buf[1024];
	int retval;

#ifdef __DEBUG__
	fprintf(stderr, "argc %d argv %s\n", argc, argv[1]);
#endif

	if (argc != 3) {
		usage();
	}

	memset((char *)&rcinfo, 0, sizeof(rcacct_t));

/*
	typedef struct _RCAcct_e
	{
		char *           m_pszServiceName;
		char *           m_pszUserName;
		char *           m_pszPassword;
		char *           m_pszAuthkey;
		char *           m_pszCertLocation;
		struct _RCAcct_e *next;
	} rcacct_t;
 */

	//rcinfo.m_pszServiceName = argv[2];
	rcinfo.m_pszUserName = argv[3];
	rcinfo.m_pszPassword = argv[4];
	//rcinfo.m_pszAuthkey = argv[5];
	//rcinfo.m_pszCertLocation = argv[6];

	::signal(SIGALRM, HandleTimeOut);
	::alarm(60);
	if ((retval = check_rc_updown(&rcinfo)) != 0) {
		fprintf(stderr, "RC %s check failed\n", rcinfo.m_pszUserName);
		usleep(1);
		exit(1);
	} else {
		fprintf(stderr, "RC %s check success\n", rcinfo.m_pszUserName);
		usleep(1);
		exit(0);
	}
	::alarm(0);
	exit(1);
}
