#
## Makefile -- I must learn autoconf one of these days...
## Author          : Julian Peterson <weaver@earthcorp.com>
## Last Modified On: Fri Sep 13 22:27:10 2002
## $Id: Makefile,v 1.4 2003/04/21 14:44:50 sessery Exp $
#

#
# User configurable options
#


# SOUND:
# set sound = USE_SOUND to compile with sound support (requires SDL_mixer), 
# put anything else for no sound
sound            = USE_SOUND
#sound            = DONT_USE_SOUND

# INSTALLATION DIRS:
# binary will be installed to $game_prefix/$game_bin
# data to $game_prefix/$game_data
game_prefix      = /usr/local
game_bin         = bin
game_data        = share/missile
game_icons       = share/pixmaps

# who the owner of the installed game will be
install_as_owner = root
install_as_group = games



########################################################################
# Shouldn't need to play with much below here, unless your compiler, 
#  install program or tar behaves differently to mine

SHELL        = /bin/sh


#  The name of the program, and its components.
#

PROGRAM      = missile
DATA         = data
ICONS        = icons
SOURCES      = missile.c
OBJECTS      = $(SOURCES:.c=.o)

GNOME_DATA   = `gnome-config --datadir`
KDE_DATA     = `kde-config --expandvars --install apps`

# for packaging
DIST_NAME    = $(PROGRAM)
DIST_VER     = 1.0.1
DIST_DIR     = $(PROGRAM)-$(DIST_VER)
DIST_RELEASE = 1


DIST_FILES_COMMON = README INSTALL COPYING.GPL Makefile Makefile.zaurus missile.desktop missile.kdelnk
DIST_FILES_SOURCE = missile.c missile.h
DIST_FILES_BIN    = $(PROGRAM)
DIST_FILES_DIRS   = $(DATA) $(ICONS)

#experiments with cross compilers
#gcc -o missile.exe missile.c `sdl-config --cflags` `sdl-config --libs` -lSDL_image -lpng -lz


#
#  Options for the C compiler and make:
#
#CC       = ccmalloc gcc
CC       =  gcc
OPTS     = $(RPM_OPT_FLAGS) -DDATA=\"$(game_prefix)/$(game_data)\" -DVERSION=\"$(DIST_VER)\" -D$(sound)
CFLAGS   = -Wall -O2 $(OPTS) `sdl-config --cflags`
#CFLAGS   = -Wall -g `sdl-config --cflags` 
LIBS     = `sdl-config --libs` -lSDL_image $(SND_LIBS)
INCLUDE  =
MAKE     = make

ARCH         = i386



ifeq ($(sound), USE_SOUND)
SND_LIBS = -lSDL_mixer
endif


# What am I missing?
STATIC_LIBS  = /usr/lib/libSDLmain.a /usr/lib/libSDL.a /usr/lib/libSDL_image.a -lSDLx11 -lICE -lPEX5 -lSM -lX11 -lXIE -lXaw -lXext -lXi -lXmu -lXp -lXt -lXtst -lICE -lesd -lpthread -lpng -ljpeg -ldl -lm -lz

#-lICE -lPEX5 -lSM -lX11 -lXIE -lXaw -lXext -lXi -lXmu -lXp -lXt -lXtst



#
#  Options for tar
#
TAR             = tar
TAR_FLAGS       = -cvz

#
#  Options for install
#
INSTALL   = install
EXEC_PERM = -m 755
FILE_PERM = -m 644
IFLAGS    = -D -o $(install_as_owner) -g $(install_as_group)

# other tools used. (make "STRIP = ls" or some no-op if you don't have it)
SED     = sed
STRIP   = strip



#
#  Remake the program, by default:
#
#all: $(PROGRAM)
all:
	@if [ ! -f missile.c ]; then \
	  $(MAKE) install; \
	else \
	  $(MAKE) $(PROGRAM); \
	fi

$(PROGRAM):	$(OBJECTS)
	$(CC) -o $(PROGRAM) $(OBJECTS) $(CFLAGS) $(LIBS)

static : $(OBJECTS)
	$(CC) -o $(PROGRAM) $(OBJECTS) $(CFLAGS) $(STATIC_LIBS)


%.o : %.c %.h
	$(CC) $(CFLAGS) $(OPTS) -c $< $(INCLUDE)


%.o : %.c
	$(CC) $(CFLAGS) -c $< $(INCLUDE)



install :
	@$(STRIP) $(PROGRAM) || \
    (echo; echo "Please run \"make\" first to build the program"; echo; exit -1)
	$(INSTALL) $(IFLAGS) $(EXEC_PERM) $(PROGRAM) $(game_prefix)/$(game_bin)/$(PROGRAM)
	cd $(DATA);\
	for f in `find . -type f`; do \
	  $(INSTALL) $(IFLAGS) $(FILE_PERM) $$f $(game_prefix)/$(game_data)/$$f;\
	done
	cd $(ICONS);\
	for f in `ls`; do \
	  $(INSTALL) $(IFLAGS) $(FILE_PERM) $$f $(game_prefix)/$(game_icons)/$$f;\
	done
#	$(MAKE) create_etc_conf
	@echo ""
	@echo "---------------------------------------------------------------------"
	@echo "Installation complete."
	@echo "---------------------------------------------------------------------"

#create_etc_conf:
#	if [ -w /etc ]; then \
#	  echo "Data=$(game_prefix)/$(game_data)" > /etc/missilecommand.conf; \
#	fi

# I need the right way to do this...
install_to_gnome_menu:
	if [ -w $(GNOME_DATA)/gnome/apps/Games/ ]; then \
	  $(INSTALL) $(IFLAGS) $(FILE_PERM) missile.desktop \
        $(GNOME_DATA)/gnome/apps/Games/; \
	fi

# and this...
install_to_kde_menu:
	if [ -w $(KDE_DATA)/Games/ ]; then \
	  $(INSTALL) $(IFLAGS) $(FILE_PERM) missile.kdelnk $(KDE_DATA)/Games/;\
	fi



uninstall :
	- rm $(game_prefix)/$(game_bin)/$(PROGRAM)
	- rm -rf $(game_prefix)/$(game_data)
	- rm $(game_prefix)/$(game_icons)/missile_icon_{black,blue,white,green,red,yellow}.png
	@echo ""
	@echo "---------------------------------------------------------------------"
	@echo "$(PROGRAM) and associated files uninstalled."
	@echo "User preferences are left in ~/.missilecommandrc"
	@echo "---------------------------------------------------------------------"


dist: nuke
	mkdir $(DIST_DIR)
	cp $(DIST_FILES_SOURCE) $(DIST_DIR)
	cp $(DIST_FILES_COMMON) $(DIST_DIR)
	cp -r $(DIST_FILES_DIRS) $(DIST_DIR)
	find $(DIST_DIR) -type d -name CVS > excludes
	$(TAR) $(TAR_FLAGS)	-X excludes -f $(DIST_NAME)-$(DIST_VER).tar.gz $(DIST_DIR)


bindist: nuke
	$(MAKE) all
	$(MAKE) clean
	$(STRIP) $(PROGRAM)
	mkdir $(DIST_DIR)
	cp $(DIST_FILES_BIN) $(DIST_DIR)
	cp $(DIST_FILES_COMMON) $(DIST_DIR)
	cp -r $(DIST_FILES_DIRS) $(DIST_DIR)
	find $(DIST_DIR) -type d -name CVS > excludes
	$(TAR) $(TAR_FLAGS)	-X excludes -f $(DIST_NAME)-$(DIST_VER).$(ARCH).tar.gz $(DIST_DIR)


spec:
	@cd rpm_spec_files;                                 \
	cat $(DIST_NAME).spec |                               \
	$(SED) "s/__VERSION_NUMBER__/$(DIST_VER)/" |        \
	$(SED) "s/__VERSION_RELEASE__/$(DIST_RELEASE)/" >   \
  $(DIST_NAME)-$(DIST_VER)-$(DIST_RELEASE).spec
	@echo "spec file written to rpm_spec_files/$(DIST_NAME)-$(DIST_VER)-$(DIST_RELEASE).spec"

rpm:
	$(MAKE) dist
	$(MAKE) spec
	mv rpm_spec_files/$(DIST_NAME)-$(DIST_VER)-$(DIST_RELEASE).spec /usr/src/redhat/SPECS
	cp $(DIST_NAME)-$(DIST_VER).tar.gz /usr/src/redhat/SOURCES
	cd /usr/src/redhat/SPECS; \
	rpm -ba $(DIST_NAME)-$(DIST_VER)-$(DIST_RELEASE).spec
	mv /usr/src/redhat/SRPMS/$(DIST_NAME)-$(DIST_VER)-$(DIST_RELEASE).src.rpm .
	mv /usr/src/redhat/RPMS/$(ARCH)/$(DIST_NAME)-$(DIST_VER)-$(DIST_RELEASE).$(ARCH).rpm .



clean:
	- rm -f *.o core `find . -name "*~"` missilecommand.conf

nuke: clean
	- rm -f $(PROGRAM)
	- rm gdb-transcript .kdbgrc.missile gmon.out
	- rm -r $(PROGRAM)-$(DIST_VER) excludes

pristine: nuke
	- rm $(DIST_NAME)-$(DIST_VER).$(ARCH).tar.gz 
	- rm $(DIST_NAME)-$(DIST_VER).tar.gz
	- rm $(DIST_NAME)-$(DIST_VER)-$(DIST_RELEASE).$(ARCH).rpm 
	- rm $(DIST_NAME)-$(DIST_VER)-$(DIST_RELEASE).src.rpm
