SHELL	 =/bin/sh
RPCGEN	 =@RPCGEN@
SSHSTATE =@SSHSTATE@
SSLPATH  =@SSLPATH@
CC	 =@CC@
SQLSTATE =@SQLSTATE@
RDPSTATE =@RDPSTATE@
PREFIX   =@prefix@
HOSTNAME =@HOSTNAME@
COLINUX  =@COLINUX@

generic:
	@if test "$(SSHSTATE)" = "YES"; then \
	cd src/sssh; \
	sh build.sh "$(SSLPATH)"; \
	fi
	@if test "$(RDPSTATE)" = "YES"; then \
	cd src/rdesktop; \
	sh build.sh; \
	fi

	@$(MAKE) CC=$(CC) @PROGS@ LIBS="@LIBS@" XFLAGS="@CFLAGS@ @CPPFLAGS@ @DEFS@" RPCGEN="@RPCGEN@"

rpcgen:
	cd src/rpcgen; $(MAKE) "LIBS=$(LIBS)" "XFLAGS=$(XFLAGS)"

all:
	cd src/misc; $(MAKE) "LIBS=$(LIBS)" "XFLAGS=$(XFLAGS)" "RPCGEN=$(RPCGEN)"
	cd src/boot; $(MAKE) "LIBS=$(LIBS)" "XFLAGS=$(XFLAGS)" "RPCGEN=$(RPCGEN)"
	cd src/port_scan; $(MAKE) "LIBS=$(LIBS)" "XFLAGS=$(XFLAGS)"
	cd src/nfs-chk; $(MAKE) "LIBS=$(LIBS)" "XFLAGS=$(XFLAGS)" "RPCGEN=$(RPCGEN)"
	cd src/yp-chk; $(MAKE) "LIBS=$(LIBS)" "XFLAGS=$(XFLAGS)" "RPCGEN=$(RPCGEN)"
	cd src/fping; $(MAKE) "LIBS=$(LIBS)" "CFLAGS=$(XFLAGS)"
	cd src/ddos_scan; $(MAKE) "LIBS=$(LIBS)" "CFLAGS=$(XFLAGS)"
	cd src/contrib; $(MAKE) "LIBS=$(LIBS)" "CFLAGS=$(XFLAGS)"
	@if test "$(SQLSTATE)" = "YES"; then \
	cd src/sqlat/freetds-0.52; \
	if test ! -f "src/tds/.libs/libtds.a"; then \
		./configure --with-tdsver=4.2; \
		$(MAKE) "LIBS=$(LIBS)" "CFLAGS=$(XFLAGS)"; \
		fi; \
	fi
	@if test "$(SQLSTATE)" = "YES"; then \
	cd src/sqlat; $(MAKE) "LIBS=$(LIBS)" "CFLAGS=$(XFLAGS)"; \
	fi
	cd src/perl-pm; ./build.sh
	perl reconfig

install:
	@if test ! -d "$(PREFIX)/sara"; then \
	mkdir "$(PREFIX)/sara"; \
	fi
	@cp -fR html $(PREFIX)/sara
	@if test "$(COLINUX)"; then \
	cp development/colinux.support.tar.gz ./; \
	gunzip colinux.support.tar.gz; \
	tar -xf colinux.support.tar; \
	rm -f colinux.support.tar; \
	cp -fR cobin "$(PREFIX)/sara"; \
	cp -fR lib "$(PREFIX)/sara"; \
	cp -f "$(PREFIX)/sara/html/images/cosara.gif" "$(PREFIX)/sara/html/images/sara.gif"; \
	fi
	@if test -d "$(PREFIX)/sara"; then \
	echo "Loading SARA in $(PREFIX)/sara ..."; \
	cp -f add_user "$(PREFIX)/sara" ; \
	cp -f sara* "$(PREFIX)/sara" ; \
	cp -f reconfig "$(PREFIX)/sara"; \
	cp -fR bin "$(PREFIX)/sara"; \
	cp -fR perl "$(PREFIX)/sara"; \
	cp -fR perllib "$(PREFIX)/sara"; \
	cp -fR config "$(PREFIX)/sara"; \
	cp -fR rules "$(PREFIX)/sara"; \
	cp -fR docs "$(PREFIX)/sara"; \
	cp -fR data "$(PREFIX)/sara"; \
        cd "$(PREFIX)/sara"; \
	rm -rf INSTALL README* Makefile* TODO src config.* configure*; \
	else \
	echo "Directory, $(PREFIX)/sara, could not be created"; \
	exit; \
	fi 
	perl reconfig

checksums:
	@find * -type f -print | sort | xargs md5

clean:
	cd src/misc; $(MAKE) clean
	cd src/boot; $(MAKE) clean
	cd src/port_scan; $(MAKE) clean
	cd src/nfs-chk; $(MAKE) clean
	cd src/yp-chk; $(MAKE) clean
	cd src/fping; $(MAKE) clean
	cd src/rpcgen; $(MAKE) clean
	cd src/ddos_scan; $(MAKE) clean
	cd src/contrib; $(MAKE) clean
	@if test "$(SSHSTATE)" = "YES"; then \
	  cd src/sssh; \
	  if test -f Makefile; then \
	    $(MAKE) distclean; \
	  fi; \
	fi
	@if test "$(SQLSTATE)" = "YES"; then \
	  cd src/sqlat; \
	  if test -f Makefile; then \
	    $(MAKE) distclean; \
	  fi; \
	  cd freetds-0.52; \
	  if test -f Makefile; then \
	   $(MAKE) distclean; \
	  fi; \
	fi
	@if test "$(RDPSTATE)" = "YES"; then \
	cd src/rdesktop; \
	$(MAKE) distclean; \
	fi
	rm -f html/sara.html html/sara_documentation.html status-* status_file
	rm -f bit_bucket tmp*

tidy:	clean
	cd src/perl-pm; sh clean.sh
	rm -f *.old *.bak *.orig */*.old */*.bak */*.orig tmp_file* tmp1_file*
	rm -f *.sh *.txt *.html *.doc *.tar core perl/core fact.* 
	rm -f  config/passwd `find ./ -name ".*swp" -print`
	rm -f html/tutorials/vulnerability/*.html
	rm -f html/tutorials/vulnerability/no_cve/*.html zap*
	rm -f config/password html/plugins/images/* sss.log
	rm -f config.*
	rm -f rules/corrections
	rm -f sss/sss.html
	rm -f perl/contrib/msadc.pl
	rm -rf cobin lib 
	rm -f Makefile *.cache
	rm -rf results status .dump
	chmod -x sara
	@echo ">>>> Delete plugins by typing 'make noplugins' <<<<"
noplugins:
	@echo "plugins will be stored in /tmp/plugins.xx.tar"
	tar -cvf "/tmp/plugins..tar" ./plugins/*
	rm -rf plugins/*
	rm -rf html/images/plugins/*

distclean:	tidy

