#@(#) $Revision: 76.1 $
#
# Common makefile for libsec -- master (top-level) makefile
#

RM=rm
GENCAT=gencat
CAT=cat
VERSION=1
LIB=libsec
LIB_COM=$(LIB:=_cs)
.SILENT:

DIRSX = libacl_x libaudit_x libmand_x libprot_x
DIRSX_SL = $(DIRSX:_x=_sl_x)
DIRSX_COM = libprot_com
DIRSX_SL_COM = $(DIRSX_COM:_com=_sl_com)

all:	archive shared

all_com:	archive_com shared_com libsec_com_cat

archive:
	echo "\tMaking archive $(LIB)..";		\
	$(MAKE) $(LIB)

archive_com:
	comdir=$(@:=);	
	echo "\tMaking archive $$comdir for commercial security...";	\
	$(MAKE) $(LIB_COM)				\
	libsec_com_cat

shared:
	if ifndef SHARED_LIBS; then			\
	    echo "\tSHARED_LIBS not defined, quiting";	\
	    exit 0;					\
	fi;						\
	echo "\tMaking shared $(LIB)...";		\
	$(MAKE) $(LIB)_sl				\
	libsec_com_cat

shared_com:
	if ifndef SHARED_LIBS; then			\
	    echo "\tSHARED_LIBS not defined, quiting";	\
	    exit 0;					\
	fi;						\
	echo "\tMaking shared commercial security $(LIB)...";	\
	$(MAKE) $(LIB_COM)_sl

$(LIB): $(DIRSX) hpux_rel.o
	answer="`find $(LIB).a -newer tmp -print 2>/dev/null`"; \
	if [ -z "$$answer" ]; then				\
	    $(RM) -f $(LIB).a;				\
	    $(MAKE) TYPE="" DIRS="$(DIRSX)" wrapup;		\
	else							\
	    echo "\`$@' is up to date.";			\
	fi

$(LIB_COM): $(DIRSX_COM) hpux_rel.o
	answer="`find $(LIB).a -newer tmp -print 2>/dev/null`"; \
	if [ -z "$$answer" ]; then				\
	    $(RM) -f $(LIB).a;				\
	    $(MAKE) TYPE="" DIRS="$(DIRSX_COM)" wrapup_com;	\
	else							\
	    echo "\`$(@:_cs=)' is up to date.";		\
	fi

libsec_com_cat:
	answer="`find $(LIB).cat -newer tmp -print 2>/dev/null`";\
	if [ -z "$$answer" ]; then				\
	    $(RM) -f $(LIB).cat;				\
	    $(GENCAT) libsec.cat *.msg;				\
	else							\
	    echo "\`$(@:=)' is up to date.";			\
	fi
$(LIB)_sl: $(DIRSX_SL) hpux_rel.o
	answer="`find $(LIB).sl -newer tmp_sl -print 2>/dev/null`"; \
	if [ -z "$$answer" ]; then				\
	    $(RM) -f $(LIB).sl;				\
	    $(MAKE) TYPE="_sl" DIRS="$(DIRSX_SL)" wrapup;	\
	else							\
	    echo "\`$@' is up to date.";			\
	fi
$(LIB_COM)_sl: $(DIRSX_SL_COM) hpux_rel.o
	answer="`find $(LIB).sl -newer tmp_sl -print 2>/dev/null`"; \
	if [ -z "$$answer" ]; then				\
	    $(RM) -f $(LIB).sl;				\
	    $(MAKE) TYPE="_sl" DIRS="$(DIRSX_SL_COM)" wrapup_com;	\
	else							\
	    echo "\`$(@:_cs_sl=_sl)' is up to date.";		\
	fi

wrapup: hpux_rel.o
	echo "\tChecking sub-directories..."
	dx=`ksh -c 'for i in $(DIRS);do echo $${i%%_*};done'`;	\
	bad=false;						\
	cd tmp$(TYPE);						\
	for i in $$dx; do					\
	    if [ ! -f OK.$$i ]; then				\
		echo "Subdirectory $$i didn't work!" >&2;	\
		bad=true;					\
	    fi;							\
	done;							\
	$$bad && exit 1;					\
	echo "\tCreating object list...";			\
	ls *.o > objlist;				\
	if [ "$(TYPE)" = "_sl" ]; then				\
	    echo "\tCreating shared library...";		\
	    ld -b -o ../$(LIB).sl ../hpux_rel.o `cat objlist`;	\
	    sleep 1; touch ../$(LIB).sl;			\
	else							\
	    echo "\tCreating archive library...";		\
	    ar qc ../$(LIB).a ../hpux_rel.o `cat objlist`;	\
	fi

wrapup_com: hpux_rel.o
	echo "\tChecking sub-directories..."
	dx=`ksh -c 'for i in $(DIRS);do echo $${i%%_*};done'`;	\
	bad=false;						\
	cd tmp$(TYPE);						\
	for i in $$dx; do					\
	    if [ ! -f OK.$$i ]; then				\
		echo "Subdirectory $$i didn't work!" >&2;	\
		bad=true;					\
	    fi;							\
	done;							\
	$$bad && exit 1;					\
	echo "\tCreating object list...";			\
	ls *.o > objlist;				\
	if [ "$(TYPE)" = "_sl" ]; then				\
	    echo "\tCreating shared commercial security library...";	\
	    ld -b +h $(LIB).$(VERSION) -o ../$(LIB).$(VERSION) ../hpux_rel.o `cat objlist`;	\
	    sleep 1; touch ../$(LIB).sl;			\
	else							\
	    echo "\tCreating commercial security archive library...";	\
	    ar qc ../$(LIB).a ../hpux_rel.o `cat objlist`;	\
	fi;

$(DIRSX):
	mkdir tmp >/dev/null 2>&1 || exit 0
	dir=$(@:_x=);						\
	if [ -f "tmp/OK.$$dir" ]; then				\
	    echo "\`$@' is up to date.";			\
	    exit 0;						\
	fi;							\
	echo "\tMaking regular objects in $$dir";		\
	cd $$dir; $(MAKE) archive

$(DIRSX_SL):
	mkdir tmp_sl >/dev/null 2>&1 || exit 0
	dir=$(@:_sl_x=);					\
	if [ -f "tmp_sl/OK.$$dir" ]; then			\
	    echo "\`$@' is up to date.";			\
	    exit 0;						\
	fi;							\
	echo "\tMaking shared objects in $$dir";		\
	cd $$dir; $(MAKE) shared

clean:
	dirs="$(DIRSX:_x=)";			\
	startdir=`pwd`;				\
	for i in $$dirs; do			\
	    cd $$i; $(MAKE) clean;		\
	    cd $$startdir;			\
	done
		$(RM) -rf tmp tmp_sl hpux_rel.o

clobber: clean
	echo "\tClobbering..."
	rm -f $(LIB).a $(LIB).sl

$(DIRSX_COM):
	mkdir tmp >/dev/null 2>&1 || exit 0
	dir=$(@:_com=);						\
	if [ -f "tmp/OK.$$dir" ]; then				\
	    echo "\`$(@:_com=)' is up to date.";		\
	    exit 0;						\
	fi;							\
	echo "\tMaking regular commercial security objects in $$dir";	\
	cd $$dir; $(MAKE) archive_com

$(DIRSX_SL_COM):
	mkdir tmp_sl >/dev/null 2>&1 || exit 0
	dir=$(@:_sl_com=);					\
	if [ -f "tmp_sl/OK.$$dir" ]; then			\
	    echo "\`$(@:_sl_com=_sl)' is up to date.";		\
	    exit 0;						\
	fi;							\
	echo "\tMaking shared commercial security objects in $$dir";	\
	cd $$dir; $(MAKE) shared_com

clean_com:
	dirs="$(DIRSX_COM:_com=)";		\
	startdir=`pwd`;				\
	for i in $$dirs; do			\
	    cd $$i; $(MAKE) clean_com;		\
	    cd $$startdir;			\
	done
	$(RM) -rf tmp tmp_sl hpux_rel.o

clobber_com: clean_com
	echo "\tClobbering..."
	rm -f $(LIB).a $(LIB).sl
