############################################
# Makefile for satellite tracking software #
############################################

##############################
# define variables and paths #
##############################

#	tell make what default target is

SAT : includes astroalmn isat bsat sunsat sawsat plotgen

#	other targets

ISAT : includes astroalmn isat
BSAT : includes astroalmn bsat
SUNSAT : includes astroalmn sunsat
SAWSAT : includes astroalmn sawsat
PLOTGEN : includes astroalmn plotgen

CC = gcc
LD = gcc

# where the executables go

BIN = /home/mhd/craig/bin

# where the gcc stuff is located

GCCHEAD = /usr/local/export/gnu

# where the almanac stuff is located

APATH = ./almanac

# where the include files are located

HPATH = ./h

# where the libraries are located

LPATH = ./lib

# the aalib

AALIB = $(LPATH)/aalib.a
SATLIB = $(LPATH)/satlib.a
MATH = -lm
LIBS = $(SATLIB) $(AALIB) $(MATH)

CFLAGS = -O -I/usr/5include  -I$(HPATH)

CFILES	= sgp4.c deep.c thetag.c fmod2p.c sdp4.c sdp8.c sgp.c sgp8.c \
        mjd.c rsat.c search.c satellite.h satproto.h aaproto.h

OFILES	= sgp4.o deep.o thetag.o fmod2p.o sdp4.o sdp8.o sgp.o sgp8.o \
        mjd.o rsat.o search.o 


# Set the include file directory search path

vpath %.h $(HPATH)

################
# Dependencies #
################

sgp4.o deep.o thetag.o fmod2p.o sdp4.o sdp8.o sgp.o sgp8.o : satellite.h
rsat.o search.o : satellite.h
driver.o  bdriver.o sunsat.o sawsat.o plotgen.o : satellite.h

$(OFILES) : satproto.h
driver.o bdriver.o sunsat.o sawsat.o plotgen.o : satproto.h

sgp.o sgp4.o sgp8.o sdp4.o sdp8.o deep.o rsat.o : aaproto.h
driver.o bdriver.o sunsat.o sawsat.o plotgen.o : aaproto.h

sawsat.o : kep.h

#########
# Rules #
#########

#	rule to build the interactive satellite driver executable

isat : driver.o $(AALIB) $(CFILES)
	make getfiles
	@echo " " 
	$(LD) -o isat driver.o $(LIBS)
	mv isat $(BIN)
	@touch isat
	@rlog -t RCS/*,v > Filelist
	@echo " "
	@rm -f $(OFILES)

#	rule to build the batch satellite driver executable

bsat : bdriver.o $(AALIB) $(CFILES)
	make getfiles
	@echo " " 
	$(LD) -o bsat bdriver.o $(LIBS)
	mv bsat $(BIN)
	@touch bsat
	@rlog -t RCS/*,v > Filelist
	@echo " "
	@rm -f $(OFILES)

#	rule to build the near sun satellite driver executable

sunsat : sunsat.o $(AALIB) $(CFILES)
	make getfiles
	@echo " " 
	$(LD) -o sunsat sunsat.o $(LIBS)
	mv sunsat $(BIN)
	@touch sunsat
	@rlog -t RCS/*,v > Filelist
	@echo " "
	@rm -f $(OFILES)

#	rule to build the near position search driver executable

sawsat : sawsat.o $(AALIB) $(CFILES)
	make getfiles
	@echo " " 
	$(LD) -o sawsat sawsat.o $(LIBS)
	mv sawsat $(BIN)
	@touch sawsat
	@rlog -t RCS/*,v > Filelist
	@echo " "
	@rm -f $(OFILES)

#	rule to build the gnuplot plot file generator 

plotgen : plotgen.o  $(AALIB)
	@echo " " 
	$(LD) -o plotgen plotgen.o $(LIBS)
	mv plotgen $(BIN)
	@touch plotgen
	@echo " "

#	rule to build the satlib.a library archive

satlib.a : $(OFILES)
	@echo " "
	ar rc $(SATLIB) $(OFILES)
	ranlib $(SATLIB)
	@echo " "
	@touch satlib

#	get the object files from the library archive

getfiles : $(CFILES) 
	@echo " "
	-csh -c "if (!(-e rsat.o) && -e $(SATLIB)) ar xo $(SATLIB) $(OFILES)"
	@echo " "
	make satlib.a
	@touch getfiles

#	rule to make sure the include files are current

includes :
	@echo " "
	cd $(HPATH); $(MAKE)
	@echo " "

#	rule to make sure the almanac library is current

astroalmn :
	@echo " "
	cd $(APATH); $(MAKE)
	@echo " "

#	default rule to make .o files from .c files

%.o : %.c
	@echo " "
####	generate code
	$(CC) $(CFLAGS) -ansi -pedantic -c -Wall $*.c

#	misc rules

clean :
	rm -f $(OFILES) junk crud core *.BAK
