/******************************************************************************
*
* Copyright (C) 2004-2017  Brian Berlin
* 
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************
*
* FILENAME: util.h
*
* DESCRIPTION:  This contains function declarations for utility functions
*
* AUTHORS:  bberlin
*
* CHANGE LOG
*
*  VER     DATE     CHANGED BY   DESCRIPTION OF CHANGE
* ------  --------  ----------   ------------------------------------
* 0.1     07/18/04  bberlin      Creation
* 0.2     03/09/06  bberlin      Added functions declarations for filename and
*                                  directory handling functions
******************************************************************************/
#ifndef util_h
#define util_h

#include "../minizip/unzip.h"
#include "../minizip/zip.h"
#ifndef _WIN32
#include <sys/stat.h>
#endif

#define KAT_PATH_MAX 4096
#define UTIL_CREATE_FILE 0x1
#define UTIL_DELETE_ON_STR 0x2
#ifdef _WIN32
#define UTIL_DEFAULT_DIR_MODE 0
#else
#define UTIL_DEFAULT_DIR_MODE (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
#endif
/*
 * Error Codes
 */
#define ERR_ROM_LOAD -1
#define ERR_BASIC_LOAD -2
#define ERR_BIOS_LOAD -3
#define ERR_PROFILE_LOAD -4
#define ERR_CPU_CRASH -5

typedef struct {
    const char *seed_tree;
    const char *dest_dir;
} t_profile_seed_target;

void util_init ( char *p_path );
int util_reset_config ( char *p_path );

/* Self-contained command line factory reset: restore the default conf AND
 * profiles synchronously (no deferred worker, no GUI) and return 0 on success.
 * Used by the --reset_config startup path, which must complete the reset and
 * exit without starting the GUI or emulation -- it exists to recover an
 * install that will not boot, so it cannot depend on normal startup running. */
int util_factory_reset_cli ( void );

/* First-use profile seeding is split from util_init so the GUI can come up
 * immediately: util_init seeds only kat5200.conf, then the large default
 * profile copy is deferred and run on a background thread behind a splash.
 *   _pending : profiles still need copying (set during util_init on first run)
 *   _worker  : the copy itself, suitable for pc_start_thread
 *   _done    : worker has finished
 *   _error   : a copy step failed (valid once _done is set)                  */
int util_first_use_seed_pending ( void );
int util_first_use_seed_worker ( void *unused );
int util_first_use_seed_done ( void );
int util_first_use_seed_error ( void );

/* True once a factory reset has restored the default conf this session.  While
 * true, config_write_file refuses to overwrite the global conf table so the
 * stale in-memory tree cannot clobber the restored defaults before restart.  */
int util_factory_reset_done ( void );

int util_set_program_path ( char *p_path );
const char * util_get_program_path ( void );
int util_strip_file_to_directory ( char * filename );
int util_strip_to_file ( char * filename );
int util_delete_delimeters ( char *string );
int util_set_file_from_relative_dir ( char *filename, char *directory );
int util_set_file_from_program_dir ( char *filename );
int util_set_file_from_current_dir ( char *filename );
int util_set_file_to_program_dir ( char *filename );
int util_set_file_to_app_dir ( char *filename );
int util_set_file_to_current_dir ( char *filename );
char util_get_dir_seperator_character ( void );
size_t util_trim_whitespace(char *out, size_t len, const char *str);
int util_all_whitespace(const char *z);
char *util_strlwr(char *str);
char * util_alloc_string ( char **s_ptr, const char *value );
int util_is_command_terminator(const char *zLine);
int util_ends_with_semicolon(const char *z, int N);
int util_make_path ( char *dir, const char *file, char *path );
char * util_make_path_alloc ( char *dir, const char *file  );
int util_mkdir ( const char *path, int mode );
int util_check_for_zip_alloc ( char *path, char **zip_file, char **path_inside );
void util_set_zip_time_info ( zip_fileinfo *info );
int util_file_copy ( char *from, char *to );
int util_unzip_to_directory ( const char *zip_path, const char *dest_dir );
int util_merge_seed_profiles_from_zip ( const char *zip_path,
        const t_profile_seed_target *targets, int target_count );
int util_schedule_missing_profile_seed_sync ( const char *zip_path,
        const t_profile_seed_target *targets, int target_count );
char * util_create_program_path ( char *path, char *filename );
char * util_create_program_path_alloc ( char *filename );
char * util_create_seed_path_alloc ( char *filename );
char * util_create_manual_path_alloc ( void );
char * util_create_file_uri_alloc ( const char *path );
int util_search_and_replace ( char *string, const char *find, const char *replace );
char * util_replace_and_copy ( const char *string1, const char *find, const char *replace );
int util_count_files_in_dir ( char *file, int scan_subs, char *search_str );
int util_check_for_line_in_file ( char *file, char *line );
int util_save_line_alpha_to_file ( char *file, char *line, char *search, int flags );
unsigned long util_get_crc ( char *file );
unsigned long util_get_all_hash ( char *file, unsigned long *crc, 
                                              unsigned char md5[16], 
                                              unsigned char sha1[20] );
int util_next_power_of_two ( int number );
int util_delay ( double seconds );
int util_inflate(const void *src, int srcLen, void *dst, int dstLen);
extern unzFile ZEXPORT util_open_zip_buffer ( const void *buffer, uLong size );

#endif
