#CFLAGS=-Iruby-1.8.2 -DDEBUG
CFLAGS=-Iruby-1.8.2

CURSES=-lncurses
RLIB=ruby-1.8.2/libruby-static.a

OBJ=bin/main.o bin/command.o bin/cmdline.o bin/dirwnd.o bin/menu.o bin/file.o bin/help.o

all: mfiler mattr mkeycode

mfiler: $(RLIB) mlib/mlib.a $(OBJ)
	if [ `uname -s` = "FreeBSD" ] || [ `uname -s` = "CYGWIN_NT-5.0" ] || [ `uname -s` = "CYGWIN_NT-5.1" ]; then \
		g++ -rdynamic -o mfiler $(OBJ) mlib/mlib.a $(CURSES) $(RLIB) -lcrypt; \
	elif [ `uname -s` = "Darwin" ]; then \
		g++ -rdynamic -o mfiler $(OBJ) mlib/mlib.a $(CURSES) $(RLIB); \
	else \
		g++ -rdynamic -o mfiler $(OBJ) mlib/mlib.a $(CURSES) $(RLIB) -ldl -lcrypt; \
	fi

mattr: mattr.cpp mlib/mlib.a
	g++ -o mattr mattr.cpp $(CURSES) $(CFLAGS)

mkeycode: mkeycode.cpp
	g++ -o mkeycode mkeycode.cpp $(CURSES)

$(RLIB): 
	cd ruby-1.8.2 && ./configure && make
   
mlib/mlib.a:
	cd mlib && make

bin/main.o: main.cpp common.h menu.h dirwnd.h 
	g++ -c main.cpp $(CFLAGS)
	mv main.o bin/

bin/command.o: command.cpp common.h menu.h dirwnd.h 
	g++ -c command.cpp $(CFLAGS)
	mv command.o bin/
      
bin/dirwnd.o: dirwnd.cpp common.h menu.h dirwnd.h 
	g++ -c dirwnd.cpp $(CFLAGS)
	mv dirwnd.o bin/
      
bin/cmdline.o: cmdline.cpp common.h menu.h dirwnd.h 
	if [ -e "/dev/ptmx" ]; then g++ -c cmdline.cpp -DUNIX98_PTY $(CFLAGS); else g++ -c cmdline.cpp $(CFLAGS); fi

	mv cmdline.o bin/
      
bin/menu.o: menu.cpp common.h menu.h dirwnd.h 
	g++ -c menu.cpp $(CFLAGS)
	mv menu.o bin/
      
bin/file.o: file.cpp common.h dirwnd.h menu.h 
	g++ -c file.cpp $(CFLAGS)
	mv file.o bin/

bin/help.o: help.cpp common.h
	g++ -c help.cpp $(CFLAGS)
	mv help.o bin/

clean:
	rm -f bin/* mfiler mattr mkeycode mfiler.exe mattr.exe mkeycode.exe
	cd mlib && make clean
