/*
 * Kangaroo Punch MultiPlayer Game Server Mark II
 * Copyright (C) 2020-2021  Scott Duensing
 *
 * 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 <https://www.gnu.org/licenses/>.
 *
 */


#ifndef OS_H
#define OS_H


// https://sourceforge.net/p/predef/wiki/Architectures/


// Common platform includes.
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdarg.h>
#include <unistd.h>
#include <sys/stat.h>


#define SECONDS_IN_DAY    86400
#define TICKS_PER_SECOND  18.2
#define TICKS_PER_DAY     (SECONDS_IN_DAY * TICKS_PER_SECOND)

#define OS_PATH_MAX  256


#ifdef __linux__

#define BITS64
#define OS_PATH_SLASH '/'

// Linux DOS replacements.
uint32_t rawclock(void);

// Linux support functions.
void linuxOsShutdown(void);
void linuxOsStartup(void);

#else

#define BITS32
#define OS_PATH_SLASH '\\'

// DOS includes.
#include <dos.h>
#include <dpmi.h>
#include <go32.h>
#include <bios.h>
#include <conio.h>
#include <sys/farptr.h>
#include <sys/nearptr.h>

#endif


// Should be after system headers in this file.
#define MEMORY_CHECK_ENABLED
#include  "memory.h"

// Now our headers.
#include "log.h"
#include "util.h"
#include "packet.h"
#include "cache.h"


// Allocation helpers.
#define NEW(t,v) (v)=(t*)calloc(1, sizeof(t))
#define DEL(v)   { if (v) { free(v); v = NULL; } }

#define SUCCESS 1
#define FAIL    0

// Some helper defines.
#define DIVISIBLE_BY_EIGHT(x)  ((((x) >> 3) << 3) == (x))
#define HIGH_BYTE(b)           ((uint8_t)(((b) & 0xFF00) >> 8))
#define LOW_BYTE(b)            ((uint8_t)((b) & 0x00FF))


typedef struct PacketThreadDataS PacketThreadDataT;


extern PacketThreadDataT *__packetThreadData;  // Declared in main.c


uint8_t  osFileExists(char *filename);
char    *osLastPathComponentGet(char *pathname);
char    *osLastPathComponentGetUpTo(char *pathname);
int8_t   osMkDir(char *dir);
uint8_t  osMkDirP(char *dir);
void     osPathSeparatorsFix(char **path, uint8_t slash);
void     osShutdown(void);
void     osStartup(void);


#endif // OS_H
