# Makefile  MAKEFILE for BYTER     (Analysis program for VORTEX)
#  To run, use '/usr/bin/make'

#  (Currently, 'make' calls a local executable shell script which
#   calls make for both byter and byterps, specifying the user name.)

SAVE_DIR = `date +%F`

OBJECTS = auto.o arm.o calc.o calib.o chg.o cross.o data.o diff.o digi.o \
	do_cmd.o file.o filt.o gdata.o gelse.o gspike.o head.o help.o lib.o \
	load.o  main.o paint.o read.o reg.o repeat.o sac.o stack.o \
	ttest.o userio.o

A_MACS = do_amac.o vestibmac.o Vestibmac.o jeffmac.o namemac.o \
	sortvor.o stimsort.o Memorymac.o sortmem.o sortlat.o \
	sortfrontal.o sorteyefrontal.o sorteyeallfrontal.o 

L_MACS = do_lmac.o vestibmac.o Vestibmac.o jeffmac.o namemac.o \
	sort.o stimsort.o cuesort.o Memorymac.o yuqingmac.o Framesmac.o \
	RewardSort.o rsvpsort.o TwoArmsmac.o cunguosort.o ReferenceSort.o \
	kubaneksort.o harrysort.o harrysimple.o gainfield.o printmac.o

S_MACS = do_smac.o vestibmac.o Vestibmac.o jeffmac.o namemac.o \
	sortvor.o stimsort.o TwoArmsmac.o ReferenceSort.o Memorymac.o 

G_MACS = do_gmac.o switch_interference_macro.o taskeffect_macro.o \
	interference.o trial_macro.o raster_macro.o bytergrab_macro.o \
	task_interference_direction_subtract.o \
	task.pref_nonpref_trials.direction.subtract.o direction_subtract.o \
	find_half_peak.o SwitchRepeat_macro.o \
	halfpeak_directional_con_inc_macro.o

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

HOSTNAME = $(shell hostname)

ifeq ($(HOSTNAME),cognition)
  DEFAULT_LIB = -lxpl.64
  DEFAULT_LIB = -lxpl
  PS_LIB = -lps.64
else
 DEFAULT_LIB =  -lxpl			# -lps or -lxpl or -lidraw
 PS_LIB = -lps
endif

# LIBS = -lm -lplot -lX11
LIBS = -lm -lX11

#   10-2012: xpl library uses math and plot, so it must preceed these!
#   Added -lplot 1-2010  -- that made it work!  Removed all these:
#		-lXaw -lXt -lXext -lXmu -lX11       # cannot find them!
#   Added X11 back in 10-2012

#				-lposix4	# posix4:nanosleep


.KEEP_STATE:				# Dependencies of main code on itself
					# (Won't see dependence on macro code!)
.SILENT:				# Run silently

FULL_OBJECTS = $(OBJECTS:%=_objects/%)

# SOME DAY MIGHT WANT TO USE THIS: (not yet complete, just gives 1st letter)
#    MAC_DIRECTORY = $(shell echo $USER | sed --quiet 's/^\(.\).*/\1/p' )

ifeq ($(USER),stoet)
   FULL_MACS  = $(G_MACS:%=_objects/_macs/%)
else
   ifeq ($(USER),steve)
      FULL_MACS  = $(S_MACS:%=_objects/_macs/%)
   else
      ifeq ($(USER), unused)
         FULL_MACS  = $(A_MACS:%=_objects/_macs/%)
      else
	 FULL_MACS  = $(L_MACS:%=_objects/_macs/%)
      endif
   endif
endif

ALL_OBJECTS = $(FULL_OBJECTS) $(FULL_MACS)

################################################################################
HOST := $(shell hostname)
WAS :=  $(shell cat .host)

SHELL = /bin/tcsh
BIN := $(shell ls -d --color=none /home/$(USER)/{cmds,bin} |& grep -v No)

CC := gcc

ifeq ($(HOST),conception)
   CFLAGS := -O3 -Wall -Wextra -Wno-unused-result -fno-strict-aliasing -Wno-unused-local-typedefs -Wno-format-contains-nul -Wno-implicit-fallthrough
else
   CFLAGS := -O3 -Wall -Wextra
endif
NO_WARN := -O3 -w
LDFLAGS = -L/data/code/byter/washu/_libs -L/usr/X11R6/lib -L/usr/lib -L/usr/local/lib $(LIBS)


#  -O3   : high level of optimization
#  -Wall : show 'all' warnings (a pain, but like lint, can be a help!)
#  -w    : no warnings
#  -Wextra : show even more warnings
#  -Wno-unused-result: don't complain about system() etc calls that
#         don't check their return value
#  NO_WARN : optional CFLAGS for buggy macros!
#   

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

start_here:
ifeq ($(HOST), $(WAS))
else
	@make clean
endif
	@make --no-print-directory all

all:
	@/usr/bin/make ps
	@/usr/bin/make regular
	hostname >! .host

regular:	$(ALL_OBJECTS)
	@$(CC) -o pup $(CFLAGS) $(ALL_OBJECTS) $(DEFAULT_LIB) $(LDFLAGS)
	@strip pup
	@mv pup $(BIN)/byter

ps:	$(ALL_OBJECTS)
	@$(CC) -o pup $(CFLAGS) $(ALL_OBJECTS) $(LDFLAGS) $(PS_LIB)
	@strip pup
	@mv pup $(BIN)/byterps

x:	$(ALL_OBJECTS)
	@$(CC) -o pup $(CFLAGS) $(ALL_OBJECTS) $(LDFLAGS) -lxpl
	@strip pup
	@mv pup $(BIN)/byterxpl

clean:	
	rm -f _objects/*.o _objects/_macs/*.o

ifeq ($(USER),stoet)
_objects/_macs/%.o : _gmacs/%.c
	$(COMPILE.c) $<
	@mv $(<:_gmacs/%.c=%.o) _objects/_macs
else
   ifeq ($(USER),steve)
_objects/_macs/%.o : _smacs/%.c
	$(COMPILE.c) $<
	@mv $(<:_smacs/%.c=%.o) _objects/_macs
   else
   ifeq ($(USER),adam)
_objects/_macs/%.o : _amacs/%.c
	$(COMPILE.c) $<
	@mv $(<:_amacs/%.c=%.o) _objects/_macs
   else
_objects/_macs/%.o : _lmacs/%.c
	$(COMPILE.c) $<
	@mv $(<:_lmacs/%.c=%.o) _objects/_macs
   endif
   endif
endif

_objects/%.o : %.c
	$(COMPILE.c) $<
	@mv $(<:%.c=%.o) _objects

save:
	-mkdir _saves/$(SAVE_DIR)
	cp -pr _?macs _filters _imports _libs [a-z]* _plot Makefile _saves/$(SAVE_DIR)

##############################################################
 _objects/arm.o: arm.c defs.h termdef.h functions.h
 _objects/auto.o: auto.c defs.h termdef.h functions.h
 _objects/calc.o: calc.c defs.h termdef.h functions.h array.h
 _objects/chg.o: chg.c defs.h termdef.h functions.h graph.h _plot/plot.h \
  _plot/pl/plot.h
 _objects/cross.o: cross.c cross.h defs.h termdef.h functions.h
 _objects/data.o: data.c defs.h termdef.h functions.h array.h
 _objects/diff.o: diff.c array.h defs.h termdef.h functions.h
 _objects/digi.o: digi.c defs.h termdef.h functions.h cross.h graph.h \
  _plot/plot.h _plot/pl/plot.h
 _objects/do_cmd.o: do_cmd.c defs.h termdef.h functions.h cross.h sac.h
 _objects/file.o: file.c defs.h termdef.h functions.h array.h
 _objects/filt.o: filt.c array.h defs.h termdef.h functions.h
 _objects/gdata.o: gdata.c array.h defs.h termdef.h functions.h graph.h \
  _plot/plot.h _plot/pl/plot.h
 _objects/gelse.o: gelse.c _imports/event.h graph.h defs.h termdef.h functions.h \
  _plot/plot.h _plot/pl/plot.h _imports/deffs.h
 _objects/gspike.o: gspike.c array.h defs.h termdef.h functions.h graph.h \
  _plot/plot.h _plot/pl/plot.h
 _objects/head.o: head.c defs.h termdef.h functions.h stack.h _imports/deffs.h \
  _imports/event.h _imports/head.h
 _objects/help.o: help.c defs.h termdef.h functions.h
 _objects/lib.o: lib.c defs.h termdef.h functions.h
 _objects/load.o: load.c defs.h termdef.h functions.h array.h version.h \
  _imports/deffs.h eyecal.h
 _objects/main.o: main.c defs.h termdef.h functions.h cross.h
 _objects/paint.o: paint.c defs.h termdef.h functions.h cross.h graph.h \
  _plot/plot.h _plot/pl/plot.h
 _objects/read.o: read.c defs.h termdef.h functions.h
 _objects/reg.o: reg.c array.h defs.h termdef.h functions.h
 _objects/repeat.o: repeat.c
 _objects/sac.o: sac.c defs.h termdef.h functions.h array.h sac.h
 _objects/stack.o: stack.c array.h defs.h termdef.h functions.h stack.h \
  _imports/deffs.h _imports/event.h _imports/event.i
 _objects/ttest.o: functions.h
 _objects/userio.o: userio.c defs.h termdef.h functions.h
 _objects/_lmacs/do_lmac.o: _lmacs/do_lmac.c defs.h termdef.h functions.h
 _objects/_gmacs/do_gmac.o: _gmacs/do_gmac.c defs.h termdef.h functions.h
 _objects/_smacs/do_smac.o: _smacs/do_smac.c defs.h termdef.h functions.h
 _objects/_gmacs/switch_interference_macro.o: _gmacs/switch_interference_macro.c \
   defs.h termdef.h functions.h array.h _imports/deffs.h _imports/event.h
 _objects/_gmacs/interference.o: _gmacs/interference.c \
   defs.h termdef.h functions.h array.h _imports/deffs.h _imports/event.h
 _objects/_gmacs/taskeffect_macro.o: _gmacs/taskeffect_macro.c \
   defs.h termdef.h functions.h array.h _imports/deffs.h _imports/event.h
 _objects/_gmacs/SwitchRepeat_macro.o: _gmacs/SwitchRepeat_macro.c \
   defs.h termdef.h functions.h array.h _imports/deffs.h _imports/event.h
 _objects/_lmacs/jeffmac.o: _lmacs/jeffmac.c defs.h termdef.h \
  functions.h array.h _imports/deffs.h _imports/event.h
 _objects/_lmacs/TwoArmsmac.o: _lmacs/TwoArmsmac.c defs.h termdef.h \
  functions.h array.h _imports/deffs.h _imports/event.h
 _objects/_lmacs/ReferenceSort.o: _lmacs/ReferenceSort.c defs.h termdef.h \
  functions.h array.h _imports/deffs.h _imports/event.h
 _objects/_lmacs/RewardSort.o: _lmacs/RewardSort.c defs.h termdef.h \
  functions.h array.h _imports/deffs.h _imports/event.h
 _objects/_lmacs/yuqingmac.o: _lmacs/yuqingmac.c defs.h termdef.h \
  functions.h array.h _imports/deffs.h _imports/event.h
 _objects/_smacs/TwoArmsmac.o: _smacs/TwoArmsmac.c defs.h termdef.h \
  functions.h array.h _imports/deffs.h _imports/event.h
 _objects/_smacs/ReferenceSort.o: _lmacs/ReferenceSort.c defs.h termdef.h \
  functions.h array.h _imports/deffs.h _imports/event.h
 _objects/_lmacs/namemac.o: _lmacs/namemac.c defs.h termdef.h \
  functions.h array.h _imports/deffs.h _imports/event.h
 _objects/_lmacs/vestibmac.o: _lmacs/do_lmac.c defs.h termdef.h \
  functions.h array.h _imports/deffs.h _imports/event.h
 _objects/_lmacs/Vestibmac.o: _lmacs/do_lmac.c defs.h termdef.h \
  functions.h array.h _imports/deffs.h _imports/event.h
 _objects/_lmacs/Framesmac.o: _lmacs/do_lmac.c defs.h termdef.h \
  functions.h array.h _imports/deffs.h _imports/event.h
 _objects/_lmacs/Memorymac.o: _lmacs/do_lmac.c defs.h termdef.h \
  functions.h array.h _imports/deffs.h _imports/event.h
 _objects/_lmacs/stimsort.o: _lmacs/do_lmac.c defs.h termdef.h \
  functions.h array.h _imports/deffs.h _imports/event.h
 _objects/_lmacs/rsvpsort.o: _lmacs/do_lmac.c defs.h termdef.h \
  functions.h array.h _imports/deffs.h _imports/event.h
 _objects/_lmacs/cuesort.o: _lmacs/do_lmac.c defs.h termdef.h \
  functions.h array.h _imports/deffs.h _imports/event.h
 _objects/_lmacs/cunguosort.o: _lmacs/do_lmac.c defs.h termdef.h \
  functions.h array.h _imports/deffs.h _imports/event.h
 _objects/_lmacs/printmac.o: _lmacs/do_lmac.c defs.h termdef.h \
  functions.h array.h _imports/deffs.h _imports/event.h
 _objects/_lmacs/kubaneksort.o: _lmacs/do_lmac.c defs.h termdef.h \
  functions.h array.h _imports/deffs.h _imports/event.h
