#
# usage:  make         - compile asmref executable
#         make clean   - touch all source files
#         make install - install files
#         make release - create release file
#
local = $(shell pwd)
home = $(HOME)
version := $(shell cat ../VERSION)

# hunt for library file, if this fails then set LIBS to file location
# hunt at local dir, parent dir, then at $HOME/.asmide/
lib1 = $(shell if test -f asmlib.a ; then echo asmlib.a ; fi)
lib2 = $(shell if test -f ../asmlib.a ; then echo ../asmlib.a ; fi)
lib3 = $(shell if test -f ../../asmlib.a ; then echo ../../asmlib.a ; fi)
lib4 = $(shell if test -f $(HOME)/.asmide/asmlib.a ; then echo $(HOME)/.asmide/asmlib.a ; fi)


ifeq "$(lib4)" ""
#$(shell echo "$HOME/.a library not found" )
else
LIBS := $(lib4)
endif

ifeq "$(lib3)" ""
#$(shell echo "../../ library not found" )
else
LIBS := $(lib3)
endif
  
ifeq "$(lib2)" ""
#$(shell echo "no parent library")
else
LIBS := $(lib2)
endif

ifeq "$(lib1)" ""
#$(shell echo "no local library")
else
LIBS := $(lib1)
endif

ifeq "$(LIBS)"  ""
LIBS = ""
endif


#hunt for executable install directory
#BIN = ""
#exp1 = $(shell if test -d $(HOME)/bin ; then echo $(HOME)/bin ; fi)
#exp2 = $(shell if test -w /etc/passwd ; then echo /usr/bin ; fi)

#ifeq "$(exp1)" ""
#$(shell echo "no local bin")
#else
#BIN := $(exp1)
#endif

#ifeq "$(exp2)" ""
#$(shell echo "no root access")
#else
#BIN := $(exp2)
#endif


all: lib_check asmref

lib_check:
	@if test $(LIBS) = "" ; \
	then \
	echo "asmlib needed to compile asmref" ; \
	else \
	echo "linking with asmlib" ; \
	fi

asmref: asmref.o $(LIBS)
	ld -static -o asmref asmref.o $(LIBS) 

asmref.o:	asmref.asm
	nasm -g -f elf -o asmref.o asmref.asm
#
# the "install" program uses flags
#        -D       create any needed directories
#        -s       strip executables
#        -m 644   set file attributes to 644 octal
install:
	@if test -d $(HOME)/bin ; \
	then \
	echo "installing asmref in $(HOME)/bin" ; \
	install -s asmref $(HOME)/bin ; \
	else \
	echo "local /bin directory not found" ; \
	  if test -w /etc/passwd ; \
	  then \
	  echo "we are root, install in /usr/bin" ; \
	  install -s asmref /usr/bin ; \
	  else \
	  echo "no local /bin and we do not have root status" ; \
	  echo "switch to root for /usr/bin install or create local /bin directory" ; \
	  echo "Note: /bin directory may need to be placed in path" ; \
	  fi \
	fi

#	@if test $(BIN) = "" ; \
#	then \
#	echo "only root can install executable" ; \
#	else \
#	install -s asmref $(BIN) ; \
#	fi
	install -D -m 644 ../doc/proc $(home)/.asmide/doc/proc
	install -D -m 644 ../doc/about $(home)/.asmide/doc/about
	install -D -m 644 ../doc/_access $(home)/.asmide/doc/_access
	install -D -m 644 ../doc/ascii $(home)/.asmide/doc/ascii
	install -D -m 644 ../doc/asmbug $(home)/.asmide/doc/asmbug
	install -D -m 644 ../doc/asmfunc $(home)/.asmide/doc/asmfunc
	install -D -m 644 ../doc/asmplan $(home)/.asmide/doc/asmplan
	install -D -m 644 ../doc/asmpub $(home)/.asmide/doc/asmpub
	install -D -m 644 ../doc/asmref $(home)/.asmide/doc/asmref
	install -D -m 644 ../doc/_file $(home)/.asmide/doc/_file
	install -D -m 644 ../doc/indexed_names $(home)/.asmide/doc/indexed_names
	install -D -m 644 ../doc/indexed_numerically $(home)/.asmide/doc/indexed_numerically
	install -D -m 644 ../doc/indexed_topic $(home)/.asmide/doc/indexed_topic
	install -D -m 644 ../doc/_ioctl $(home)/.asmide/doc/_ioctl
	install -D -m 644 ../doc/_memory $(home)/.asmide/doc/_memory
	install -D -m 644 ../doc/_process $(home)/.asmide/doc/_process
	install -D -m 644 ../doc/_signal $(home)/.asmide/doc/_signal
	install -D -m 644 ../doc/structures $(home)/.asmide/doc/structures
	install -D -m 644 ../doc/_system $(home)/.asmide/doc/_system
	install -D -m 644 ../doc/termios $(home)/.asmide/doc/termios
	install -D -m 644 ../doc/_timing $(home)/.asmide/doc/_timing
	install -D -m 644 ../doc/_undoc $(home)/.asmide/doc/_undoc
	install -D -m 644 ../doc/utility $(home)/.asmide/doc/utility
	install -D -m 644 ../doc/vt_term $(home)/.asmide/doc/vt_term
	install -D -m 644 ../doc/asmedit $(home)/.asmide/doc/asmedit
	install -D -m 644 ../doc/asmlib.txt $(home)/.asmide/doc/asmlib.txt
	install -D -m 644 ../doc/includes $(home)/.asmide/doc/includes
	install -D -m 644 ../doc/a_quick.txt $(home)/.asmide/doc/a_quick.txt
	install -D -m 644 ../doc/syserr.txt $(home)/.asmide/doc/syserr.txt
	install -D -m 644 ../doc/x86_ref.txt $(home)/.asmide/doc/x86_ref.txt
	install -D -m 644 ../doc/x86_nasm.txt $(home)/.asmide/doc/x86_nasm.txt
	install -D -m 644 ../doc/tutor1 $(home)/.asmide/doc/tutor1
	install -D -m 644 ../doc/tutor2 $(home)/.asmide/doc/tutor2

	@if test -d $(HOME)/.asmide/edit ; \
	then \
	echo "good, edit directory available, buffer command will work" ; \
	else \
	echo "creating directory $(HOME)/.asmide/edit for buffer command" ; \
	mkdir $(HOME)/.asmide/edit ; \
	fi

clean:
	rm -f ../doc/*~
	rm -f asmref.o *~

release: tar rpm

tar:
	if [ ! -e "release" ] ; then mkdir release ; fi
	tar cfz ./release/asmref$(version)tar.gz --exclude=release -C .. AsmRef doc

rpm: