MOD=libnss_vserver.so.2
INST=/lib/tls/
OBJS=passwd.o shadow.o group.o
COMMH=config.h commdef.h commctx.h
DBG=-g3

all: ${MOD}

clean:
	rm -f *.o
	rm -f ${MOD}

install: ${MOD}
	cp -a ${MOD} ${INST}
	cp -a /etc/nsswitch.conf /etc/nsswitch.conf.orig
	perl -pe 's#\n# vserver\n# if(m/^\W*(passwd|group|shadow)\W*:/ && !m/vserver/);' </etc/nsswitch.conf.orig >/etc/nsswitch.conf

uninstall:
	rm -fr ${INST}${MOD}
	cp -a /etc/nsswitch.conf /etc/nsswitch.conf.orig
	perl -pe 's#vserver## if(m/^\W*(passwd|group|shadow)\W*:/ && m/vserver/); s#\W+\n#\n#;' </etc/nsswitch.conf.orig >/etc/nsswitch.conf

test:
	@if [ ! -x ${INST}${MOD} ]; then echo "Warning! Test should be done AFTER install"; fi
	@echo "== Empty output below is fine unless =="
	@echo "== you have already created vservers =="
	getent passwd | grep vslogin | tail -n 4
	getent shadow | egrep '^[a-z0-9_A-Z]+[-@][a-z0-9_A-Z]+:' | tail -n 4
	@echo "== These runs should provide some output if installed correctly =="
	getent passwd 9999
	getent group 9999
	getent group vserver
	@echo "== Testing finished =="

${MOD}: ${OBJS}
	gcc -shared ${DBG} -o ${MOD} -lpthread -Wl,-soname,${MOD} ${OBJS}

passwd.o: passwd.c shared.h ${COMMH}
	gcc -c -O2 ${DBG} -o$@ $*.c

shadow.o: shadow.c shared.h ${COMMH}
	gcc -c -O2 ${DBG} -o$@ $*.c

group.o: group.c ${COMMH}
	gcc -c -O2 ${DBG} -o$@ $*.c
