# GNU Makefile to install ATP 1.50
# install ATP

.PHONY	: bin configr ghostscript install man uninstall wrap

ifdef ATP
atphome = $(ATP)
else
atphome = $(HOME)/atpqwk
endif

prefix = /usr/local
exec_prefix = $(prefix)/bin
man_prefix = $(prefix)/man
man_own	=`ls -l /usr/man/man1/cp.1* | awk '{ print $$3 }'`
man_grp	=`ls -l /usr/man/man1/cp.1* | awk '{ print $$4 }'`
man_prm	= 644
bin_prm	= 755

# this is where your ATP data and configuration files will reside
#atphome = $$HOME/atpqwk

install: configr man bin wrap

configr:	
	@echo " "
	@echo "Installing configuration files . . ."
	@[ -d $(atphome) ] || mkdir -p $(atphome)
	@if [ -f $(atphome)/atprc ] ;\
	then  mv $(atphome)/atprc $(atphome)/atprc.old ;\
	echo "NOTE: atprc already exists, check $(atphome)/atprc.old" ;\
	fi
	@if [ -f $(atphome)/.atprc ] ;\
	then  mv $(atphome)/.atprc $(atphome)/.atprc.old ;\
	echo "NOTE: .atprc already exists, check $(atphome)/.atprc.old" ;\
	fi
	@install -c -m 640 atprc $(atphome)
	@if [ -f $(atphome)/taglines.atp ] ;\
	then  echo "NOTE: taglines.atp already exists, check $(atphome)/taglines.atp" ;\
	else  install -c -m 640 taglines.atp $(atphome) ;\
	fi

bin:
	@echo "Installing binaries . . ."
	install -c -m $(bin_prm) atp $(exec_prefix)/atp
	install -c -m $(bin_prm) atpdiag $(exec_prefix)/atpdiag
	install -c -m $(bin_prm) rot13 $(exec_prefix)/rot13

wrap:
	@echo " "
	@echo "Now it's YOUR turn, please do this:"
ifneq "${origin ATP}" "environment"
	@echo "Set and export the environment variable: ATP=$(atphome)"
endif
	@echo "Edit your $(atphome)/atprc and then read the docs!"

man:
	@echo "Installing man pages . . ."
	@install -c -m $(man_prm) -o $(man_own) -g $(man_grp) atp.1 $(man_prefix)/man1
	@install -c -m $(man_prm) -o $(man_own) -g $(man_grp) atpdiag.1 $(man_prefix)/man1
	@install -c -m $(man_prm) -o $(man_own) -g $(man_grp) rot13.1 $(man_prefix)/man1
	@install -c -m $(man_prm) -o $(man_own) -g $(man_grp) editline.3 $(man_prefix)/man3

uninstall:
	rm -f $(man_prefix)/man1/{atp.1,atpdiag.1,rot13.1}
	rm -f $(man_prefix)/man3/editline.3
	rm -f $(exec_prefix)/{atp,atpdiag,rot13}
	rm -f $(atphome)/{atprc,taglines.atp}

uncfg: uninstall
	rmdir $(atphome)

atp.txt	: atp.1
	groff -man -t -Tascii $< | col -bx > $@

atp.dvi : atp.1
	groff -man -t -Tdvi $< >$@
	
# atp.ps : atp.1
#	groff -man -t -Tps $< >$@
	
# alternate way might produce slightly nicer output
atp.ps: atp.1
	$(MAKE) atp.dvi
	dvips -f < atp.dvi > $@

atp_end.ps:
	echo quit > $@

ghostscript: atp.ps atp_end.ps
	gs -dNOPAUSE -sOutputFile=atp-gs.out atp.ps atp_end.ps

clean:
	rm -f atp.dvi atp-gs-out*

tar:
	cd .. ; tar zchvf atp-1.50-bin.tar.gz atp-1.50-bin
