#define SERVER_NAME cgiServerName
#define SAVED_ENVIRONMENT "/tmp/cgicsave.env"
#define INDEV	stdin
#define OUTDEV	stdout

#define DEFAULT_BUF_SIZE	1024
#define RESERVED		5

#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "SHSSDK.h"
#include "SHCSDK.h"

int main(void)
{
	char service[DEFAULT_BUF_SIZE + RESERVED];
	char service_host[DEFAULT_BUF_SIZE + RESERVED];
	char auth_string[DEFAULT_BUF_SIZE + RESERVED];
	char *auth_key, *auth_file;
	char *__service_host = NULL;
	char *__auth_string = NULL;
	HSHSDK hsdk;
	char source[DEFAULT_BUF_SIZE + RESERVED];
	char target[DEFAULT_BUF_SIZE + RESERVED];

	/* get service id */
	strcpy(service, "demoweb");
	if (service && !strcmp(service, "demoweb")) {
		auth_key = "demoweb";
		auth_file = "/tmp/demo40.cert";
	} else {
		fprintf(OUTDEV, "서비스 ID가 존재하지 않습니다.\n");
		goto exit_prg;
	}

	/* SSDK : get service host */
	__service_host = (char *)sh_get_service_host("demo", "ktidc", service);
	if (!__service_host) {
		fprintf(OUTDEV, "서비스 호스트를 받아오지 못했습니다.\n");
		goto exit_prg;
	}

	/* SSDK : get auth string */
	__auth_string = (char *)sh_get_auth_string("demo", "ktidc", service, auth_key, auth_file, time(0)+100000);
	if (!__auth_string) {
		fprintf(OUTDEV, "인증 스트링을 받아오지 못했습니다.\n");
		goto exit_prg;
	}
	snprintf(service_host, DEFAULT_BUF_SIZE, "%s",
		__service_host);

	snprintf(auth_string, DEFAULT_BUF_SIZE, "%s",
		__auth_string);

	/* CSDK : init */
	fprintf(stderr, "service_host %s\n", service_host);
	hsdk = sh_init(service_host, auth_string, NULL, NULL);
	if (hsdk == NULL) {
		fprintf(OUTDEV, "클라이언트 SDK 초기화를 실패하였습니다.\n");
		goto exit_prg;
	}

	/* specify source and target */
	strcpy(source, "/source_to_copy");
	strcpy(target, "/target_to_copy");

	/* check variable */
	if (!source[0]) {
		fprintf(OUTDEV, "원본 경로가 존재하지 않습니다.\n");
		goto exit_prg;
	}
	if (!target[0]) {
		fprintf(OUTDEV, "대상 경로가 존재하지 않습니다.\n");
		goto exit_prg;
	}
	if (source[0] != '/') {
		fprintf(OUTDEV, "원본이 경로가 아닙니다.\n");
		goto exit_prg;
	}

	/* CSDK : get list */
	if (!sh_copy(hsdk, source, target, 1)) {
		fprintf(OUTDEV, "복사를 실패하였습니다.\n");
		goto exit_prg;
	}

	fprintf(OUTDEV, "복사가 성공하였습니다.\n");
exit_prg:
	if (__service_host) free(__service_host);
	if (__auth_string) free(__auth_string);

	return 0;
}
