srcdir = @srcdir@
topdir = @topdir@
#VPATH = $(srcdir)

include ../Makefile.inc
topdir=..
topincdir=../include
srcdir= @srcdir@

DO_DEBUG = @DO_DEBUG@
CC = @CC@
CXX = @CXX@
CPP = @CPP@
CPPFLAGS = @CPPFLAGS@

EXE=

prefix=@prefix@
exec_prefix=@exec_prefix@
bindir=@bindir@
sbindir=@sbindir@
libexecdir=@libexecdir@
datadir=@datadir@
mandir=@mandir@
mansubdir=@mansubdir@

GTAR=@GTAR@
POSIXSHELL=@POSIXSHELL@

ifeq ($(DO_DEBUG),)
DEBUGLIBOBJECTS = 
DO_CPP_ASSEM = 
DO_C_ASSEM = 
else
DEBUGLIBOBJECTS = 
DO_CPP_ASSEM = $(CXX) $(CFLAGS) -S -gstabs+ -Wno-unused $(CPPFLAGS) -c -o $(OUT_PATH)/$*.S $<
DO_C_ASSEM = $(CC) $(CFLAGS) -S -gstabs+ -c -o $(OUT_PATH)/$*.S $<
endif


INSTALL= @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@
#LIBEFENCE = -lefence
#DEBUG = -g
installprefix=$(ROOT)

#LIBEFENCE = -lefence

VPATH = $(srcdir)
WARNINGS =

BINDIR=$(prefix)/bin
RPMBINDIR=$(subst usr/bin,bin,$(BINDIR))
MANDIR=$(prefix)/man/man8
LIBDIR=$(libdir)
INCDIR=$(prefix)/include
ETCDIR=$(ROOT)/etc

INSTSUBDIRS = lib @PO@
OTHERSUBDIRS =
SWINSTALL_OBJS = swinstall_lib.o
OBJS = fmgetpass.o swcommon.o swcommon0.o 
PROGS = swpackage swinstall swcopy swbisparse swverify swign swbis rpm2swbis rpm2psf
OUT_PATH = .
LIBS = @LIBS@
RPMLIBS = @RPMLIBS@
LIB= swproglib.a
#LOADLIBES = $(topdir)/swstructlib/swstructlib.a  $(topdir)/swsupplib/swsupplib.a

CLOADLIBES = \
$(topdir)/swsupplib/swsupplib.a

CPPLOADLIBES = \
$(topdir)/swstructlib/src/swexport/swexport.a \
$(topdir)/swstructlib/src/swmetadata/swmetadata.a \
$(topdir)/swstructlib/src/archive/archive.a \
$(topdir)/swstructlib/src/misc/misc.a

SOURCES = *.c *.cxx

ifeq ($(POSIXSHELL),)
POSIXSHELL=/bin/sh
endif

ifeq ($(GTAR),)
GTAR=tar
endif

ifeq (.depend,$(wildcard .depend))
TARGET=everything
else
TARGET=depend everything
endif

.SUFFIXES:
.SUFFIXES: .cxx .o
.SUFFIXES: .c .o

.cxx.o:
	$(CXX) $(CFLAGS) -Wno-unused $(CPPFLAGS) -c -o $(OUT_PATH)/$*.o  $<; $(DO_CPP_ASSEM)

.c.o:
	$(CC) $(CFLAGS) -c -o $(OUT_PATH)/$*.o  $<; $(DO_C_ASSEM)

all: $(TARGET)

everything: $(PROGS) runswpackagetest

lib: $(LIB)

$(LIB): $(OBJS)
	ar rc $(LIB) $(OBJS)
	ranlib $(LIB)

runswpackagetest: runswpackagetest.o $(OBJS)
	$(CC)  $(LDFLAGS) ${@}.o $(LIBS) $(CLOADLIBES) -o $@

swlist: swlist.o $(OBJS)
	$(CC)  $(LDFLAGS) ${@}.o $(OBJS) $(CLOADLIBES) -o $@ 

swpackage.o: swpackage.cxx
	$(CXX) $(CFLAGS) -Wno-unused $(CPPFLAGS) -c -o $@ swpackage.cxx

swpackage: swpackage.o $(OBJS) $(LIB) $(CPPLOADLIBES) $(CLOADLIBES)
	$(CXX)  $(LDFLAGS) ${@}.o fmgetpass.o swcommon0.o $(LIBS) $(CPPLOADLIBES) $(CLOADLIBES) -o $@

swcopy: swcopy.o $(OBJS) $(LIB) $(CLOADLIBES)
	$(CC)  $(LDFLAGS) ${@}.o $(OBJS) $(CLOADLIBES) $(LIBS) -o $@

swinstall: swinstall.o $(OBJS) $(SWINSTALL_OBJS) $(LIB) $(CLOADLIBES)
	$(CC)  $(LDFLAGS) ${@}.o $(OBJS) $(SWINSTALL_OBJS) $(CLOADLIBES) $(LIBS) -o $@

swverify: swverify.sh.in
	sed \
	 -e "s@\@SWBISLIBEXECDIR\@@"$(libexecdir)@ \
	 -e "s@\@POSIXSHELL\@@"$(POSIXSHELL)@ \
	 -e "s@\@SWBISGTAR\@@"$(GTAR)@ \
	 -e "s@\@SWBIS_RELEASE\@@"`cat ../include/swprog_versions.h | \
		grep SWBIS_RELEASE | \
		expand | \
		awk '{ print $$3 }'`@ \
	<${@}.sh.in >$@; \
	chmod 775 $@

swbis: swbis.sh.in
	sed \
	 -e "s@\@SWBISLIBEXECDIR\@@"$(libexecdir)@ \
	 -e "s@\@SWBIS_BINDIR\@@"$(bindir)@ \
	 -e "s@\@POSIXSHELL\@@"$(POSIXSHELL)@ \
	 -e "s@\@SWBISGTAR\@@"$(GTAR)@ \
	 -e "s@\@SWBIS_RELEASE\@@"`cat ../include/swprog_versions.h | \
		grep SWBIS_RELEASE | \
		expand | \
		awk '{ print $$3 }'`@ \
	<${@}.sh.in >$@; \
	chmod 775 $@

rpm2psf: rpm2psf.sh.in
	sed -e "s@\@SWBISLIBEXECDIR\@@"$(libexecdir)@ <${@}.sh.in >$@
	chmod 775 $@

swign: swign.sh.in
	sed \
	-e "s@\@SWBISLIBEXECDIR\@@"$(libexecdir)@ \
	 -e "s@\@POSIXSHELL\@@"$(POSIXSHELL)@ \
	 -e "s@\@SWBISGTAR\@@"$(GTAR)@ \
	 -e "s@\@SWBIS_RELEASE\@@"`cat ../include/swprog_versions.h | \
		grep SWBIS_RELEASE | \
		expand | \
		awk '{ print $$3 }'`@ \
	<${@}.sh.in >$@
	chmod 775 $@

rpm2swbis: rpm2swbis.sh.in
	sed -e "s@\@SWBISLIBEXECDIR\@@"$(libexecdir)@ <${@}.sh.in >$@
	chmod 775 $@

swbisparse: $(LIB) $(OBJS) $(CPPLOADLIBES) $(CLOADLIBES) swbisparse.o
	$(CXX) -o $@  $(LDFLAGS) swbisparse.o $(OBJS)  $(LIB) $(CPPLOADLIBES) $(CLOADLIBES) $(LIBS) $(LIBEFENCE)

sw_2tar: $(LIB) $(OBJS) $(CPPLOADLIBES) $(CLOADLIBES) sw_2tar.o
	$(CXX) -o $@  $(LDFLAGS) sw_2tar.o $(OBJS)  $(LIB) $(CPPLOADLIBES) $(CLOADLIBES) $(LIBS) $(LIBEFENCE)

$(topdir)/swstructlib/swstructlib.a:
	( cd $(topdir)/swstructlib ; $(MAKE) )

$(topdir)/swstructlib/swfdecl.a:
	( cd $(topdir)/swstructlib ; $(MAKE) )

$(PROGS): $(OBJS) 

install: all
	strip swpackage$(EXE)
	strip swcopy$(EXE)
	strip swinstall$(EXE)
	strip swbisparse$(EXE)
	if [ ! -d $(libexecdir) ]; then \
		echo "creating $(libexecdir)"; \
		$(INSTALL) -m 755 -d $(libexecdir); \
	fi
	if [ ! -d $(bindir) ]; then \
		echo "creating $(bindir)"; \
		$(INSTALL) -m 755 -d $(bindir); \
	fi
	if [ ! -d $(LIBDIR)/swbis ]; then \
		echo "creating $(LIBDIR)/swbis"; \
		$(INSTALL) -m 755 -d $(LIBDIR)/swbis; \
	fi
	if [ ! -f $(LIBDIR)/swbis/swdefaults ]; then \
		echo "Installing defaults file $(LIBDIR)/swbis/swdefaults"; \
		$(INSTALL) -m 644 ../etc/swdefaults  $(LIBDIR)/swbis/swdefaults; \
	fi
	if [ ! -f $(LIBDIR)/swbis/swbisdefaults ]; then \
		echo "Installing swbisdefaults file $(LIBDIR)/swbis/swbisdefaults"; \
		$(INSTALL) -m 644 ../etc/swbisdefaults  $(LIBDIR)/swbis/swbisdefaults; \
	fi

	$(INSTALL) -m 755 rpm2psf $(bindir)
	$(INSTALL) -m 755 swverify $(bindir)
	$(INSTALL) -m 755 swign $(bindir)
	$(INSTALL) -m 755 swbis $(bindir)
	$(INSTALL) -m 755 rpm2swbis $(bindir)
	$(INSTALL) -m 755 swpackage$(EXE) $(bindir)
	$(INSTALL) -m 755 swcopy$(EXE) $(bindir)
	$(INSTALL) -m 755 swinstall$(EXE) $(bindir)
	$(INSTALL) -m 755 -d $(libexecdir)/swbis
	$(INSTALL) -m 755 swbisparse$(EXE) $(libexecdir)/swbis

.PHONY:		clean
clean:
	rm -f *.S *.a *.o core *~ $(PROGS) rpm.shared tmp/*
	find . -name "*.orig" -exec rm {} \;
	find . -name core -exec rm {} \;
	rm -f runswpackagetest

.PHONY:		distclean
distclean:	
	$(MAKE) -s clean
	rm -f *.S *.a *.o core *~ $(PROGS) rpm.shared librpmrc find-provides.sh find-requires.sh tmp/*
	rm -f .depend Makefile Makefile.inc config.h config.cache config.status config.log lib-rpmrc

.PHONY:		depend
depend:
	( >.depend)
#	($(CPP) $(CFLAGS) -M $(srcdir)/$(SOURCES) > .depend; exit 0)

noconfig:
	find . -name "Makefile" -exec rm {} \; 
	rm -f *gz *rpm config.*

ifeq (.depend,$(wildcard .depend))
include .depend
endif
