#
## Makefile -- I must learn autoconf one of these days...
## Author          : Julian Peterson <weaver@earthcorp.com>
## Last Modified On: Fri Jun 30 14:17:09 2000
## $Id: Makefile,v 0.98 2000/04/12 20:25:23 julian Exp julian $
#

# 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


# for packaging
dist_name      	 = $(PROGRAM)
dist_dir       	 = $(PROGRAM)
dist_ver       	 = 0.99.7
dist_release   	 = 1


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


#  The name of the program, its components, and the locations of include
#  files and libraries:
#

PROGRAM      = missile
DATA         = data
ICONS        = icons
SOURCES      = missile.c
OBJECTS      = $(SOURCES:.c=.o)
LIBS         = `sdl-config --libs` -lSDL_image
INCLUDE      =

GNOME_DATA   = `gnome-config --datadir`
KDE_DATA     =

ARCH         = i386

# 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


#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      = gcc
OPTS    = -DDATA=\"$(game_prefix)/$(game_data)\" -DVERSION=\"$(dist_ver)\"
#CFLAGS  = -Wall -O $(OPTS) `sdl-config --cflags` 
CFLAGS  = -Wall -g `sdl-config --cflags` 
SHELL   = /bin/sh
MAKE    = make

#
#  Options for tar
#
TAR             = tar
TAR_FLAGS       = -cvz
TAR_EXCLUDE  = --exclude $(dist_dir)/RCS --exclude $(dist_dir)/scrap          \
               --exclude $(dist_dir)/rpm_spec_files                           \
               --exclude $(dist_name)-$(dist_ver)-$(dist_release).$(ARCH).rpm \
               --exclude $(dist_name)-$(dist_ver)-$(dist_release).src.rpm     \
               --exclude $(dist_name)-$(dist_ver).$(ARCH).tar.gz              \
               --exclude $(dist_name)-$(dist_ver).src.tar.gz
TAR_BIN_EXCLUDE = --exclude $(dist_dir)/$(PROGRAM).c                          \
                  --exclude $(dist_dir)/$(PROGRAM).h
#
#  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 `ls`; 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 /usr/share/applnk/Games/ ]; then \
	  $(INSTALL) $(IFLAGS) $(FILE_PERM) missile.kdelnk /usr/share/applnk/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
	if [ -w /etc/missilecommand.conf ]; then \
	  rm /etc/missilecommand.conf; \
	fi
	@if [ -w `gnome-config --datadir`/gnome/apps/Games/missile.desktop ]; then \
	  rm `gnome-config --datadir`/gnome/apps/Games/missile.desktop;\
	fi
	@if [ -w /usr/share/applnk/Games/missile.kdelnk ]; then \
	  rm /usr/share/applnk/Games/missile.kdelnk;\
	fi
	@echo ""
	@echo "---------------------------------------------------------------------"
	@echo "$(PROGRAM) and associated files uninstalled."
	@echo "User preferences are left in ~/.missilecommandrc"
	@echo "---------------------------------------------------------------------"



dist: nuke
	cd ..;\
  $(TAR) $(TAR_FLAGS) $(TAR_EXCLUDE) \
		-f $(dist_name)-$(dist_ver).src.tar.gz \
		$(dist_dir); \
		mv $(dist_name)-$(dist_ver).src.tar.gz $(dist_dir)


bindist: nuke
	$(MAKE) all
	$(MAKE) clean
	$(STRIP) $(PROGRAM)
	cd ..;\
  $(TAR) $(TAR_FLAGS) $(TAR_EXCLUDE) $(TAR_BIN_EXCLUDE) \
		-f $(dist_name)-$(dist_ver).$(ARCH).tar.gz \
		$(dist_dir); \
		mv $(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).src.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

pristine: nuke
	- rm $(dist_name)-$(dist_ver).$(ARCH).tar.gz 
	- rm $(dist_name)-$(dist_ver).src.tar.gz
	- rm $(dist_name)-$(dist_ver)-$(dist_release).$(ARCH).rpm 
	- rm $(dist_name)-$(dist_ver)-$(dist_release).src.rpm
