#ifeq ($(os),sunos)
# CIOLIB-MT_LIBS		+=	$(UL_PRE)curses$(UL_SUF)
#else
# CIOLIB-MT_LIBS		+=	$(UL_PRE)ncursesw$(UL_SUF) $(UL_PRE)tinfow$(UL_SUF)
#endif

# OSs with vasprintf...
ifeq ($(os),darwin)
 CFLAGS += -DHAVE_VASPRINTF
endif
ifeq ($(os),linux)
 CFLAGS += -DHAVE_VASPRINTF
endif
ifeq ($(os),freebsd)
 CFLAGS += -DHAVE_VASPRINTF
endif
ifeq ($(os),netbsd)
 CFLAGS += -DHAVE_VASPRINTF
endif
ifeq ($(os),openbsd)
 CFLAGS += -DHAVE_VASPRINTF
endif
ifdef win
 CFLAGS += -I${3RDP_ROOT}/win32.release/sdl2/include
endif

ifndef win
 ifeq ($(shell pkg-config ncursesw --exists && echo YES), YES)
  CIOLIB-MT_LIBS += $(shell pkg-config ncursesw --libs)
  CIOLIB-MT_CFLAGS += $(shell pkg-config ncursesw --cflags)
 else
  ifdef USE_SYSTEM_CURSES
   CIOLIB-MT_LIBS		+=	$(UL_PRE)curses$(UL_SUF)
  else
   ifeq ($(os),darwin)
    CIOLIB-MT_LIBS		+=	$(UL_PRE)ncurses$(UL_SUF)
   else
    CIOLIB-MT_LIBS		+=	$(UL_PRE)ncursesw$(UL_SUF)
   endif
  endif
 endif
 ifndef NO_X
  ifdef STATIC
   CIOLIB-MT_LIBS		+=	-L$(X_PATH)$(DIRSEP)lib -lX11
  else
   ifeq ($(os),darwin)
    ifeq ($(shell uname -r),6.8)
     CIOLIB-MT_LIBS		+=  $(UL_PRE)dl$(UL_SUF)
    endif
   endif
   ifeq ($(os),linux)
    CIOLIB-MT_LIBS		+=  $(UL_PRE)dl$(UL_SUF)
   endif
   ifeq ($(os),sunos)
    CIOLIB-MT_LIBS		+=  $(UL_PRE)dl$(UL_SUF)
   endif
  endif
 else
  CIOLIB-MT_CFLAGS		+=  -DDISABLE_X11=1 -DNO_X
 endif
else
 CIOLIB-MT_LIBS += -lkernel32
 NO_X			 :=  1
 CIOLIB-MT_CFLAGS        +=  -DDISABLE_X11=1 -DNO_X
 ifndef WITHOUT_GDI
  CIOLIB-MT_LIBS += -lgdi32 -mwindows
  CIOLIB-MT_CFLAGS += -DWITH_GDI
  NEED_BITMAP := 1
  IS_WINDOWS := 1
 endif
 ifdef USE_SDL
  CIOLIB-MT_LIBS += -lgdi32
  ifndef IS_WINDOWS
   CIOLIB-MT_LIBS += -mwindows
  endif
  WITH_SDL := 1
 endif
endif

ifdef WITH_RETRO
 CIOLIB-MT_CFLAGS += -DWITH_RETRO
endif

ifndef NO_X
 ifeq ($(shell pkg-config xrender --exists && echo YES), YES)
  CIOLIB-MT_CFLAGS += $(shell pkg-config xrender --cflags)
  CIOLIB-MT_CFLAGS += -DWITH_XRENDER
 endif
 ifeq ($(shell pkg-config xinerama --exists && echo YES), YES)
  CIOLIB-MT_CFLAGS += $(shell pkg-config xinerama --cflags)
  CIOLIB-MT_CFLAGS += -DWITH_XINERAMA
 endif
 ifeq ($(shell pkg-config xrandr --exists && echo YES), YES)
  CIOLIB-MT_CFLAGS += $(shell pkg-config xrandr --cflags)
  CIOLIB-MT_CFLAGS += -DWITH_XRANDR
 endif
endif

# Find SDL headers!
ifdef USE_SDL
 ifdef SDL_CONFIG
  ifeq ($(shell command -v ${SDL_CONFIG} > /dev/null && if [ `${SDL_CONFIG} --version | sed -E 's/\..*$$//'` -ge 2 ] ; then echo 'YES' ; fi),YES)
   WITH_SDL	:=	1
  endif
 endif
 ifndef WITH_SDL
  ifeq ($(shell command -v sdl2-config > /dev/null && sdl2-config --cflags > /dev/null 2>&1 && echo YES),YES)
   SDL_CONFIG := sdl2-config
   WITH_SDL	:=	1
  else
   ifeq ($(shell command -v sdl-config > /dev/null && if [ `sdl-config --version | sed -E 's/\..*$$//'` -ge 2 ] ; then echo 'YES' ; fi),YES)
    SDL_CONFIG := sdl-config
    WITH_SDL	:=	1
   endif
  endif
 endif
 ifndef WITH_SDL
  ifeq ($(os),darwin)
   ifeq ($(shell if [ -d /Library/Frameworks/SDL2.framework ] ; then echo YES ; fi),YES)
    SDL_FRAMEWORK_PATH ?= /Library/Frameworks
    CFLAGS += -F${SDL_FRAMEWORK_PATH}
   endif
   ifdef SDL_FRAMEWORK_PATH
    WITH_SDL	:=	1
    CIOLIB-MT_LDFLAGS += -Wl,-rpath,@executable_path/../Frameworks
    CIOLIB-MT_LDFLAGS += -Wl,-rpath,${SDL_FRAMEWORK_PATH}
   endif
  endif
 endif
endif

ifdef WITH_SDL
 NEED_BITMAP := 1
 # Prefer local include over system includes.
 #CFLAGS += -I$(SRC_ROOT)/../include/sdl
 ifdef SDL_CONFIG
  ifeq ($(shell ${SDL_CONFIG} --cflags > /dev/null 2>&1 && echo YES),YES)
   CIOLIB-MT_CFLAGS	+=	-DWITH_SDL
   CIOLIB-MT_CFLAGS	+=	$(shell $(SDL_CONFIG) --cflags)
   CIOLIB_CFLAGS	+=	-DWITH_SDL
   CIOLIB_CFLAGS	+=	$(shell $(SDL_CONFIG) --cflags)
   ifdef STATIC_SDL
    CIOLIB-MT_LIBS	+=	$(shell $(SDL_CONFIG) --static-libs)
    CIOLIB-MT_CFLAGS	+=	-DSTATIC_SDL
    CIOLIB-LIBS	+=	$(shell $(SDL_CONFIG) --static-libs)
    CIOLIB-CFLAGS	+=	-DSTATIC_SDL
   else
    ifeq ($(os),darwin)
     ifeq ($(shell uname -r),6.8)
      CIOLIB-MT_LIBS		+=  $(UL_PRE)dl$(UL_SUF)
      CIOLIB_LIBS		+=  $(UL_PRE)dl$(UL_SUF)
     endif
    endif
    ifeq ($(os),linux)
     CIOLIB-MT_LIBS		+=  $(UL_PRE)dl$(UL_SUF)
     CIOLIB_LIBS		+=  $(UL_PRE)dl$(UL_SUF)
    endif
    ifeq ($(os),sunos)
     CIOLIB-MT_LIBS		+=  $(UL_PRE)dl$(UL_SUF)
     CIOLIB_LIBS		+=  $(UL_PRE)dl$(UL_SUF)
    endif
   endif
  endif
 else
  ifeq ($(os),darwin)
   CIOLIB-MT_CFLAGS	+=	-DWITH_SDL -DSTATIC_SDL
   WITH_SDL	:=	1
   STATIC_SDL	:=	1
   ifdef SDL_FRAMEWORK_PATH
    CIOLIB-MT_CFLAGS	+=	-I${SDL_FRAMEWORK_PATH}/SDL2.framework/Headers
    CIOLIB-MT_LIBS	+=	-F${SDL_FRAMEWORK_PATH} -framework SDL2 -I${SDL_FRAMEWORK_PATH}/SDL2.framework/Headers
    CIOLIB_CFLAGS	+=	-DWITH_SDL -DSTATIC_SDL
    CIOLIB_CFLAGS	+=	-I${SDL_FRAMEWORK_PATH}/SDL2.framework/Headers
    CIOLIB_LIBS	+=	-F${SDL_FRAMEWORK_PATH} -framework SDL2 -I${SDL_FRAMEWORK_PATH}/SDL2.framework/Headers
   endif
  else
   ifdef win
    CIOLIB-MT_CFLAGS	+=	-DWITH_SDL
    WITH_SDL	:=	1
    CIOLIB_CFLAGS	+=	-DWITH_SDL
    CIOLIB-MT_LIBS := -lmingw32 $(CIOLIB-MT_LIBS)
    CIOLIB_LIBS := -lmingw32 $(CIOLIB_LIBS)
   endif
  endif
 endif
endif

ifndef NO_X
 NEED_BITMAP := 1
 CIOLIB_CFLAGS += -DHAS_VSTAT
 CIOLIB-MT_CFLAGS += -DHAS_VSTAT
else
 ifdef WITH_SDL
  CIOLIB_CFLAGS += -DHAS_VSTAT
  CIOLIB-MT_CFLAGS += -DHAS_VSTAT
 endif
endif

ifndef NEED_BITMAP
 CIOLIB-MT_CFLAGS		+=	-DNO_FONTS
 CIOLIB_CFLAGS			+=	-DNO_FONTS
endif

ifdef NEED_BITMAP
 CIOLIB_CFLAGS += -DHAS_BITMAP
 CIOLIB-MT_CFLAGS += -DHAS_BITMAP
endif
