#include "homedir.h"

void setup_homedir(void){
	char dirname[100];
	char filename[100];
	
	DIR * dirptr;
	FILE *out;
	int ret_val = 0;

#ifndef WIN32
	strcpy(dirname, "/home/soul/.lsbench/");
#else
	strcpy(dirname, "%UserProfile%\\lsbench\\");
#endif
	dirptr = opendir((const char *)&dirname);

	if(dirptr == NULL){
		

		printf("%s: opendir - %s\n", program_invocation, strerror(errno) );
		printf("creating lsbench local dir\n");
		//fprintf(stderr, "Warn: %s\n", gai_strerror(dirptr));

#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 %s, exiting!\n", dirname);
			exit(1);
		}
		//create conf file also
		//strcat(dirname, "\lsbench.conf");
#else
		//ret_val = mkdir((const char *)dirname, S_IRWXU);
		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 %s, exiting!\n", dirname);
			exit(1);
		}
		//create conf file also
		//strcat(dirname, "/lsbench.conf");
#endif
	}


	//check for conf file
	(void)strcpy(filename, (const char *)dirname);
	(void)strcat(filename, "lsbench.conf");
	if( out = fopen(filename, "r") ){
		fclose(out);
	}else{
		//create it

		if( ( out = fopen( filename, "w" ) ) == NULL ) {
			printf("%s: fopen - %s\n", program_invocation, strerror(errno) );
			printf("Error: cannot create file %s, exiting!\n", filename);
			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", filename);
			exit(1);
		}

	}	
	fclose(out);	
	//main dir exists already, check for subdir now
	//dirname[strlen(dirname)-strlen("/lsbench.conf")] = '\0';

	strcat(dirname, "cache");
	dirptr = opendir((const char *)&dirname);
	if(dirptr == NULL){
		//printf("%s: opendir - %s\n", program_invocation, strerror(errno) );
#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 %s, exiting!\n", dirname);
			exit(1);
		}
		if(ret_val == 0){
			printf("%s: SetFileAttributes - %s\n", program_invocation, strerror(errno) );
			printf("Error: cannot create %s, exiting!\n", dirname);
			exit(1);
		}
#else
		//ret_val = mkdir((const char *)dirname, S_IRWXU);
		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 %s, exiting!\n", dirname);
			exit(1);
		}
#endif
	}


	//unistd.h
	//int access(const char *path, int amode); //returns 0 for success

	if( closedir(dirptr) != 0){
		fprintf(stderr, "closedir: %s\n", gai_strerror(errno));
	}

}
