##!/usr/bin/make
##-f/home/dsa/svn/loli2/nuesiru/make/bin/simusol-make
## estas dos era cuando era el makefile de simusol
# **************************************************************
# *                  SIMUSOL PROYECT                           *
# *        makefile.lib   simusol-make    libreria/ejecutable  *
# *                                                            *
# *   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                                   *
# **************************************************************
# needs nothing, all simusol needs it
#
#    This 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.
#
#    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 it.  If not, see <http://www.gnu.org/licenses/>.

# para que no ejecute un objetivo de aca, salvo se lo explicite

#$(info LEYENDO makefile.lib)

alllib:


DESTDIR=
PREFIX=/usr/local
ETCROOT=/
PACK=$(shell  ls *.summa| sed -e s/\.summa$$//)
PACKV=$(shell echo -n $(PACK)|sed -e  's/\-/_/')
VERSIONSUMMA=$(shell $(shellpath)  cat ${PACK}.summa 2>/dev/null| awk -F= '/BASEVERSION/ {print $$2}')
TYPE=$(shell  cat ${PACK}.summa 2>/dev/null |  awk -F= '/SIMUSOLTYPE/ {print $$2}')

#SVN_REV:="$(shell svnversion -n ${project_loc})"
############ OJO EN CASTELLANO SI NO ESTA BAJO SVN dice "Directorio no versionado"
########### OJO si no esta instalado el paquete subversion
#### MEJORAR PARA QUE SOLO ACTUE EN CASO DE ESTAR INSTALADO SVN Y EL ARBOL SER
SVN_REVT:=$(shell svnversion -n | sed -e "s/.*://"   | sed -e "s/[MSP]*$$//" )

ifneq ($(SVN_REVT),Unversioned directory)
$(shell $(shellpath) echo $(SVN_REVT) >SVVERSION  )      
endif
# se hace esto porque el directorio de un rpm o deb no tiene subversion

SVN_REV=$(shell $(shellpath)  cat SVVERSION 2>/dev/null| sed -e's/\n//')
VERSION=$(VERSIONSUMMA).R$(SVN_REV)
$(info PACK: >${PACK}<; PACKV: >${PACKV}<  VERSION: >${VERSION}<  TYPE pkgs: >${TYPE}<)


# PUEDE FALLAR Si no esta instalado subversion o si el arbol no es svn: caso de instalacion, igual en tales casos no se usa esta variable


# This definition finds the file with the latest commit version number
#SVNREV:='"$(shell LC_ALL=C svnversion -cn ../ | sed -e "s/.*://" -e "s/\([0-9]*\).*/\1/" | grep "[0-9]" )"'


$(info PACK: >${PACK}<; PACKV: >${PACKV}<  VERSION: >${VERSION}<  TYPE pkgs: >${TYPE}<) 

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


define findfirst
$(shell $(shellpath) test -x /usr/$1  && echo /usr/$1;\
                 test -x /usr/local/$1  && echo /usr/local/$1;\
                 test -x ~/$1  && echo ~/$1;)
endef

# ya debiera estar y es diferente
define findfirstSB
$(shell $(shellpath) test -x /usr/$1  && echo /usr;\
                 test -x /usr/local/$1  && echo /usr/local;\
                 test -x ~/$1  && echo ~;)
endef


define findfirstS
$(shell $(shellpath) test -s $(CURDIR)/$1  && echo  $(CURDIR)/$1;\
                 test -s ~/$1  && echo ~/$1;\
		 test -s /$1  && echo /$1;)
endef

YAML=$(call  findfirstS,etc/summapack.yaml)

#$(shell sed -e 's/:[^:\/\/]/="/g;s/$/"/g;s/ *=/=/g' /etc/simusol.yaml > file.sh)


SERVERBAJAR=$(shell cat ${YAML}/summapack.yaml 2>/dev/null | grep SERVERBAJAR| sed -e 's/SERVERBAJAR\s*:\s*//'| sed -e 's/\s*$$//' )

ifeq ($(SERVERBAJAR),)
SERVERBAJAR=/downloads
endif

SERVERNAME=$(shell cat ${YAML}/summapack.yaml 2>/dev/null | grep SERVERNAME | sed -e 's/SERVERNAME\s*:\s*//'| sed -e 's/\s*$$//' )

ifeq ($(SERVERNAME),)
SERVERNAME=simusol.org
endif

#$(info SERVERNAME $(SERVERNAME))
# MAL GREP SERVERNAME
SERVERNAME=simusol.org

SERVERDIRU=$(shell cat ${YAML}/summapack.yaml 2>/dev/null | grep SERVERDIR| sed -e 's/SERVERDIR\s*:\s*//'| sed -e 's/\s*$$//' )
ifeq ($(SERVERDIRU),)
SERVERDIRU=/var/www/$(SERVERNAME)/public_html
endif
#/srv/www/htdocs/simusol/simusolsite/downloads/$2.md5

SERVERDIR=$(SERVERDIRU)$(SERVERBAJAR)

$(info SERVERDIR: ${SERVERDIR})

#wwwrun
SERVERUSER=$(shell cat ${YAML}/summapack.yaml 2>/dev/null | grep SERVERUSER| sed -e 's/SERVERUSER\s*:\s*//'| sed -e 's/\s*$$//' )






ifeq ($(SERVERUSER),)
SERVERUSER=www-data
endif

# $(info YAML: >$(YAML)< ; SERVERNAME: >$(SERVERNAME)<; SERVERDIR: >$(SERVERDIR)<;  SERVERUSER: >$(SERVERUSER)<; )


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


#/srv/www/htdocs/simusol/simusolsite/downloads
#SERVERDIR=/var/www/simusol.org/public_html/downloads
#SERVERUSER=www-data


#SERVERUSER=www-data
#SERVERNAME=simusol.org
#SERVERDOWN=downloads
#SERVERDIR=/var/www/$(SERVERSUBIR)/public_html/$(SERVERDOWN)


#    $(info No hay ${HOME}/etc/simusol.yaml)
#    WEBSERVER=$(shell cat /etc/simusol.yaml 2>/dev/null |  grep webserver| sed -e 's/webserver\s*:\s*//'| sed -e 's/\s*$$//'# )
#    ifeq ($(WEBSERVER),)
#       $(info No hay /etc/simusol.yaml)
#      else
#       $(info WEBSERVER /etc/simusol.yaml >$(WEBSERVER)< )  
#      endif
#  else
#    $(info WEBSERVER ${HOME}/etc/simusol.yaml >$(WEBSERVER)< )  


#ifeq ($(WEBSERVER),)
#  SERVERBAJAR=http://simusol.org
#  SERVERSUBIR=simusol.org
#else
#  SERVERBAJAR=$(WEBSERVER)
#  SERVERSUBIR=$(WEBSERVER)
#endif
# revisar http://
#SERVERNAME=www.simusol.org






dirpro = $(CURDIR)   # directorio donde esta el proyecto original
wdir=$(CURDIR)       #directorio de trabajo

# wdir es creado antes de usarlo, si dirpro es overlayed pero no existe, realpath no funcionara ni avisa, queda vacio
# en wdir escribe los resultados si se los quiere poner en el arbol inicial poner wdir=rdirpro como parametro

$(shell $(shellpath) mkdir -p $(dirpro) 2>/dev/null)
$(shell $(shellpath) mkdir -p $(wdir) 2>/dev/null)
rdirpro= $(realpath $(dirpro))
rwdir= $(realpath $(wdir))
LISTADO=$(rdirpro)/listado.ls

sdirpro=$(shell $(shellpath) echo $(rdirpro) | sed -e 's/\//\\\//g' )

# cada vez que lo invocas lo hace?
#BASETMP = $(shell $(shellpath) mktemp -d |sed -e's/\n//')



#USERXA=$(shell echo ${XAUTHORITY} | awk -F'/'  '{print $$3}' )
# ojo requiere instalar
# no va mas desde ubuntu 17.10
### seguro que /tmp/usuario falla
USERXA=$(shell ls -l /tmp/usuario  2>/dev/null | awk -F' ' '{print $$3}' )
USERLN=$(shell echo ${LOGNAME} )
USERSU=$(shell echo ${SUDO_USER} )
USERU=$(shell echo ${USER} )


USERACTUAL=$(shell id -un)
#
# falla porque Make viene de un tar que pone el usuario por numero del que lo hizo
#USERBASE2=$(shell who | awk '{print $$1}' )
# si hay varios usuarios logeados es un lio
# varios sudos invalidan SUDO_USER y LOGNAME
# terminales en X11 invalidan LOGNAME
# XAUTH EN X11 y SUDO_USER en terminal (cruzar dedos que no sean sudo sudo)


ifeq ($(USERXA),)
        USERR=$(shell echo $(USERSU) )
else
        USERR=$(USERXA)
endif

ifeq ($(USERR),)
        USUARIO=$(shell echo $(USERU) )
else
        USUARIO=$(USERR)
endif

WWHO=$(shell who am i | awk -F ' ' '{print $$1}')
# se repite para 0
LC=$(shell  who am i|wc -l)


ifeq ($(LC),1)
        USUARIO=$(WWHO)
endif

USERBASE=$(USUARIO)
USERNAME=$(USUARIO)
#USERNAME=${USER}
#USERNAME=$(shell whoami)
#$(info USERNAME $(USERNAME))
homedir=$(shell getent passwd "$(USERNAME)" | cut -d: -f6 )
# ./download.sh simusol.org downloads distro.sh "" ""
#$(info USERNAME $(USERNAME))

#aqui sigue a simusol, ahora quizas lo logico seria instalar todo en /usr/local siempre
# que se es root
USERID=$(shell $(shellpath) id -u)
#USERID=$(shell id -u)
NUID=1
# ojo aparecera nuevouid solo si se esta llamando esto con sudo.sh en cygwin
NUID=$(shell $(shellpath) /bin/echo $$NUEVOUID)


# en cygwin tambien
# VER QUE PASa EN CYGWIN SIN SUDO
VEROID=$(USERID)
ifeq ($(NUID),-1)
  VEROID=0
endif
#ifeq ($(VEROID),1)
#                $(info Running as root, No correr con sudo, cuando necesite el programa lo hara.)
#                $(shell exit 2); 
#endif

$(info HOMEDIR $(homedir)) 
$(info XA $(USERXA) - $(USERLN) - $(USERSU) -  $(USERU) - $(USERR) -$(USEREBASE))
$(info User Id is $(USERID) - $(USERACTUAL) - ${USER} - Su ${SUDO_USER} - BASE $(USERBASE) -  USERNAME  $(USERNAME))
$(info User ID: $(VEROID))
#USERID=$(shell id -u)
#USERNAME=$(shell id -un)
#FEC=$(shell date '+%Y-%m-%d')



#ifeq (exists, $(shell test -d /usr/share/simusol  && echo exists ))
#	                PREFIX=/usr
# NO PORQUE solo se deben instalar en /usr los paquetes tanto de simusol como de documentos si son rpm o deb o simil
# para usar /usr local hacerlo a mano
#endif
DOT=/
DOC=doc/simpack
# OJO MAL!!!!!!!!!!!!!!! no puede ser solo para simplack
#
SHARE=/share
SUDO=sudo
USERS=`ls /home`

#el dia siempre estara instalado por paquete
PREFIXD=/usr
SPREFIXD=\/usr
#Simusol siempre lo instalaremos en /usr/local o en home
PREFIX=/usr/local
SPREFIX=\/usr\/local



STABLE=last
STABLE=UNSTABLE

#rsync -av --exclude='*.svn*' --exclude='*/.*' ./ ${BASETMP}/${PACKC}; 
solosolopack: 
	@echo "Make solopack ${DESTINO}/${PACKC}.s.tgz";
	      	         @BASET=${BASETMP};\
	                 rm -rf $${BASET}/$${PACKC}; \
	        	         mkdir -p $${BASET}/$${PACKC};  2>/dev/null\
	                 chmod -R 777 $${BASET}; \
	                 rsync -a --exclude='*.svn*' --exclude '*.gui' --exclude "${PACKC}.s.tgz" --exclude='*/.*' ./ $${BASET}/${PACKC$
	                 touch -r $${BASET}/$${PACKC}/Makefile $${BASET}/$${PACKC} ;\
	                 tar --exclude .svn --exclude "${PACKC}.s.tgz" --exclude .gui -C$${BASET} -c ${PACKC} --owner=0 --group=0 |gzip -n> ${DESTINO}/${PA$
	                 echo   Creado ${DESTINO}/${PACKC}.s.tgz;
	                 #cp ${DESTINO}/${PACKC}.s.tgz ${DESTINO}/../${PACKC}.s.tgz



#$(info User Id is $(USERID))
ifeq ($(ASUSER),1)                     #llamar make ASUSER=1
	        DOC=simpack/docs
	        DOC=simusol/docs/$(PACK)
		SHARE=
		SUDO=
		PREFIX=${HOME}
		SPREFIX=$(shell $(shellpath) echo ${HOME} | sed -e 's/\//\\\//g' )
	        PREFIXD=${PREFIX}
	        SPREFIXD=${SPREFIXD}
                #$(info SPREFIX $(SPREFIX))
	        DOT=/.
		ETCROOT=${HOME}/.simusol/
	        SHARE=
                ifeq (Cygwin,$(shell $(shellpath) uname -o))
                  SUDO=
                 else
                  SUDO=$(shell $(shellpath) sudo -n ls / 2>/dev/null |tail -n1 )  
                  SUDOPR=$(shell $(shellpath) sudo -n ls / 2>&1 |tail -n1)
                endif
                $(info SUDO: $(SUDO) PR: $(SUDOPR))	
		USERS=$(shell id -un)
endif

#$(info DESTDIR $(DESTDIR) PREFIX $(PREFIX))

DISTROSCRIPT=$(call  findfirst,bin/distro.sh)
DISTRO=
# cambio error por info, para que no se cuelque cuando la instalas desde otro paquete a proposito.


ifeq ($(DISTROSCRIPT),)
   DISTROSCRIPT=/usr/local/bin/distro.sh
   ifneq ($(shell $(shellpath) test -e  $(DISTROSCRIPT) && echo -n yes),yes)
    $(info  ADVERTENCIA distro.sh de summapack no esta instalada, SHELLPATH $(shellpath))

    ifeq ($(shell $(shellpath) test x"$(VEROID)" = x0 && echo -n yes),yes)
         DISTROSCRIPT=$(DESTDIR)/usr/local/bin/distro.sh
	else
          DISTROSCRIPT=$(DESTDIR)$(HOME)/bin/distro.sh
	endif
    # no se porque no aparece nada en osc build --local-package
    NSOUIP=$(shell $(shellpath) host simusol.org 8.8.8.8 | grep 'simusol.org has address' | awk -F' ' '{print $$4}')
    $(shell $(shellpath) wget --no-check-certificate  $(NSOUIP)/downloads/distro.sh  -O $(DISTROSCRIPT))
    $(shell $(shellpath) chmod a+x $(DISTROSCRIPT))
    $(info  DISTRO $(DISTRO) DS $(DISTROSCRIPT) HOST $(NOSUIP))
    DISTRO=$(shell $(DISTROSCRIPT))
   else
    DISTRO=$(shell $(DISTROSCRIPT))
   endif
 else
   DISTRO=$(shell $(DISTROSCRIPT))
endif

  $(info  DISTRO $(DISTRO) DS $(DISTROSCRIPT))
# caso summapack u otros que se quiera poner
ifeq ($(DISTRO),)
   DISTROSCRIPT=/home/abuild/rpmbuild/BUILD/summapack*/bin/distro.sh
    $(info  ADVERTENCIA distro.sh de summapack se usa de las fuentes)
   DISTRO=$(shell $(DISTROSCRIPT))
endif


# echo 0 despues de wget si no anda internet

$(info DISTRO: >$(DISTRO)<;  DISTROSCRIPT: >$(DISTROSCRIPT)<;)

#  $(shell sudo install bin/distro.sh $(PREFIX)/bin)

################################# cuando se usa en summapack distro puede no estar instalado, ahi se intento
#igual en ese caso no la usa y la va a instalar minutos despues

#$(info DISTRO $(DISTRO))



#EXGRP := $(shell cat /etc/group|grep ^www|awk -F: '{print $$1}' |head -n1)
#$(info GRP $(EXGRP)) 


webvhost=/etc/apache2/sites-enabled #ubuntu
SRV = /var
WEBDOC = /var/www
WEBCGI = /usr/lib/cgi-bin
USR=www-data
GRP=www-data

ifneq ($(DISTRO),DEBIAN)
 ifneq ($(DISTRO),UBUNTU)
        webvhost=/etc/apache2
	SRV = /usr/local
	WEBCGI = /usr/local/httpd/cgi-bin
	WEBDOC = /usr/local/httpd/htdocs
	USR=nobody
	GRP=nogroup
 endif
endif


ifeq ($(DISTRO),SUSE)
#SUSE
        webvhost=/etc/apache2/vhosts.d
	SRV = /srv
	WEBDOC = /srv/www/htdocs
	WEBCGI = /srv/www/cgi-bin
	USR=wwwrun
	GRP=www
endif


SHELL=/bin/bash
PATH := ${HOME}/bin:/usr/local/bin:${PATH}
DESTINO=.


ifeq (Cygwin,$(shell /bin/uname -o))
          FC=g77
          shellpath = 
          WAPP  = $(shell perl -MWin32 -MFile::Basename -e 'print Win32::GetFolderPath(0x0026)'|sed -e's/^\([A-Z]:\)/\L\1/'|sed -e's/:\\/\//'|sed -e's/\\/\//g'|sed -e's/ /\\ /g')
          PREFIXDIAW=/cygdrive/$(WAPP)/Dia

define SIMUSOLBASH
pathmunge () {
        if ! echo $$PATH | /bin/egrep -q "(^|:)$$1($$|:)" ; then
           if [ "$$2" = "after" ] ; then
              PATH=$$PATH:$$1
           else
              PATH=$$1:$$PATH
           fi
        fi
}
pathmunge /usr/bin
pathmunge /usr/local/bin
pathmunge ~/bin
pathmunge `cygpath -S` after
pathmunge `cygpath -W` after
endef

else
          FC=gfortran
          shellpath=
          WAPP=

define SIMUSOLBASH
pathmunge () {
        if ! echo $$PATH | /bin/egrep -q "(^|:)$$1($$|:)" ; then
           if [ "$$2" = "after" ] ; then
              PATH=$$PATH:$$1
           else
              PATH=$$1:$$PATH
           fi
        fi
}

pathmunge /usr/bin
pathmunge /usr/local/bin
pathmunge ~/bin
pathmunge /usr/x11R6/bin after

endef


endif


# ojo con ls /home y skel?????

# PREFIXD porque dia puede estar instalado por paquete del sistema pero simusol no, es lo habitual
#dia no busca en el /usr donde no esta
# como lo va a instalar despues al dia, dejo como defecto el /usr
# en cambio el simusol es al reves

#ifeq ($(UUU),1)
#                $(info Running as root, No correr como sudo, cuando necesite el programa lo hara.)
#                $(shell exit 2); 
#                ifeq (exists, $(shell $(shellpath) test -d /usr/share/simusol  && echo exists ))
#	                PREFIX=/usr
#	                SPREFIX=\/usr
 #                     else 
#			PREFIX=/usr/local
#			SPREFIX=\/usr\/local
 #               endif
#                ifeq (exists, $(shell $(shellpath) test -d /usr/local/share/dia  && echo exists ))
#	                PREFIXD=/usr/local
#	                SPREFIXD=\/usr\/local
#                      else 
#			PREFIXD=/usr
#			SPREFIXD=\/usr
 #               endif
#	        DOT=/
#	        DOC=doc/simusol/pkgs/$(PACK)
#	        SHARE=/share
#		SUDO=
#		USERS=`ls /home`
#           else
#
#		PREFIX=${HOME}
#		SPREFIX=$(shell $(shellpath) echo ${HOME} | sed -e 's/\//\\\//g' )
#	        PREFIXD=${PREFIX}
#	        SPREFIXD=${SPREFIXD}
 #               #$(info SPREFIX $(SPREFIX))
#	        DOT=/.
#	        DOC=simusol/docs/$(PACK)
#	        SHARE=
#                ifeq (Cygwin,$(shell $(shellpath) uname -o))
#                  SUDO=
#                 else
#                  SUDO=$(shell $(shellpath) sudo -n ls / 2>/dev/null |tail -n1 )  
#                  SUDOPR=$(shell $(shellpath) sudo -n ls / 2>&1 |tail -n1)
#                endif
#                $(info SUDO: $(SUDO) PR: $(SUDOPR))	#
#		USERS=$(shell id -un)
#          endif

ifeq (Cygwin,$(shell uname -o))
   OS=CYGWIN
   #PREFIX=
   #DOC=doc/simusol
   #SPREFIX=
   #DOT=/
   #SHARE=/usr/share
   #EXT=.pl
else
   #EXT=
endif

#si necesita pass forzar que la pida si es necesario

PREFIXSIM=${PREFIX}${SHARE}${DOT}simusol
PREFIXDIA=${PREFIXD}${SHARE}${DOT}dia
PREFIXDOCS=${PREFIX}${SHARE}${DOT}${DOC}

PREFIXBIN=${PREFIX}/bin


#DIRDOC=simusol/simusolsite/downloads      #VIEJO
DIRDOC=simusol.org/public_html/downloads



#dia --nosplash --size=1024 -e ttt.png tanquebgui.dia 

# size in pixels not sure

### PROBLEMAS SI SE USA sudo make install
# deja regalitos en el home del usuario ~/lib ~/.dia ~/.cpanm .simusolbash .bashrc .bashprofile
# .gnupg
# .gnuplot-wxt supongo que el ejemplo de simusol-gui todos estos propiedad de root
# usar sudo su; luego make install
# asi solo queda INSTALA_COMPANION
# la mayoria de los problemas lib, cpanm etc se causan por el ejemplo gui

# esto es para win32

# C:\Dwimperl\perl\bin\wperl.exe C:\cygwin\bin\simusol-gui.pl

#> to the very end of /etc/profile.  It's probably not the 'right' way to do it,
#> but who cares.  It works fine.

#Well, the "official" way of doing this is to delete your ~/.bash_profile
#and reinstall the "base-files" package (or simply "cp -p
#/etc/defaults/etc/skel/.bash_profile ~").  Alternatively, you can simply
#"ln -s ~/.bashrc ~/.bash_profile".
#http://puracoding.wordpress.com/2007/09/28/cygwin-wont-read-bashrc/
#http://tobiascohen.com/2009/08/26/making-cygwin-look-at-your-bashrc/
#function path(){
#    old=$IFS
#    IFS=:
#    printf "%s\n" $PATH
#    IFS=$old
#}
#http://www.cyberciti.biz/faq/redhat-linux-pathmunge-command-in-shell-script/
#pathmunge () {
#        if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
#           if [ "$2" = "after" ] ; then
#              PATH=$PATH:$1
#           else
#              PATH=$1:$PATH
#           fi
#        fi
#}

#poner esto en el .bash_profile
#[[ -r ~/.bashrc ]] && . ~/.bashrc

#http://stackoverflow.com/questions/415403/whats-the-difference-between-bashrc-bash-profile-and-environment

#it was on ./cygwin/etc/skel/.bash_profile

# bash profile ~./bash_profile
# source the system wide bashrc if it exists
#if [ -e /etc/bash.bashrc ] ; then
#  source /etc/bash.bashrc
#fi

# source the users bashrc if it exists
#if [ -e "${HOME}/.bashrc" ] ; then
#  source "${HOME}/.bashrc"
#fi


#http://padre.perlide.org/download.html

#http://cygwin.com/ml/cygwin/2007-01/msg00520.html  cygports
#http://www.mpi-inf.mpg.de/~dfischer/manual/manual_topic38.html
#http://upx.sourceforge.net/
#http://search.cpan.org/~autrijus/PAR-0.85_01/script/pp
#http://www.andre-simon.de/doku/wxwidgets/devpak_install.html
#http://www.wxperl.co.uk/strawberry.html
#http://search.cpan.org/dist/Wx-Perl-Packager/bin/wxpar
#http://www.nntp.perl.org/group/perl.wxperl.users/2010/08/msg7474.html
#http://wiki.tcl.tk/11329

# gnuplot sudo apt-get install  libgd2-xpm-dev
# ./configure --without-x

#emacs cygwin
#http://www.khngai.com/emacs/cygwin.php

# ojo cygwin vi que firewall de windows bloquio acceso de perl.exe a internet, tambien cygwin

#PERL_MM_USE_DEFAULT=1 perl -MCPAN -e 'install My::Module'
#It should make CPAN answer the default to all prompts.
#cpan> o conf prerequisites_policy follow
#cpan> o conf commit


#Ultima version: http://www.simusol.org.ar/downloads/Makefile

#Instrucciones de instalacion de SimuSol:
# make install | sudo make install
#http://xn--cgi.ws/Li
# esta make se instala a si mismo, unico archivo que es necesario bajar a mano, autoinstalable





# no se pone simusolbash en etc, porque no vale la pena, asi se lo pongo a todos igual
changebash:
	if test x${OS} != xCYGWIN; then \
	   if test x$(USERID) = x0 ; then \
	    echo "$$SIMUSOLBASH" > /etc/simusolbash ;\
	    for USER in ${USERS} ; do \
	    echo USER $$USER; \
	     if test -d /home/$$USER ; then \
	      echo BUENUSER $$USER; \
              ESUSER=`grep "^$$USER:" /etc/passwd| awk -F' ' '{print $$1}'`;\
	      echo BUENUSER $$USER $$ESUSER; \
	      if test x"$$ESUSER" != x ; then \
	       su $$USER -c "cp /etc/simusolbash  /home/$$USER/.simusolbash ; ";\
	       if test x"`cat /home/$$USER/.bashrc | grep simusolbash`" = x ; then \
	            echo "cambio bashrc";\
	            su $$USER -c "echo '[[ -r /home/$$USER/.simusolbash ]] && . /home/$$USER/.simusolbash' >>/home/$$USER/.bashrc" ; \
	            fi ;\
	        if test x"`cat /home/$$USER/.bash_profile | grep simusolbash`" = x ; then \
	            echo "cambio bash_profile";\
	            su $$USER -c "echo '[[ -r /home/$$USER/.simusolbash ]] && . /home/$$USER/.simusolbash' >>/home/$$USER/.bash_profile" ; \
	            fi;\
	        if test -d /home/$$USER/Escritorio ; then \
	          echo "Es escritorio";\
	        fi ;\
	        fi;\
	     fi;\
	    done;\
	    else \
	        echo "NO ROOT" ;\
	  	echo "$$SIMUSOLBASH" > ~/.simusolbash ;\
	        if test x"`cat ~/.bashrc | grep simusolbash`" = x ; then \
	            echo '[[ -r ~/.simusolbash ]] && . ~/.simusolbash' >>~/.bashrc ; \
	            fi ;\
	        if test x"`cat ~/.bash_profile | grep simusolbash`" = x ; then \
	            echo '[[ -r ~/.simusolbash ]] && . ~/.simusolbash' >>~/.bash_profile ; \
	            fi;\
        fi;\
	else \
	   echo SICYG ;\
	   echo "$$SIMUSOLBASH" > ~/.simusolbash ;\
	   if test x"`cat ~/.bashrc | grep simusolbash`" = x ; then \
	            echo '[[ -r ~/.simusolbash ]] && . ~/.simusolbash' >>~/.bashrc ; \
	            fi ;\
	   if test x"`cat ~/.bash_profile | grep simusolbash`" = x ; then \
	            echo '[[ -r ~/.simusolbash ]] && . ~/.simusolbash' >>~/.bash_profile ; \
	            fi;\
	fi;


#if test -d /home/$$USER/Desktop ; then \
#    su $$USER -c "cp /home/$$USER/Desktop/$$FILEP /home/$$USER/Escritorio" ;\
#fi; \



# lo ideal es si sos root hacerlo en /etc/profile a ambos
# no se para que x11 en cygwin!

#When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.

#When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of /etc/bash.bashrc and ~/.bashrc.

#As far as I understand, a login shell means a session where you log in to the system and directly end up in Bash, like a remote ssh session or logging in through a non-graphical text terminal. A non-login shell is then the type of shells you open after logging in: typically in a graphical session when you open a new terminal window.

#How I think things are supposed to work (for a typical setup):

#    .profile is for things that are not specifically related to Bash, like environment variables PATH and friends, and should be available anytime. For example, .profile should also be loaded when starting a graphical desktop session.
#    .bashrc is for the configuring the interactive Bash usage, like Bash aliases, setting your favorite editor, setting the Bash prompt, etc.
#    .bash_profile is for making sure that both the things in .profile and .bashrc are loaded for login shells. For example, .bash_profile could be something simple like

#    . ~/.profile
#    . ~/.bashrc

#    As stated in the man page excerpt above, if you would omit .bash_profile, only .profile would be loaded.



#echo 'PATH=~/bin:$$PATH' >>~/.bashrc



############################################################################# instalar paquetes



/usr/bin/sudo:
	echo CYGWIN SUDO;  \
	echo '#!/bin/bash' >sudo.sh ;\
	echo 'if test x$$1 = x"-n" ; then ' >>sudo.sh ;\
	echo '   export NUEVOUID=-1; $$2 $$3 $$4 $$5 $$6 $$7 $$8 $$9 $${10} $${11}'>>sudo.sh ;\
	echo ' else ' >>sudo.sh ;\
	echo '   export NUEVOUID=-1; $$1 $$2 $$3 $$4 $$5 $$6 $$7 $$8 $$9 $${10} $${11}'>>sudo.sh ;\
	echo 'fi ' >>sudo.sh ;\
	install sudo.sh /usr/bin/sudo


#cygwin strawberry /usr/bin/shortcut.pl emacswin diawin gnuplot




cygwin:
	      echo Cygwyn;\
              if ! test -e ~/bin/setup.exe ; then \
	        mkdir ~/bin  2>/dev/null;\
	        wget -O ~/bin/setup.exe  http://cygwin.com/setup.exe  ;\
	        chmod a+x ~/bin/setup.exe ;\
	      fi; \
	      ~/bin/setup.exe -s "http://lug.mtu.edu/cygwin" -q -P zip -P unzip -P openssh -P gcc -P gcc-g77 -P make -P binmutils -P autoconf -P automake -P gettext -P libgcc1 -P libstdc++ ;\
	      ~/bin/setup.exe -s "http://lug.mtu.edu/cygwin" -q -P readline -P subversion  -P emacs -P mc -P nano -P perl -P perlwin32-gui -P perlXMLsimple -P perllibwin32 -P wget;\
	      ~/bin/setup.exe -s "http://lug.mtu.edu/cygwin" -q -P  util-linux  -P bsdtar -P md5deep -P lynx -P ncftp -P gnupg -P rsync ;





#DOT=/
#DOC=doc/simusol/pkgs/$(PACK)
#SHARE=/share
#PREFIXSIM=${PREFIX}${SHARE}${DOT}simusol
#PREFIXDOWN=${PREFIX}${SHARE}${DOT}down
#DOWNLOAD = ${PREFIXDOWN}/pkg
############## Revisar

PREFIXDOWN=$(DESTDIR)/var/cache
DOWNLOAD = ${PREFIXDOWN}/summapack


ifneq ($(shell test -d  $(DOWNLOAD) -a -w $(DOWNLOAD) && echo -n yes),yes)
   $(info  ADVERTENCIA DOWNLOAD: $(DOWNLOAD) no esta creado o no es escribible, summapack es el unico que lo crea en zona que requiere root)
    DOWNLOAD=$(DESTDIR)$(HOME)/var/cache/summapack
    $(info  Sera creado en $(DOWNLOAD))
    $(shell $(shellpath) mkdir -p $(DOWNLOAD) 2>/dev/null)
    endif
    #no vacio a download porque hay partes en que se hace rm sobre el y no se si tienen en cuenta que puede estar vacio

ifeq ($(shell test x"$(VEROID)" = x0 && echo -n yes),yes)
	DOWNLOAD=$(PREFIXDOWN)/summapack
        $(info  VEROID $(VEROID) DOWN $(DOWNLOAD)  )
        $(shell $(shellpath) mkdir -p $(DOWNLOAD) 2>/dev/null)
        $(shell $(shellpath) chmod -R a+w $(DOWNLOAD) 2>/dev/null)
        $(shell $(shellpath) chmod -R a+r $(DOWNLOAD) 2>/dev/null)
    endif


$(info DOWNLOAD: ${DOWNLOAD} )


#"dia" -integrated
$(DOWNLOAD)/dia-setup-0.97.2.exe:
	 wget -O $(DOWNLOAD)/dia-setup-0.97.2.exe http://sourceforge.net/projects/dia-installer/files/dia-win32-installer/0.97.2/dia-setup-0.97.2.exe/download 

/cygdrive/c/Program\ Files/Dia/bin/dia.exe: $(DOWNLOAD)/dia-setup-0.97.2.exe
	chmod a+x $(DOWNLOAD)/dia-setup-0.97.2.exe
	$(DOWNLOAD)/dia-setup-0.97.2.exe; exit 0
	touch "/cygdrive/c/Program Files/Dia/bin/dia.exe"


diawin: /cygdrive/c/Program\ Files/Dia/bin/dia.exe
	rm -f /usr/bin/dia 
	ln -s "c:\\Program Files\\Dia\\bin\\dia.exe"  /usr/bin/dia
	./shortcut.pl dia "c:\\Program Files\\Dia\\bin\\dia.exe" 

# cpan cygwin m,al





#echo if fails do "sudo -rf ${PREFIXDOWN}/summapack or  sudo chmod a+w /usr/local/share/down/pkg/*"

cyggfortran:
	 if ! test -e  /usr/bin/gfortran.vie ; then \
	    mv /usr/bin/gfortran /usr/bin/gfortran.vie;\
	 fi;\
	 rm -f /usr/bin/gfortran ;\
	 ln -s /usr/bin/g77 /usr/bin/gfortran ;\


$(DOWNLOAD)/shortcut.pl: /cygdrive/c/SSstrawberry/perl/bin/perl.exe
	wget -O  $(DOWNLOAD)/shortcut.pl "$(SERVERNAME)/$(SERVERBAJAR)/shortcut.pl"
	touch $(DOWNLOAD)/shortcut.pl


# por algun motivo el wget no cambia la fecha de shortcut


/usr/bin/shortcut.pl: $(DOWNLOAD)/shortcut.pl
	echo install shortcut
	install $(DOWNLOAD)/shortcut.pl /usr/bin

#ojo antes era gnuplot
gnuplotW: /cygdrive/c/Program\ Files/gnuplot/bin/wgnuplot.exe
	if ! test -e  /usr/bin/gnuplot.vie ; then \
	    mv /usr/bin/gnuplot /usr/bin/gnuplot.vie;\
	fi;\
	rm -f /usr/bin/gnuplot ;\
        ln -s "C:/Program Files/gnuplot/bin/wgnuplot.exe" /usr/bin/gnuplot;\

$(DOWNLOAD)/gp46rc1-win32-setup.exe:
	wget -O  $(DOWNLOAD)/gp46rc1-win32-setup.exe http://sourceforge.net/projects/gnuplot/files/gnuplot/4.6.rc1/gp46rc1-win32-setup.exe/download

/cygdrive/c/Program\ Files/gnuplot/bin/wgnuplot.exe: $(DOWNLOAD)/gp46rc1-win32-setup.exe
	chmod a+x $(DOWNLOAD)/gp46rc1-win32-setup.exe
	$(DOWNLOAD)/gp46rc1-win32-setup.exe; exit 0
	touch "/cygdrive/c/Program Files/gnuplot/bin/wgnuplot.exe"


$(DOWNLOAD)/emacs-23.4-bin-i386.zip:
	wget -O $(DOWNLOAD)/emacs-23.4-bin-i386.zip http://ftp.gnu.org/gnu/emacs/windows/emacs-23.4-bin-i386.zip

/cygdrive/c/Program\ Files/emacs-23.4/bin/runemacs.exe: $(DOWNLOAD)/emacs-23.4-bin-i386.zip /cygdrive/c/strawberry/perl/bin/perl.exe
	unzip $(DOWNLOAD)/emacs-23.4-bin-i386.zip -d "c:\\Program Files" 
	touch "/cygdrive/c/Program Files/emacs-23.4/bin/runemacs.exe"
	/cygdrive/c/strawberry/perl/bin/perl.exe shortcut.pl emacs "c:\\Program Files\\emacs-23.4\\bin\\runemacs.exe" 

# sacado porque perl del cpan del cygnois en esta maquina anda mal


$(DOWNLOAD)/cygwin-mount.el:
	wget -O $(DOWNLOAD)/cygwin-mount.el http://www.khngai.com/emacs/cygwin-mount.el



#usar perl!!!! cygwin parece no funcionar con espacios

emacswin:  $(DOWNLOAD)/cygwin-mount.el /cygdrive/c/Program\ Files/emacs-23.4/bin/runemacs.exe
	if ! test -e  /usr/bin/emacs.vie ; then \
	       mv /usr/bin/emacs /usr/bin/emacs.vie;\
	fi ;\
	rm -f /usr/bin/emacs ;\
        ln -s "/cygdrive/c/Program\ Files/emacs-23.4/bin/runemacs.exe" /usr/bin/emacs;
	/cygdrive/c/strawberry/perl/bin/perl.exe shortcut.pl emacs "c:\\Program Files\\emacs-23.4\\bin\\runemacs.exe" 



#WHOME=`perl -MWin32 -MFile::Basename -e 'print Win32::GetFolderPath(0x0000)'|sed -e's/\\\\Desktop$$//'|sed -e's/\\\\Escritorio$$//'|sed -e's/^\([A-Z]:\)/\L\1/'|sed -e's/:\\\\/\//'|sed -e's/\\\\/\//g'|sed -e's/ /\\\\ /g'`; \
	WHOMEWIN=`perl -MWin32 -MFile::Basename -e 'print Win32::GetFolderPath(0x0000)'|sed -e's/\\\\Desktop$$//'|sed -e's/\\\\Escritorio$$//'` ; \
	UWHOMEWIN=`cygpath -u $$WHOMEWIN/Application Data/Roaming | sed -e's/ /X/g'`; \
	echo UW $$UWHOMEWIN;\
	homeemacs1="/cygdrive/$$WHOME/AppData/Roaming/.emacs";\
	homeemacs="/cygdrive/$$WHOME/Application\\ Data/Roaming";\
	ls "$$UWHOMEWIN";\
	mkdir -p $$homeemacs ;\
	echo $$homeemacs ;\
	ls $$homeemacs ;\
	ls "$$homeemacs" ;\
	homeemacs="/cygdrive/$$WHOME/Application\ Data/Roaming/.emacs";\
	echo "(setenv \"PATH\" (concat \"c:/cygwin/bin;\" (getenv \"PATH\")))"   >> $$homeemacs ;\
	echo "(setq exec-path (cons \"c:/cygwin/bin/\" exec-path))"   > $$homeemacs ;\
	echo "(require 'cygwin-mount)"  >> $$homeemacs ;\
	echo "(cygwin-mount-activate)"   >> $$homeemacs ;\
	echo "(add-hook 'comint-output-filter-functions"       >> $$homeemacs ;\
	echo "  'shell-strip-ctrl-m nil t)"                    >> $$homeemacs ;\
	echo "(add-hook 'comint-output-filter-functions"       >> $$homeemacs ;\
	echo "    'comint-watch-for-password-prompt nil t)"    >> $$homeemacs ;\
	echo " (setq explicit-shell-file-name \"bash.exe\")"      >> $$homeemacs ;\
	echo ";; For subprocesses invoked via the shell"       >> $$homeemacs ;\
	echo ";; (e.g., \"shell -c command\")"                 >> $$homeemacs ;\
	echo "(setq shell-file-name explicit-shell-file-name)" >> $$homeemacs ;\
	cp  cygwin-mount.el   "c:\\Program Files\\emacs-23.4\\site-lisp" 


STRAWV=5.12.3.0
#STRAWV=5.14.2.1-32bits

#http://strawberry-perl.googlecode.com/files/strawberry-perl-5.14.2.1-32bit.msi
#http://strawberry-perl.googlecode.com/files/strawberry-perl-5.14.2.1-32bit.msi
#http://strawberry-perl.googlecode.com/files/strawberry-perl-5.12.3.0.msi

######################################################## Funciones


$(DOWNLOAD)/strawberry-perl-$(STRAWV).msi:
	wget -O $(DOWNLOAD)/strawberry-perl-$(STRAWV).msi http://strawberry-perl.googlecode.com/files/strawberry-perl-$(STRAWV).msi



/cygdrive/c/strawberry/perl/bin/perl.exe: $(DOWNLOAD)/strawberry-perl-$(STRAWV).msi
	CAM=`echo "C://cygwin$(DOWNLOAD)/strawberry-perl-$(STRAWV).msi"|sed -e's/\//\\\\/g'` ;\
	echo $$CAM ;\
	msiexec.exe /passive /I $$CAM 
	touch /cygdrive/c/strawberry/perl/bin/perl.exe

# ponmer para borar y listar lo bajado y retraer

#/quit no muestra nada /passive informa

strawberry:  /cygdrive/c/strawberry/perl/bin/perl.exe
	if ! test -e ppdlisto ; then \
	/cygdrive/c/strawberry/perl/bin/perl.exe c://strawberry/perl/bin/ppm.pl install \
	http://www.wxperl.co.uk/repository/Alien-wxWidgets.ppd \
	http://www.wxperl.co.uk/repository/Wx.ppd \
	http://www.wxperl.co.uk/repository/Wx-TreeListCtrl.ppd \
	http://www.wxperl.co.uk/repository/Wx-GLCanvas.ppd \
	http://www.wxperl.co.uk/repository/Wx-Perl-ListView.ppd \
	http://www.wxperl.co.uk/repository/Wx-Demo.ppd ; \
	touch ppdlisto ;\
	fi;
	export PATH=C:\strawberry\c\bin:C:\strawberry\site\bin:C:\strawberry\perl\bin  ; echo $$PATH; /cygdrive/c/strawberry/perl/bin/perl c://strawberry/perl/bin/cpan Win32::Shortcut Clipboard Capture::Tiny ExtUtils::Command Shell::Command  Win32::Process Win32::Pipe



# ojo si baja mal no dejka el archivo como antes!!!!!!!!!!!!!!!!!!!!!!!!
#	$(call download,$(SCEPTREBIN),$(DOWNLOAD),${sceptreNAME},${sceptreDIR},sceptre-inenco,Sceptre,$(call sceptrecomp))
#                        1            2            3              4             5              6       7

# ojo mkdir a veces requiere sudo, se debe poner otro objetivo para crear esto y darle derechos
define download
	@echo "$6 esta en: >$1<"
	if test x"$1" = x ; then \
	 touch INSTALA_companion ;\
	 echo Instalar $6; \
	 mkdir -p $2  2>/dev/null; \
	 rm -f  $2/$3.md5.new ;\
	 wget --no-check-certificate -c -O  $2/$3.md5.new  $4.md5 ; \
	 NOMBRE=$3 ; \
	 NOMBREDIR=$4 ; \
	 if test ! -s  $2/$3.md5.new ; then\
	    echo "Internet (o sin permiso: sudo) falla bajando $4.md5" ;\
	    rm -f $2/$3.md5.new ;\
	    NOMBRE=`echo $3 | sed -e 's/UNSTABLE/last/'`;\
	    NOMBREDIR=`echo $4 | sed -e 's/UNSTABLE/last/'`;\
	    wget --no-check-certificate -c -O  $2/$$NOMBRE.md5.new  $$NOMBREDIR.md5 ; \
	    if test ! -s  $2/$$NOMBRE.md5.new ; then\
	      echo "Internet (o sin permiso: sudo) falla bajando $$NOMBREDIR.md5" ;\
	      rm -f $2/$$NOMBRE.md5.new;\
	    fi; \
	 fi; \
	 MD5NEW=`cat $2/$$NOMBRE.md5.new 2>/dev/null| cut -d' ' -f1` ;\
	 MD5=`cat $2/$$NOMBRE.md5 2>/dev/null| cut -d' ' -f1`;\
	 echo md5 $$MD5 md5new $$MD5NEW ;\
	 if test \( -s $2/$$NOMBRE.md5.new -a \
                    \( ! -e $2/$$NOMBRE -o  \
                       ! -e $2/$$NOMBRE.md5 -o \
                       x$$MD5 != x$$MD5NEW \) \
                 \) ; then \
	    echo Bajando  $$NOMBREDIR ;\
	    mkdir -p /tmp/saved  2>/dev/null; \
	    chmod a+w /tmp/saved; \
	    mv -f $2/$$NOMBRE /tmp/saved 2>/dev/null;\
	    rm -f $2/$$NOMBRE; \
	    wget --no-check-certificate  -P $2 $$NOMBREDIR; \
	    if test ! -s  $2/$$NOMBRE ; then\
	       echo Internet falla bajando $$NOMBREDIR ;\
	       rm -f $2/$$NOMBRE;\
	     else \
	       rm -f   $2/$$NOMBRE.md5 ;\
	       md5sum -t   $2/$$NOMBRE > $2/$$NOMBRE.md5 ; \
	       MD5BAJ=`cat  $2/$$NOMBRE.md5 | cut -d' ' -f1` ;\
	       MD5OK=`cat $2/$$NOMBRE.md5.new 2>/dev/null| cut -d' ' -f1`;\
	       echo md5 $$MD5OK md5new $$MD5BAJ ;\
	       if test x$$MD5OK != x$$MD5BAJ ; then \
	          echo EL archivo $$NOMBREDIR bajo mal, lo renombro;\
	          mv -f  $2/$$NOMBRE $2/$$NOMBRE.mal 2>/dev/null ;\
	       fi;\
	       chmod a+w $2/$$NOMBRE ;  chmod a+w $2/$$NOMBRE.md5 ; chmod a+w $2/$$NOMBRE.md5.new ; echo ''; \
	       rm -f $2/$$NOMBRE.md5.new  2>/dev/null;\
	    fi; \
	 else \
	    echo No bajo: $$NOMBREDIR ;\
	 fi; \
         if test x${BASETMP} != x; then \
	   if  test -e $2/$$NOMBRE ; then \
	     echo Instalando $6;\
             if test x"$5" != x; then \
	      rm -rf /tmp/${BASETMP} ; \
              mkdir -p /tmp/${BASETMP} ;\
	      cd /tmp/${BASETMP} ;\
	      tar -zxvf $2/$$NOMBRE >/dev/null ; \
	      DIRBU=`echo $5*| sed -e 's/ //g'` ;\
	      if test -d $$DIRBU  ; then   \
		    echo Encontro directorio  $$DIRBU con raiz $5; \
	            cd $5* ; \
	 	    $7 ;\
	            echo $6 fue instalado en ${PREFIX};\
	         else \
	            echo "No encontro directorio con raiz $5, encontro $$DIRBU" ;\
	      fi; \
	      fi; \
	     else \
	      echo No puede bajar $6 ;\
	     fi;\
	 else \
	   echo No hay BASETMP, no puedo descomprimir ;\
	 fi;\
	else \
	   echo No se instala: $6 ya esta instalado en $1;\
	fi
endef
# si no encuentra directorio con los padrones devuelve $5*, o sea nunca es vacio!
# se le saca los espacios por si son varios directorios con esa raiz
# se prueba exito porque si el cd no funciona se ejecuta el make actual y es un lio de entender


# ojo, si se lo llama dentro de sudo hay problemas!!!!!!!!!!!!
define rmbin 
	@echo rm -f $1 - $3 ;\
	if test x$(SUDO) != x ; then \
	   sudo -n rm -f /usr/local/bin/$2 /usr/bin/$2  ;\
	else \
	  if  test x"$(SUDOPR)" != x ; then \
	   sudo rm -f /usr/local/bin/$2 /usr/bin/$2  ;\
	  else \
	    echo No se pudo ejecutar ningún sudo, si esta en algun /usr  sigue instalado;\
	  fi ;\
	fi; \
	rm -f ~/bin/$2 
endef


define rmfbin
	@echo rmf  $1 ;\
	if test x$(SUDO) != x ; then \
	   sudo -n rm -f /usr/local/share/simusol/pkg/$2 /usr/share/simusol/pkg/$2 ;\
	else \
	  if  test x"$(SUDOPR)" != x ; then \
	    sudo rm -f /usr/local/share/simusol/pkg/$2 /usr/share/simusol/pkg/$2 ; \
	   else \
	    echo No se pudo ejecutar ningún sudo, si esta en algun /usr  sigue instalado;\
	  fi ;\
	fi; \
	rm -f ~/.simusol/pkg/$2
endef



# solo borra targets
# sudo sin n fallara enla interfaz grafica

# en realidad sceptre solo se instala por ahora en /usr/local
# /usr esta reservado para paquetes buinarios del sistema, o sea este make nunca instalara asi
# por ahora no se puede instalar sin sudo

# se podria verificar la fecha o algun cheksum, para proceder (siempre) o antes de proceder (a pedido)


define mkmd5
	mkdir -p /tmp/saved  2>/dev/null; exit 0;
	chmod a+w /tmp/saved; exit 0;
	mv -f $1/$2 /tmp/saved; exit 0;
	rm -f $1/$2; exit 0;
	wget --no-check-certificate -c -P $1 $3; 
	md5sum -t   $1/$2 > $1/$2.md5 ;
	scp  $1/$2.md5  $(SERVERUSER)@$(SERVERNAME):$(SERVERDIR)/$2.md5
endef



#uso XXX\. porque \. es diferente de . para que no se autoreeemplace
define sedmake
	 echo sedmake $1 ;\
	 sed -i'' -e 's/-fXXX\./-f$2\/bin\/simusol-make/' $1/bin/simusol-make; \
	 sed -i'' -e 's/,XXX\./,$2\/bin\/simusol-make/' $1/bin/simusol-make; \
	 sed -i'' -e 's/XXX\.:/$2\/bin\/simusol-make:/' $1/bin/simusol-make; 
endef

define sedmake2
	      sed -i'' -e 's/-f$1/-fXXX\./'  $(ESTEMAKE); \
	      sed -i'' -e 's/,$1/,XXX\./'  $(ESTEMAKE); \
	      sed -i'' -e 's/$1:/XXX\.:/'  $(ESTEMAKE); 
endef











############### ojo no se porque no hace echo en algunos casos a listado.ls, busque pero no entiendo porque no

#define installfiles
#	for FILE in $1 ; do \
#	   if test x"$$FILE" != x"." ; then \
#	    FILEP=`echo -n $$FILE|sed -e  's/^\.\/$3//' ` ;\
#	    $4  install  $$FILE  "$2/$$FILEP";\
#	    echo "A:$2/$$FILEP" >> $(LISTADO) ;\
#	    echo "A:$2/$$FILEP"  ;\
#	   fi;\
#	done;
#endef






##################3 Mal usar el .pl para instalar paquetes

################ OJO NO ES SUMMAPACK, VER EN TABULAR USA ESTO
# era gnuplot5 para versiones de ubuntu previas a la 17.10
unzip libgsl-dev gsl-bin libgsl2 swig gnuplot-x11 gnuplot libjson-perl libreadline-dev subversion check:
	@if dpkg -s "$@"  2>&1 |grep "dpkg-query: package" |grep " is not installed"; then \
	  echo $@ NO esta instalado; \
	  export DEBIAN_FRONTEND=noninteractive ; sudo apt-get install $@; echo "";\
	else \
	  echo $@ SI esta instalado; \
	fi


$(DOWNLOAD)/check-0.12.0.tar.gz:  ${DOWNLOAD}
	echo if fails do "sudo -rf ${PREFIXDOWN}/summapack or  sudo chmod a+w /var/cache/summapack/*"
	cd $(DOWNLOAD); wget https://github.com/libcheck/check/releases/download/0.12.0/check-0.12.0.tar.gz -O check-0.12.0.tar.gz ; chmod a+w check-0.12.0.tar.gz; touch  check-0.12.0.tar.gz


#https://github.com/libcheck/check/releases/download/0.10.0/check-0.10.0.tar.gz
# no lograo hacer que el wget y el tar funcionen separados!
$(DOWNLOAD)/check-0.12.0/README:  | $(DOWNLOAD)/check-0.12.0.tar.gz
	cd $(DOWNLOAD); tar -zxvf check-0.12.0.tar.gz ; chmod -R a+w $(DOWNLOAD)/check-0.12.0 ; touch $(DOWNLOAD)/check-0.12.0/README

/usr/local/lib/libcheck.a:  $(DOWNLOAD)/check-0.12.0/README
	cd $(DOWNLOAD)/check-0.12.0; ./configure
	$(MAKE) -C $(DOWNLOAD)/check-0.12.0
	sudo $(MAKE) -C $(DOWNLOAD)/check-0.12.0 install
	sudo ldconfig

#rm -rf $(DOWNLOAD)/check-0.10.0     # YA NO por lib*.a. deja el directorio para que make sepa esta instalado
# PONER EN CLEAN
# revisar como hacer que no destaree siempre

# mkdir no protesta pero chmod si no puede protesta, a veces sale
#	mkdir -p $(DOWNLOAD)
#	chmod a+w $(DOWNLOAD)/exprtk.zip; echo 0;

exprtkhpp: $(DOWNLOAD)/exprtk.zip
	cd $(DOWNLOAD); unzip exprtk.zip
	mv  $(DOWNLOAD)/exprtk/exprtk.hpp $(DOWNLOAD)/exprtk.hpp
	chmod a+w $(DOWNLOAD)/exprtk.hpp
	touch $(DOWNLOAD)/exprtk.hpp # unzip dates files in 1999!
	rm -rf $(DOWNLOAD)/exprtk

$(DOWNLOAD)/exprtk.zip:
	echo bajando exprtk.zip
	mkdir -p $(DOWNLOAD)
	NSOUIP=`host partow.net 8.8.8.8 | grep 'partow.net has address' | awk -F' ' '{print $$4}'`;\
	wget --header="Host: partow.net" --no-check-certificate -P  $(DOWNLOAD) $$NSOUIP/downloads/exprtk.zip; echo 0; 
	chmod a+w $(DOWNLOAD)/exprtk.zip; echo 0;
	touch $(DOWNLOAD)/exprtk.zip; echo 0; # unzip dates files in 1999!


exprtkbajar:
	rm $(DOWNLOAD)/exprtk.zip
	make exprtkhpp
	cp $(DOWNLOAD)/exprtk.hpp share/exprtkpack/data


#/exprtk.zip;

$(DOWNLOAD)/exprtk2.zip:
	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}'`;\
	wget --no-check-certificate  -P  /tmp/${BASETMP}  $$NSOUIP/downloads/exprtk.zip; echo 0;


#sh /tmp/${BASETMP}/$(NAMESP) "$(DESTDIR)" $(PREFIX)	 



$(DOWNLOAD)/exprtk.hpp: exprtkhpp


File/Slurp Cwd Module/Refresh Graph/Undirected Term/ReadLine/Gnu:   # solo para modulos CPAN   no usar :: aca porque a make no le gusta
	export RSV=$$(echo $@ | sed -e 's/\//::/g'); \
	if perl -e "use $$RSV;"    2>&1 | grep "Can't locate"; then\
	  echo "$$RSV" NO esta instalado; \
	  sudo cpan -i "$$RSV";  \
	 else\
	  echo "$$RSV" SI esta instalado; \
	fi




#apt-get --reinstall install *
PACKSDEBIAN = iceweasel gnuplot dia gcc   emacs  subversion  gfortran make\
              gnome-terminal xdg-utils  imagemagick libxml-parser-perl \
              libwww-perl libyaml-perl  libcrypt-ssleay-perl libtext-asciitable-perl \
              data-dumper  libfile-homedir-perl  libgtk2.0-dev libwxbase2.8-0 \
              libwxgtk2.8-0 libwxbase2.8-dev libwxgtk2.8-dev  wx2.8-headers\
               wx-common xclip libterm-readline-perl-perl xtail

PACKSUBUNTU =  firefox gnuplot dia gcc  emacs subversion  gfortran make \
               gnome-terminal xdg-utils imagemagick libxml-parser-perl libwww-perl  \
               libyaml-perl libcrypt-ssleay-perl libtext-asciitable-perl  \
               data-dumper  libfile-homedir-perl  libgtk2.0-dev libwxbase2.8-0 \
               libwxgtk2.8-0 libwxbase2.8-dev libwxgtk2.8-dev   wx2.8-headers\
               wx-common xclip libterm-readline-perl-perl xtail



#	   sudo -n apt-get -y install $(PACKSUBUNTU)

# ver como enterarse de los errores del apt y reportar
# quizas correr de nuevo el dpkg query y si no da vacio error

#PACKSUTUTO = firefox-10.0.2 gnuplot-4.6_rc1 dia-0.97.2 gcc-4.5.3-r2 emacs-23.4-r1 subversion-1.7.2 make-3.82-r3 gnome-terminal-3.2.1 xdg-utils-1.1.0_rc1_p20111003 XML-LibXML-1.900.0 libwww-perl-6.30.0 yaml-0.71 Crypt-SSLeay-0.58 File-HomeDir-0.91 wxGTK-2.8.12.1 Term-ReadLine-Perl-1.30.300 xclip-0.12 xtail-2.1

PACKSUTUTO = firefox-10 gnuplot-4 dia-0 gcc-4 emacs-23 subversion-1 make-3 gnome-terminal-3 xdg-utils-1 XML-LibXML-1 libwww-perl-6 yaml-0 Crypt-SSLeay-0 File-HomeDir-0 wxGTK-2 Term-ReadLine-Perl-1 xclip-0 xtail-2

PACKSUTUTO = firefox gnuplot dia gcc emacs subversion make gnome-terminal xdg-utils XML-LibXML libwww-perl yaml Crypt-SSLeay File-HomeDir wxGTK Term-ReadLine-Perl xclip xtail


PACKSSUSE=  MozillaFirefox gnuplot dia gcc  emacs  subversion gcc-fortran make gnome-terminal xdg-utils  imagemagick perl-libxml-perl  perl-libwww-perl perl-YAML    perl-Crypt-SSLeay perl-Text-ASCIITable  perl-Getopt-Long perl-File-HomeDir wxWidgets-devel wxGTK-devel perl-Wx   xclip perl-Term-ReadLine-Perl xtail

#NO Term-ReadLine-Perl xtail xclip
#           LANG=''; dpkg-query -W  $(PACKSUBUNTU) 2>&1 1>/dev/null| sed -e's/^No packages found matching //'|sed -e's/\.$$//'| xargs sudo apt-get -y install ; \

#   LANG=''; dpkg-query -s  $(PACKSUBUNTU) 2>&1 1>/dev/null| sed -e's/^is not installed //'|sed -e's/\.$$//'| xargs sudo apt-get -y install ; \


# zypper    --non-interactive -n --quiet -q --auto-agree-with-licenses --gpg-auto-import-keys
#wget --no-check-certificate -O - http://cpanmin.us | sudo perl - --self-upgrade ;\
#sudo curl  https://raw.github.com/miyagawa/cpanminus/master/cpanm > /usr/local/bin/cpanm \
#		   chmod +x /usr/local/bin/cpanm \
#		   cpanm --self-upgrade --sudo \




BINDIRSRC=bin/
ETCDIRSRC=etc/
LIBDIRSRC=lib/
SHAREDIRSRC=share/
PKGDIRSRC=
SRCDIRSRC=src/

IBINFILES=$(shell $(shellpath) find  -type f -not -wholename '*.svn*'| grep ^\./$(BINDIRSRC) | grep -v '^\#' | grep -v '~$$')

ISHAREFILES=$(shell $(shellpath) find  -type f -not -wholename '*.svn*'| grep ^\./$(SHAREDIRSRC) | grep -v '^\#' | grep -v '~$$')

ILIBFILES=$(shell $(shellpath) find  -type f -not -wholename '*.svn*'| grep ^\./$(LIBDIRSRC) | grep -v '^\#' | grep -v '~$$')

IETCFILES=$(shell $(shellpath)  find -type f -not -wholename '*.svn*'| grep ^\./$(ETCDIRSRC) | grep -v '^\#' | grep -v '~$$')

IPKGFILES =$(shell /bin/ls -l $(PKGDIRSRC) 2>/dev/null | grep '^-' |  awk -F' ' '{print $$9}' | grep -v '^\#' | grep -v '~$$'| grep -v 'listado.ls')

ISHAREDIRS=$(shell $(shellpath) find  -type d -not -wholename '*.svn*'| grep ^\./$(SHAREDIRSRC) )
ILIBDIRS=$(shell $(shellpath) find  -type d -not -wholename '*.svn*'| grep ^\./$(LIBDIRSRC) )

#IFILDESKTOP=$(shell find  -type f -not -wholename '*.svn*'| grep  ".desktop" )   # launchers/


ISRCFILES=$(shell $(shellpath) find  -type f -not -wholename '*.svn*'| grep ^\./$(SRCDIRSRC) | grep -v '^\#' | grep -v '~$$')
ISRCDIRS=$(shell $(shellpath) find  -type d -not -wholename '*.svn*'| grep ^\./$(SRCDIRSRC) )


# LIB INCLUDE SBIN SRC LOCALE GAMES

#$(info DEBUG $(DEBUG))
#DEBUG ?= 1
ifeq ($(DEBUG),1)
$(info IBINFILES $(IBINFILES))
$(info ISHAREFILES $(ISHAREFILES))
$(info ISHAREDIRS $(ISHAREDIRS))
$(info ILIBFILES $(ILIBFILES))
$(info ILIBDIRS $(ILIBDIRS))
$(info IETCFILES $(IETCFILES))
$(info IPKGFILES $(IPKGFILES))
$(info ISRCFILES $(ISRCFILES))
$(info ISRCDIRS $(ISRCDIRS))
endif




define installdirs
	@for FILE in $1  ; do \
	   if test x"$$FILE" != x"." ; then \
	    FILEP=`echo -n $$FILE|sed -e  's/\.\/$3//' ` ;\
	    install  -d $2/$$FILEP; test 0=0;\
	    echo "D:$5/$$FILEP" >> listado.ls ; test 0=0;\
	   fi;\
	done;	
endef
# antes de crearlo y ponerlo en listado verificar si no existe, no poner en listado si esta
# esto hace que el orden de instalacion de paquetes sea relevante para los listados.ls
#no se

#@echo D Ultimoarg #============= $1 == $2 == $3 == $4 == $5
#echo "D:$5/$$FILEP"  ;
define installfiles
	@for FILE in $1 ; do \
	   if test x"$$FILE" != x"." ; then \
	    FILEP=`echo -n $$FILE|sed -e  's/\.\/$3//' ` ;\
	    if test x"$5" = x ; then \
	       FILEC=$$FILE; \
	      else \
               FILEC=`echo $$FILE|  sed -e  's/^\./\.\/$5/'`; \
	    fi; \
	    echo "A:$6/$$FILEP" >> listado.ls ; test 0=0;\
	    echo "A:$6/$$FILEP"; test 0=0;\
	    install $$FILEC  $2/$$FILEP; test 0=0;\
fi;\
	done;
endef
#@echo F Ultimoarg #=========== $1 == $2 == $3 == $4 == $5 == $6
#echo "A:$6/$$FILEP"  

ETCROOT=/
BINDIR=bin
ETCRES=etc
ETCDIR=$(ETCROOT)etc
SHAREDIR=share
#PKGDIR=share/$(PACK)/pkg-data
# este en osc viene del spec
PKGDIR=$(PREFIX)/share/doc/packages/$(PACK)
$(info PKGDIR: $(PKGDIR)) 

#	$(call copytree,$(PREFIXDIA),$(ETCDIR),$(SUDO), prefixsintmp)


# OJO PKGDIR es distinto en diferentes distros, se lo ve en osc, por eso no va el $1, y el $1 se lo pone normalemente  en la VARIABLE
# ojo si no hay DESTDIR
define copytree
	@$3 mkdir -p $1/${SHAREDIR}; test 0 = 0;
        echo "D:$4/${SHAREDIR}" >> listado.ls ; test 0 = 0;
	$(call installdirs,${ISHAREDIRS},$1,,$3,$4)
	$(call installfiles,${ISHAREFILES},$1,,$3,,$4)
	@for FILE in ${ISHAREFILES} ; do \
	     chmod a-x $1/$$FILE ; test 0 = 0; \
	done; 
	@$3 mkdir -p $1/${LIBDIR}; test 0 = 0;
	$(call installdirs,${ILIBDIRS},$1,,$3,$4)
	$(call installfiles,${ILIBFILES},$1,,$3,,$4)
	@$3 mkdir -p $1/${BINDIR}; test 0 = 0;
	$(call installfiles,${IBINFILES},$1,,$3,,$4)
	@$3 mkdir -p $2${PKGDIR}; test 0 = 0;
	$(call installfiles,${IPKGFILES},$2${PKGDIR},,$3,,$2${PKGDIR})
	chmod a-x $2${PKGDIR}/* ; test 0 = 0
	@if test x"${IETCFILES}" != x ; then \
	        $3 mkdir -p $2/etc ;\
                echo "D:$2/etc" >> listado.ls ; test 0 = 0;\
	fi;
	$(call installfiles,${IETCFILES},$2,,$3,,$2)
	@for FILE in ${IETCFILES} ; do \
	   chmod a-x $2/$$FILE ; test 0 = 0 ; \
	done;
	mkdir -p $1/share/man/man1;
        echo "D:$4/share/man/man1" >> listado.ls ; test 0 = 0;
        @for FILE in ${IBINFILES} ; do \
	   FILEP=`echo -n $$FILE|sed -e  's/\.\/bin\///' ` ;\
           ESSH=`echo $$FILEP | grep sh | sed -e 's/\.sh$$//'` ;\
           ESPL=`echo $$FILEP | grep pl | sed -e 's/\.pl$$//'` ;\
           if test x"$$ESSH" != x ; then \
               help2man $$FILE >> $1/share/man/man1/$$ESSH.1 ;test 0 = 0 ; \
	       gzip -9n -f $1/share/man/man1/$$ESSH.1 ; test 0 = 0 ; \
               echo "A:$4/share/man/man1/$$ESSH.gz" >> listado.ls ; test 0=0;\
           elif test x"$$ESPL" != x ; then \
               help2man $$FILE >> $1/share/man/man1/$$ESPL.1 ; test 0 = 0 ; \
               gzip -9n -f  $1/share/man/man1/$$ESPL.1 ; test 0 = 0 ; \
               echo "A:$4/share/man/man1/$$ESPL.gz " >> listado.ls ; test 0=0;\
           fi ;\
	done;
endef
#--no-discard-stderr
# antes se iba a usar pod2help
# si hay paginas man hechas a mano instalar despues de help2man
# sacar chmod en PKGDIR, protesta osc, hacerlo con el comando install y opciones



# crear etc solo se justifica en destdir o distros nuevas!
#echo DIR ETC $2 ;\

#echo copytree $1 - $2 - $3 - $4
#echo SHARE $(SHAREDIRSRC)
#echo LIB $(LIBDIRSRC)
#echo BIN $(BINDIRSRC)
#echo PKG $(PKGDIRSRC)
#echo ETC $(ETCDIRSRC)
#OJOOOOOOOOOOOOOO 	    install  $$FILE  ${PREFIX}/share/$(PACK)/launchers/$$KKK;\
#instalar esto, oya esta?????

#gnome
idesktop:  #### es tentador ponerlo aca y en el arbol, pero que pasa con prefix!!!!!!!
	@for FILE in ${IFILDESKTOP} ; do \
	   if test x"$$FILE" != x"." ; then \
	    echo "JJJ $$FILE"; \
	    KKK=`echo $$FILE | sed -e's/\.\/share\/$(PACK)\/launchers\///'`; echo "MMMMMM $$KKK"; \
	    install  $$FILE  /usr/share/applications/$$KKK;\
	    install  $$FILE  ${PREFIX}/share/$(PACK)/launchers/$$KKK;\
	    chmod -x  /usr/share/applications/$$KKK;\
	    echo "sed -i'' -e 's/XXX/${SPREFIX}/'      ${PREFIX}/share/${DIRICON}/launchers/$$KKK" ; \
	    sed -i'' -e 's/XXX/${SPREFIX}/'      ${PREFIX}/share/${DIRICON}/launchers/$$KKK;\
	   fi;\


inslaunchersXXX:
	@for FILE in ${IFILDESKTOP} ; do \
	   if test x"$$FILE" != x"." ; then \
	    echo "JJJ $$FILE"; \
	    KKK=`echo $$FILE | sed -e's/\.\/share\/$(PACK)\/launchers\///'`; echo "MMMMMM $$KKK"; \
	    install  $$FILE  /usr/share/applications/$$KKK;\
	    install  $$FILE  ${PREFIX}/share/$(PACK)/launchers/$$KKK;\
	    chmod -x  /usr/share/applications/$$KKK;\
	    echo "sed -i'' -e 's/XXX/${SPREFIX}/'      ${PREFIX}/share/${DIRICON}/launchers/$$KKK" ; \
	    sed -i'' -e 's/XXX/${SPREFIX}/'      ${PREFIX}/share/${DIRICON}/launchers/$$KKK;\
	   fi;\
	   for USER in ${USERS} ; do \
	       ESUSER=`grep "^$$USER:" /etc/passwd| awk -F':' '{print $$1}'`; \
	       if test x"$$ESUSER" != x ; then \
	         echo "	icon_install  $$USER ${PREFIX}/share/${DIRICON}/launchers " ;\
	         icon_install  $$USER ${PREFIX}/share/${DIRICON}/launchers;\
	       fi; \
	     done;\
	done;

clean::
	rm -rf *~
	rm -f listado.ls

# gzip -n para obtener mismo md5
dist: clean
	echo "SOURCE DEVELOPMENT DISTRIBUTION FILE ${PACKV}-${VERSION}.tar.gz"
	mkdir -p /tmp/$(BASETMP)/${PACK}-${VERSION}
	rm -rf /tmp/$(BASETMP)/${PACK}-${VERSION}/*
	cp -La .  /tmp/${BASETMP}/${PACK}-${VERSION} 
	tar -C /tmp/${BASETMP} -c ${PACK}-${VERSION} --owner=0 --group=0  | gzip -n>  $(CURDIR)/../${PACKV}-${VERSION}.tar.gz 
	md5sum  ../${PACKV}-${VERSION}.tar.gz>../${PACKV}-${VERSION}.tar.gz.md5



# no se pone dist para no hacer siempre de nuevo
# hacer dist antes de usar

upload: dist
	wget -O SP.md5 "http://$(SERVERNAME)/$(SERVERBAJAR)/${PACKV}-UNSTABLE.tar.gz.md5"; exit 0;
	touch SP.md5 ;
	DIF=`diff SP.md5  ../${PACKV}-${VERSION}.tar.gz.md5` ;\
	rm -f diff SP.md5 ; \
	if test x"$$DIF" != x ; then \
	   echo "DIF= $$DIF"; \
	   echo scp "../${PACKV}-${VERSION}.tar.gz.md5"  "../${PACKV}-${VERSION}.tar.gz" "${SERVERUSER}@${SERVERNAME}:${SERVERDIR}" ;\
	   scp ../${PACKV}-${VERSION}.tar.gz.md5  ../${PACKV}-${VERSION}.tar.gz "${SERVERUSER}@${SERVERNAME}:${SERVERDIR}" ;\
	   ssh ${SERVERUSER}@${SERVERNAME} "cd ${SERVERDIR}; rm -f ${PACKV}-UNSTABLE.tar.gz; ln -s ${PACKV}-${VERSION}.tar.gz ${PACKV}-UNSTABLE.tar.gz; rm -f ${PACKV}-UNSTABLE.tar.gz.md5; ln -s ${PACKV}-${VERSION}.tar.gz.md5 ${PACKV}-UNSTABLE.tar.gz.md5";\
	fi;
	rm -f *.md5; exit 0

#	   

stable:
	ssh $(SERVERUSER)@$(SERVERNAME) "cd $(SERVERDIR); mv  ${PACKV}-UNSTABLE.tar.gz ${PACKV}-last.tar.gz; mv  ${PACKV}-UNSTABLE.tar.gz.md5 ${PACKV}-last.tar.gz.md5" 






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

define mexec
	@for FILE in $(IBINFILES) ; do \
	   if test x"$$FILE" != x"." ; then \
	    FILEP=`echo -n $$FILE|sed -e  's/\.\/$3//' ` ;\
	    chmod a+x  $1$(PREFIX)/$$FILEP;\
	  fi; \
	done;
endef


define compilation
	   make ;\
	   sudo -n make install 
endef


#echo chmod a+x  $1$(PREFIX)/$$FILEP
###############################################################


#ESUSER solo para verificar que esta en passwd
#echo ESUSER"$$ESUSER"X;\
#	    echo "sed -i'' -e 's/YYY/${SPREFIXDOCS}/'  ${PREFIX}/share/${DIRICON}/launchers/$$KKK" ;\
#    sed -i'' -e 's/YYY/${SPREFIXDOCS}/'  ${PREFIXP}/share/${DIRICON}/launchers/$$KKK;\




#USERS=`ls /home`		
#if test x$(USERID) = x0 ; then #
#	    for USER in ${USERS} ; do 
#             echo USER $$USER; 
#	     if test -d /home/$$USER ; then 
#	      echo BUENUSER $$USER; 
#             ESUSER=`grep "^$$USER:" /etc/passwd| awk -F' ' '{print $$1}'`;
#	      echo BUENUSER $$USER $$ESUSER; 
#	      
#                # instalar en un usuario	       
#	      fi;
#	     done;
#	    else 
 #                # instalar en un usuario	       
#	    fi;
#	done; 


#PREFIXDOC=${PREFIX}${DOT}${DOC}
#PREFIX=${PREFIXP}/share
#PREFIXS=${PREFIXP}/lib
#PREFIXD=/usr/share
#DOT=/
#DOC=doc/simusol
# mientras dia se insatle como paquete usar /usr, si no /usr local
#PREFIXDIA=/usr
#PREFIXDIASIMUSOL=/usr/local
#PREFIXSIM=${PREFIXS}${DOT}simusol
#PREFIXDIA=${PREFIXD}${DOT}dia
#PREFIXDOCS=${PREFIX}${DOT}${DOC}


cpmakefilelib:
	mkdir -p share/summapack/data; test 0 = 0;
	cp ../summapack/share/summapack/data/makefile.lib share/summapack/data/makefile.lib ; test 0 = 0;


# esto conviene ponerlo en el make de cada paquete, aqui es si ya esta, pero como este archivpo se puede poner solo....
NAMESP=installsumma.sh  
SOU=http://simusol.org/downloads/${NAMESP}
installsummapackMAKE:
	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); echo 0;
	sh /tmp/${BASETMP}/$(NAMESP) "$(DESTDIR)" $(PREFIX)	 
