.PHONY: all clean distclean install uninstall

DLOPEN=1
#USE_ZLIB=1
USE_DISCMAGE=1
#USE_USB=1
USE_LIBCD64=1

CC=gcc
CFLAGS=-I. -Wall -W -O3 -DHAVE_CONFIG_H
LDFLAGS=-s
TARGETS=

ifdef USE_DISCMAGE
LIBNAME_DM=discmage
endif

# The test for Cygwin should be done before the test for DJGPP, because the
# environment variable DJGPP can be set under Bash for people who have
# installed both GCC (and friends) ports.

GCC_WIN=0
# test cygwin before DJGPP; OSTYPE is not exported on Cygwin
ifeq ($(TERM),cygwin)
GCC_WIN=1
endif
# test msys before DJGPP; MSYS, MinGW's POSIX build environment
ifeq ($(OSTYPE),msys)
GCC_WIN=1
endif

ifeq ($(GCC_WIN),1)

ifdef USE_DISCMAGE
FULLLIBNAME_DM=$(LIBNAME_DM).dll
ifndef DLOPEN
LDFLAGS+=libdiscmage/$(LIBNAME_DM).a
endif
endif

else
ifdef DJGPP

ifdef USE_DISCMAGE
FULLLIBNAME_DM=$(LIBNAME_DM).dxe
ifndef DLOPEN
LDFLAGS+=libdiscmage/$(LIBNAME_DM).a
endif
endif

else                                    # Unix, BeOS or Mac OS X (Darwin)

ifeq ($(findstring openbsd,$(OSTYPE)),openbsd) # for example "openbsd3.4"
# i386_iopl() is located in libi386.a
LDFLAGS+=
endif

ifdef USE_DISCMAGE
ifeq ($(findstring darwin,$(OSTYPE)),darwin) # for example "darwin7.0"
FULLLIBNAME_DM=$(LIBNAME_DM).dylib
else
FULLLIBNAME_DM=$(LIBNAME_DM).so
endif
endif

ifdef DLOPEN

ifneq ($(OSTYPE),beos)
ifeq ($(findstring freebsd,$(OSTYPE)),) # false if OSTYPE contains "freebsd"
ifeq ($(findstring openbsd,$(OSTYPE)),) # false if OSTYPE contains "openbsd"
LDFLAGS+=-ldl
endif
endif
endif

else                                    # DLOPEN
ifdef USE_DISCMAGE                      # GNU specific: "simply expanded variable"
FULLLIBNAME_DM:=$(addprefix lib,$(FULLLIBNAME_DM))
LDFLAGS+=-Llibdiscmage -l$(LIBNAME_DM)
endif

endif

endif                                   # DJGPP
endif                                   # GCC_WIN

TARGETS+=libdiscmage/$(FULLLIBNAME_DM)


ifdef USE_LIBCD64
LDFLAGS+=backup/libcd64/libcd64.a
TARGETS+=backup/libcd64/libcd64.a
endif


OBJECTS=ucon64.o ucon64_dat.o ucon64_misc.o ucon64_opts.o \
        misc/chksum.o misc/file.o misc/getopt.o misc/getopt2.o \
        misc/misc.o misc/parallel.o misc/property.o misc/string.o \
        patch/aps.o patch/bsl.o patch/gg.o patch/ips.o patch/pal4u.o \
        patch/ppf.o patch/xps.o \
        console/dc.o console/gb.o console/gba.o console/genesis.o \
        console/jaguar.o console/lynx.o console/n64.o console/neogeo.o \
        console/nes.o console/ngp.o console/pce.o console/psx.o console/sms.o \
        console/snes.o console/swan.o \
        backup/cd64.o backup/cmc.o backup/dex.o backup/doctor64.o \
        backup/doctor64jr.o backup/f2a.o backup/fal.o backup/ffe.o \
        backup/fig.o backup/gbx.o backup/gd.o backup/interceptor.o \
        backup/lynxit.o backup/mccl.o backup/mcd.o backup/md-pro.o \
        backup/mgd.o backup/msg.o backup/pce-pro.o backup/pl.o \
        backup/psxpblib.o backup/sflash.o backup/smc.o backup/smd.o \
        backup/smsgg-pro.o backup/ssc.o backup/swc.o backup/tototek.o \
        backup/ufo.o backup/yoko.o backup/z64.o
ifdef USE_ZLIB
LDFLAGS+=-lz
OBJECTS+=misc/archive.o misc/map.o misc/unzip.o
endif
ifdef USE_USB
LDFLAGS+=-lusb
OBJECTS+=misc/usb.o
endif

ifdef DLOPEN
OBJECTS+=misc/dlopen.o
ifndef USE_ZLIB
ifeq ($(GCC_WIN),1)
else
ifdef DJGPP                             # DJGPP code in dlopen needs map code
OBJECTS+=misc/map.o
endif                                   # DJGPP
endif                                   # GCC_WIN
endif                                   # USE_ZLIB
else
ifeq ($(GCC_WIN),1)                     # Cygwin/MinGW code in ucon64_misc needs dlopen code
OBJECTS+=misc/dlopen.o
endif                                   # GCC_WIN
endif                                   # DLOPEN


TARGET=ucon64
ifeq ($(GCC_WIN),1)
TARGET:=$(addsuffix .exe,$(TARGET))     # adding .exe avoids "problems" with Cygwin/MinGW
else
ifdef DJGPP                             # OSTYPE is not defined by default under DOS
TARGET:=$(addsuffix .exe,$(TARGET))
endif                                   # DJGPP
endif                                   # GCC_WIN
TARGETS+=$(TARGET)

all: $(TARGETS)


CLEAN_CMD=rm -f $(TARGET) $(OBJECTS) *.core *.stackdump *~ */*~ */*/*~; \
cd libdiscmage && $(MAKE) clean; \
cd ../backup/libcd64 && $(MAKE) clean

clean:
ifeq ($(GCC_WIN),1)
	$(CLEAN_CMD)
else
ifdef DJGPP
	del *.o
	del patch\*.o
	del console\*.o
	del backup\*.o
	del misc\*.o
	del $(TARGET)
	cd libdiscmage
	$(MAKE) clean
	cd ../backup/libcd64
	$(MAKE) clean
	cd ../..
else                                    # Unix, BeOS or Mac OS X (Darwin)
	$(CLEAN_CMD)
endif                                   # DJGPP
endif                                   # GCC_WIN


DISTCLEAN_CMD=rm -f Makefile config.log config.status config.cache config.h; \
cd libdiscmage && $(MAKE) distclean; \
cd ../backup/libcd64 && $(MAKE) clean
# libcd64 Makefile has no distclean target

distclean: clean
ifeq ($(GCC_WIN),1)
	$(DISTCLEAN_CMD)
else
ifdef DJGPP
	del Makefile
	del config.log
	del config.status
	del config.cache
	del config.h
	cd libdiscmage
	$(MAKE) distclean
	cd ../backup/libcd64
	$(MAKE) clean
	cd ../..
else
	$(DISTCLEAN_CMD)
endif                                   # DJGPP
endif                                   # GCC_WIN


.c.o:
	$(CC) $(CFLAGS) -c $< -o $@

backup/cd64.o: backup/cd64.c
	$(CC) $(CFLAGS) -Ibackup/libcd64 -c $< -o $@


ifdef USE_DISCMAGE
libdiscmage/$(FULLLIBNAME_DM):
ifeq ($(GCC_WIN),1)
	cd libdiscmage && $(MAKE)
else
ifdef DJGPP
	cd libdiscmage
	$(MAKE)
	cd ..
else
	cd libdiscmage && $(MAKE)
endif                                   # DJGPP
endif                                   # GCC_WIN
endif                                   # USE_DISCMAGE

ifdef USE_LIBCD64
backup/libcd64/libcd64.a:
ifeq ($(GCC_WIN),1)
	cd backup/libcd64 && $(MAKE)
else
ifdef DJGPP
	cd backup/libcd64
	$(MAKE)
	cd ../..
else
	cd backup/libcd64 && $(MAKE)
endif                                   # DJGPP
endif                                   # GCC_WIN
endif                                   # USE_DISCMAGE

$(TARGET): $(OBJECTS)
	$(CC) $(OBJECTS) $(LDFLAGS) -o $@


install:
ifeq ($(TERM),cygwin)                   # test cygwin before DJGPP
else
ifeq ($(OSTYPE),msys)                   # test msys before DJGPP
else
ifdef DJGPP
else
ifeq ($(OSTYPE),beos)
	./install_beos.sh
else
	./install.sh
endif                                   # beos
endif                                   # DJGPP
endif                                   # msys
endif                                   # cygwin
ifndef DLOPEN
	cd libdiscmage && $(MAKE) install
endif


uninstall:
ifeq ($(TERM),cygwin)                   # test cygwin before DJGPP
else
ifeq ($(OSTYPE),msys)                   # test msys before DJGPP
else
ifdef DJGPP
else
ifeq ($(OSTYPE),beos)
	rm -f $(HOME)/config/bin/$(TARGET)
else
	rm -f /usr/local/bin/$(TARGET)
endif                                   # beos
endif                                   # DJGPP
endif                                   # msys
endif                                   # cygwin
ifndef DLOPEN
	cd libdiscmage && $(MAKE) uninstall
endif


# Dependencies

# Most source files include these
UCON64_STD_H=ucon64.h ucon64_misc.h misc/misc.h config.h ucon64_defines.h

misc/archive.o: misc/archive.h misc/map.h config.h
misc/chksum.o: misc/chksum.h config.h
misc/dlopen.o: misc/dlopen.h misc/dxedll_pub.h config.h
misc/getopt.o: misc/getopt.h
misc/map.o: misc/map.h config.h
misc/misc.o: misc/misc.h misc/archive.h config.h
misc/parallel.o: misc/parallel.h config.h
misc/usb.o: misc/usb.h config.h
misc/unzip.o: misc/unzip.h config.h
ucon64.o: misc/dlopen.h misc/getopt.h ucon64_dat.h ucon64_opts.h \
          console/dc.h console/gb.h console/gba.h console/genesis.h \
          console/jaguar.h console/lynx.h console/n64.h console/neogeo.h \
          console/nes.h console/ngp.h console/pce.h console/psx.h console/sms.h \
          console/snes.h console/swan.h \
          backup/cd64.h backup/dex.h backup/doctor64.h backup/doctor64jr.h \
          backup/f2a.h backup/fal.h backup/ffe.h backup/fig.h backup/gbx.h \
          backup/gd.h backup/interceptor.h backup/lynxit.h backup/mccl.h \
          backup/mcd.h backup/md-pro.h backup/mgd.h backup/msg.h \
          backup/pce-pro.h backup/pl.h backup/smc.h backup/smd.h \
          backup/smsgg-pro.h backup/ssc.h backup/swc.h backup/tototek.h \
          backup/ufo.h backup/yoko.h backup/z64.h \
          patch/aps.h patch/bsl.h patch/gg.h patch/ips.h patch/pal4u.h \
          patch/ppf.h patch/xps.h $(UCON64_STD_H)
ucon64_dat.o: ucon64_dat.h $(UCON64_STD_H)
ucon64_misc.o: misc/dlopen.h $(UCON64_STD_H)
ucon64_opts.o: misc/dlopen.h misc/getopt.h ucon64_dat.h ucon64_opts.h \
               console/dc.h console/gb.h console/gba.h console/genesis.h \
               console/jaguar.h console/lynx.h console/n64.h console/neogeo.h \
               console/nes.h console/ngp.h console/pce.h console/psx.h \
               console/sms.h console/snes.h console/swan.h \
               backup/cd64.h backup/dex.h backup/doctor64.h \
               backup/doctor64jr.h backup/f2a.h backup/fal.h backup/ffe.h \
               backup/fig.h backup/gbx.h backup/gd.h backup/interceptor.h \
               backup/lynxit.h backup/mccl.h backup/mcd.h backup/md-pro.h \
               backup/mgd.h backup/msg.h backup/pce-pro.h backup/pl.h \
               backup/smc.h backup/smd.h backup/smsgg-pro.h backup/ssc.h \
               backup/swc.h backup/tototek.h backup/ufo.h backup/yoko.h \
               backup/z64.h \
               patch/aps.h patch/bsl.h patch/gg.h patch/ips.h patch/pal4u.h \
               patch/ppf.h patch/xps.h $(UCON64_STD_H)
console/dc.o: console/dc.h $(UCON64_STD_H)
console/gb.o: console/gb.h backup/mgd.h $(UCON64_STD_H)
console/gba.o: console/gba.h $(UCON64_STD_H)
console/genesis.o: console/genesis.h backup/smd.h backup/mgd.h $(UCON64_STD_H)
console/jaguar.o: console/jaguar.h $(UCON64_STD_H)
console/lynx.o: console/lynx.h $(UCON64_STD_H)
console/n64.o: console/n64.h $(UCON64_STD_H)
console/neogeo.o: console/neogeo.h $(UCON64_STD_H)
console/nes.o: console/nes.h $(UCON64_STD_H)
console/ngp.o: console/ngp.h $(UCON64_STD_H)
console/pce.o: console/pce.h backup/mgd.h $(UCON64_STD_H)
console/psx.o: console/psx.h $(UCON64_STD_H)
console/sms.o: console/sms.h backup/smd.h backup/mgd.h $(UCON64_STD_H)
console/snes.o: console/snes.h backup/mgd.h $(UCON64_STD_H)
console/swan.o: console/swan.h $(UCON64_STD_H)
backup/cd64.o: backup/cd64.h $(UCON64_STD_H)
backup/cmc.o: backup/cmc.h $(UCON64_STD_H)
backup/dex.o: backup/dex.h backup/psxpblib.h $(UCON64_STD_H)
backup/doctor64.o: backup/doctor64.h $(UCON64_STD_H)
backup/doctor64jr.o: backup/doctor64jr.h $(UCON64_STD_H)
backup/f2a.o: backup/f2a.h $(UCON64_STD_H)
backup/fal.o: backup/fal.h backup/cartlib.c $(UCON64_STD_H)
backup/ffe.o: backup/ffe.h $(UCON64_STD_H)
backup/fig.o: backup/fig.h console/snes.h $(UCON64_STD_H)
backup/gd.o: backup/gd.h console/snes.h $(UCON64_STD_H)
backup/gbx.o: backup/gbx.h $(UCON64_STD_H)
backup/lynxit.o: backup/lynxit.h $(UCON64_STD_H)
backup/mccl.o: backup/mccl.h $(UCON64_STD_H)
backup/mcd.o: backup/mcd.h $(UCON64_STD_H)
backup/md-pro.o: backup/md-pro.h backup/tototek.h $(UCON64_STD_H)
backup/mgd.o: backup/mgd.h $(UCON64_STD_H)
backup/msg.o: backup/msg.h backup/ffe.h $(UCON64_STD_H)
backup/pce-pro.o: backup/pce-pro.h backup/tototek.h $(UCON64_STD_H)
backup/pl.o: backup/pl.h $(UCON64_STD_H)
backup/psxpblib.o: backup/psxpblib.h $(UCON64_STD_H)
backup/sflash.o: backup/sflash.h backup/tototek.h $(UCON64_STD_H)
backup/smc.o: backup/smc.h backup/ffe.h $(UCON64_STD_H)
backup/smd.o: backup/smd.h backup/ffe.h $(UCON64_STD_H)
backup/smsgg-pro.o: backup/smsgg-pro.h backup/tototek.h $(UCON64_STD_H)
backup/swc.o: backup/swc.h backup/ffe.h console/snes.h $(UCON64_STD_H)
backup/tototek.o: backup/tototek.h $(UCON64_STD_H)
backup/ufo.o: backup/ufo.h $(UCON64_STD_H)
backup/yoko.o: backup/yoko.h $(UCON64_STD_H)
backup/z64.o: backup/z64.h $(UCON64_STD_H)
patch/aps.o: patch/aps.h $(UCON64_STD_H)
patch/bsl.o: patch/bsl.h $(UCON64_STD_H)
patch/gg.o: patch/gg.h $(UCON64_STD_H)
patch/ips.o: patch/ips.h $(UCON64_STD_H)
patch/pal4u.o: patch/pal4u.h $(UCON64_STD_H)
patch/ppf.o: patch/ppf.h $(UCON64_STD_H)
patch/xps.o: patch/xps.h $(UCON64_STD_H)
