## ==================================================================
##  
## SSC Makefile
## 
##  $Header: /var/cvs/ssc_core/Makefile.ssc,v 1.63 2002/06/21 05:51:31 martin Exp $
## ==================================================================

## ==================================================================
##	     Internal Variables
## ==================================================================

#
# The CURRENT_TIMESTAMP is important for Repligard-realted targets
#
# I am using %H instead of %k because it doesnot create spaces
CURRENT_TIMESTAMP=$(shell /bin/date +%Y%m%d%H%M%S)

 
LOCAL_DIR	= $(DIST_DIR)/local
SRC_DIR		= $(LOCAL_DIR)/src

MIDGARD_RCS_DIR	= $(DIST_DIR)/var/rcs

MIDGARD_BLOB_DIR	= $(DIST_DIR)/var/live_blobs
MIDGARD_CACHE_DIR	= $(DIST_DIR)/var/live_cache

MIDGARD_STAGING_BLOB_DIR	= $(DIST_DIR)/var/staging_blobs
MIDGARD_STAGING_CACHE_DIR	= $(DIST_DIR)/var/staging_cache

REPLIGARD_TMP	= $(DIST_DIR)/var/tmp/repligard/$(CURRENT_TIMESTAMP)

#
# APXS Path - You shouldn't touch this UNLESS you 
# (a) know what you are doing AND
# (b) are planning NOT to compile Apache with this makefile
# 
APXS            = $(LOCAL_DIR)/$(APACHE)/bin/apxs



## ==================================================================
##	     Targets
## ==================================================================

#   default target
all: build

## ------------------------------------------------------------------
##	     Build Target
## ------------------------------------------------------------------
build:
	#make download
	#make untar-src
	make php-cli
	make apache-php
	make apache-conf
	make scripts
	make db
	make midgard
	
## ------------------------------------------------------------------
##	     Download
## ------------------------------------------------------------------

download:

	@echo "DOWNLOADING" 
	cd $(SRC_DIR) && \
	wget -nd --mirror http://www.php.net/distributions/$(PHP).tar.bz2 &&\
	wget -nd --mirror http://www.apache.org/dist/httpd/$(APACHE).tar.gz	  

	cd $(SRC_DIR) && \
	wget -nd --mirror $(MOD_MIDGARD) 						     &&\
	wget -nd --mirror $(MIDGARD-LIB) 						     &&\
	wget -nd --mirror $(MIDGARD-DATA) 						     &&\
	wget -nd --mirror $(MIDGARD-PHP4) 		

## ------------------------------------------------------------------
##	     Untar sources
## ------------------------------------------------------------------
untar-src:

	make rm-src

	@echo "UNTARRING tar.gz"
	@cd $(SRC_DIR) && ls *.tar.gz | xargs -n1 --verbose tar -xzf 
	@cd $(SRC_DIR) && ls *.tar.bz2 | xargs -n1 --verbose tar --bzip -xf

## ------------------------------------------------------------------
##	     Remove Sources
## ------------------------------------------------------------------
rm-src:

	@echo "REMOVING OLD SOURCES"
	@find  $(SRC_DIR)/* -prune -type d | grep -v "/CVS" | xargs -n1 --verbose rm -fr


## ------------------------------------------------------------------
##	     PHP Command Line
## ------------------------------------------------------------------
php-cli:
	cd $(SRC_DIR)/$(PHP) && \
	./configure \
		--prefix=$(LOCAL_DIR)/$(PHP) \
		--with-dom=$(DOM) \
		--with-mysql=$(MYSQL) \
		--enable-track-vars \
		--enable-ftp \
		--with-zlib-dir=/sw && \
	make && make install


## ------------------------------------------------------------------
##	     Apache-PHP
## ------------------------------------------------------------------
apache-php:

	cd $(SRC_DIR)/$(APACHE) && \
	./configure 
	
	cd $(SRC_DIR)/$(PHP) && \
	./configure \
		--prefix=$(LOCAL_DIR)/$(PHP) \
		--with-apache=../$(APACHE) \
		--with-dom \
		--with-mysql=$(MYSQL) \
		--enable-track-vars \
		--with-dom=$(DOM) \
		--enable-ftp \
		--with-zlib-dir=/sw && \
	make && make install

	cd $(SRC_DIR)/$(APACHE) && \
	./configure --prefix=$(LOCAL_DIR)/$(APACHE) \
		--with-layout=Apache \
		--activate-module=src/modules/php4/libphp4.a \
		--enable-module=all && \
	make && make install
	
	cp $(SRC_DIR)/$(PHP)/php.ini-dist $(LOCAL_DIR)/$(PHP)/lib/php.ini
	ln -f -s $(LOCAL_DIR)/$(PHP)/lib/php.ini $(DIST_DIR)/etc/php.ini
 
	@echo "Please add a line to your httpd.conf file to include"
	@echo "etc/ssc_vhost.conf" 



## ------------------------------------------------------------------
##	     Database
## ------------------------------------------------------------------
db:

	@echo "prepping mysql"
	echo " \
		GRANT ALL ON $(MYSQL_DB_MAILINGLIST).* TO '$(MYSQL_USER)' IDENTIFIED BY '$(MYSQL_PW)'; \
		GRANT ALL ON $(MYSQL_DB_SEARCH_CACHE).* TO '$(MYSQL_USER)' IDENTIFIED BY '$(MYSQL_PW)'; \
	       FLUSH PRIVILEGES; "\
	      | $(MYSQL_CLIENT) -h $(MYSQL_HOST) -uroot -p$(MYSQL_ROOT_PW)

	@# Drop the databases to flush the install. 
	-echo  " \
		DROP DATABASE $(MYSQL_DB_MAILINGLIST) ; \
		DROP DATABASE $(MYSQL_DB_SEARCH_CACHE) ; " \
		| $(MYSQL_CLIENT) -h $(MYSQL_HOST) -uroot -p$(MYSQL_ROOT_PW)

	echo " \
		CREATE DATABASE $(MYSQL_DB_MAILINGLIST) ; \
		CREATE DATABASE $(MYSQL_DB_SEARCH_CACHE) ; " \
	     | $(MYSQL_CLIENT) -h $(MYSQL_HOST) -uroot -p$(MYSQL_ROOT_PW)

	@echo putting db in!
	cat data/mailinglist.sql  | $(MYSQL_CLIENT) -h $(MYSQL_HOST) -uroot -p$(MYSQL_ROOT_PW) $(MYSQL_DB_MAILINGLIST)
	cat data/search_cache.sql | $(MYSQL_CLIENT) -h $(MYSQL_HOST) -uroot -p$(MYSQL_ROOT_PW) $(MYSQL_DB_SEARCH_CACHE)
		
## ------------------------------------------------------------------
##	     Apache Config
## ------------------------------------------------------------------

apache-conf:
	
	-mkdir -p logs
	-mkdir -p htdocs/logs

	cd etc && \
	cp ssc_vhost.conf.dist ssc_vhost.conf && \
	cp midgard_live.conf.dist midgard_live.conf && \
	cp midgard_staging.conf.dist midgard_staging.conf	

	perl -pi -e " 	s#DIST_DIR#$(DIST_DIR)#g ; s#APACHE_IP#$(APACHE_IP)#g ;  s#APACHE_PORT#$(APACHE_PORT)#g ;  s#APACHE_VHOST#$(APACHE_VHOST)#g ;  s#APACHE_STAGING_PORT#$(APACHE_STAGING_PORT)#g ;  s#APACHE_STAGING_VHOST#$(APACHE_STAGING_VHOST)#g ;   s#MIDGARD_MYSQL_USER#$(MIDGARD_MYSQL_USER)#g ;  s#MIDGARD_MYSQL_PW#$(MIDGARD_MYSQL_PW)#g ;  s#MIDGARD_MYSQL_DB#$(MIDGARD_MYSQL_DB)#g ;  s#MIDGARD_BLOB_DIR#$(MIDGARD_BLOB_DIR)#g ;  s#MIDGARD_STAGING_MYSQL_DB#$(MIDGARD_STAGING_MYSQL_DB)#g ;  s#MIDGARD_STAGING_BLOB_DIR#$(MIDGARD_STAGING_BLOB_DIR)#g ; "  etc/ssc_vhost.conf  etc/midgard_live.conf  etc/midgard_staging.conf

	@# NOTE I AM USING \z AS AN EQUIVALENT OF $ IN THE REGEXP
	perl -pi -e "s#^port\s+\d+\s*\z#Port $(APACHE_PORT)\n#gi" local/$(APACHE)/conf/httpd.conf
	perl -pi -e "s#^user\s+\w+\s*\z#User $(APACHE_USER)\n#gi" local/$(APACHE)/conf/httpd.conf
	perl -pi -e "s#^group\s+\w+\s*\z#Group $(APACHE_GROUP)\n#gi" local/$(APACHE)/conf/httpd.conf
	perl -pi -e "s#^maxrequestsperchild\s+\d+\s*\z#MaxRequestsPerChild 10000\n#gi" local/$(APACHE)/conf/httpd.conf
	perl -pi -e "s#^maxclients\s+\d+\s*\z#MaxClients 40\n#gi" local/$(APACHE)/conf/httpd.conf
	
	@# Commment out old include lines
	perl -pi -e "s/^(include.*ssc_vhost.*)/#\1\n/gi" local/$(APACHE)/conf/httpd.conf
	
	echo "" >> local/$(APACHE)/conf/httpd.conf
	echo "" >> local/$(APACHE)/conf/httpd.conf
	echo "" >> local/$(APACHE)/conf/httpd.conf
	echo "# Added by apache-conf $(CURRENT_TIMESTAMP)" >> local/$(APACHE)/conf/httpd.conf
	echo "include $(DIST_DIR)/etc/ssc_vhost.conf" >> local/$(APACHE)/conf/httpd.conf

	cp htdocs/etc/ssc_php.inc.dist htdocs/etc/ssc_php.inc
	perl -pi -e "\
			s#MYSQL_HOST#$(MYSQL_HOST)#g ; \
			s#MYSQL_USER#$(MYSQL_USER)#g ; \
			s#MYSQL_PW#$(MYSQL_PW)#g ;  \
			s#MYSQL_DB_MAILINGLIST#$(MYSQL_DB_MAILINGLIST)#g ; \
			s#MYSQL_DB_SEARCH_CACHE#$(MYSQL_DB_SEARCH_CACHE)#g ; \
			s#MYSQL_DB#$(MYSQL_DB)#g ; \
			s#GENI_IP#$(GENI_IP)#g ; \
			s#GENI_PORT#$(GENI_PORT)#g ;\
			s#GENI_PATH#$(GENI_PATH)#g ; \
			s#MIDGARD_RCS_DIR#$(MIDGARD_RCS_DIR)#g ;\
			s#DIST_DIR#$(DIST_DIR)#g ;\
			s#AT_CMD#$(AT_CMD)#g ;\
			s#ML_BOUNCE_THRESHOLD#$(ML_BOUNCE_THRESHOLD)#g ;\
			"  htdocs/etc/ssc_php.inc  


## ------------------------------------------------------------------
##	     Scripts: Copy & Config
## ------------------------------------------------------------------

scripts:

	@cp util/mgd-utils/golive.sh.dist util/mgd-utils/golive.sh
	@chmod ugo+rx util/mgd-utils/golive.sh.dist util/mgd-utils/golive.sh
	@perl -pi -e "\
				s#DIST_DIR#$(DIST_DIR)#g ;\
			"  util/mgd-utils/golive.sh
			
	@cp util/mailinglist/send.sh.dist util/mailinglist/send.sh
	@chmod ugo+rx util/mailinglist/send.sh.dist util/mailinglist/send.sh
	@perl -pi -e '\
				s#DIST_DIR#$(DIST_DIR)#g ;\
				s#ML_SENDER_DOMAIN#$(ML_SENDER_DOMAIN)#g ;\
				s#ML_SENDER_ADDRESS#$(ML_SENDER_ADDRESS)#g ;\
				s#ML_SENDER_NAME#$(ML_SENDER_NAME)#g ;\
				s#MYSQL_HOST#$(MYSQL_HOST)#g ;\
				s#MYSQL_DB_MAILINGLIST#$(MYSQL_DB_MAILINGLIST)#g ;\
				s#MYSQL_USER#$(MYSQL_USER)#g ;\
				s#MYSQL_PW#$(MYSQL_PW)#g ;\
				s#ML_CONTROL_URL#$(ML_CONTROL_URL)#g ;\
				s#ML_BOUNCE_THRESHOLD#$(ML_BOUNCE_THRESHOLD)#g ;\
				s#ML_NEWS_URL#$(ML_NEWS_URL)#g ;\
				s#MIDGARD_ADMIN_USER#$(MIDGARD_ADMIN_USER)#g ;\
				s#MIDGARD_ADMIN_PW#$(MIDGARD_ADMIN_PW)#g ;\
			'  util/mailinglist/send.sh
			
	@cp util/mailinglist/dot-qmail-default.dist util/mailinglist/dot-qmail-default
	@chmod 644 util/mailinglist/dot-qmail-default
	@perl -pi -e '\
				s#DIST_DIR#$(DIST_DIR)#g ;\
				s#MYSQL_HOST#$(MYSQL_HOST)#g ;\
				s#MYSQL_DB_MAILINGLIST#$(MYSQL_DB_MAILINGLIST)#g ;\
				s#MYSQL_USER#$(MYSQL_USER)#g ;\
				s#MYSQL_PW#$(MYSQL_PW)#g ;\
			'  util/mailinglist/dot-qmail-default


	@cp util/mailinglist/dot-qmail.dist util/mailinglist/dot-qmail
	@chmod 644 util/mailinglist/dot-qmail-default
	@perl -pi -e '\
				s#ML_ADMIN_ADDRESS#$(ML_ADMIN_ADDRESS)#g ;\
			'  util/mailinglist/dot-qmail
	
## ------------------------------------------------------------------
##	     Apache Start - convenient way for scripts to start apache
## ------------------------------------------------------------------

apache-start:
	
	local/$(APACHE)/bin/apachectl start
	
	
## ------------------------------------------------------------------
##	     Midgard Config
## ------------------------------------------------------------------

midgard-conf:
	
	# Fix extension_dir in php.ini
	# Note we are using the real file as Perl will 
	# not work properly with symlinks
	@#find $(DIST_DIR)/local/$(PHP) -name midgard.so  | \
	tail -n1 |  xargs dirname | \
	xargs -n1 -iFILE --verbose \
	perl -pi -e "s#^extension_dir =.*#extension_dir = FILE #" \
	$(LOCAL_DIR)/$(PHP)/lib/php.ini

	# production settings!
	perl -pi -e "s#^display_errors =.*#display_errors = off #" \
	$(LOCAL_DIR)/$(PHP)/lib/php.ini
	
	perl -pi -e "s#^log_errors =.*#log_errors = on #" \
	$(LOCAL_DIR)/$(PHP)/lib/php.ini

	perl -pi -e "s#^file_uploads =.*#file_uploads = off #" \
	$(LOCAL_DIR)/$(PHP)/lib/php.ini



## ------------------------------------------------------------------
##	     Midgard
## ------------------------------------------------------------------
midgard:
	make midgard-lib
	make midgard-data
	make mod-midgard
	make midgard-php4
	make midgard-conf
	@echo "Please remember to include"
	@echo "local/src/midgard-data-1.4.2-1/midgard-data.conf"
	@echo "in httpd.conf"

## ------------------------------------------------------------------
##	     Midgard-Lib 
## ------------------------------------------------------------------
midgard-lib:

	# patch REPLIGARD_DELETE so we get deletions replicated
	# nicely to LIVE. 
	cd $(SRC_DIR)/midgard-lib-$(MIDGARD_VERSION) && \
	patch midgard/midgard.h.in $(DIST_DIR)/util/build/midgard.h.in.patch

	cd $(SRC_DIR)/midgard-lib-$(MIDGARD_VERSION) && \
	./configure \
		--prefix=$(LOCAL_DIR)/midgard-$(MIDGARD_VERSION) \
		--with-mysql=$(MYSQL) --with-repligard-owner=$(APACHE_USER) \
		--with-sitegroups --with-mgd-eval-hack
		
	cd $(SRC_DIR)/midgard-lib-$(MIDGARD_VERSION) && \
	make
	
	cd $(SRC_DIR)/midgard-lib-$(MIDGARD_VERSION) && \
	make install	


## ------------------------------------------------------------------
##	     Midgard-Data 
## ------------------------------------------------------------------
midgard-data: 

	@echo "Midgard-Data"
	# prep mysql
	@echo "Setting up a new user in the db."

	
	
	echo \
		"GRANT ALL ON $(MIDGARD_MYSQL_DB).* TO '$(MIDGARD_MYSQL_USER)' \
						 IDENTIFIED BY '$(MIDGARD_MYSQL_PW)'; \
		GRANT ALL ON $(MIDGARD_STAGING_MYSQL_DB).* TO '$(MIDGARD_MYSQL_USER)' \
						 IDENTIFIED BY '$(MIDGARD_MYSQL_PW)'; \
						 FLUSH PRIVILEGES;"\
	      | $(MYSQL_CLIENT) -uroot -p$(MIDGARD_MYSQL_ROOT_PW)

	#
	# dropping the db if it exists!
	#
	-echo	"DROP DATABASE $(MIDGARD_MYSQL_DB)  ; \
		 DROP DATABASE $(MIDGARD_STAGING_MYSQL_DB) "\
	      | $(MYSQL_CLIENT) -uroot -p$(MIDGARD_MYSQL_ROOT_PW)

    
	#
	# flushing any possible old blobdir
	#
	-rm -fr $(MIDGARD_RCS_DIR)
	-rm -fr $(MIDGARD_BLOB_DIR)
	-rm -fr $(MIDGARD_CACHE_DIR)
	-rm -fr $(MIDGARD_STAGING_BLOB_DIR)
	-rm -fr $(MIDGARD_STAGING_CACHE_DIR)

	mkdir -p $(MIDGARD_RCS_DIR)	
	mkdir -p $(MIDGARD_BLOB_DIR)
	mkdir -p $(MIDGARD_CACHE_DIR)
	mkdir -p $(MIDGARD_STAGING_BLOB_DIR)
	mkdir -p $(MIDGARD_STAGING_CACHE_DIR)
	

	# FLUSH
	find  $(SRC_DIR)/* -name "midgard-data*" -prune  -type d | grep -v CVS | xargs -n1 --verbose rm -fr
	cd $(SRC_DIR) && ls midgard-data*.tar.bz2 | xargs -n1 --verbose tar --bzip -xf



	#
	#   LIVE
	#
	@echo "Patching dbinstall.in"
	cd $(SRC_DIR)/midgard-data-$(MIDGARD_VERSION) && \
	patch dbinstall.in $(DIST_DIR)/util/build/dbinstall.in.patch	

	cd $(SRC_DIR)/midgard-data-$(MIDGARD_VERSION) && \
	./configure \
		--with-db-host=$(MIDGARD_MYSQL_HOST) \
		--with-db-admin-user=$(MIDGARD_MYSQL_ROOT_USER) \
		--with-db-admin-password=$(MIDGARD_MYSQL_ROOT_PW) \
		--with-db-user=$(MIDGARD_MYSQL_USER) \
		--with-db-user-password=$(MIDGARD_MYSQL_PW)  \
		--with-db=$(MIDGARD_MYSQL_DB) \
		--with-host=$(MIDGARD_VHOST) \
		--with-port=$(MIDGARD_PORT) \
		--with-blobdir=$(MIDGARD_BLOB_DIR) \
		--with-pagecache=$(MIDGARD_CACHE_DIR) \
		--with-midgard=$(LOCAL_DIR)/midgard-$(MIDGARD_VERSION) \
		--with-apxs=$(APXS) \
		--with-apache-user=$(APACHE_USER) \
		--with-apache-group=$(APACHE_GROUP) \
		--with-adminsite \
		--with-lang=en \
		--with-asgard  \
		--with-php=4 \
		--with-install=new
	
	

	
	cd $(SRC_DIR)/midgard-data-$(MIDGARD_VERSION) && \
	./dbinstall \

	# FLUSH
	find  $(SRC_DIR)/* -name "midgard-data*" -prune  -type d | grep -v CVS | xargs -n1 --verbose rm -fr
	cd $(SRC_DIR) && ls midgard-data*.tar.bz2 | xargs -n1 --verbose tar --bzip -xf

	
	#
	#   STAGING
	#
	@echo "Patching dbinstall.in"
	cd $(SRC_DIR)/midgard-data-$(MIDGARD_VERSION) && \
	patch dbinstall.in $(DIST_DIR)/util/build/dbinstall.in.patch	

	cd $(SRC_DIR)/midgard-data-$(MIDGARD_VERSION) && \
	./configure \
		--with-db-host=$(MIDGARD_MYSQL_HOST) \
		--with-db-admin-user=$(MIDGARD_MYSQL_ROOT_USER) \
		--with-db-admin-password=$(MIDGARD_MYSQL_ROOT_PW) \
		--with-db-user=$(MIDGARD_MYSQL_USER) \
		--with-db-user-password=$(MIDGARD_MYSQL_PW)  \
		--with-db=$(MIDGARD_STAGING_MYSQL_DB) \
		--with-host=$(MIDGARD_STAGING_VHOST) \
		--with-port=$(MIDGARD_STAGING_PORT) \
		--with-blobdir=$(MIDGARD_STAGING_BLOB_DIR) \
		--with-pagecache=$(MIDGARD_STAGING_CACHE_DIR) \
		--with-midgard=$(LOCAL_DIR)/midgard-$(MIDGARD_VERSION) \
		--with-apxs=$(APXS) \
		--with-apache-user=$(APACHE_USER) \
		--with-apache-group=$(APACHE_GROUP) \
		--with-adminsite \
		--with-lang=en \
		--with-asgard  \
		--with-php=4 \
		--with-install=new
	
	cd $(SRC_DIR)/midgard-data-$(MIDGARD_VERSION) && \
	./dbinstall \

	@echo "copying images into place"	
	cd $(SRC_DIR)/midgard-data-$(MIDGARD_VERSION) && \
	cp -pr images/* $(DIST_DIR)/htdocs/		


	#
	# Restore libraries (should this be in midgard-data?)
	#
	# Ensure configuration is correct
	make repligard-update-conf
	
	# Restore NemeinRCS to staging and live
	./local/midgard-$(MIDGARD_VERSION)/bin/repligard \
	-c ./etc/repligard_local/repligard_dev_db.conf \
	-i ./data/NemeinRCS.xml
	./local/midgard-$(MIDGARD_VERSION)/bin/repligard \
	-c ./etc/repligard_local/repligard_live_db.conf \
	-i ./data/NemeinRCS.xml

	# Disabling the Example Site on STAGING
	echo " DELETE FROM host WHERE prefix='/example' AND sitegroup=0; DELETE FROM topic WHERE up=0 AND sitegroup=0 AND name LIKE '%example%'; DELETE FROM page  WHERE up=0 AND sitegroup=0 AND name LIKE '%VMUC%'; DELETE FROM style WHERE up=0 AND sitegroup=0 AND name LIKE '%Virtual Midgard Using Company%';" | \
	mysql -u$(MIDGARD_MYSQL_USER) -p$(MIDGARD_MYSQL_PW) $(MIDGARD_STAGING_MYSQL_DB)

	# Disabling the Example Site on LIVE
	echo " DELETE FROM host WHERE prefix='/example' AND sitegroup=0; DELETE FROM topic WHERE up=0 AND sitegroup=0 AND name LIKE '%example%'; DELETE FROM page  WHERE up=0 AND sitegroup=0 AND name LIKE '%VMUC%'; DELETE FROM style WHERE up=0 AND sitegroup=0 AND name LIKE '%Virtual Midgard Using Company%';" | \
	mysql -u$(MIDGARD_MYSQL_USER) -p$(MIDGARD_MYSQL_PW) $(MIDGARD_MYSQL_DB)



	#
	# You may want to issue a restore
	# using `make rdrestore`
	# and   `make rcrestorehost`
	#


## ------------------------------------------------------------------
##	     Mod_Midgard
## ------------------------------------------------------------------
mod-midgard:
	@echo "Mod_Midgard"
	cd $(SRC_DIR)/mod_midgard-$(MIDGARD_VERSION) && \
	./configure \
		--prefix=$(LOCAL_DIR)/midgard-$(MIDGARD_VERSION) \
		--with-apxs=$(APXS) \
		--with-midgard=$(LOCAL_DIR)/midgard-$(MIDGARD_VERSION)  \
		--with-midgard-config=$(LOCAL_DIR)/midgard-$(MIDGARD_VERSION)/bin/midgard-config  \
		--with-mysql=$(MYSQL_CLIENT) \
		--with-sitegroups

		
	cd $(SRC_DIR)/mod_midgard-$(MIDGARD_VERSION) && \
	make 

	cd $(SRC_DIR)/mod_midgard-$(MIDGARD_VERSION) && \
	make install 

	# make conf is deprecated

## ------------------------------------------------------------------
##	     Midgard-Php4 
## ------------------------------------------------------------------
midgard-php4:  		
  	
	#
	# I will 
	# - defang mkall (disable make/make install)
	# - run mkall
	# - re-run configure 
	# - make && make install 
	# I'll just have to keep putting the complete path there, 
	# just in case. 
	#

	#
	# Replacing calendar.c with a patched version
	#
	cp ./util/build/calendar.c $(SRC_DIR)/midgard-php4-$(MIDGARD_VERSION)/
	
	#
	# Defanging mkall	
	#
	cd $(SRC_DIR)/midgard-php4-$(MIDGARD_VERSION) && \
	perl -pi -e "s#^make#\# make#" mkall 
	
	cd $(SRC_DIR)/midgard-php4-$(MIDGARD_VERSION) && \
	perl -pi -e "s#^\./configure#\# ./configure#" mkall 
	
	#
	# Running (defanged) mkall (disregarding silly errors that autopconf gives!)
	#
	-PATH=$(LOCAL_DIR)/$(PHP)/bin:$(LOCAL_DIR)/$(APACHE)/bin:$(LOCAL_DIR)/midgard-$(MIDGARD_VERSION)/bin:$(PATH) && \
	cd $(SRC_DIR)/midgard-php4-$(MIDGARD_VERSION) && \
	sh mkall 
	
	#
	# Re-running configure
	#
	PATH=$(LOCAL_DIR)/$(PHP)/bin:$(LOCAL_DIR)/$(APACHE)/bin:$(LOCAL_DIR)/midgard-$(MIDGARD_VERSION)/bin:$(PATH) && \
	cd $(SRC_DIR)/midgard-php4-$(MIDGARD_VERSION) && \
	./configure  \
		--prefix=$(LOCAL_DIR)/midgard-$(MIDGARD_VERSION)  \
		--with-midgard=$(LOCAL_DIR)/midgard-$(MIDGARD_VERSION)/bin/midgard-config \
		--with-apxs=$(APXS)  \
		--with-php=$(LOCAL_DIR)/$(PHP) \
		--with-php-config=$(LOCAL_DIR)/$(PHP)/bin/php-config \
		--with-sitegroups \
		--with-old-mod-midgard

	cd $(SRC_DIR)/midgard-php4-$(MIDGARD_VERSION) && \
	make && make install
	
	echo ""
	echo "; Midgard Module" >> $(LOCAL_DIR)/$(PHP)/lib/php.ini
	echo "module=midgard.so" >> $(LOCAL_DIR)/$(PHP)/lib/php.ini 
	echo ""


	@echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++"
	@echo ""
	@echo " PLEASE edit your apachectl script to ensure     "
	@echo " the env variable LD_LIBRARY_PATH is configured  "
	@echo ""
	@echo " Add a line containing:"
	@echo " export LD_LIBRARY_PATH=$(LOCAL_DIR)/midgard-$(MIDGARD_VERSION)/lib"
	@echo ""
	@echo " and change your php.ini extension_dir to   "
	@echo " the location of midgard.so "
	@echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++"

	perl -pi -e "s#^ERROR#export LD_LIBRARY_PATH=$(LOCAL_DIR)/midgard-$(MIDGARD_VERSION)/lib\nERROR#g" local/$(APACHE)/bin/apachectl


## ------------------------------------------------------------------
##	     Mod_Midgard-Preparse 
## ------------------------------------------------------------------
midgard-mmp:


	@echo "MMP has not been elected/implemented here yet."



## ------------------------------------------------------------------
##	     Midgard-Filetemplates 
## ------------------------------------------------------------------
midgard-filetemplates:

	@echo "Filetemplates is not yet implemented"	
		


## ------------------------------------------------------------------
##	     Test
## ------------------------------------------------------------------
test:
	#
	# Run tests 
	# If you want to add tests
	# check in the util/test/t directory
	# or add them to runtests.sh
	#
	   export VHOST=http://$(APACHE_VHOST):$(APACHE_PORT) \
	&& sh ./util/test/runtests.sh
	
## ------------------------------------------------------------------
##	     Log (Changelog)
## ------------------------------------------------------------------
log:

	#
	# This generates the change logs in the docs directory
	#
	sh ./util/cvs-utils/cvs2cl.pl -f doc/Changelog.txt
## ------------------------------------------------------------------
##	     Log-HTML (Changelog in html)
## ------------------------------------------------------------------
	
log-html: 
	#
	# An HTML Version too! (doesn't work perfect though)
	# and it is slow....
	#
	-mkdir doc/Changelog_html
	-rm -fr doc/Changelog_html/*
	perl util/cvs-utils/cvs2html.pl -O doc/Changelog_html -a


## ------------------------------------------------------------------
##           Sitedump
## ------------------------------------------------------------------
site-dump:
	#
	# Will download the site to generate a 
	# CDROM-friendly version using a custom
	# version of lwp-rget
	#
	-rm -fr ./var/site_dump
	mkdir -p ./var/site_dump
	cd ./var/site_dump && \
	$(DIST_DIR)/util/lwp-rget \
	--hier \
	--tolower \
	--auth=$(APACHE_AUTH_USER):$(APACHE_AUTH_PASS) \
	--sleep=0 \
	--tolower \
	--keepext=text/css \
	http://$(APACHE_VHOST)/styles/
	
	cd ./var/site_dump && \
	$(DIST_DIR)/util/lwp-rget \
	--hier \
	--depth=20 \
	--auth=$(APACHE_AUTH_USER):$(APACHE_AUTH_PASS) \
	--limit=10000 \
	--nospace \
	--tolower \
	--sleep=0 \
	--prefix=http://$(APACHE_VHOST)/ \
	http://$(APACHE_VHOST)/homepage.php



## ------------------------------------------------------------------
##	     Repligard-related targets
## ------------------------------------------------------------------

## ------------------------------------------------------------------
##	     repligard-update-conf - repligard update of config files
## ------------------------------------------------------------------
repligard-update-conf:
	@#
	@# This target will take the repligard files from their .conf 
	@# version, and create the local version
	@#
	
	@#
	@# Create/Flush the local directory
	@#
	@-mkdir -p etc/repligard_local
	@-rm etc/repligard_local/* 
	@#
	@#Copy Files
	@#
	@cp etc/repligard*.conf etc/repligard_local/
	@#
	@# Update configs!
	@#
	@# Live setting in the replicate confs
	@find etc/repligard_local/ -type f -name "repligard_live*.conf" | \
	xargs \
	perl -pi -e "s#MIDGARD_MYSQL_USER#$(MIDGARD_MYSQL_USER)#g; s#MIDGARD_MYSQL_PW#$(MIDGARD_MYSQL_PW)#g; s#MIDGARD_MYSQL_DB#$(MIDGARD_MYSQL_DB)#g;s#DIST_DIR#$(DIST_DIR)#g; s#MIDGARD_ADMIN_USER#$(MIDGARD_ADMIN_USER)#g; s#MIDGARD_ADMIN_PW#$(MIDGARD_ADMIN_PW)#g; s#MIDGARD_BLOB_DIR#$(MIDGARD_BLOB_DIR)#g;" 

	
	@# General settings
	@find etc/repligard_local/ -type f -name "repligard*.conf" | \
	xargs \
	perl -pi -e "s#MIDGARD_MYSQL_USER#$(MIDGARD_MYSQL_USER)#g; s#MIDGARD_MYSQL_PW#$(MIDGARD_MYSQL_PW)#g; s#MIDGARD_MYSQL_DB#$(MIDGARD_STAGING_MYSQL_DB)#g;s#DIST_DIR#$(DIST_DIR)#g; s#MIDGARD_ADMIN_USER#$(MIDGARD_ADMIN_USER)#g; s#MIDGARD_SITEGROUP#$(MIDGARD_SITEGROUP)#g; s#MIDGARD_ADMIN_PW#$(MIDGARD_ADMIN_PW)#g; s#MIDGARD_BLOB_DIR#$(MIDGARD_STAGING_BLOB_DIR)#g;" 
	
	
## ------------------------------------------------------------------
##	     rddump - repligard developer dump
## ------------------------------------------------------------------
r-dump:
	#
	# this will dump all the configured midgard files
	# for cvs committing and for distribution
	# 
	make repligard-update-conf
	
	# Adding missing GUIDs
	./local/midgard-$(MIDGARD_VERSION)/bin/repligard -m  \
	-c ./etc/repligard_local/repligard_dev_host.conf 
	
	#
	# DUMPING HOST       
	#
	./local/midgard-$(MIDGARD_VERSION)/bin/repligard -a  \
	-c ./etc/repligard_local/repligard_dev_host.conf \
	-e ./mdg-data/repligard_dev_host.xml.gz
	#
	# DUMPING PAGE 
	#
	./local/midgard-$(MIDGARD_VERSION)/bin/repligard -a  \
	-c ./etc/repligard_local/repligard_dev_page.conf \
	-e ./mdg-data/repligard_dev_page.xml.gz 
	#
	# DUMPING TOPICS AND ARTICLES
	#
	./local/midgard-$(MIDGARD_VERSION)/bin/repligard -a  \
	-c ./etc/repligard_local/repligard_dev_topic.conf \
	-e ./mdg-data/repligard_dev_topic.xml.gz

	#
	# DUMPING NEWS_PARTICIPATE
	#
	./local/midgard-$(MIDGARD_VERSION)/bin/repligard -a  \
	-c ./etc/repligard_local/repligard_news_participate.conf \
	-e ./mdg-data/repligard_news_participate.xml.gz

	#
	# DUMPING TOPICS ONLY
	#
	#./local/midgard-$(MIDGARD_VERSION)/bin/repligard -a  \
	#-c ./etc/repligard_local/repligard_dev_topic_strict.conf \
	#-e ./mdg-data/repligard_dev_topic_strict.xml.gz
	
	#
	# DUMPING STYLE
	#
	./local/midgard-$(MIDGARD_VERSION)/bin/repligard -a  \
	-c ./etc/repligard_local/repligard_dev_style.conf \
	-e ./mdg-data/repligard_dev_style.xml.gz
	#
	# DUMPING SNIPPETDIR
	#
	./local/midgard-$(MIDGARD_VERSION)/bin/repligard -a  \
	-c ./etc/repligard_local/repligard_dev_snippetdir.conf \
	-e ./mdg-data/repligard_dev_snippetdir.xml.gz
	#
	# DUMPING GROUP
	#
	./local/midgard-$(MIDGARD_VERSION)/bin/repligard -a  \
	-c ./etc/repligard_local/repligard_dev_group.conf \
	-e ./mdg-data/repligard_dev_group.xml.gz
	#
	# DUMPING ADMIN Portal Operations Team
	#
	./local/midgard-$(MIDGARD_VERSION)/bin/repligard -a  \
	-c ./etc/repligard_local/repligard_admin_pot.conf \
	-e ./mdg-data/repligard_admin_pot.xml.gz
	#
	# DUMPING SITEGROUPS
	#
	./local/midgard-$(MIDGARD_VERSION)/bin/repligard -a  \
	-c ./etc/repligard_local/repligard_sitegroup.conf \
	-e ./mdg-data/repligard_sitegroup.xml.gz


	#
	# DUMPING Version Control Snippet
	# DISABLED
	#./local/midgard-$(MIDGARD_VERSION)/bin/repligard -a  \
	#-c ./etc/repligard_local/repligard_dev_version_control.conf \
	#-e ./mdg-data/repligard_dev_version_control.xml.gz


	#
	# UNZIP'EM
	#
	gzip -f -d ./mdg-data/*.gz

	#
	# UN-Windows-'EM
	#
	dos2unix ./mdg-data/*.xml
	
	#
	# FIXING HOST 
	#
	perl -pi -e "s#<name>$(MIDGARD_STAGING_VHOST)</name>#<name>MIDGARD_STAGING_VHOST</name>#g" ./mdg-data/repligard_dev_host.xml
	perl -pi -e "s#<port>$(MIDGARD_STAGING_PORT)</port>#<port>MIDGARD_STAGING_PORT</port>#g" ./mdg-data/repligard_dev_host.xml
	
	# 
	# FIXING CHANGED FIELD
	#
	# This is important to reduce noise in the diffs and to
	# be able to know what's really changed in CVS.
	#
	# On import/restore we replace the date again for a current date. 
	#
	ls ./mdg-data/*.xml | xargs perl -pi -e 's#changed="\d{14}"#changed="00000000000000"#g'  
	
	#
	# SORTING INTERNAL CONTENTS
	#
	ls ./mdg-data/*.xml | xargs --verbose  -n1 perl util/mdg-data_parser.pl 
	
	
	#
	# GREPPING THEM FOR THE [TEST-DATA] STRING
	#
	-grep -l "TEST-DATA" \
		./mdg-data/repligard_dev_group.xml \
		./mdg-data/repligard_dev_snippetdir.xml \
		./mdg-data/repligard_dev_style.xml \
		./mdg-data/repligard_dev_topic_strict.xml \
		./mdg-data/repligard_dev_page.xml \
		./mdg-data/repligard_dev_host.xml \
		./mdg-data/repligard_admin_pot.xml 
		
	# An error here means it's OK

## ------------------------------------------------------------------
##	     rddump - repligard developer dump
## ------------------------------------------------------------------
r-dump-live-host:
	#
	# this will dump all the configured midgard files
	# for cvs committing and for distribution
	# 

	make repligard-update-conf
	
	#
	# DUMPING HOST       
	#
	./local/midgard-$(MIDGARD_VERSION)/bin/repligard -a \
	-c ./etc/repligard_local/repligard_live_host.conf \
	-e ./mdg-data/repligard_live_host.xml.gz

	#
	# UNZIP'EM
	#
	gzip -f -d ./mdg-data/repligard_live_host*.xml.gz

	#
	# UN-Windows-'EM
	#
	dos2unix ./mdg-data/repligard_live_host*.xml

	#
	# AUTO-PATCH'EM
	#
	ls ./mdg-data/repligard_live_host*.xml | xargs -n1 -iFILE patch -N FILE ./util/build/repligard_conf.patch 

	#
	# FIXING HOST 
	#
	perl -pi -e "s#<name>$(MIDGARD_VHOST)</name>#<name>MIDGARD_VHOST</name>#g" ./mdg-data/repligard_live_host.xml
	perl -pi -e "s#<port>$(MIDGARD_PORT)</port>#<port>MIDGARD_PORT</port>#g" ./mdg-data/repligard_live_host.xml
	
	# 
	# FIXING CHANGED FIELD
	#
	# This is important to reduce noise in the diffs and to
	# be able to know what's really changed in CVS.
	#
	# On import/restore we replace the date again for a current date. 
	#
	ls ./mdg-data/repligard_live_host*.xml | xargs perl -pi -e 's#changed="\d{14}"#changed="00000000000000"#g'  
	
	#
	# SORTING INTERNAL CONTENTS
	#
	ls ./mdg-data/repligard_live_host**.xml | xargs --verbose  -n1 perl util/mdg-data_parser.pl 
	

## ------------------------------------------------------------------
##	     r-restore - repligard developer restore
## ------------------------------------------------------------------
r-restore:
	make r-restore-code
	make r-restore-data

	#
	# NOTE: We have not restored the Host table
	#       use 'make r-restore-staging-host' if you need. 
	#
## ------------------------------------------------------------------
##	     r-restore - repligard developer restore
## ------------------------------------------------------------------
r-restore-code:

	#
	# this will load all the developed code 
	# and sample content to build a development environment
	#

	# Ensure configuration is correct
	make repligard-update-conf

	# Ensure updated fields are up to date
	ls ./mdg-data/*.xml | xargs perl -pi -e 's#changed="00000000000000"#changed="$(CURRENT_TIMESTAMP)"#g'
	
	#
	# RESTORING SITEGROUP
	#
	./local/midgard-$(MIDGARD_VERSION)/bin/repligard \
	-c ./etc/repligard_local/repligard_dev_db.conf \
	-i ./mdg-data/repligard_sitegroup.xml

	# 
	# RESTORING GROUP (and users!)
	#
	./local/midgard-$(MIDGARD_VERSION)/bin/repligard \
	-c ./etc/repligard_local/repligard_dev_db_sg.conf \
	-i ./mdg-data/repligard_dev_group.xml

	#
	# RESTORING ROOT Page and subpages			
	#
	./local/midgard-$(MIDGARD_VERSION)/bin/repligard \
	-c ./etc/repligard_local/repligard_dev_db_sg.conf \
	-i ./mdg-data/repligard_dev_page.xml

	#
	# RESTORING STYLES
	#
	./local/midgard-$(MIDGARD_VERSION)/bin/repligard \
	 -c ./etc/repligard_local/repligard_dev_db_sg.conf \
	 -i ./mdg-data/repligard_dev_style.xml
	#
	# RESTORING SNIPPETDIR
	#
	./local/midgard-$(MIDGARD_VERSION)/bin/repligard \
	 -c ./etc/repligard_local/repligard_dev_db_sg.conf \
	 -i ./mdg-data/repligard_dev_snippetdir.xml

	#
	# RESTORING ADMIN Portal Operations Team
	#
	./local/midgard-$(MIDGARD_VERSION)/bin/repligard \
	-c ./etc/repligard_local/repligard_dev_db_sg.conf \
	-i ./mdg-data/repligard_admin_pot.xml

	#
	# RESTORING Version Control Snippet
	# 
	./local/midgard-$(MIDGARD_VERSION)/bin/repligard -a \
	-c ./etc/repligard_local/repligard_dev_db_sg.conf \
	-i ./mdg-data/repligard_dev_version_control.xml


	# Now resetting CHANGED field
	ls ./mdg-data/*.xml | xargs perl -pi -e 's#changed="\d{14}"#changed="00000000000000"#g'  



## ------------------------------------------------------------------
##	     r-restore-data - repligard 
## ------------------------------------------------------------------
r-restore-data:

	#
	# this will load all the developed code 
	# and sample content to build a development environment
	#

	# Ensure configuration is correct
	make repligard-update-conf


	# Ensure updated fields are up to date
	ls ./mdg-data/*.xml | xargs perl -pi -e 's#changed="00000000000000"#changed="$(CURRENT_TIMESTAMP)"#g'
	
	#
	# RESTORING TOPICS 
	#
	./local/midgard-$(MIDGARD_VERSION)/bin/repligard \
	 -c ./etc/repligard_local/repligard_dev_db_sg.conf \
	 -i ./mdg-data/repligard_dev_topic.xml
	#
	# RESTORING NEWS_PARTICIPATE ARTICLES
	#
	./local/midgard-$(MIDGARD_VERSION)/bin/repligard \
	 -c ./etc/repligard_local/repligard_dev_db_sg.conf \
	 -i ./mdg-data/repligard_news_participate.xml

	# Now resetting CHANGED field
	ls ./mdg-data/*.xml | xargs perl -pi -e 's#changed="\d{14}"#changed="00000000000000"#g'  


## ------------------------------------------------------------------
##	     r-restore-staging-host - repligard staging restore host
## ------------------------------------------------------------------
r-restore-staging-host:

	# Ensure configuration is correct
	make repligard-update-conf

	#
	# RESTORING HOST TABLE
	#
	# This is a tricky task, so 
	# we'll make a nice effort.
	# 
	# You can always do it by hand 
	# if it proves to be too much for me. 
	#
	#
	# COPY AND LOCALIZE THE FILE
	#
	cp ./mdg-data/repligard_dev_host.xml ./mdg-data/repligard_dev_host.xml.tmp
	perl -pi -e "s#<name>MIDGARD_STAGING_VHOST</name>#<name>$(MIDGARD_STAGING_VHOST)</name>#g" ./mdg-data/repligard_dev_host.xml.tmp
	perl -pi -e "s#<port>MIDGARD_STAGING_PORT</port>#<port>$(MIDGARD_STAGING_PORT)</port>#g" ./mdg-data/repligard_dev_host.xml.tmp

	# Ensure updated fields are up to date
	perl -pi -e 's#changed="00000000000000"#changed="$(CURRENT_TIMESTAMP)"#g' ./mdg-data/repligard_dev_host.xml.tmp

	#
	# INSERT THE LOCAL FILE
	#
	./local/midgard-$(MIDGARD_VERSION)/bin/repligard \
	-c ./etc/repligard_local/repligard_dev_db_sg.conf \
	-i ./mdg-data/repligard_dev_host.xml.tmp
	
	#
	# REMOVE THE LOCAL FILE
	#
	-rm ./mdg-data/repligard_dev_host.xml.tmp
	
	#
	# GET INTO MYSQL AND ENSURE ITS CORRECT
	#
	echo "UPDATE host SET name='$(MIDGARD_STAGING_VHOST)',port=$(MIDGARD_STAGING_PORT)" | \
	mysql -u$(MIDGARD_MYSQL_USER) -p$(MIDGARD_MYSQL_PW) $(MIDGARD_STAGING_MYSQL_DB)

## ------------------------------------------------------------------
##	     r-restore-live-host - repligard staging restore host
## ------------------------------------------------------------------
r-restore-live-host:

	# Ensure configuration is correct
	make repligard-update-conf


	#
	# RESTORING HOST TABLE
	#
	# This is a tricky task, so 
	# we'll make a nice effort.
	# 
	# You can always do it by hand 
	# if it proves to be too much for me. 
	#
	#
	# COPY AND LOCALIZE THE FILE
	#
	cp ./mdg-data/repligard_live_host.xml ./mdg-data/repligard_live_host.xml.tmp
	perl -pi -e "s#<name>MIDGARD_VHOST</name>#<name>$(MIDGARD_VHOST)</name>#g" ./mdg-data/repligard_live_host.xml.tmp
	perl -pi -e "s#<port>MIDGARD_PORT</port>#<port>$(MIDGARD_PORT)</port>#g" ./mdg-data/repligard_live_host.xml.tmp

	# Ensure updated fields are up to date
	perl -pi -e 's#changed="00000000000000"#changed="$(CURRENT_TIMESTAMP)"#g' ./mdg-data/repligard_live_host.xml.tmp

	#
	# INSERT THE LOCAL FILE
	#
	./local/midgard-$(MIDGARD_VERSION)/bin/repligard \
	-c ./etc/repligard_local/repligard_live_db_sg.conf \
	-i ./mdg-data/repligard_live_host.xml.tmp
	
	#
	# REMOVE THE LOCAL FILE
	#
	-rm ./mdg-data/repligard_live_host.xml.tmp
	
	#
	# GET INTO MYSQL AND ENSURE ITS CORRECT
	#
	echo "UPDATE host SET name='$(MIDGARD_VHOST)',port=$(MIDGARD_PORT)" | \
	mysql -u$(MIDGARD_MYSQL_USER) -p$(MIDGARD_MYSQL_PW) $(MIDGARD_MYSQL_DB)



## ------------------------------------------------------------------
##	     r-golive - repligard go live!
## ------------------------------------------------------------------
r-golive:

	@echo
	@echo
	@echo "[$(CURRENT_TIMESTAMP)] RUNNING r-golive"	
	@echo
	@# prep my directory
	@mkdir -p $(REPLIGARD_TMP)
	
	@# update config
	@make repligard-update-conf

	
	@# run perl script
	@util/mgd-utils/downgrade_unapproved_articles.pl \
		--dbhost $(MYSQL_HOST) \
		--dbname $(MIDGARD_STAGING_MYSQL_DB) \
		--dbuser $(MIDGARD_MYSQL_USER) \
		--dbpass $(MIDGARD_MYSQL_PW) 
	
	# run repligard export
	./local/midgard-$(MIDGARD_VERSION)/bin/repligard  \
	-c ./etc/repligard_local/repligard_dev_golive.conf  \
	-e $(REPLIGARD_TMP)/golive.xml.gz

	
	# run repligard import
	@./local/midgard-$(MIDGARD_VERSION)/bin/repligard  \
	-c ./etc/repligard_local/repligard_live_db_sg.conf  \
	-i $(REPLIGARD_TMP)/golive.xml.gz
	
	
	# clean up after ourselves
	@rm -fr $(REPLIGARD_TMP)

## ------------------------------------------------------------------
##          r-golive-force - repligard go live - triggered from web
## ------------------------------------------------------------------
r-golive-force:

	@echo
	@echo "==> Triggered from web interface"
	@echo
	make r-golive


## ------------------------------------------------------------------
##	     r-init-live - repligard force update live
## ------------------------------------------------------------------
r-init-live:

	# prep my directory
	mkdir -p $(REPLIGARD_TMP)
	
	# update config
	make repligard-update-conf

	
	#
	# RESTORING SITEGROUP
	#
	./local/midgard-$(MIDGARD_VERSION)/bin/repligard \
	-c ./etc/repligard_local/repligard_live_db.conf \
	-i ./mdg-data/repligard_sitegroup.xml

	
	# run repligard export
	./local/midgard-$(MIDGARD_VERSION)/bin/repligard -a  \
	-c ./etc/repligard_local/repligard_dev_golive.conf  \
	-e $(REPLIGARD_TMP)/golive.xml.gz

	
	# run repligard import
	./local/midgard-$(MIDGARD_VERSION)/bin/repligard  \
	-c ./etc/repligard_local/repligard_live_db_sg.conf  \
	-i $(REPLIGARD_TMP)/golive.xml.gz
	
	
	# clean up after ourselves
	rm -fr $(REPLIGARD_TMP)

## ------------------------------------------------------------------
##	     r-flush - 
## ------------------------------------------------------------------
r-flush:
	make midgard-data
	make r-restore
	make r-restore-staging-host
	make r-init-live
	make r-restore-live-host


## ------------------------------------------------------------------
##	     r-upgrade - 
## ------------------------------------------------------------------
r-upgrade:
	make r-restore-code
	make r-restore-staging-host
	
	# everything but articles are forced to golive
	# to ensure we replicate the code through 
	# without breaking the QA process.
	echo "UPDATE repligard SET updated=0 WHERE (realm <> 'article')" | \
	mysql -u$(MIDGARD_MYSQL_USER) -p$(MIDGARD_MYSQL_PW) $(MIDGARD_STAGING_MYSQL_DB)

	make r-restore-live-host

## ------------------------------------------------------------------
##	     ssc-news-import - Perform import
## ------------------------------------------------------------------
ssc-news-import:

	# dbname is tmp_$(CURRENT_TIMESTAMP)
	
	# set up permissions
	echo \
		"GRANT ALL ON tmp_$(CURRENT_TIMESTAMP).* TO '$(MIDGARD_MYSQL_USER)' \
						 IDENTIFIED BY '$(MIDGARD_MYSQL_PW)'; \
						 FLUSH PRIVILEGES;"\
	      | $(MYSQL_CLIENT) -uroot -p$(MIDGARD_MYSQL_ROOT_PW) -h $(MYSQL_HOST)

	#
	# dropping the db if it exists!
	#
	-echo "DROP DATABASE tmp_$(CURRENT_TIMESTAMP) ; "\
	      | $(MYSQL_CLIENT) -uroot -p$(MIDGARD_MYSQL_ROOT_PW) -h $(MYSQL_HOST)

	#
	# create the db
	#
	-echo "CREATE DATABASE tmp_$(CURRENT_TIMESTAMP) ; "\
	      | $(MYSQL_CLIENT) -uroot -p$(MIDGARD_MYSQL_ROOT_PW) -h $(MYSQL_HOST)
	      
	#
	# load the db
	#
	zcat data/database-dump-nzgo.gz \
	| iconv -f MS-ANSI -t UTF8      \
	| $(MYSQL_CLIENT) -u$(MIDGARD_MYSQL_USER) -p$(MIDGARD_MYSQL_PW) -h $(MYSQL_HOST) tmp_$(CURRENT_TIMESTAMP)
	
	#
	# call ssc_news_import.pl
	#
	perl ./util/ssc_news_import.pl \
	     --dbhost $(MYSQL_HOST) \
	     --dbname tmp_$(CURRENT_TIMESTAMP) \
	     --dbuser $(MIDGARD_MYSQL_USER) \
	     --dbpass $(MIDGARD_MYSQL_PW) \
	     --destdbname $(MIDGARD_STAGING_MYSQL_DB) 
		
	#
	# dropping the db - we are finished
	#
	echo "DROP DATABASE tmp_$(CURRENT_TIMESTAMP) ; "\
	      | $(MYSQL_CLIENT) -uroot -p$(MIDGARD_MYSQL_ROOT_PW) -h $(MYSQL_HOST)

## ------------------------------------------------------------------
##	     load-topic-map
## ------------------------------------------------------------------	      
load-topic-map:
	GET http://$(MIDGARD_STAGING_VHOST):$(APACHE_PORT)/control/load_topic_map.php

