# $(DEST) is the path where the files will be installed
# e.g. /usr or /usr/local 
# creates $(DEST)/lib,$(DEST)/include and $(DEST)/man/man7

DEST=/usr/local

# $(CP) is used to install the files. If you use filelinks (ln -s), don't 
# removed the current directory after installation !

CP= ln -s 
MYPATH= `pwd`

CC= cc -g
CFLAGS=  -c -Wall 
OFLAG= -o
AR= ar
ARFLAGS= rv
MKDIR= mkdir -p
ROFF= groff -man -Tascii 
CHMOD= chmod 644

all: test libselfdir.a
	@echo 
	@echo type \'make readmanpage\' if you want to read the manpage without installing it
	@echo type \'make install\' if you want to install in $(DEST)
	@echo
 
readmanpage: selfdir.7
	$(ROFF) selfdir.7 | more

libselfdir.a : selfdir.o
	$(AR) $(ARFLAGS) libselfdir.a selfdir.o

example/bin/mainself : example/mainself.o selfdir.o
	$(CC) $(OFLAG) $@ example/mainself.o selfdir.o

example/mainself.o : example/mainself.c
	$(CC) $(CFLAGS) $(OFLAG) example/mainself.o example/mainself.c

selfdir.o : selfdir.c selfdir.h
	$(CC) $(CFLAGS) selfdir.c

test: example/bin/mainself
	@echo
	@echo testing example 
	@echo
	@echo starting executable \'example/bin/mainself\' direct 
	example/bin/mainself 
	@echo
	@echo starting with expanding PATH by a directory with the executable
	sh -c 'PATH=$$PATH:./example/bin;mainself'
	@echo
	@echo starting with expanding PATH by a directory with a link
	sh -c 'PATH=$$PATH:./example/temp;mainself'
	@echo
	@echo starting with expanding PATH by a directory with a link to a link
	sh -c 'PATH=$$PATH:./example/usr/bin;mainself'
	@echo
	@echo test successfull, if you got the following 4 times:
	@echo
	@cat example/data/dataexample
	@echo

install: libselfdir.a selfdir.h selfdir.7
	$(MKDIR) $(DEST)/lib
	$(CHMOD) libselfdir.a 
	$(CP) $(MYPATH)/libselfdir.a $(DEST)/lib/libselfdir.a
	$(MKDIR) $(DEST)/include
	$(CHMOD) selfdir.h
	$(CP) $(MYPATH)/selfdir.h $(DEST)/include/selfdir.h
	$(MKDIR) $(DEST)/man/man7
	$(MKDIR) $(DEST)/man/cat7
	$(CHMOD) selfdir.7
	$(CP) $(MYPATH)/selfdir.7 $(DEST)/man/man7/selfdir.7
        
clean: ; rm -f selfdir.o example/mainself.o example/bin/mainself
 

