#  Grafx2 - The Ultimate 256-color bitmap paint program
#  
#  Copyright 2008 Peter Gordon
#  Copyright 2008 Yves Rizoud
#  Copyright 2007 Adrien Destugues
#  Copyright 1996-2001 Sunset Design (Guillaume Dorme & Karl Maritaud)
#  
#  Grafx2 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; version 2
#  of the License.
#  
#  Grafx2 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 Grafx2; if not, see  or
#  write to the Free Software Foundation, Inc.,
#  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
# Overridable defaults
  prefix = /usr/local
  exec_prefix = $(prefix)
  bindir = $(exec_prefix)/bin
  datarootdir = $(prefix)/share
  datadir = $(datarootdir)
  STRIP = strip
### PLATFORM DETECTION AND CONFIGURATION ###
# There is no uname under windows, but we can guess we are there with the COMSPEC env.var
# Windows specific
ifdef COMSPEC
  DELCOMMAND = rm -f
  MKDIR = mkdir -p
  RMDIR = rmdir
  CP = cp
  BIN = grafx2.exe
  COPT = -W -Wall -Wdeclaration-after-statement -O -g -ggdb `sdl-config --cflags` $(TTFCOPT) $(JOYCOPT)
  LOPT = `sdl-config --libs` -lSDL_image $(TTFLOPT) -lpng
  CC = gcc
  OBJDIR = obj/win32
  # Resources (icon)
  WINDRES = windres.exe
  OBJRES = $(OBJDIR)/winres.o
  CFGOBJRES = $(OBJDIR)/wincfgres.o
  PLATFORM = win32
  PLATFORMFILES = SDL.dll SDL_image.dll libpng13.dll zlib1.dll gfx2cfg.ico gfx2.ico $(TTFLIBS) #some misc files we have to add to the release archive under windows.
  ZIP = zip
else
#For all other platforms, we can rely on uname
  PLATFORM = $(shell uname)
  #AmigaOS4 specific
  ifeq ($(PLATFORM),AmigaOS)
    DELCOMMAND = rm -rf
    MKDIR = mkdir -p
    RMDIR = rmdir
    CP = cp
    BIN = grafx2
    COPT = -Wall -c -gstabs -mcrt=newlib `sdl-config --cflags` -D__USE_INLINE__ $(TTFCOPT)
    LOPT = `sdl-config --libs` -lSDL_image -lpng -ljpeg -lz $(TTFLOPT) -lft2
    CC = gcc
    OBJDIR = obj/amiga
    ZIP = lha
    ZIPOPT = a
  else ifeq ($(PLATFORM),AROS)
  #AROS specific
    DELCOMMAND = rm -rf
    MKDIR = mkdir -p
    RMDIR = rmdir
    CP = cp
    BIN = grafx2
    COPT = -Wall -g `sdl-config --cflags` $(TTFCOPT)
    LOPT = -lSDL_image `sdl-config --libs` -lpng -ljpeg -lz $(TTFLOPT) -lfreetype2shared
    CC = gcc
    OBJDIR = obj/aros
    ZIP = lha
    ZIPOPT = a
  else ifeq ($(PLATFORM),MorphOS) 
  #MorphOS specific
    DELCOMMAND = rm -rf
    MKDIR = mkdir -p
    RMDIR = rmdir
    CP = cp
    BIN = grafx2
    COPT = -Wall -gstabs -c `sdl-config --cflags` $(TTFCOPT)
    LOPT = -lSDL_image `sdl-config --libs` -lpng -ljpeg -lz $(TTFLOPT)
    CC = gcc
    OBJDIR = obj/morphos
    ZIP = lha
    ZIPOPT = a
  else ifeq ($(PLATFORM),BeOS)
  #BeOS specific
    DELCOMMAND = rm -rf
    MKDIR = mkdir -p
    RMDIR = rmdir
    CP = cp
    BIN = grafx2
    COPT = -W -Wall -c -g `sdl-config --cflags` $(TTFCOPT) -I/boot/home/config/include
    LOPT = `sdl-config --libs` -lSDL_image -lpng -ljpeg -lz $(TTFLOPT)
    CC = gcc
    OBJDIR = obj/beos
    ZIP = zip
  else ifeq ($(PLATFORM),Haiku)
  #Haiku specific
    DELCOMMAND = rm -rf
    MKDIR = mkdir -p
    RMDIR = rmdir
    CP = cp
    BIN = grafx2
    COPT = -W -Wall -c -g `sdl-config --cflags` $(TTFCOPT) -I/boot/common/include
    LOPT = `sdl-config --libs` -lSDL_image -lpng -ljpeg -lz $(TTFLOPT)
    CC = gcc
    OBJDIR = obj/haiku
    ZIP = zip
  else ifeq ($(PLATFORM),skyos)
  #SkyOS specific
    DELCOMMAND = rm -rf
    MKDIR = mkdir -p
    RMDIR = rmdir
    CP = cp
    BIN = grafx2
    COPT = -W -Wall -Wdeclaration-after-statement -c -g `sdl-config --cflags` $(TTFCOPT)
    LOPT = `sdl-config --libs` -lSDL_image -lpng -ljpeg -lz $(TTFLOPT)
    CC = gcc
    OBJDIR = obj/skyos
    ZIP = zip
  else
  
# If we got here, we haven't found any specific system for uname. There is a little problem : Amiga 68k does not have uname !
    # The trick here is to use the "version" command and see if the answer starts with Kickstart. This is the typical footprint of an amiga system.
    # Te problem is these commands are also launched for other platforms where the "version" command is not available, so sh gives an error under linux/freebsd...
    $(shell version >PIPE:gfx2ver)
    AMIGA=$(shell sed -e s/\(Kickstart\).*/\1/g  version.c
version : delversion version.c $(OBJDIR)/version.o
delversion :
	$(DELCOMMAND) version.c
$(OBJDIR)/%.o :
	$(if $(wildcard $(OBJDIR)),,$(MKDIR) $(OBJDIR))
	$(CC) $(COPT) -c $*.c -o $(OBJDIR)/$*.o
depend :
	$(CC) -MM *.c | sed 's:^[^ ]:$$(OBJDIR)/&:' > Makefile.dep
# Link the icons to the program under windows
$(OBJDIR)/winres.o : gfx2.ico
	echo "1 ICON \"gfx2.ico\"" | $(WINDRES) -o $(OBJDIR)/winres.o
clean :
	$(DELCOMMAND) $(OBJ) $(OBJDIR)/version.o $(OBJRES)
	$(DELCOMMAND) $(BIN)
# Linux installation of the program
install : $(BIN)
	echo "#!/bin/sh" > $(bindir)/grafx2
	echo $(datadir)/grafx2/$(BIN) '$$*' >> $(bindir)/grafx2
	chmod 755 $(bindir)/grafx2
	$(if $(wildcard $(datadir)/grafx2),,$(MKDIR) $(datadir)/grafx2)
	$(CP) $(BIN) $(datadir)/grafx2/
	$(CP) gfx2def.ini $(datadir)/grafx2/
	$(CP) gfx2gui.gif $(datadir)/grafx2/
	$(CP) gfx2.gif $(datadir)/grafx2/
	$(CP) gfx2cfg.gif $(datadir)/grafx2/
	$(if $(wildcard $(datadir)/grafx2/fonts),,$(MKDIR) $(datadir)/grafx2/fonts)
	cd fonts && $(CP) * $(datadir)/grafx2/fonts/
	@echo Install complete
  
# Linux uninstallation of the program
uninstall :
	$(DELCOMMAND) $(bindir)/grafx2
	$(DELCOMMAND) $(datadir)/grafx2/$(BIN)
	$(DELCOMMAND) $(datadir)/grafx2/gfx2def.ini
	$(DELCOMMAND) $(datadir)/grafx2/gfx2gui.gif
	$(DELCOMMAND) $(datadir)/grafx2/gfx2.gif
	$(DELCOMMAND) $(datadir)/grafx2/gfx2cfg.gif
	$(DELCOMMAND) $(datadir)/grafx2/fonts/*
	$(if $(wildcard $(datadir)/grafx2/fonts),,$(RMDIR) $(datadir)/grafx2/fonts)
	$(if $(wildcard $(datadir)/grafx2),,$(RMDIR) $(datadir)/grafx2)
	@echo Uninstall complete
-include Makefile.dep