PROG = snes9express
VER = 1.26
REL = 2
PV = $(PROG)-$(VER)
PVR = $(PV)-$(REL)
CC = g++

DEPENDS = frend.h defines.h config.h

CFLAGS = -Wall -g -O2 -c
API_CFLAGS = `sh frend-config --cflags`
LIBS = 
API_LIBS = `sh frend-config --libs`

O_FILES1 = frend.o profiler.o joystick.o rom.o sound.o video.o
O_FILES2 = cpu.o netplay.o extra.o prefs.o misc.o interface.o
O_FILES = $(O_FILES1) $(O_FILES2)

all: config.h $(PROG)

$(PROG): Makefile $(O_FILES)
	$(CC) -g $(API_LIBS) $(LIBS) $(O_FILES) -o $(PROG)

run: all
	./$(PROG)

fresh: distclean all

config.h: configure.sh
	sh ./configure.sh $(PROG) $(VER) $(REL)

frend.o: frend.cc frend.h
	$(CC) $(CFLAGS) $(API_CFLAGS) frend.cc -o frend.o

interface.o: interface.cc *.h pix/logo.xpm pix/nordlund.xpm
	$(CC) $(CFLAGS) $(API_CFLAGS) interface.cc -o interface.o

rom.o: rom.cc rom.h $(DEPENDS) pix/rom.xpm pix/bnr.xpm
	$(CC) $(CFLAGS) $(API_CFLAGS) rom.cc -o rom.o

sound.o: sound.cc sound.h $(DEPENDS)
	$(CC) $(CFLAGS) sound.cc -o sound.o

video.o: video.cc video.h $(DEPENDS)
	$(CC) $(CFLAGS) video.cc -o video.o

joystick.o: joystick.cc joystick.h $(DEPENDS) pix/controller.xpm
	$(CC) $(CFLAGS) $(API_CFLAGS) joystick.cc -o joystick.o

cpu.o: cpu.cc cpu.h $(DEPENDS) pix/cpu.xpm
	$(CC) $(CFLAGS) cpu.cc -o cpu.o

netplay.o: netplay.cc netplay.h $(DEPENDS) pix/netplay.xpm
	$(CC) $(CFLAGS) netplay.cc -o netplay.o

extra.o: extra.cc extra.h $(DEPENDS)
	$(CC) $(CFLAGS) extra.cc -o extra.o

prefs.o: prefs.cc prefs.h $(DEPENDS)
	$(CC) $(CFLAGS) prefs.cc -o prefs.o

misc.o: misc.cc misc.h $(DEPENDS)
	$(CC) $(CFLAGS) misc.cc -o misc.o

profiler.o: profiler.cc profiler.h $(DEPENDS)
	$(CC) $(CFLAGS) $(API_CFLAGS) profiler.cc -o profiler.o

clean:
	rm -f $(PROG) $(O_FILES) core

distclean: clean
	rm -f config.h frend-config install.dir $(PROG).spec

install: all
	strip $(PROG)
	sh install.sh --install

uninstall:
	sh install.sh --uninstall

$(PROG).spec: rpm.spec.in
	sed -e s/PROG/$(PROG)/g \
	    -e s/VER/$(VER)/g   \
	    -e s/REL/$(REL)/g   \
	    < rpm.spec.in > $(PROG).spec

dist: $(PROG).spec
	rm -f ../$(PVR).src.tar.gz
	if [ -f config.h ]; then mv config.h config.Hbk; fi
	cd .. && tar -cf $(PVR).src.tar $(PV)/[A-Z]* $(PV)/pix/*.xpm $(PV)/*.[cshi]*
	if [ -f config.Hbk ]; then mv config.Hbk config.h; fi
	gzip ../$(PVR).src.tar

rpm: dist
	rpm -ta ../$(PVR).src.tar.gz
	@echo
	@echo "Packages have been made, and are ...where ever RPM keeps them"
	@echo "on your system"
