#
# **************************************************************
# *                  SIMUSOL PROYECT                           *
# *                                                            *
# *         sceptre_simusol for SIMUSOL                        *
# *                                                            *
# *                                                            *
# *   Modified by                                             *
# *   Dolores Alia  and Diego.Saravia@gmail.com                *
# *   2017    
# *   form adaptation by Prof. 
# *   original developed by          for US air force.              *
# *   and could be used with GPL                               *
# *   or every free license with copyleft needed.              *
# *   http://www.simusol.org                                   *
# **************************************************************
# Requiere la instalacion PREVIA de summapack
# This file is part of Simusol proyect
# 
#
#    sceptre_simusol is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Table is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Table.  If not, see <http://www.gnu.org/licenses/>.

# antes de lib
all:
#compile  no porque ahora se compila en post, all no hace nada



#install  # mejor que install se especifique adrede



define findfirstSBX
$(shell $(shellpath) if test -s ./$1 ; then \
                   echo .;\
                 elif test -s ~/$1 ; then \
                    echo ~;\
                 elif test -s /usr/local/$1 ; then \
                   echo /usr/local;\
                 elif  test -s /usr/$1 ; then \
                    echo /usr;\
                 fi;)
endef

#cpmakefilelib:
#	cp ../summapack/share/summapack/data/makefile.lib ../tabular/share/summapack/data/makefile.lib 

PREFIXMLIB=$(call findfirstSBX,share/summapack/data/makefile.lib)

ifneq ("$(wildcard $(PREFIXMLIB)/share/summapack/data/makefile.lib)","")
  include $(PREFIXMLIB)/share/summapack/data/makefile.lib
endif
$(info MAKELIB: ${PREFIXMLIB})

#include /usr/local/share/summapack/data/makefile.lib
# buscar donde esta!!!


#BASETMP := $(shell $(shellpath) mktemp -d |sed -e's/\n//'|sed -e's/^\/tmp\///')
# con := permanece cte
#$(info BASETMP: ${BASETMP}) 
#BASETMP=`mktemp -d`;

#################################################################33
# para hacer install sobre un directorio
#DBP=/tmp/BDsceptresimusol
#DBP=


# PACKV corrije, si el nombre tiene - lo pasa a _, no es conveniente nombres de paquetes con - en las tgz o tar.gz
# tgz se usa en paquetes AI o SRC/BIN/DOC/DEV, AI es de arquitectura independiente, si no lo es se divide en cuatro SRC BIN DEV y DOC
DIRICON=$(PACK)



# SIN DBP instala, con DBP crea arbol para distribucion 

# USO COMO TARGZ DESARROLLO (estandar) (make + sudo make install)
# compilacion instalacion a mano, uso como desarrollador
 DBP=
 PREFIX = ${DBP}/usr/local
 SRCDIR=src


# USO COMO TARGZ PARA DISTRIBUCION (otros lo instalen e instalen fuentes) (sudo make install)
# para instalar a mano completo desde make, sin paquete
# DBP=
# PREFIX = ${DBP}/usr/local
# SRCDIR=${PREFIX}/src


# USO COMO TARGZ PARA DISTRIBUCION (otros lo instalen e instalen con fuentes, en su HOME sin sudo ) (make, make install)
# para instalar a mano completo desde make, sin paquete
# DBP=
# PREFIX = /home/USER
# SRCDIR=${PREFIX}/src


# USO COMO TARGZ PARA DISTRIBUCION (otros lo instalen e instalen sin fuentes, en su HOME sin sudo ) (make, make install)
# para instalar a mano completo desde make, sin paquete
# DBP=
# PREFIX = /home/USER
# SRCDIR=src

# PAQUETE DISTRO QUE COMPILA AL INSTALAR (CON FUENTES), POSTINST (Recomendado) o sin ejecutar POSTINST como paquete fuente
# para crear paquete fuente  para distro no compilado 
# DBP=/tmp/BDsceptresimusol
# PREFIX = ${DBP}/usr

#  SRCDIR=${PREFIX}/src

# PAQUETE DISTRO CON BINARIOS (SIN FUENTES), PRECOMPILADO (forma estandar, pero inconveniente en eete caso, obliga a tener paquetes por arquitectura)
# Se debe hacer por separado PAQUETE DISTRO FUENTE
# para crear paquete para distro no precompilado, (no  paquete fuente, para compilar con postinst)
# DBP=/tmp/BDsceptresimusol
# PREFIX = ${DBP}/usr
# SRCDIR=src

# HACER INSTALA FUENTES Y NO COMPILADOS???

#Nota: el sudo make install solo compila, si SRCDIR != DIR y solo instala binarios si estan compilados en  SRCDIR




#include  share/sceptre_simusol/COMPILE.LIB
FC      = gfortran
FFLAGS  = -fno-automatic  -finit-local-zero -O -W -Wall -Wextra -g
# salvo etc en home

compile:	
	cd ${SRCDIR}/packages/$(PACK)/auxpro;  ${MAKE} "FC=${FC}" "FFLAGS=${FFLAGS}"  DESTDIR=$(DESTDIR) PREFIX=$(PREFIX)  PKGDIR=$(PKGDIR)  
	cd ${SRCDIR}/packages/$(PACK)/phase1;  ${MAKE} "FC=${FC}" "FFLAGS=${FFLAGS}"  DESTDIR=$(DESTDIR) PREFIX=$(PREFIX)  PKGDIR=$(PKGDIR) 
	cd ${SRCDIR}/packages/$(PACK)/phase2;  ${MAKE} "FC=${FC}" "FFLAGS=${FFLAGS}" DESTDIR=$(DESTDIR) PREFIX=$(PREFIX)  PKGDIR=$(PKGDIR) 
	cd ${SRCDIR}/package/$(PACK)/extras;  ${MAKE} "FC=${FC}" "FFLAGS=${FFLAGS}"   DESTDIR=$(DESTDIR) PREFIX=$(PREFIX)  PKGDIR=$(PKGDIR) 

tests:
	rm -f /tmp/sceptretest/LOG
	EF=`ls share/sceptre_simusol/examples/*.d`;\
	mkdir -p /tmp/sceptretest;\
	cd /tmp/sceptretest; \
	for FILE in $${EF} ; do \
	echo $${FILE};\
	        echo                                                  >> LOG ;\
	        echo ======================================= $${FILE} >> LOG ;\
		sceptre  $(CURDIR)/$${FILE} | tee -a LOG | grep -P "(ended|aborted|deleted|error)";\
	done;

#Fortran runtime error
# nota el caso 3 se arregla con sudo



#ETCROOT=/
#BINDIR=bin
#ETCRES=etc
#ETCDIR=$(ETCROOT)etc
#SHAREDIR=share





# si SCRDIR=src se entiende que no se quiere instalar, dado que no tiene sentido instalrse sobre si misma

# dividir esto en install y post install

# osea  ./configure; sudo make prep; make; sudo make install; sudo make post

#	ls
#	ls /home/abuild


install:
	echo "Fin copy tree ${SRCDIR} - ${ISRCDIRS} - $(PREFIX) - $(SUDO) - $(DESTDIR) -  $(PACK)"
	$(call copytree,$(DESTDIR)$(PREFIX),$(DESTDIR),,$(DESTDIR)$(PREFIX))
	mandb >/dev/null
	echo fin instalacion $(PACK)


#	$(call copytree,$(PREFIX),$(ETCDIR),$(SUDO),$(PREFIX))
#	$(call listado,)
#	@echo "Installation finished Makefile sceptre_simusol"

#$pase ifeq de ifneq

installpostcompile:	
	cd ${SRCDIR}/packages/$(PACK)/auxpro;  ${MAKE} "FC=${FC}" "FFLAGS=${FFLAGS}"  DESTDIR=$(DESTDIR) PREFIX=$(PREFIX)  PKGDIR=$(PKGDIR)   install
	cd ${SRCDIR}/packages/$(PACK)/phase1;  ${MAKE} "FC=${FC}" "FFLAGS=${FFLAGS}"  DESTDIR=$(DESTDIR) PREFIX=$(PREFIX)  PKGDIR=$(PKGDIR) install
	cd ${SRCDIR}/packages/$(PACK)/phase2;  ${MAKE} "FC=${FC}" "FFLAGS=${FFLAGS}" DESTDIR=$(DESTDIR) PREFIX=$(PREFIX)  PKGDIR=$(PKGDIR) install
	cd ${SRCDIR}/packages/$(PACK)/extras;  ${MAKE} "FC=${FC}" "FFLAGS=${FFLAGS}"   DESTDIR=$(DESTDIR) PREFIX=$(PREFIX)  PKGDIR=$(PKGDIR) install
###	make  DESTDIR=$(DESTDIR) PREFIX=$(PREFIX)  PKGDIR=$(PKGDIR) -C src/sceptre_simusol/extras install


# no ser preparan paquetes binarios dependientes de arquitectura, dado que el usuario debe compilar
# sus programas, no se gana nada, y es muy poco mas lo que se hace luego de la instalacion y no cada usuario
# entonces hacemos un solo paquete binario universal que luego de instalarse se compila.

post:
	$(PREFIX)/bin/postinst_sceptre_simusol "${PREFIX}" MAKE "${DESTDIR}" "${PKGDIR}"


#	$(call mexec,)
# OJO ESTO SE DEBIERA PODER HACER con "make DESTDIR=XXX install"
#y luego un tar y md5



#	$(call mexec,/tmp/${BASETMP})


#for FILE in ${BINFILES} ; do \
#		FILEP=`basename  $${FILE}`;\
#		install -m 755  $${FILE} ${PREFIX}/${BINDIR}/$${FILEP}  ;\
#		echo "A:${PREFIX}/${BINDIR}/$${FILEP}" >> listado.ls ; \
#		echo "A:${PREFIX}/${BINDIR}/$${FILEP}" ; \
#		done;
#	for FILE in ${LIBFILES} ; do \
#		FILEP=`basename  $${FILE}`;\
#		install -m 644  $${FILE} ${PREFIX}/${LIBDIR}/$${FILEP}  ;\
#		echo "A:${PREFIX}/${LIBDIR}/$${FILEP}" >> listado.ls ; \
#		echo "A:${PREFIX}/${LIBDIR}/$${FILEP}"  ; \
#		done;


#NOMAKE para que no compile como sudo
#si debe compilar en postinst de un paquete, del tipo de simusol, Arquitectura independiente pero con binarios.
# usuarios compilan# se hace esto para no distribuir binaris, para simusol dond eusa el fortran no tiene sentido distriburio binarios


#	chown ${USERBASE}:${USERBASE} listado.ls 
#	install -d ${PREFIX}/${PACK}/ls
#	mv  ${PREFIX}/${PACK}/ls/${PACK}.ls ${PREFIX}/${PACK}/ls/${PACK}.ls.viejo 2>/dev/null; exit 0;
#	install listado.ls ${PREFIX}/${PACK}/ls/${PACK}.ls



# no se puede hacer clean pq compila

#	for FILE in ${BINFILES} ; do \
#		FILEP=`basename  $${FILE}`;\
#		install -m 644  $${FILE}  /tmp/${BASETMP}${PREFIX}/${BINDIR}/$${FILEP}  ;\
#		echo "A:${PREFIX}/${BINDIR}/$${FILEP}" >> listado.ls ; \
#		echo "A:${PREFIX}/${BINDIR}/$${FILEP}" ; \
#		done;
#	for FILE in ${LIBFILES} ; do \
#		FILEP=`basename  $${FILE}`;\
#		install -m 644  $${FILE}  /tmp/${BASETMP}${PREFIX}/${LIBDIR}/$${FILEP}  ;\
#		echo "A:${PREFIX}/${LIBDIR}/$${FILEP}" >> listado.ls ; \
#		echo "A:${PREFIX}/${LIBDIR}/$${FILEP}"  ; \
#		done;


#if test x${DBP} != x ; then \
#fi
#	 install -s -m 755 src/auxpro/asa            ${BINDIR}
#        no se lo crea en el momento de la instalacion sino en la ejecucion
#        install -m 644    ${SRCDIR}/phase2/sceptre2.o        ${LIBDIR};\
#	   echo "A:${LIBDIR}/sceptre2.o" >> listado.ls ; \


clean::
	rm -f listado.ls
	rm -rf *~
	cd ${SRCDIR}/packages/$(PACK)/auxpro; ${MAKE} $@ 
	cd ${SRCDIR}/packages/$(PACK)/phase1; ${MAKE} $@
	cd ${SRCDIR}/packages/$(PACK)/phase2; ${MAKE} $@
	cd ${SRCDIR}/packages/$(PACK)/extras; ${MAKE} $@

nonbinclean:
	rm -f listado.ls
	rm -rf *~
	cd ${SRCDIR}/packages/$(PACK)/auxpro; ${MAKE} $@ 
	cd ${SRCDIR}/packages/$(PACK)/phase1; ${MAKE} $@
	cd ${SRCDIR}/packages/$(PACK)/phase2; ${MAKE} $@
	cd ${SRCDIR}/packages/$(PACK)/extras; ${MAKE} $@

#dist: ../$(DISTDIR).tar.gz
#../$(DISTDIR).tar.gz: clean $(FILES)
#	mkdir -p /tmp/basura
#	rm -rf /tmp/basura/*
#	mkdir -p /tmp/basura/$(DISTDIR)
#	for FILE in ${DIRS} ; do \
#	  mkdir  /tmp/basura/$(DISTDIR)/$${FILE};\
#	done;
#	for FILE in ${FILES} ; do \
#	  cp $${FILE} /tmp/basura/$(DISTDIR)/$${FILE};\
#	done;
#	ACTDIR=`pwd`;\
#	cd /tmp/basura; tar cvzf $${ACTDIR}/../${DISTDIR}.tar.gz $(DISTDIR); 
#	md5sum ../$(DISTDIR).tar.gz > ../$(DISTDIR).tar.gz.md5; \
#       ls -l ../$(DISTDIR).tar.gz*;  \
#
## no necesariamente binaria, es la distribucion ya ubicada en el arbol del host
#bindist: install
#	if test x${DBP} = x ; then \
#	   echo "Defina DBP par poder usar, preponderantemente en \tmp"; \
#	   exit 1; \
#	fi
#	echo "CUIDADO BORRA todo ${DBP}"
#	rm -r ${DBP};  ########## CUIDADO NO SACAR PREGUNTA ANTERIOR
#	$(MAKE) install
#	if test ${SRCDIR} != src ; then \
#	   NAME=$(DISTDIR)-DistroS; \
#	 else \
 #          NAME=$(DISTDIR)-DistroB; \
#	fi ;\
#	DIRO=`pwd`;\
#	echo "ARCHIVO TAR.GZ CREADO $${DIRO}/$${NAME}.tar.gz";\
#	cd ${DBP}; tar -zcvf $${DIRO}/../$${NAME}.tar.gz .
#

#upload: dist
#	scp  ../$(DISTDIR).tar.gz  ../$(DISTDIR).tar.gz.md5 www-data@simusol.org:/var/www/simusol.org/public_html/downloads ;\
#	ssh www-data@simusol.org "cd /var/www/simusol.org/public_html/downloads; rm -f sceptre_simusol-last.tar.gz;  rm -f sceptre_simusol-last.tar.gz.md5; ln -s $(DISTDIR).tar.gz sceptre_simusol-last.tar.gz; ln -s $(DISTDIR).tar.gz.md5 sceptre_simusol-last.tar.gz.md5;"






#       install -d 			    ${BINDIR}
#	echo "D:${BINDIR}" >> listado.ls ;
#       install -d 			    ${ETCDIR}
#	echo "D:${SHAREDIRSCEPTRE}" >> listado.ls ;
#	install -d 			    ${SHAREDIRSCEPTRE}/examples
#	echo "D:${SHAREDIRSCEPTRE}/examples" >> listado.ls ;
#	install -d 			    ${SHAREDIRSCEPTRE}/data
#	echo "D:${SHAREDIRSCEPTRE}/data" >> listado.ls ;
#	install -d 			    ${SHAREDIRSCEPTRE}/launchers
#	echo "D:${SHAREDIRSCEPTRE}/launchers" >> listado.ls ;
#	 install -d                          ${SRCDIR};\
#	 echo "D:${SRCDIR}" >> listado.ls ; \
#	 install -d                          ${SRCDIR}/auxpro;\
#	 echo "D:${SRCDIR}/auxpro" >> listado.ls ; \
#	 install -d                          ${SRCDIR}/phase1;\
#	 echo "D:${SRCDIR}/phase1" >> listado.ls ; \
#	 install -d                          ${SRCDIR}/phase2 ;\
#	 echo "D:${SRCDIR}/phase2" >> listado.ls ; \
#	install -m 755    scripts/sceptre           ${BINDIR}
#	echo "A:${BINDIR}/sceptre" >> listado.ls ;
#	install -m 755    scripts/sceptre_simusol-install      ${BINDIR}
#	echo "A:${BINDIR}/sceptre_simusol-install" >> listado.ls ;
#	install -m 755    scripts/postinst           ${BINDIR}
#	echo "A:${BINDIR}/postinst" >> listado.ls ;
#	install -m 755    scripts/ngp               ${BINDIR}
#	echo "A:${BINDIR}/ngp" >> listado.ls ;
#	install -d                          ${SHAREDIRSCEPTRE}
#       install -m 644    share/modellib.dat ${SHAREDIRSCEPTRE}
#	echo "A:${SHAREDIRSCEPTRE}/modellib.dat" >> listado.ls ;	
#	install -m 644  share/data/Manual.png ${SHAREDIRSCEPTRE}/data  ;\
#	echo "A:${SHAREDIRSCEPTRE}/data/Manual.png" >> listado.ls ; \
#	install -m 644  share/launchers/Simusol-Sceptre-Manual.desktop ${SHAREDIRSCEPTRE}/launchers; \
#	echo "A:${SHAREDIRSCEPTRE}/launchers/Simusol-Sceptre-Manual.png" >> listado.ls ; \
#	install -m 644    share/usrlib.a     ${SHAREDIRSCEPTRE} ; echo 0;   ### esto es fuente????
#	   for FILE in ${SRCA} ; do \
#	        install -m 644  $${FILE} ${SRCDIR}/auxpro;\
#	        echo "A:${SRCDIR}/auxpro/$${FILE}" >> listado.ls ; \
#	        done;\
#	   for FILE in ${SRC1} ; do \
#	        install -m 644  $${FILE} ${SRCDIR}/phase1;\
#	        echo "A:${SRCDIR}/phase1/$${FILE}" >> listado.ls ; \
#	        done;\
#	   for FILE in ${SRCA} ; do \
#	        install -m 644  $${FILE} ${SRCDIR}/phase2;\
#	        echo "A:${SRCDIR}/phase2/$${FILE}" >> listado.ls ; \
#	        done; \
