#
# ***************************************************************
# *                  Tabular - SIMUSOL PROYECT                  *
# *                                                             *
# *   Table extension for SIMUSOL PROYECT, for use with:        *                
# *       +  C++ Mathematical Expression Toolkit Library        *
# *          which is located in URL:                           *
# *          http://www.partow.net/programming/exprtk/index.html*
# *          used in Siru/Simusol                               *
# *       +  SCEPTRE (System for Circuit Evaluation             *
# *          and Prediction of Transient Radiation Effects)     *
# *          http://alpha.fh-friedberg.de/iem/84.html           *
# *          used in Simusol                                    *
# *   The extension was developed by                            *
# *   Diego.Saravia@ututo.org, 2016                             * 
# *   and could be used in the same terms of the toolkit        *
# *   or every free license with copyleft needed.               *
# *   http://www.simusol.org                                    *
# ***************************************************************#
# This file is part of Tabular
#
#    Tabular is free software: you can redistribute it and/or modify
#    it under the terms of
#
#     1)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.
#
#    2) Any other free licence with copyleft recognized by the FSF
#
#    Tabular 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 Tabular.  If not, see <http://www.gnu.org/licenses/>.




#VERSION=0.1
#NAME=simusol_table


PREFIX=usr/local/
DESTDIR=

#exprtk 
SUBDIRS= src checkdir examples   #src esta incluido en los otros # no parece que no # src primero, di no encuentra simusol_table.h

.PHONY:  subdirs  $(SUBDIRS)


# SOLO PARA DEBIAN UBUNTU!!!! si no se usa summapack completo
# /usr/local/share/summapack/data/makefile.lib

# gnuplot5 para ubuntu menor a 17.10

all: $(SUBDIRS)

define findfirstSB
$(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

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

# SI NO ESTA BAJAR DEL SITIO 
ifneq ($(shell $(shellpath) test -e  $(PREFIXMLIB) && echo -n yes),yes)
    $(info  ADVERTENCIA makefile.lib de summapack no esta instalada)
    ifeq ($(shell $(shellpath) test x"$(VEROID)" = x0 && echo -n yes),yes)
	   PREFIXMLIB=$(DESTDIR)/usr/local
          else
           PREFIXMLIB=$(DESTDIR)$(HOME)
	endif
	$(shell $(shellpath) wget --no-check-certificate -P http://summapack.org/downloads/makefile.lib  $(PREFIXMLIB)/share/summapack/data/makefile.lib); echo 0;
	$(shell $(shellpath) chmod a+r $(PREFIXMLIB)/share/summapack/data/makefile.lib); echo 0;
endif

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

preparedistro: subversion check unzip gnuplot-x11 gnuplot
preparelocal:   installsummapack  exprtkbajar

# despues de makefile.lib por DOWNLOAD
install:
	echo echo PKGDIR $(PKGDIR)
	for dir in src; do \
	 echo install $$dir ;\
	  $(MAKE) DESTDIR=$(DESTDIR) PREFIX=$(PREFIX)  PKGDIR=$(PKGDIR)   -C $$dir  install; echo test 0 = 0; \
	done
	$(call copytree,$(DESTDIR)$(PREFIX),$(DESTDIR),,$(DESTDIR)$(PREFIX))
	mandb >/dev/null
	@echo fin instalacion tabular

SERVERBAJARWD=http://simusol.org/downloads

post:
	ldconfig


$(info SERVERSUBIR: ${SERVERSUBIR})
$(info SERVERBAJAR: ${SERVERBAJAR})
$(info SERVERBAJARWD: ${SERVERBAJARWD})
$(info SERVERDIR: ${SERVERDIR})
$(info SERVERUSER: ${SERVERUSER})

summapackNAME=summapack-last.tar.gz
summapackDIR=$(SERVERBAJARWD)/${summapackNAME}

include makefile.inc

# OJO  NO ES UPD
/usr/local/share/summapack/data/makefile.lib: 
	sudo make updsummapack


subdirs: $(SUBDIRS)

$(SUBDIRS):
	$(MAKE)  DESTDIR=$(DESTDIR) PREFIX=$(PREFIX) -C $@

clean::
	rm -f *~  \#*
	rm -f gnudata.txt
	for dir in $(SUBDIRS); do \
	  $(MAKE) -C $$dir  clean; \
	  done

#DESTDIR=${DESTDIR} PREFIX=${PREFIX}
#install:
#	for dir in src; do \
#	 echo install $$dir ;\
#	  $(MAKE) -C $$dir DESTDIR=$(DESTDIR) PREFIX=$(PREFIX)   install; echo test 0 = 0; \
#	done

updsummapack:  ${DOWNLOAD}
	$(call download,,$(DOWNLOAD),${summapackNAME},${summapackDIR},summapack,SummaPack,$(call compilation))


NAMESP=installsumma.sh  
SOU=http://simusol.org/downloads/${NAMESP}

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

installsummapack:
	mkdir -p /tmp/${BASETMP}
	rm -rf /tmp/${BASETMP}/*
	NSOUIP=`host simusol.org 8.8.8.8 | grep 'simusol.org has address' | awk -F' ' '{print $$4}'`;\
	echo IP: $$NSOUIP;\
	wget --no-check-certificate  -P  /tmp/${BASETMP}  $$NSOUIP/downloads/$(NAMESP); test 0 = 0;
	sh /tmp/${BASETMP}/$(NAMESP) "$(DESTDIR)" $(PREFIX)	 ; test 0 = 0;




installdist: clean
	echo "PACKAGE DISTRIBUTION FILE ${PACKV}-${VERSION}-AI.tgz (architecture independent) $(PREFIX)"
	rm -rf /tmp/$(BASETMP) ; 
	mkdir -p /tmp/$(BASETMP) ;
	make DESTDIR=/tmp/$(BASETMP) PREFIX=/usr PKGDIR=$(PKGDIR) install
	cd  /tmp/${BASETMP}; tar -c . | gzip -n> $(CURDIR)/../${PACKV}-${VERSION}-AI.tgz
	md5sum  ../${PACKV}-${VERSION}-AI.tgz>../${PACKV}-${VERSION}-AI.tgz.md5.

