#include "libublsettings.h"

// parsing functions

int yon_password_get_min_len(){
    int leng=6;
    int login_size;
    config_str login_defs = yon_file_open("/etc/security/pwquiality.conf",&login_size);
	for (int i=0;i<login_size;i++){
		if (login_defs[i][0]!='#'){
			char *copy = yon_char_new(login_defs[i]);
			char *divided = yon_char_divide(copy,7);
			if (divided){
				if (!strcmp(divided,"minlen")){
					leng = atol(strstr(login_defs[i],"\t"));
				}
            free(divided);
			}
            free(copy);
		}
	}
	yon_char_parsed_free(login_defs,login_size);
    return leng;
}

float yon_size_convert_automatic(int bytes, int *size){
    float byte_float=bytes;
    for (*size=-1;byte_float>1024;*size=*size+1){
        byte_float=byte_float/1024;
    }
    if (*size==-1) {
        *size=0;
        byte_float=byte_float/1024;
    }
    return byte_float;
}

long yon_size_to_bytes(long size, int size_mod){
    long final = size;
    for (int i=0;i<size_mod;i++){
        final=final*1024;
    }
    return final;
}

int yon_get_size_get_from_letter(char size){
    switch (size){
        case 'K': return 0;
        break;
        case 'M': return 1;
        break;
        case 'G': return 2;
        break;
        case 'T': return 3;
        break;
        default: return -1;
        break;
    }
}

char *yon_size_get_mod(int size){
    char *size_modifier = NULL;
    switch (size){
        case 0: size_modifier = "K";
            break;
        case 1: size_modifier = "M";
            break;
        case 2: size_modifier = "G";
            break;
        case 3: size_modifier = "T";
            break;
    }
    return size_modifier;
}

double yon_size_long_convert_to_mod(double size, char mod){
    int sizemod = yon_get_size_get_from_letter(mod);
    double final_size = size;
    for (int i=-1;i<sizemod;i++){
        final_size/=1024;
    }
    return final_size;
}

double yon_size_long_convert_automatic(unsigned long bytes, char *size){
    int repeats;
    double byte_float=bytes;
    for (repeats=-1;byte_float>1024;repeats++){
        byte_float=byte_float/1024;
    }
    if (repeats==-1) {
        repeats=0;
        byte_float=byte_float/1024;
    }
    switch(repeats){
        case 0: (*size)='K';
        break;
        case 1: (*size)='M';
        break;
        case 2: (*size)='G';
        break;
        case 3: (*size)='T';
        break; 
    }
    return byte_float;
}

config_str yon_dir_get_contents(char *dir_path, int *size){
    config_str dir = NULL;
    *size=0;
    if (!access(dir_path,F_OK)){
        DIR *directory = opendir(dir_path);
        struct dirent *de;
        while ((de = readdir(directory))){
            if (dir){ dir = yon_char_parsed_append(dir,size,yon_char_new(de->d_name));
            } else dir = yon_char_parsed_new(size,yon_char_new(de->d_name),NULL);
        }
        closedir(directory);
    }
    return dir;
}

config_str yon_dir_get_by_mask(char *path, char *mask, int *size){
    (*size)=0;
    if (yon_char_count(mask,"*")<=1){
        char *lpart;
        char *rpart=yon_char_new(mask);
        lpart = yon_char_divide_search(rpart,"*",-1);
        config_str files = NULL;
        int found_size;
        config_str found_files = yon_file_ls(path,&found_size);
        if(found_size>0){
            int found = 1;
            int rfound = 0;
            for (int i=0;i<found_size;i++){
                int c;
                for (c=0;c<strlen(found_files[i]);c++){
                    if (found_files[i][c]!=lpart[c]){
                        break;
                    }
                    if (c==strlen(lpart)) break;
                }
                if (found){
                    for (int l=c;l<strlen(found_files[i]);l++){
                        if (found_files[i][l]==rpart[l]) rfound++;
                        else rfound=0;
                    }
                    if (rfound==strlen(rpart)) {
                        yon_char_parsed_add_or_create_if_exists(files,size,found_files[i]);
                    }
                }
            }
            free(found_files);
        }
    }
}

// terminal-using functions

int yon_launch_app_with_arguments(char *name, char *args)
{
    char *path = yon_char_unite("/usr/bin/", name, " ", args,NULL);
    pthread_t thread_id;
    char *command = NULL;
    command = path;
    pthread_create(&thread_id, NULL, (void *)yon_launch, command);
};

void yon_launch(char *command)
{
    int ansver = system(command);
}

#define os_name_get_command "sed -En 's/^PRETTY_NAME=[\"]*([^\"]+)[\"]*/\\1/p' /etc/os-release"
char *yon_ubl_get_os_name(){
    char *os_name = NULL;
    int size;
    config_str ret = yon_config_load(os_name_get_command,&size);
    if (size){
        yon_char_remove_last_symbol(ret[0],'\n');
        os_name = yon_char_new(ret[0]);
        yon_char_parsed_free(ret,size);
    }
    return os_name;
}