#
# This file is part of pam_dotfile.
# 
# pam_dotfile is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#  
# pam_dotfile is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#  
# You should have received a copy of the GNU General Public License
# along with pam_dotfile; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
#

VERSION=0.4
CFLAGS=-Wall -g -O2
LDFLAGS=-g
DISTNAME=pam_dotfile-$(VERSION)
TARFILE=$(DISTNAME).tar.gz
PREFIX=/usr/local
XMLTOMAN=xmltoman

all: pamtest pam_dotfile.so pam-dotfile-gen pam-dotfile-helper pam-dotfile-gen.1 pam-dotfile-helper.8

%.1: %.1.xml
	$(XMLTOMAN) $^ > $@

%.8: %.8.xml
	$(XMLTOMAN) $^ > $@

man: pam-dotfile-gen.1 pam-dotfile-helper.8

pamtest: pamtest.o
	$(CC) -o $@ $^ $(LDFLAGS) -ldl -lpam -lpam_misc

pam_dotfile-pic.o: pam_dotfile.c
	$(CC) -fPIC $(CFLAGS) -c -o $@ $<

md5-pic.o: md5.c
	$(CC) -fPIC $(CFLAGS) -c -o $@ $<

md5util-pic.o: md5util.c
	$(CC) -fPIC $(CFLAGS) -c -o $@ $<

log-pic.o: log.c
	$(CC) -fPIC $(CFLAGS) -c -o $@ $<

common-pic.o: common.c
	$(CC) -fPIC $(CFLAGS) -c -o $@ $<

pam_dotfile.so: pam_dotfile-pic.o md5-pic.o md5util-pic.o log-pic.o common-pic.o
	ld -x --shared -o $@ $^ -ldl -lpam -lpam_misc

pam-dotfile-helper: pam-dotfile-helper.o md5.o md5util.o log.o common.o
	$(CC) -o $@ $^ $(LDFLAGS)

pam-dotfile-gen: pam-dotfile-gen.o md5.o md5util.o
	$(CC) -o $@ $^ $(LDFLAGS)

suid:
	chown root:root pam-dotfile-helper
	chmod 4755 pam-dotfile-helper

clean:
	rm -f *.o \
		*.so \
		*~ \
		pamtest \
		pam-dotfile-gen \
		pam-dotfile-helper \
	 	core

mrproper: clean
	rm -f *.8 *.1

install: pam_dotfile.so pam-dotfile-gen pam-dotfile-helper
	mkdir -p $(DESTDIR)/lib/security $(DESTDIR)/sbin $(DESTDIR)/$(PREFIX)/share/man/man1 $(DESTDIR)/$(PREFIX)/share/man/man8
	install -D -s -g root -o root -m 644 pam_dotfile.so  $(DESTDIR)/lib/security/pam_dotfile.so
	install -D -s -g root -o root -m 4755 pam-dotfile-helper  $(DESTDIR)/sbin/pam-dotfile-helper
	install -D -s -g root -o root -m 755 pam-dotfile-gen $(DESTDIR)/$(PREFIX)/bin/pam-dotfile-gen
	install -D -g root -o root -m 644 pam-dotfile-gen.1 $(DESTDIR)/$(PREFIX)/share/man/man1/pam-dotfile-gen.1
	install -D -g root -o root -m 644 pam-dotfile-helper.8 $(DESTDIR)/$(PREFIX)/share/man/man8/pam-dotfile-helper.8

deinstall:
	rm -f $(DESTDIR)/lib/security/pam_dotfile.so \
		$(DESTDIR)/sbin/pam-dotfile-helper $(DESTDIR)/$(PREFIX)/bin/pam-dotfile-gen \
		$(DESTDIR)/$(PREFIX)/share/man/man1/pam-dotfile-gen.1 \
		$(DESTDIR)/$(PREFIX)/share/man/man8/pam-dotfile-helper.8

tar: mrproper man
	mkdir ../$(DISTNAME)
	rmdir ../$(DISTNAME)
	cp -av `pwd` ../$(DISTNAME)
	rm -f ../$(TARFILE)
	tar czv -C .. -f ../$(TARFILE) $(DISTNAME)
	rm -rf ../$(DISTNAME)

.PHONY: all clean install deinstall suid tar man mrproper
