# PREFIX  puede ser modificada (override) com argumento en la linea de comando
# make PREFIX=/pepito
# instala   sheets y shapes dia, documentos
USERID=$(shell id -u)
PACK=$(shell  ls *.summa| sed -e s/\.summa$$//)
$(info PAQUETE: ${PACK}) 

 #$(info User Id is $(USERID))
 ifeq ($(USERID),0)
                $(info Running as root)
                ifeq (exists, $(shell test -d /usr/share/simusol  && echo exists ))
	                PREFIXP=/usr
                      else 
			PREFIXP=/usr/local
                endif
		PREFIX=${PREFIXP}/share
		PREFIXS=${PREFIXP}/lib
		PREFIXD=/usr/share
	        DOT=/
	        DOC=doc/simusol
		USERS=`ls /home`
           else
                $(info Running as user)
		PREFIX=${HOME}
		PREFIXP=${HOME}
		PREFIXD=${HOME}
		PREFIXS=${HOME}
	        DOT=/.
	        DOC=simusol/doc
		USERS=$(shell id -un)
endif

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




ifeq (exists, $(shell test -d ${PREFIXSIM}  && echo exists ))
        $(info PREFIXSIM: ${PREFIXSIM})
   else
        $(info Simusol no parece estar instalado, PREFIXSIM: ${PREFIXSIM})
endif
ifeq (exists, $(shell test -d ${PREFIXDIA}  && echo exists ))
        $(info PREFIXDIA: ${PREFIXDIA})
   else
        $(info Dia no parece estar instalado, PREFIXDIA: ${PREFIXDIA})
endif
IFILSHAPES=$(shell find  -type f -not -wholename '*.svn*'| grep "shapes/" )
IFILDOCS=$(shell find  -type f -not -wholename '*.svn*'| grep  "docs/" )
IFILSHEETS=$(shell find  -type f -not -wholename '*.svn*'| grep  "sheets/" )
#IFILSIMUSOL=$(shell find  -type f -not -wholename '*.svn*'| grep -v "shapes/" |grep -v "sheets/" |grep -v "docs/")
IFILSIMUSOLB=$(shell find  -type f -not -wholename '*.svn*'| grep  "bin/" )
IFILSIMUSOLE=$(shell find  -type f -not -wholename '*.svn*'| grep  "etc/" )
IFILSIMUSOLS=$(shell find  -type f -not -wholename '*.svn*'| grep  "share/" )
IFILSIMUSOLL=$(shell find  -type f -not -wholename '*.svn*'| grep  "lib/" )
IFILDESKTOP=$(shell find  -type f -not -wholename '*.svn*'| grep  ".desktop" )   # launchers/
IFILELEMENTOS=$(shell find  -type f -not -wholename '*.svn*'| grep  "elementos/"|grep -v "conelementos/" )


#$(info SHAPES ${IFILSHAPES})
#$(info DOCS ${IFILDOCS})
#$(info SHEETS ${IFILSHEETS})
#$(info SIMUSOL ${IFILSIMUSOL})
IDSHAPES=$(shell find  -type d -not -wholename '*.svn*'| grep "shapes/" )
IDDOCS=$(shell find  -type d -not -wholename '*.svn*'| grep  "docs/" )
IDSHEETS=$(shell find  -type d -not -wholename '*.svn*'| grep  "sheets/" )
#IDSIMUSOL=$(shell find  -type d -not -wholename '*.svn*'| grep -v "shapes/"|grep -v "sheets/" |grep -v "docs/")
IDSIMUSOLB=$(shell find  -type d -not -wholename '*.svn*'| grep  "bin/" )
IDSIMUSOLE=$(shell find  -type d -not -wholename '*.svn*'| grep  "etc/" )
IDSIMUSOLS=$(shell find  -type d -not -wholename '*.svn*'| grep  "share/" )
IDSIMUSOLL=$(shell find  -type d -not -wholename '*.svn*'| grep  "lib/" )
#$(info D SHAPES ${IDSHAPES})
#$(info D DOCS ${IDDOCS})
#$(info D SHEETS ${IDSHEETS})
#$(info D SIMUSOL ${IDSIMUSOL})

# falta documento del sceptre como icono

install:
	rm -f listado.ls
	install -d ${PREFIX}
	#caso overriden por command line
	install -d ${PREFIXDIA}
	install -d ${PREFIXDIA}/sheets
	install -d ${PREFIXDOCS}
	install -d ${PREFIXSIM}
	install -d ${PREFIXP}
	install -d ${PREFIXP}/share/simusol/launchers
	install -d ${PREFIXP}/lib/simusol/elementos     # mover a share
	install -d ${PREFIXP}/lib/simusol               # mover a share
	#install -d ${PREFIXP}/bin
	#install -d ${PREFIXP}/etc
	#install -d ${PREFIXP}/share
	#install -d ${PREFIXP}/lib
	# caso overrriden por command line
	for FILE in  ${IDSIMUSOLB} ${IDSIMUSOLE} ${IDSIMUSOLS} ${IDSIMUSOLL} ; do \
	   if test x"$$FILE" != x"." ; then \
	    FILEP=`echo -n $$FILE|sed -e  's/\.\///' ` ;\
	    install  -d ${PREFIXP}/$$FILEP;\
	    #echo "D:${PREFIXP}/$$FILEP"  ;\
	    echo "D:${PREFIXP}/$$FILEP" >> listado.ls ;\
	fi;\
	done;
	for FILE in  ${IDSHEETS} ${IDSHAPES}; do \
	   if test x"$$FILE" != x"." ; then \
	    FILEP=`echo -n $$FILE|sed -e  's/\.\///' ` ;\
	    install  -d ${PREFIXDIA}/$$FILEP;\
	    echo "D:${PREFIXDIA}/$$FILEP" >> listado.ls ;\
	   fi;\
	done;
	for FILE in  ${IDDOCS} ; do \
	   if test x"$$FILE" != x"." ; then \
	    FILEP=`echo -n $$FILE|sed -e  's/\.\/docs\///' ` ;\
	    install  -d ${PREFIXDOCS}/$$FILEP;\
	    echo "D:${PREFIXDOCS}/$$FILEP" >> listado.ls ;\
	   fi;\
	done;
	for FILE in  ${IFILSIMUSOLB} ${IFILSIMUSOLE} ${IFILSIMUSOLS} ${IFILSIMUSOLL} ; do \
	   if test x"$$FILE" != x"." ; then \
	    FILEP=`echo -n $$FILE|sed -e  's/\.\///' ` ;\
	    install  $$FILE ${PREFIXP}/$$FILEP;\
	    echo "A:${PREFIXP}/$$FILEP" >> listado.ls ;\
	   fi;\
	done;
	for FILE in  ${IFILSHEETS} ${IFILSHAPES}; do \
	   if test x"$$FILE" != x"." ; then \
	    FILEP=`echo -n $$FILE|sed -e  's/\.\///' ` ;\
	    install   $$FILE ${PREFIXDIA}/$$FILEP;\
	    echo "A:${PREFIXDIA}/$$FILEP" >> listado.ls ;\
	   fi;\
	done;
	for FILE in  ${IFILDOCS} ; do \
	   if test x"$$FILE" != x"." ; then \
	    FILEP=`echo -n $$FILE|sed -e  's/\.\/docs\///' ` ;\
	    install  $$FILE  ${PREFIXDOCS}/$$FILEP;\
	    echo "A:${PREFIXDOCS}/$$FILEP" >> listado.ls ;\
	   fi;\
	done;
	echo "ELEMENTOS"
	for FILE in  ${IFILELEMENTOS} ; do \
	   if test x"$$FILE" != x"." ; then \
	    install  $$FILE  ${PREFIXP}/lib/simusol/$$FILE;\
	    echo "A:${PREFIXP}/lib/simusol/$$FILE" >> listado.ls ;\
	   fi;\
	done;
	for FILE in ${IFILDESKTOP} ; do \
	   if test x"$$FILE" != x"." ; then \
	    mkdir -p ${PREFIXP}/share/simusol/launchers;\
	    install  $$FILE  ${PREFIXP}/share/simusol/$$FILE;\
	    sed -i'' -e 's/XXX/${SPREFIXP}/'      ${PREFIXP}/share/simusol/$$FILE;\
	    sed -i'' -e 's/YYY/${SPREFIXDOCS}/'  ${PREFIXP}/share/simusol/$$FILE;\
	   fi;\
	   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; \
	      if test x"$$ESUSER" != x ; then \
	        su $$USER -c "xdg-desktop-icon install ${PREFIXP}/share/simusol/$$FILE";\
	        echo "su $$USER -c xdg-desktop-icon install ${PREFIXP}/share/simusol/$$FILE";\
	        if test -d /home/$$USER/Escritorio ; then \
	             if test -d /home/$$USER/Desktop ; then \
                         FILEP=`echo -n $$FILE|sed -e  's/\.\/launchers\///' ` ;\
                         echo  su $$USER -c cp  /home/$$USER/Desktop/$$FILEP /home/$$USER/Escritorio;\
	                 su $$USER -c "cp  /home/$$USER/Desktop/$$FILEP /home/$$USER/Escritorio" ;\
	                 fi; \
	             fi; \
		mkdir -p /home/$$USER/simusol_ejemplos; \
		cp -r ${PREFIXP}/share/simusol/examples/projects/* /home/$$USER/simusol_ejemplos;  \
		chown -R $$USER /home/$$USER/simusol_ejemplos ;\
		fi;\
	     fi;\
	    done;\
	    else \
	       xdg-desktop-icon install ${PREFIXP}/share/simusol/$$FILE;\
		echo UNSOLO $$USER ;\
	        if test -d ~/Escritorio ; then \
	             if test -d ~/Desktop ; then \
			 FILEP=`echo -n $$FILE|sed -e  's/\.\/launchers\///' ` ;\
	                 cp  ~/Desktop/$$FILEP ~/Escritorio ;\
	                 fi; \
	            fi; \
	    fi;\
	done; \
	install -d ${PREFIXSIM}/ls
	mv  ${PREFIXSIM}/ls/${PACK}.ls ${PREFIXSIM}/ls/${PACK}.ls.viejo 2>/dev/null; exit 0;
	install listado.ls ${PREFIXSIM}/ls/${PACK}.ls


clean:
	echo OK

#mkdir -p /home/$$USER/.local/share/nautilus/scripts/ ;\
#cp OpenTerminalHere  /home/$$USER/.local/share/nautilus/scripts/ ; \
#chmod a+x /home/$$USER/.local/share/nautilus/scripts/* ;\
#sudo apt-get install nautilus-open-terminal
######################## FUNCTIONS

define installdirs
	@echo D Ultimoarg============================= $3
	@for FILE in $1  ; do \
	   if test x"$$FILE" != x"." ; then \
	    FILEP=`echo -n $$FILE|sed -e  's/\.\/$3//' ` ;\
	    install  -d $2/$$FILEP;\
	    echo "D:$2/$$FILEP" >> listado.ls ;\
	   fi;\
	done;
endef

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





