#include "homedir.h"

void setup_homedir(void){
	char *dirname;
	char *name_tmp;
	size_t strlength = 0;
	size_t reqlength = 0;
	DIR *dirptr;
	FILE *out;
	int ret_val = 0;

#ifndef _WIN32
	strlength = strlen( getenv("HOME") );
	dirname = (char *)malloc( strlength );
	if(dirname == NULL){
		printf("%s: malloc - %s\n", program_invocation, strerror(errno) );
		printf("failed to allocate memory, exiting!\n");
		exit(1);
	}
	(void)strncpy(dirname, (const char *)getenv("HOME"), strlength );
	strlength += strlen("/.lsbench");
	dirname = (char *)realloc(dirname, strlength);
	(void)strncat(dirname, "/.lsbench", strlen("/.lsbench") );
#else
	strlength = strlen( getenv("%userprofile%");
	dirname = (char *)malloc( strlength );

	//reglength = ExpandEnvironmentStrings("%userprofile%",dirname,strlength);
	//if(reglength > strlength){
	//	strlength = reglength;
	//	dirname = (char *)realloc(dirname, strlength);
	//	ExpandEnvironmentStrings("%userprofile%",dirname,strlength);
	//}

	//DWORD WINAPI GetLastError(void); no thanks
	strlength += strlen("\\lsbench");
	dirname = (char *)realloc(dirname, strlength);
	(void)strncat(dirname, "\\lsbench", strlen("\\lsbench") );
#endif
	dirptr = opendir((const char *)dirname);
	if(dirptr == NULL){
		printf("%s: opendir - %s\n", program_invocation, strerror(errno) );
		printf("creating lsbench local dir: %s\n", dirname);

#ifdef _WIN32
		ret_val = _mkdir((const char *)dirname); //returns 0 for success
		//CreateDirectory( name, NULL ); //returns 0 for failure
		if(ret_val != 0){
			//ret_val = SetFileAttributes( dirname, FILE_ATTRIBUTE_HIDDEN ); //returns 0 for failure
			//}else{
			printf("%s: _mkdir - %s\n", program_invocation, strerror(errno) );
			printf("Error: cannot create directory, exiting!\n");
			exit(1);
		}

		//if(ret_val == 0){
		//	printf("%s: SetFileAttributes - %s\n", program_invocation, strerror(errno) );
		//	printf("Error: cannot create directory, exiting!\n");
		//	exit(1);
		//}
#else
		ret_val = mkdir((const char *)dirname, S_IRWXU | S_IRGRP | S_IROTH | S_IXGRP | S_IXOTH); //returns 0 for success
		if(ret_val != 0){
			printf("%s: mkdir - %s\n", program_invocation, strerror(errno) );
			printf("Error: cannot create directory %s , exiting!\n");
			exit(1);
		}
#endif
	}else{
		if( closedir(dirptr) != 0){
			printf("%s: closedir - %s\n", program_invocation, strerror(errno) );
			printf("Error: cannot close directory %s, exiting!\n", dirname);
			exit(1);
		}

	}

	//check for conf file
#ifdef _WIN32
	reqlength = strlength + strlen("\\lsbench.conf");
	name_tmp = (char *)malloc( reqlength );
        (void)strncpy(name_tmp, (const char *)dirname, strlength );
	(void)strncat(name_tmp, "\\lsbench.conf", strlen("\\lsbench.conf") );
#else
	reqlength = strlength + strlen("/lsbench.conf");
	name_tmp = (char *)malloc( reqlength );
	(void)strncpy(name_tmp, (const char *)dirname, strlength );
	(void)strncat(name_tmp, "/lsbench.conf", strlen("/lsbench.conf") );
#endif

	if( out = fopen(name_tmp, "r") ){
		if(fclose(out) != 0){
			printf("%s: fclose - %s\n", program_invocation, strerror(errno) );
			printf("Error: cannot close file %s , exiting!\n", name_tmp);
			exit(1);
		}
	}else{
		//create it
		if( ( out = fopen( name_tmp, "w" ) ) == NULL ) {
			printf("%s: fopen - %s\n", program_invocation, strerror(errno) );
			printf("Error: cannot create file %s , exiting!\n", name_tmp);
			exit(1);
		}
		if(fprintf(out, "PORT: 3940\n") < 0 ){
			printf("%s: fprintf - %s\n", program_invocation, strerror(errno) );
			printf("Error: cannot write to file %s , exiting!\n", name_tmp);
			exit(1);
		}
		if(fclose(out) != 0){
			printf("%s: fclose - %s\n", program_invocation, strerror(errno) );
			printf("Error: cannot close file %s , exiting!\n", name_tmp);
			exit(1);
		}
	}	


#ifdef _WIN32	
	strlength += strlen("\\cache");
	dirname = (char *)realloc(dirname, strlength);
	(void)strncat(dirname, "\\cache", strlen("\\cache") );
#else
	strlength += strlen("/cache");
	dirname = (char *)realloc(dirname, strlength);
	(void)strncat(dirname, "/cache", strlen("/cache") );
#endif

	dirptr = opendir((const char *)dirname);
	if(dirptr == NULL){
		printf("%s: opendir - %s\n", program_invocation, strerror(errno) );
		printf("creating directory: %s\n", dirname);
#ifdef _WIN32
		ret_val = _mkdir((const char *)dirname); //returns 0 for success
		if(ret_val != 0){
			//ret_val = SetFileAttributes( &dirname, FILE_ATTRIBUTE_HIDDEN ); //returns 0 for failure
			//}else{
			printf("%s: _mkdir - %s\n", program_invocation, strerror(errno) );
			printf("Error: cannot create directory %s , exiting!\n", dirname);
			exit(1);
		}
		//if(ret_val == 0){
		//	printf("%s: SetFileAttributes - %s\n", program_invocation, strerror(errno) );
		//	printf("Error: cannot create directory, exiting!\n");
		//	exit(1);
		//}
#else
		ret_val = mkdir((const char *)dirname, S_IRWXU | S_IRGRP | S_IROTH | S_IXGRP | S_IXOTH); //returns 0 for success
		if(ret_val != 0){
			printf("%s: mkdir - %s\n", program_invocation, strerror(errno) );
			printf("Error: cannot create directory %s, exiting!\n", dirname);
			exit(1);
		}
#endif
	}else{
		//unistd.h
		//int access(const char *path, int amode); //returns 0 for success
		if( closedir(dirptr) != 0){
			printf("%s: closedir - %s\n", program_invocation, strerror(errno) );
			printf("Error: cannot close directory %s, exiting!\n", dirname);
			exit(1);
		}
	}
	free(dirname);
	free(name_tmp);
}
