#########################################
# Makefile of Fucd			#
#########################################


CC:=$(shell wx-config --cxx)
CCC:=gcc
CFLAGS:=$(shell wx-config --cxxflags) 
CFLAGS1:=$(shell wx-config --libs)
CCFLAGS:= -O2 -Wall -Wstrict-prototypes
OUTDIR:="/usr/local/bin"


# All object files
COBJS:= atmelmain.o dfu.o
OBJS:= FucdApp.o FucdLayer.o dfu.o atmel.o

usage:
	@echo -e "make clean\tRemove object files and executables"
	@echo -e "make fucd\tBuild X upgrade utility"
	@echo -e "make atmelup\tBuild command line upgrade utility"
	@echo -e "make all\tBuild both utilities"

all: fucd atmelup

fucd: wxconfig $(OBJS: .o=.c)
	@echo -e "Building fucd wxWidgets application..."
	$(CC) -o fucd $(OBJS) $(CFLAGS1) -lusb 

atmelup: $(COBJS)
	@echo -e "Building atmelup command-line tool..."
	$(CCC) $(CCFLAGS) $(COBJS) -lusb -o $@

wxconfig:
	@config_exists=no;\
	for path_dir in `echo $(PATH) | tr : ' '`; do\
	  if test -f $$path_dir/wx-config; then\
	    config_exists=yes;\
	  fi\
	done;\
	if test $$config_exists = 'no'; then\
	  echo 'It seems that the wxWindows library is not installed.';\
	  echo 'wx-config, the script providing information about wxWindows';\
	  echo 'installation, is missing. Please, refer to the README.linux';\
	  echo 'or visit www.wxwindows.org for further information.';\
	  echo 'We are sorry about the inconvenience.';\
	  exit -1;\
	fi

FucdApp.o: FucdApp.cpp FucdApp.h
	$(CC) $(CFLAGS) -c $<

FucdLayer.o: FucdLayer.cpp FucdLayer.h 
	$(CC) $(CFLAGS) -c $<

atmel.o: atmel.c 
	$(CCC) $(CCFLAGS) -c $< 

dfu.o: dfu.c dfu.h
	$(CCC) $(CCFLAGS) -c $<

atmelmain.o: atmel.c	
	$(CCC) $(CCFLAGS) -DCONSOLE_APP -c $< -o $@

clean:
	@echo -e "Removing all objects and executables..."
	@find . -name '*.o' | xargs rm -f *.o
	@rm -f fucd atmelup


install:
	@echo -e "Installing fucd to $(OUTDIR)"
	@if [ -e fucd ]; then install -m 755 fucd $(OUTDIR) ; fi
	@echo -e "Installing atmelup to $(OUTDIR)"
	@if [ -e atmelup ]; then install -m 755 atmelup $(OUTDIR) ; fi
