#
# Additional compilation flags may be passed via CFLAGS, CXXFLAGS and CLINKFLAGS
#
# Linux:
#   make CFLAGS=-m32
#   make CFLAGS=-m64
#
# Ubuntu 9.04 64-bit
#   make CFLAGS=-m32 CXXFLAGS=-m32 CLINKFLAGS="-L/lib32 -L/usr/lib32 -Wl,-rpath-link,/lib32:/usr/lib32"
#
# OSX:
#   make 'CFLAGS=-arch ppc'    'CXXFLAGS=-arch ppc'
#   make 'CFLAGS=-arch i386'   'CXXFLAGS=-arch i386'
#   make 'CFLAGS=-arch x86_64' 'CXXFLAGS=-arch x86_64'

UNAME := $(shell uname)

GLEW  := ../../glew

C_SOURCE   := gs_md2bump.c gli_convert.c gli_merge.c gli_misc.c gli_scale.c gli_tga.c loadtex.c md2file.c md2normals.c normalmap.c matrix.c
CPP_SOURCE := md2render.cpp
OBJ        := $(C_SOURCE:.c=.o) $(CPP_SOURCE:.cpp=.o) 
BIN        := gs_md2bump

CC         =  gcc
CXX        =  g++
C_FLAGS    += -Wall
CPP_FLAGS  += -Wall
C_FLAGS    += -I"$(GLEW)/include"
C_FLAGS    += -DGLEW_STATIC
CPP_FLAGS  += -I"$(GLEW)/include"
CPP_FLAGS  += -DGLEW_STATIC

ifdef CG_INC_PATH
  C_FLAGS  += -I"$(CG_INC_PATH)"
  CPP_FLAGS+= -I"$(CG_INC_PATH)"
endif

ifdef CG_LIB_PATH
    LINKFLAGS  += -L"$(CG_LIB_PATH)"
endif

ifeq ($(UNAME), Darwin)
  LINKFLAGS  += -L"$(GLEW)"
  LINKFLAGS  += -lglewlib
  LINKFLAGS  += -framework Cg
  LINKFLAGS  += -framework GLUT
  LINKFLAGS  += -framework OpenGL
else
  ifeq ($(findstring CYGWIN, $(UNAME)), CYGWIN)
    BIN = gs_md2bump.exe
    C_FLAGS    += -D_WIN32
    CPP_FLAGS  += -D_WIN32
    C_FLAGS    += -DGLUT_DISABLE_ATEXIT_HACK
    CPP_FLAGS  += -DGLUT_DISABLE_ATEXIT_HACK

    C_FLAGS    += -I"C:\Program Files\NVIDIA Corporation\Cg\include"
    CPP_FLAGS  += -I"C:\Program Files\NVIDIA Corporation\Cg\include"
    C_FLAGS    += -DUSE_OPENGL32
    CPP_FLAGS  += -DUSE_OPENGL32
    LINKFLAGS  += -L"$(GLEW)"
    LINKFLAGS  += -lglewlib
    LINKFLAGS  += -L"C:\Program Files\NVIDIA Corporation\Cg\lib"
    LINKFLAGS  += -lcgGL -lcg
    LINKFLAGS  += -lglut32
    LINKFLAGS  += -lglu32 -lopengl32 -lgdi32
  else
    ifeq ($(UNAME), SunOS)
      C_FLAGS    += -fPIC
      CPP_FLAGS  += -fPIC
      LINKFLAGS  += -L"$(GLEW)"
      LINKFLAGS  += -lglewlib
      LINKFLAGS  += -lCgGL -lCg
      LINKFLAGS  += -L/usr/X11R6/lib64 -L/usr/X11R6/lib
      LINKFLAGS  += -lglut -lXi -lXmu -lX11 -lm
      LINKFLAGS  += -lGLU -lGL
      LINKFLAGS  += -lpthread
    else
      LINKFLAGS  += -L"$(GLEW)"
      LINKFLAGS  += -lglewlib
      LINKFLAGS  += -lCgGL -lCg
      LINKFLAGS  += -L/usr/X11R6/lib64 -L/usr/X11R6/lib
      LINKFLAGS  += -lglut -lXi -lXmu -lX11 -lm
      LINKFLAGS  += -lGLU -lGL
      LINKFLAGS  += -lpthread
    endif
  endif
endif

override CFLAGS := $(CFLAGS) $(C_FLAGS)
override CXXFLAGS := $(CXXFLAGS) $(CPP_FLAGS)

all: gs_md2bump

glew:
	$(MAKE) -C '$(GLEW)' -f Makefile glewlib

.PHONY: glew

gs_md2bump: glew $(OBJ)
	$(CXX) $(CFLAGS) $(OBJ) -o $(BIN) $(LINKFLAGS) $(CLINKFLAGS)

test:
	./$(BIN)

run: all
	./$(BIN)

clean:
	$(RM) $(BIN) $(OBJ) 
	$(MAKE) -C '$(GLEW)' -f Makefile clean

clobber: clean
	$(RM) *.bak *.o *~

.PHONY: all test run clean clobber
