/***************************************************************************
                Sample.cpp
                -----------------------------------------
    copyright  : (C) 2005 SolutionBox Inc.
    author     : Service 1 Team
    email      : svc1@solbox.com
    
    CopyRight(C) 2005 SolutionBox Inc. All Rights reserved.
    Redistribution and use in source and binary forms, with or with out
    modification, are not permitted in outside of SolutionBox Inc.
***************************************************************************/

#include "stdafx.h"

#define INDEV	stdin
#define OUTDEV	stdout

#define DEFAULT_BUF_SIZE	1024
#define RESERVED		5


#include <windows.h>
#include <atlstr.h>
#include "SHSSDK.h"
#include "SHCSDK.h"


// 아래는 개통정보 전달시 전달된 내용입니다.
// 단, cert 파일은 샘플소스와 함께 전달됩니다.
#define ID "xeron"
#define PWD "xeron@#$"
#define FILE_PATH "./xeron345.cert"
#define SERVICE "comtopsy"
#define AUTHSTR	"didqkdgid"

/* Callback Funtion */
int __stdcall CallbackProc(void *param, long long int result)
{
	// progress
	printf("File Transfer : %lld\n", result);

	// 1 : Stop
	// 0 : Continue
	return 1;
}

int main(void)
{
	char service[DEFAULT_BUF_SIZE + RESERVED];
	char path[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;
	HSHFILELIST hsdf;
	int i;
	PSHFILE_STRUCT pshf;
	
	strcpy(service, SERVICE);
	if (service && !strcmp(service, SERVICE)) {
		auth_key	= AUTHSTR;
		auth_file	= FILE_PATH;
	} else {
		fprintf(OUTDEV, "There isn't the service ID\n");
		return -1;
	}

	///////////////////////// SSDK ////////////////////////////////////////////////////////////////////
	// !!! 중요 !!!
	// SSDK 가 End User에게 배포가되면 안됩니다.
	// 그 이유는 보안상 취약점이 발생 하기때문입니다.
	// 해당 소스는 샘플용일 뿐이며, SSDK는 인증서버를 마련하여 해당 서버에서 
	// 생성해 전달 될 수 있도록 제작되어야 합니다.

	/* SSDK : get service host */
	__service_host = (char *)sh_get_service_host(ID, PWD, service);
	if (!__service_host) {
		fprintf(OUTDEV, "Cannot get the service host.\n");
		goto ERR_EXIT;
	}

	/* SSDK : get auth string */
	__auth_string = (char *)sh_get_auth_string(ID, PWD, service, auth_key, auth_file, time(0)+100000);
	if (!__auth_string) {
		fprintf(OUTDEV, "Cannot get the auth string.\n");
		goto ERR_EXIT;
	}
	///////////////////////// SSDK ////////////////////////////////////////////////////////////////////


	sprintf(service_host, "%s", __service_host);

	sprintf(auth_string, "%s", __auth_string);

	/* CSDK : init */
	fprintf(stderr, "service_host!!! %s\n", service_host);
	hsdk = sh_init(service_host, auth_string, CallbackProc, NULL);
	if (hsdk == NULL) {
		fprintf(OUTDEV, "Failed to initiate the SDK.\n");
		goto ERR_EXIT;
	}
	path[0] = '\0';


	/* TODO : listing base uri copy to path */
	strcpy(path, "/");
	/* CSDK : get list */
	hsdf = sh_get_filelist(hsdk, path, 1);
	
	if (hsdf == NULL) {
		fprintf(OUTDEV, "Failed to retrieve the file list.\n");
		goto ERR_EXIT;
	}

	for (i = 0; i< sh_get_filelist_count(hsdk, hsdf); i++) {
		pshf = sh_get_file(hsdk, hsdf, i);
		if (pshf->attr == SHFILEATTR_FOLDER) {
			fprintf(OUTDEV, "Folder Name : %s\n", pshf->path);
		} else {
			fprintf(OUTDEV, "file Name : %s(%lld byte)\n",
					pshf->path, pshf->size);
		}
	}
	sh_free_filelist(hsdk, hsdf);

	/* Upload Test */
	if (!sh_upload(hsdk, "./test.txt", "/test.txt", SHWRITEPOLICY_OVERWRITE, -1, NULL))
	{
		printf("sh_upload ERROR 1: %d, %s\n", sh_get_error_number(hsdk), sh_get_error_message(hsdk));
		goto ERR_EXIT;
	} else {
		printf("sh_upload SUCCESS\n");
	}

	/* Dowload Test */
	if (!sh_download(hsdk, "/test.txt", "./test.txt", SHWRITEPOLICY_OVERWRITE, -1, NULL))
	{
		printf("sh_download ERROR : %d, %s\n", sh_get_error_number(hsdk), sh_get_error_message(hsdk));
		goto ERR_EXIT;
	} else {
		printf("sh_download SUCCESS\n");
	}

	/* CSDK : get list */
	hsdf = sh_get_filelist(hsdk, path, 1);
	
	if (hsdf == NULL) {
		fprintf(OUTDEV, "Failed to retrieve the file list.\n");
		goto ERR_EXIT;
	}

	for (i = 0; i< sh_get_filelist_count(hsdk, hsdf); i++) {
		pshf = sh_get_file(hsdk, hsdf, i);
		if (pshf->attr == SHFILEATTR_FOLDER) {
			fprintf(OUTDEV, "Folder Name : %s\n", pshf->path);
		} else {
			fprintf(OUTDEV, "file Name : %s(%lld byte)\n",
					pshf->path, pshf->size);
		}
	}
	sh_free_filelist(hsdk, hsdf);

	/* File delete test */
	if (sh_delete(hsdk, "/test.txt") == 0)
	{
		printf("sh_delete ERROR : %d, %s\n", sh_get_error_number(hsdk), sh_get_error_message(hsdk));
		goto ERR_EXIT;
	}
	else
	{
		printf("sh_delete SUCCESS\n");
	}

	/* CSDK : get list */
	hsdf = sh_get_filelist(hsdk, path, 1);
	
	if (hsdf == NULL) {
		fprintf(OUTDEV, "Failed to retrieve the file list.\n");
		goto ERR_EXIT;
	}

	for (i = 0; i< sh_get_filelist_count(hsdk, hsdf); i++) {
		pshf = sh_get_file(hsdk, hsdf, i);
		if (pshf->attr == SHFILEATTR_FOLDER) {
			fprintf(OUTDEV, "Folder Name : %s\n", pshf->path);
		} else {
			fprintf(OUTDEV, "file Name : %s(%lld byte)\n",
					pshf->path, pshf->size);
		}
	}
	sh_free_filelist(hsdk, hsdf);

	sh_free(hsdk);

	ERR_EXIT:
	if(__service_host)
		sh_mem_free(__service_host);

	if(__auth_string)
		sh_mem_free(__auth_string);

	return 0;
}

