# 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}) 
UBUNTU=$(shell cat /etc/issue| grep Ubuntu)
$(info UBU: ${UBUNTU}) 
 #$(info User Id is $(USERID))

#Create a root owned .desktop file in /etc/xdg/autostart folder for all users to access, and for Exec= line , create a symbolic link to the program you want users to access. For instance,
#ln -s /usr/share/applications/gedit.desktop /home/$USER/Desktop/gedit-link
#The link will be created on user's desktop every time they log in.

# solo correr bajo SUDO o sea userid = 0 
# como dia se instala en /usr solo lee sheets en /usr
#http://askubuntu.com/questions/522949/adding-my-launcher-to-the-unity-launcher-automatically-how

 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

 # para que unity encuentre  usr share pallications

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/\.\///' ` ;\
	    echo "D:${PREFIXP}/$$FILEP"  ;\
	    install  -d ${PREFIXP}; \
	    install  -d ${PREFIXP}/$$FILEP; \
	    echo "D:${PREFIXP}/$$FILEP" >> listado.ls ;\
	fi;\
	done;
	for FILE in  ${IDSHAPES}; do \
	   echo "LDA:$$FILE" ;\
	   if test x"$$FILE" != x"." ; then \
	    FILEP=`echo -n $$FILE|sed -e  's/\.\///' ` ;\
	    install  -d ${PREFIXDIA}/$$FILEP;\
	    rm  -f ${PREFIXDIA}/$$FILEP/*;\
	    echo "D:${PREFIXDIA}/$$FILEP" >> listado.ls ;\
	    echo "D:${PREFIXDIA}/$$FILEP" ;\
	   fi;\
	done;
	for FILE in  ${IDSHEETS}; do \
	   echo "LDE:$$FILE" ;\
	   if test x"$$FILE" != x"." ; then \
	    FILEP=`echo -n $$FILE|sed -e  's/\.\///' ` ;\
	    install  -d ${PREFIXDIA}/$$FILEP;\
	    echo "D:${PREFIXDIA}/$$FILEP" >> listado.ls ;\
	    echo "D:${PREFIXDIA}/$$FILEP" ;\
	   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/\.\///' ` ;\
	    echo "SHAPES $$FILE -- $$FILEP"  ;\
	    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;\
	    echo "JJJ $$FILE"; \
	    KKK=`echo $$FILE | sed -e's/\.\/launchers\///'`; echo "MMMMMM $$KKK"; \
	    install  $$FILE  ${PREFIXP}/share/simusol/$$FILE;\
	    install  $$FILE  /usr/share/applications/$$KKK;\
	    chmod -x  /usr/share/applications/$$KKK;\
	    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 \
                if  test  \( x"${UBUNTU}" = x \) -o \( x"$$FILE" = x"./launchers/Simusol-Manual.desktop" \) -o \( x"$$FILE" = x"./launchers/Simusol-Tutorial.desktop" \) -o \( x"$$FILE" = x"./launchers/Simusol-Sceptre-Manual.desktop" \) ; then \
		 echo "KKKKK ${UBUNTU} - $$FILE" ;\
	         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; \
	        if  test x"$$UBUNTU" != x ; then \
			APPS=`gsettings get com.canonical.Unity.Launcher favorites`; \
			echo APPS UNITY $$APPS; \
			NAPPS="'application:\/\/gnome-terminal.desktop', 'application:\/\/dia.desktop',";\
			MAPPS=`echo -n $$APPS |sed -e"s/$$NAPPS//g"` ;\
			echo MAPPS UNITY $$MAPPS; \
			NNAPSS=`echo -n $$MAPPS |sed -e"s/\[/\[$$NAPPS/" ` ;\
			gsettings set com.canonical.Unity.Launcher favorites "$$NNAPSS" ;\
		fi;\
	       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
	echo BORRE TODO LO QUE TENGA EN SU DIRECTORIO OCULTO .dia, a menos que ud sepa que tiene algo de su interes.

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




### eliminar en el .dia de cada usuario todo pero ver si el usuario no tiene algo propio
# es complejo
