# --------------------------------------------------------------------------- #
# --------------------------------------------------------------------------- #
#									      #
#                 OOO    HH   HH    AAA    NN   NN    AAA		      #
#               OO   OO  HH   HH  AA   AA  NNN  NN  AA   AA		      #
#               OO   OO  HHHHHHH  AAAAAAA  NN N NN  AAAAAAA		      #
#               OO   OO  HH   HH  AA   AA  NN  NNN  AA   AA		      #
#                 OOO    HH   HH  AA   AA  NN   NN  AA   AA		      #
#      								              #
# --------------------------------------------------------------------------- #
# --------------------------------------------------------------------------- #

# This makefile compiles all Ohana package programs in the Elixir
# system, and their libraries.  Most of the components in this
# directory were created for Ohana, but there are a few libraries
# which are from external sources: libjpeg, libpng, zlib, and
# readline.  Extra care should be taken in compiling those libraries. 

# Edit Configure to reflect the location of your installation 
# or use the script 'configure'

include Configure

# .SILENT:								       
default: all

# The collection of Ohana programs.  Other components in the src
# directory may be compiled by going to those directories. 
PROGRAM =   \
addstar     \
delstar     \
elixir      \
gastro      \
gastro2     \
getstar     \
gcompare    \
imclean     \
imregister  \
kapa        \
kii         \
misc        \
mosastro    \
nightd      \
opihi       \
perl        \
relphot     \
shell       \
tcl         \
uniphot

# any of these programs can be built with 'make (name)' 
EXTRAS =     \
opihi.v1    \
fixcat      \
gophot      \
getusno     \
lightcurve  \
markrock    \
photdbc     \
markstar    \
mosastro.v1 \
mosastro.v2 \
skycalc     

OLD = mana status addusno addrefs addspphot

# Required libraries depends on the architecture
LIBS-linux = libtap libohana libfits libdvo libkapa
LIBS-lin64 = libtap libohana libfits libdvo libkapa
LIBS-linrh = libtap libohana libfits libdvo libkapa
LIBS-sid   = libtap libohana libfits libdvo libkapa
LIBS-sol   = libtap libohana libfits libdvo libkapa 

# general build targets:
libs:
	@if [ "$(ARCH)" = "" ]; then echo ""; echo " *** please define ARCH ***"; echo; exit 1; fi
	@if [ "$(LIBS-$(ARCH))" = "" ]; then echo; echo " *** LIBS-$(ARCH) is missing"; echo; exit 1; fi
	mkdir -p $(DESTINC)
	mkdir -p $(DESTLIB)
	mkdir -p $(DESTBIN)
	for i in $(LIBS-$(ARCH)); do make $$i.install || exit; done
	chmod +x ohana-config
	cp -f ohana-config $(DESTBIN)/

bins: 
	for i in $(PROGRAM); do make $$i; done

all:
	make libs || exit
	for i in $(PROGRAM); do make $$i || exit; done

extras:
	for i in $(EXTRAS); do make $$i || exit; done

pantasks:
	make libs.install || exit
	cd src/opihi; make pclient.install && exit
	cd src/opihi; make pcontrol.install && exit
	cd src/opihi; make pantasks.install && exit

mana:
	make libs.install
	make kii.install
	make kapa.install
	cd src/opihi; make mana.install && exit

dvoshell:
	make libs.install
	make kii.install
	make kapa.install
	cd src/opihi; make dvo.install && exit

clean:
	@if [ "$(ARCH)" = "" ]; then echo ""; echo " *** please define ARCH ***"; echo; exit 1; fi
	@if [ "$(LIBS-$(ARCH))" = "" ]; then echo; echo " *** LIBS-$(ARCH) is missing"; echo; exit 1; fi
	for i in $(LIBS-$(ARCH)); do make $$i.clean || exit; done
	for i in $(PROGRAM); do make $$i.clean || exit; done
	for i in $(EXTRAS); do make $$i.clean || exit; done
	@rm -f `find . -name .mana`
	@rm -f `find . -name .dvo`

dist:
	@if [ "$(ARCH)" = "" ]; then echo ""; echo " *** please define ARCH ***"; echo; exit 1; fi
	@if [ "$(LIBS-$(ARCH))" = "" ]; then echo; echo " *** LIBS-$(ARCH) is missing"; echo; exit 1; fi
	for i in $(LIBS-$(ARCH)); do make $$i.dist || exit; done
	for i in $(PROGRAM); do make $$i.dist || exit; done

install:
	@if [ "$(ARCH)" = "" ]; then echo ""; echo " *** please define ARCH ***"; echo; exit 1; fi
	@if [ "$(LIBS-$(ARCH))" = "" ]; then echo; echo " *** LIBS-$(ARCH) is missing"; echo; exit 1; fi
	for i in $(LIBS-$(ARCH)); do make $$i.install || exit; done
	for i in $(PROGRAM); do make $$i.install || exit; done

install.extras:
	for i in $(EXTRAS); do make $$i.install || exit; done

# standard rules: targets are foo, foo.clean, foo.install, foo.dist
$(PROGRAM) $(LIBS-$(ARCH)) $(EXTRAS): 
	if [ -d "src/$@" ]; then (cd src/$@ && make); fi

%.install:
	mkdir -p bin/$(ARCH)
	if [ -d "src/$*" ]; then (cd src/$* && make install); fi

%.clean:
	if [ -d "src/$*" ]; then (cd src/$* && make clean); fi

%.dist:
	(cd src/$* && make dist)
