###############################################################################
#                                                                             #
#   Copyright 2005 University of Cambridge Computer Laboratory.               #
#                                                                             #
#   This file is part of Nprobe.                                              #
#                                                                             #
#   Nprobe is free software; you can redistribute it and/or modify            #
#   it under the terms of the GNU General Public License as published by      #
#   the Free Software Foundation; either version 2 of the License, or         #
#   (at your option) any later version.                                       #
#                                                                             #
#   Nprobe is distributed in the hope that it will be useful,                 #
#   but WITHOUT ANY WARRANTY; without even the implied warranty of            #
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             #
#   GNU General Public License for more details.                              #
#                                                                             #
#   You should have received a copy of the GNU General Public License         #
#   along with Nprobe; if not, write to the Free Software                     #
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA #
#                                                                             #
###############################################################################

#
# Where to find various stuff
#

# Archive root
NPROBE_ROOT = ../../..

# Import all relative paths defined at top level
include $(NPROBE_ROOT)/Makefile.paths

###############################################################################

SWIG_DEFINES = -python -shadow

CCOPT = -Dmain=oldmain

INCLUDES = -I. -I.. -I$(NPROBE_DIR) $(PYTHON_INCLUDES) \
	-I$(FILE_CMD_DIR) -I$(FILE_CMD_SRC_DIR)

# Standard CFLAGS
CFLAGS = $(CCOPT) $(DEFINES) $(INCLUDES)

CC = gcc -g -Wall -fpic
#CC = cc -g

AS = as
MAKE = make


.c.o:
	rm -f $@;
	$(CC) $(CFLAGS) -c $*.c

MAGIC_OBJ = $(addprefix $(FILE_CMD_SRC_DIR)/, magic.o apprentice.o softmagic.o ascmagic.o compress.o is_tar.o readelf.o print.o fsmagic.o funcs.o apptype.o)

MAGIC_SRC = $(patsubst %.o,%.c,$(MAGIC_OBJ))

FILE_CMD_HDRS = $(FILE_CMD_DIR)/config.h $(FILE_CMD_SRC_DIR)/file.h \
		$(FILE_CMD_SRC_DIR)/magic.h

MAGIC_LIB = fmagicc.so fmagic.py
MAGIC_LIB_ABS = $(addprefix $(shell pwd)/, $(MAGIC_LIB))
CURR_DIR = shell(pwd)



all: Pymod_fmagic install

file_cmd_config:
	@if [ ! -d $(FILE_CMD_DIR) ]; \
		then \
			tar -xzf $(ANAL_FMAGIC_DIR)/file-$(FILE_CMD_VERS).tar.gz; \
			mv file-$(FILE_CMD_VERS) $(ANAL_FMAGIC_DIR); \
	fi
	@if [ ! -f $(FILE_CMD_DIR)/config.h ]; \
		then \
			cd $(FILE_CMD_DIR) && ./configure; \
	fi

$(FILE_CMD_DIR): 
	@if [ ! -d $(FILE_CMD_DIR) ]; \
		then \
			tar -xzf $(ANAL_FMAGIC_DIR)/file-$(FILE_CMD_VERS).tar.gz; \
			mv file-$(FILE_CMD_VERS) $(ANAL_FMAGIC_DIR); \
	fi

$(FILE_CMD_DIR)/config.h: $(FILE_CMD_DIR)
	@if [ ! -f $(FILE_CMD_DIR)/config.h ]; \
		then \
			cd $(FILE_CMD_DIR) && ./configure; \
	fi

$(MAGIC_OBJ): $(FILE_CMD_DIR)/config.h
	echo foo
	cd $(FILE_CMD_DIR) && make;


fmagic_wrap.o: fmagic.i $(FILE_CMD_HDRS)
	$(SWIG) $(SWIG_DEFINES) -I$(FILE_CMD_SRC_DIR) fmagic.i
	libtool $(CC) $(CFLAGS) -c -DHAVE_CONFIG_H fmagic_wrap.c

Pymod_fmagic: fmagic_wrap.o $(MAGIC_OBJ)
	gcc -shared -o fmagicc.so fmagic_wrap.o $(MAGIC_OBJ)
	ln -sf $(MAGIC_LIB_ABS) $(ANAL_DIR)

links:
	ln -sf $(MAGIC_LIB_ABS) $(ANAL_DIR)

install update_install: Pymod_fmagic
	@cp -uvf $(MAGIC_LIB) $(INSTALL_LIB_DIR)

tidy:
	rm -f *.o

clean very_clean:
	rm -f *.o *.lo *.a *.la fmagic_wrap.c fmagic_wrap.doc fmagic.py \
	fmagicc.so *.pyc 
	rm -rf .libs
	if [ -d $(FILE_CMD_SRC_DIR) ]; \
		then \
			cd $(FILE_CMD_SRC_DIR) && make clean; \
	fi

clean_links:
	rm -f $(addprefix $(ANAL_DIR)/, $(MAGIC_LIB))

clobber: clean clean_links
	rm -f $(addprefix $(INSTALL_LIB_DIR)/, $(MAGIC_LIB))
	rm -rf $(FILE_CMD_DIR)

depend:
	makedepend -- $(DEFINES) -- $(INCLUDES) -- $(CSRC) -- $(SSRC) --$(HDR)

# DO NOT DELETE THIS LINE -- make depend depends on it.
