# Do "make install" to copy the pages to their destination.
# Do "make gz" before "make install" if you would like
#     to use compressed source pages.
# Do "make remove" before "make gz" if you would like to remove man
#     pages from system
# Do "make ungz" to revert compressed man pages to the plain files

MANDIR=/usr/man/cs

all: remove gz install

remove:
	# wipe the old pages
	for i in man?; do for j in $$i/*; do rm -f $(MANDIR)/$$i/$$j; done; done

gz:
	# we have to gzip only real text files
	find man? -type f -exec gzip {} \;
	# ... and rename symbolic links
	LC_ALL=C find . -type l | \
		xargs file | \
		sed 's/:.*symbolic link to/:/' | \
		awk -F: '{print "rm "$$1; \
		  print "ln -s "$$2".gz "substr($$1,3,length($$1)-2)".gz"}' \
		| sh -

install:
	# create dirs
	for i in man?; do \
		install -d -m 755 $(MANDIR)/$$i; \
	done
	# install the files
	if echo $(LANG) $(LC_ALL) | grep -qi UTF-8; then \
		find man? -type f -exec iconv -f ISO-8859-2 -t UTF-8 {} -o $(MANDIR)/{} \; ; \
		find man? -type f -exec chmod 644 $(MANDIR)/{} \; ; \
	else \
		find man? -type f -exec install -m 644 {} $(MANDIR)/{} \; ; \
	fi
	# copy the links
	find man? -type l -exec cp -a {} $(MANDIR)/{} \;

ungz:
	# we have to gunzip only real text files
	find man? -type f -exec gunzip {} \;
	# ... and rename symbolic links back
	find man? -type l | \
		awk '{print "mv "$$0" "substr($$0,1,length($$0)-3)}' | \
		sh -
	
