# Makefile for leda package in PLEINPOT
#############################################################################
## PLEINPOT is developed and maintained by Philippe Prugniel
##            Observatoire de Lyon, F-69561 Saint Genis Laval
##            tel. (+33) 04 78 86 83 83 fax. (+33) 04 72 72 87 87
##            prugniel@obs.univ-lyon1.fr
##            http://www-obs.univ-lyon1.fr/~prugniel/
#############################################################################
# PhP 08/06/02 makefile for "packages/leda"
# version 08/02/03 (the html and cgi are now with the rest of HyperLeda http front-end)
# version 21/09/03 modified by chil for new table structure
# PhP 2003/09/22, fix mistakes in table owner...
# PhP 2003/12/01, fix mistakes error handling...
# PhP 2012/01/10, add a semafore to warn program for maintenance
#############################################################################

include ${PL_ROOT}/local_definitions.mk

# variables: DBPORT DBNAME DBADMI DBUSER defined in local_definitions.mk
# variables: PSQL PG_CONFIG defined in local_definitions.mk

# variable: PERL (path to perl + init) defined in local_definitions.mk


HLPSQL = ${PSQL} -h "${DBHOST}" -p ${DBPORT}  -d ${DBNAME} -U ${DBADMI}
# For connection with postgres superuser priviliges
APSQL = ${PSQL} -h "${DBHOST}" -p ${DBPORT}  -d ${DBNAME}

LEDA_DUMP = LEDAdump.sql.gz

HBINCAT = ${PL_ROOT}/bin/cat/N001.cat     ${PL_ROOT}/bin/cat/N003.cat \
                                          ${PL_ROOT}/bin/cat/N009.cat \
                                          ${PL_ROOT}/bin/cat/N011.cat \


#------------------------------------------------------------------------------
all: ${PL_ROOT}/bin/cat/Date_install ${HBINCAT}

${PL_ROOT}/bin/cat/Date_install: check_hl
	@ cp conscat/index_cat.log ${PL_ROOT}/bin/cat/index_cat.log
	@ cat conscat/C/*/log > ${PL_ROOT}/bin/cat/index_sam.log
	@ cp conscat/log.txt ${PL_ROOT}/conscat/.
	@ cp conscat/L*.txt ${PL_ROOT}/conscat/.
	@ mkdir -p ${PL_ROOT}/conscat/C/1012/doc && cp conscat/C/1012/doc/* ${PL_ROOT}/conscat/C/1012/doc/
	@ mkdir -p ${PL_ROOT}/conscat/C/1013/doc && cp conscat/C/1013/doc/* ${PL_ROOT}/conscat/C/1013/doc/
	@ mkdir -p ${PL_ROOT}/conscat/C/1017/doc && cp conscat/C/1017/doc/* ${PL_ROOT}/conscat/C/1017/doc/
	@ mkdir -p ${PL_ROOT}/conscat/C/1018/doc && cp conscat/C/1018/doc/* ${PL_ROOT}/conscat/C/1018/doc/
	@ ${CP} conscat/Date_build* ${PL_ROOT}/bin/cat/.
	@ date > ${PL_ROOT}/bin/cat/Date_install


backup_views:
# I do not know why we are doing this???
	-@test "${DBADMI}" && \
	${HLPSQL} -c "\dt pg_views_save" | grep "(1 row)" 2> /dev/null && \
	${HLPSQL} -t -c "DROP TABLE pg_views_save"  || exit 0;
	-@test "{DBADMI}" && \
	${HLPSQL} -c \
	  "SELECT * INTO TABLE pg_views_save FROM pg_views;"; \
	exit 0;

drop_table:  backup_views
# Create a semaphore for the cgi programs indicating a maintenance process
	-@date > ${HYP}/MAINTENANCE
# The list of leda tables is found in the table "hltables" (views are dropped
# by cascade)
	-@test "${DBADMI}" && \
	for table in `${APSQL} -t -c "select relname from hltables where dump is true and reltype='table'" | grep -v hltables `; do \
	  ${HLPSQL} -c "\dt $$table" | grep "(1 row)" 2> /dev/null && \
	  ${HLPSQL} -t -c "DROP TABLE \"$$table\" CASCADE" ; \
	done ||\
	exit 0;
	-@test "${DBADMI}" && \
	${HLPSQL} -c "\dt hltables" | grep "(1 row)" 2> /dev/null && \
	${HLPSQL} -t -c "DROP TABLE hltables"  || exit 0;


# In present version "meandata" is a VIEW but it was a table in old versions
# So, DROP meandata if it exists
	${HLPSQL} -c "\dt meandata" | grep "(1 row)" 2> /dev/null && \
	  ${HLPSQL} -c "DROP TABLE \"meandata\" CASCADE" || \
	  exit 0;

# In present version "a000" is a VIEW but it was a table in old versions
# So, DROP meandata if it exists
	${HLPSQL} -c "\dt a000" | grep "(1 row)" 2> /dev/null && \
	  ${HLPSQL} -c "DROP TABLE \"a000\" CASCADE" || \
	  exit 0;


populate_db: drop_table
	@test "${DBADMI}" && cd db &&\
	echo "loading dump (`date`)" &&\
	echo "... the reconstructed database will occupy about 9 GB on the disk" &&\
	echo "... this operation will take a long while" &&\
	gunzip -c ${LEDA_DUMP} | \
	${HLPSQL} || \
	{ echo "Failed to restore tables from dump"; exit 1; }

# grant all rights to DBADMI for all the tables and views
	@test "${DBADMI}" && \
	for table in `${APSQL} -t -c "select relname from hltables where dump is true"`; do\
	  ${HLPSQL} -c "GRANT ALL on \"$$table\" to \"${DBADMI}\"" ;\
	done || \
	{ echo "Failed to grant rights to \"${DBADMI}\""; exit 1; }


	@test "${DBADMI}" && \
	for table in `${APSQL} -t -c "select relname from hltables where dump is true and reltype='table'"`; do\
	  ${HLPSQL} -c "VACUUM ANALYZE \"$$table\" " ;\
	done || \
	{ echo "Failed to VACUUM ANALYSE the HyperLeda tables"; exit 1; }


	@${RM} ${LEDA_DUMP}
	@echo "database successfully restored"

restore_views: populate_db
	@echo "Restoring views build on top of the LEDA tables (if any)"
	@( ${HLPSQL} -t -c "SELECT 'CREATE OR REPLACE VIEW '||viewname||' AS '||definition||'  GRANT ALL ON '||viewname||' TO public;' FROM pg_views_save WHERE schemaname NOT IN ('pg_catalog','information_schema') AND viewname NOT IN  (SELECT viewname FROM pg_views WHERE schemaname NOT IN ('pg_catalog','information_schema'));" | grep CREATE | ${APSQL} ) || \
		{ echo "Warning: User-defined views were not restored correctly"; \
		  echo "Check your data in the database."; }
	@${HLPSQL} -c "DROP TABLE pg_views_save" || exit 0;

create_functions: restore_views
	@if test "${DBADMI}"; then \
	   cd db && ${HLPSQL} <functions.sql;\
	else \
	   echo "Use external HyperLeda database";\
	fi;


# run regression tests if database has been reconstructed (chil 2004/07)
check_hl: create_functions 
	-@${RM} ${HYP}/MAINTENANCE
	@ echo "Testing HyperLeda tables integrity .. (`date`)";\
	echo "[execute regtests.c using the table 'testsuite']";\
	${CCOMP} conscat/regtests.c ${LDFLAGS} ${LIBRARIES} ${LF2C} -o regtests; \
	${RUN} ./regtests 2>&1 || { ${RM} regtests; exit 1; }

	@${RM} regtests; 


#-----------------------------------------------------------------------------
${PL_ROOT}/bin/cat/N001.cat:  conscat/N001.cat;  @ ${CP} $? $@ 
${PL_ROOT}/bin/cat/N003.cat:  conscat/N003.cat;  @ ${CP} $? $@ 
${PL_ROOT}/bin/cat/N009.cat:  conscat/N009.cat;  @ ${CP} $? $@ 
${PL_ROOT}/bin/cat/N011.cat:  conscat/N011.cat;  @ ${CP} $? $@ 


#-----------------------------------------------------------------------------
# compile and install the functions for SQL access to HyperLeda extensions
#  This target is not executed yet. There is no SPI functions...
pg_hyperleda: 
	@ test -x "${PG_CONFIG}" || { \
	echo "--------------------- ERROR -----------------------------------";\
	echo "The pg_config script cannot be executed, the leda package";\
	echo "cannot be installed";\
	echo "check if the PATH environment variable is correctly set";\
	echo "---------------------------------------------------------------";\
	exit 1; }

	@ mkdir -p `${PG_CONFIG} --pkglibdir`

	@ for file in conscat/src/*.c; do \
	   ${CCOMP} ${DEFS} -c $$file -I`${PG_CONFIG} --includedir-server` -o $$file.o; \
	done || { \
	echo "--------------------- ERROR -----------------------------------";\
	echo "Could not compile the pg_hyperleda modules";\
	echo "The leda package cannot be installed";\
	echo "---------------------------------------------------------------";\
	exit 1; }

	@ OBJS=conscat/src/*.o; \
	test "x$$OBJS" = "x" && exit 0 || \
	${LD_SHARED} $$OBJS ${LDFLAGS} ${LIBRARIES} ${LF2C} -o `${PG_CONFIG} --pkglibdir`/pg_hyperleda.so &&\
	${RM} $$OBJS || { \
	echo "--------------------- ERROR -----------------------------------";\
	echo "Could not link the pg_hyperleda modules";\
	echo "The leda package cannot be installed";\
	echo "---------------------------------------------------------------";\
	exit 1; }
# Must drop and create the SQL functions for spi here...

##############################################################################
