UNITEX_FUNC void UNITEX_CALL persistence_public_unload_fst2(const char*filename)
{
	return standard_unload_persistence_fst2(filename);
}
int main_PersistResource(int argc,char* const argv[]) {
if (argc==1) {
   usage();
   return SUCCESS_RETURN_CODE;
}

const char *resource_file = NULL;
int res_graph=0;
int res_alphabet=0;
int res_dico=0;
int unpersist=0;
int verbose=0;
VersatileEncodingConfig vec = VEC_DEFAULT;
int val,index=-1;
bool only_verify_arguments = false;
const char*output_file = NULL;
const char*resource_type = NULL;
UnitexGetOpt options;

while (EOF!=(val=options.parse_long(argc,argv,optstring_PersistResource,lopts_PersistResource,&index))) {
   switch(val) {
   case 'a': res_alphabet = 1; resource_type = "alphabet"; break;
   case 'g': res_graph = 1; resource_type = "graph"; break;
   case 'd': res_dico = 1; resource_type = "dictionary"; break;
   case 'u': unpersist = 1; break;
   case 'v': verbose = 1; break;
   case 'o': if (options.vars()->optarg[0]=='\0') {
                error("Empty output argument\n");
                return USAGE_ERROR_CODE;
             }
             output_file = options.vars()->optarg; // FIXME(gvollant)
             break;
   case 'V': only_verify_arguments = true;
             break;   
   case 'h': usage(); 
             return SUCCESS_RETURN_CODE;
   case ':': index==-1 ? error("Missing argument for option -%c\n",options.vars()->optopt):
                         error("Missing argument for option --%s\n",lopts_PersistResource[index].name);
             return USAGE_ERROR_CODE;
   case '?': index==-1 ? error("Invalid option -%c\n",options.vars()->optopt) :
                         error("Invalid option --%s\n",options.vars()->optarg);
             return USAGE_ERROR_CODE;
   case 'k':
   case 'q': /* ignore -k and -q parameter instead to raise an error */
             break;
   }
   index=-1;
}

if ((res_graph+res_alphabet+res_dico) != 1) {
	error("Invalid arguments: rerun with --help\n");
	return USAGE_ERROR_CODE;
}

if ((output_file!=NULL) && (unpersist!=0)) {
	error("Invalid arguments: rerun with --help\n");
	return USAGE_ERROR_CODE;
}

if (options.vars()->optind!=argc-1) {
	error("Invalid arguments: rerun with --help\n");
	return USAGE_ERROR_CODE;
}

if (only_verify_arguments) {
	// freeing all allocated memory
	return SUCCESS_RETURN_CODE;
}

resource_file = argv[options.vars()->optind];
size_t size_buf_persisted_filename = strlen(resource_file) + 0x200;
char* buf_persisted_filename = (char*)malloc(size_buf_persisted_filename +1);
if (buf_persisted_filename == NULL) {
	alloc_error("PersistResource's main");
	return ALLOC_ERROR_CODE;
}
*buf_persisted_filename='\0';
if (unpersist == 0) {
	int result = 0;
	if (res_alphabet)
		result = standard_load_persistence_alphabet(resource_file, buf_persisted_filename, size_buf_persisted_filename);
	if (res_graph)
		result = standard_load_persistence_fst2(resource_file, buf_persisted_filename, size_buf_persisted_filename);
	if (res_dico)
		result = standard_load_persistence_dictionary(resource_file, buf_persisted_filename, size_buf_persisted_filename);
	if (result && verbose)
		u_printf("Success on persist %s resource %s to persisted name %s\n", resource_type, resource_file, buf_persisted_filename);
	if (!result)
		error("The %s resource %s cannnot be persisted\n", resource_type, resource_file);


	if (result && (output_file != NULL)) {
		U_FILE* text = u_fopen(&vec, output_file, U_WRITE);
		if (text == NULL) {
			error("Cannot create text file %s\n", output_file);
			free(buf_persisted_filename);
			return DEFAULT_ERROR_CODE;
		}
		u_fprintf(text, "%s", buf_persisted_filename);
		u_fclose(text);
	}

	free(buf_persisted_filename);
	return result ? SUCCESS_RETURN_CODE : DEFAULT_ERROR_CODE;
}
else
{
	if (res_alphabet)
		standard_unload_persistence_alphabet(resource_file);
	if (res_graph)
		standard_unload_persistence_fst2(resource_file);
	if (res_dico)
		standard_unload_persistence_dictionary(resource_file);
	u_printf("The %s resource %s unpersisted\n", resource_type, resource_file);
	return SUCCESS_RETURN_CODE;
}

}