#############################
#############################
# FIXED PART - DO NOT TOUCH #
#############################
#############################

BEEPDIR	=beep
WATCHDIR=watch
STOPWATCHDIR=stopwatch
ALARMDIR=alarm
BUTTONDIR=button
DISPLAYDIR=display
CMAINDIR=cmain
TKMAINDIR=tkmain


STRL=$(WATCHDIR)$(PATHSEP)watch.strl \
     $(STOPWATCHDIR)$(PATHSEP)stopwatch.strl \
     $(ALARMDIR)$(PATHSEP)alarm.strl \
     $(BUTTONDIR)$(PATHSEP)button.strl \
     $(DISPLAYDIR)$(PATHSEP)display.strl \
     wristwatch.strl

STRL_CFLAGS = $(OPT)      \
	      -DLOAD_BEEP \
	      $(SIMUL) \
	      -I. \
	      -I$(BEEPDIR) -I$(WATCHDIR) -I$(STOPWATCHDIR) -I$(ALARMDIR) \
              -I$(BUTTONDIR) -I$(DISPLAYDIR) -I$(CMAINDIR) -I$(TKMAINDIR) \
	      -I$(ESTEREL_DIR)$(PATHSEP)tcltk$(PATHSEP)include

CFLAGS= $(CC_CFLAGS) $(STRL_CFLAGS) $(TK_CFLAGS) $(OTHER_CFLAGS)

OBJECTS= $(BEEPDIR)$(PATHSEP)beep_data$(OBJ) \
	 $(WATCHDIR)$(PATHSEP)watch_data$(OBJ) \
	 $(STOPWATCHDIR)$(PATHSEP)stopwatch_data$(OBJ) \
	 $(ALARMDIR)$(PATHSEP)alarm_data$(OBJ) \
	 $(DISPLAYDIR)$(PATHSEP)display_data$(OBJ)

TKOBJECTS=$(TKMAINDIR)$(PATHSEP)main$(OBJ) \
	  $(TKMAINDIR)$(PATHSEP)tkWatch$(OBJ) \
	  $(TKMAINDIR)$(PATHSEP)watch_script$(OBJ)



all :  tkwatch xeswatch clientserver swatch

swatch : sww$(EXE)
	sww$(EXE) < sww.esi > sww.eso 2>&1
	-diff -w sww.eso sww_ref.eso

tkwatch : tkww.exe
	$(XES) tkww.exe

xeswatch : xww.exe
	$(XES) xww.exe

tkww.exe : wristwatch$(OBJ) $(TKOBJECTS)
	@echo "*** making tkww.exe"
	-$(IFEXIST) SIMUL $(THEN) $(RM) $(OBJECTS) SIMUL
	-$(IFEXIST) SIMULRT $(THEN) $(RM) $(OBJECTS) SIMULRT
	$(MAKE) -f $(MAKEFILE) OPT="$(OPT)" $(OBJECTS)
	$(XES) -o tkww \
	      wristwatch$(OBJ) \
	      $(OBJECTS) \
	      $(TKOBJECTS)


sww$(EXE) : wristwatch_sim$(OBJ)
	@echo "*** making sww"
	-$(IFNOTEXIST) SIMUL $(THEN) $(RM) $(OBJECTS) SIMULRT
	$(MAKE) -f $(MAKEFILE) OPT="$(OPT)" SIMUL=-DSIMUL $(OBJECTS)
	echo simul > SIMUL
	$(LINKER) $(CONSOLE) $(LINKER_FLAGS) \
	      wristwatch_sim$(OBJ) \
	      $(OBJECTS) \
	      $(CSIMUL_A) 

xww.exe : wristwatch_sim$(OBJ)
	@echo "*** making xww.exe"
	-$(IFNOTEXIST) SIMUL $(THEN) $(RM) $(OBJECTS) SIMULRT
	$(MAKE) -f $(MAKEFILE) OPT="$(OPT)" SIMUL=-DSIMUL $(OBJECTS)
	echo simul > SIMUL
	$(XES) -o xww \
	      wristwatch_sim$(OBJ) \
	      $(OBJECTS)

xwwrt.exe : wristwatch_simrt$(OBJ)
	@echo "*** making xwwrt.exe"
	-$(IFNOTEXIST) SIMULRT $(THEN) $(RM) $(OBJECTS) SIMUL
	$(MAKE) -f $(MAKEFILE) OPT="$(OPT)" SIMUL=-DSIMULRT $(OBJECTS)
	echo simulrt > SIMULRT

	$(XES) -o xwwrt \
	      wristwatch_simrt$(OBJ) \
	      $(OBJECTS)

wristwatch.c : $(STRL)
	$(ESTEREL_COMMAND)  $(ESTEREL_FLAGS) $(STRL) -B wristwatch

wristwatch$(OBJ) : wristwatch.c
	$(CC) -c $(CFLAGS) wristwatch.c


wristwatch_sim.c : $(STRL)
	$(ESTEREL_COMMAND)  $(ESTEREL_FLAGS_FOR_SIMUL) \
                            -simul $(STRL) -B wristwatch
	$(MV) wristwatch.c wristwatch_sim.c

wristwatch_sim$(OBJ) : wristwatch_sim.c
	$(CC) -c $(CFLAGS) -DSIMUL wristwatch_sim.c

wristwatch_simrt$(OBJ) : wristwatch_sim.c
	$(CC) -c $(CFLAGS) -DSIMULRT wristwatch_sim.c


$(BEEPDIR)$(PATHSEP)beep_data$(OBJ) : $(BEEPDIR)$(PATHSEP)beep_data.c
	$(CC) -c $(CFLAGS) $(BEEPDIR)$(PATHSEP)beep_data.c

$(WATCHDIR)$(PATHSEP)watch_data$(OBJ) : $(WATCHDIR)$(PATHSEP)watch_data.c
	$(CC) -c $(CFLAGS) $(WATCHDIR)$(PATHSEP)watch_data.c

$(STOPWATCHDIR)$(PATHSEP)stopwatch_data$(OBJ) : $(STOPWATCHDIR)$(PATHSEP)stopwatch_data.c
	$(CC) -c $(CFLAGS) $(STOPWATCHDIR)$(PATHSEP)stopwatch_data.c

$(ALARMDIR)$(PATHSEP)alarm_data$(OBJ) : $(ALARMDIR)$(PATHSEP)alarm_data.c
	$(CC) -c $(CFLAGS) $(ALARMDIR)$(PATHSEP)alarm_data.c

$(DISPLAYDIR)$(PATHSEP)display_data$(OBJ) : $(DISPLAYDIR)$(PATHSEP)display_data.c
	$(CC) -c $(CFLAGS) $(DISPLAYDIR)$(PATHSEP)display_data.c

$(TKMAINDIR)$(PATHSEP)main$(OBJ) : $(TKMAINDIR)$(PATHSEP)main.c
	$(CC) -c $(CFLAGS) $(TKMAINDIR)$(PATHSEP)main.c

$(TKMAINDIR)$(PATHSEP)tkWatch$(OBJ) : $(TKMAINDIR)$(PATHSEP)tkWatch.c
	$(CC) -c $(CFLAGS) $(TKWATCH_CFLAGS) $(TKMAINDIR)$(PATHSEP)tkWatch.c

$(TKMAINDIR)$(PATHSEP)watch_script$(OBJ) : $(TKMAINDIR)$(PATHSEP)watch_script.c
	$(CC) -c $(CFLAGS) $(TKMAINDIR)$(PATHSEP)watch_script.c

clean :
	-$(RM) wristwatch.c *sim*.c *.ic *.oc *.lc *.sc *.ssc *$(OBJ) \
	 $(OBJECTS) $(TKOBJECTS) *.esp  SIMUL SIMULRT \

clobber : clean
	-$(RM) sww$(EXE) xww.exe xwwrt.exe tkww.exe 
