#!/bin/bash

############## ojo cambiar /usr/local!!!!!
# ojo sacando /usr/bin/env y poniendo directo /usr/bin/xdgopen
#verificar que este bien

if test x"$1" = x--help ; then
    echo AYUDA
    exit 0
    fi

if test x"$1" = x--version ; then
    echo icon_install.sh version 3
    exit 0
    fi


# icon_install
# se lo pone aqui porque nuevos usuarios deberan instalarlo si no se pone en el skeleton estos files
# lo usan los programas que instalan iconos: dia-simusol, simusol, sceptre

#los programas instalan los iconos a los usuarios preexistentes, no al skeleton
# nuevos usuarios deben usarlo, en general cada paquete tiene un XX_user_install que llama a este programa



#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.


#http://askubuntu.com/questions/522949/adding-my-launcher-to-the-unity-launcher-automatically-how



UBUNTU=`cat /etc/issue| grep Ubuntu`
USERACTUAL=`id -un`

#DIRICON=dia-simusol
#ICON='application:\/\/dia.desktop',
#DIRICON=$2


echo "icon_install $1 $2"

USERID=`id -u`;
if  test x"$1" = x ; then
    USER=$USERACTUAL;
else    
    USER=$1;
fi
#
#USERS=`id -un`
ESUSER=`grep "^$USER:" /etc/passwd| awk -F':' '{print $1}'`; #solo para verificar que esta en passwd

if test x"$ESUSER"  = x ; then
      echo "Usuario indefinido";
      exit;
fi


USRDIR=/home/$USER
if test x"$USERID" != x0 ; then 
    if test x"$USERACTUAL" != x"$ESUSER" ; then
       echo "No es root y pretende instalar a usuario $USER que no es $USERACTUAL, userid: $USERID , esuser: $ESUSER \n";
       exit;       
      else
       USRDIR=/root
       fi	
fi    

#if test -e /home/$USER/share/$DIRICON ; then 
#    SHAREDIR="/home/$USER/share/$DIRICON"
#elif test -e /usr/local/share/$DIRICON ; then
#    SHAREDIR="/usr/local/share/$DIRICON"
#elif test -e /usr/share/$DIRICON ; then
#    SHAREDIR=/usr/share/$DIRICON
#else
#    echo $DIRICON no está instalado
#    exit;
#fi    
#NOTA AHORA AL ejecutar simusol se crea un HOME/lib vacio


#FILES = $SHAREDIR/launchers/*
FILES=`ls $2/*`

echo FILES: $FILES;
#FILE=$2

for FILE in ${FILES} ; do 
   if test x"$USERID" = x0 ; then 
	         su $USER -c "xdg-desktop-icon install $FILE";
	         echo "su $USER -c xdg-desktop-icon install $FILE";
	         if test -d /home/$USER/Escritorio ; then 
                     echo  su $USER -c cp  $FILE /home/$USER/Escritorio;
	             su $USER -c "cp  $FILE /home/$USER/Escritorio" ;
	             fi; 
      else
	         xdg-desktop-icon install $FILE;
	         echo "xdg-desktop-icon install $FILE";
	         if test -d /home/$USER/Escritorio ; then 
                     echo  cp  $FILE /home/$USER/Escritorio;
	             cp  $FILE /home/$USER/Escritorio ;
                 fi;
	fi;	 
done;

for FILE in ${FILES} ; do 
if  test  x"${UBUNTU}" != x ; then 
			APPS=`gsettings get com.canonical.Unity.Launcher favorites`; 
			echo APPS UNITY $APPS;
			BASENAME=`basename $FILE`;
			NAPPS="'application:\/\/$BASENAME.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;
done;

echo "Installation Finished"

exit;


 #IFILDESKTOP=$(shell find  -type f -not -wholename '*.svn*'| grep  ".desktop" )   # launchers/
#for FILE in ${IFILDESKTOP} ; do  
#		 echo "KKKKK ${UBUNTU} - $FILE" ;


#	             if test -d /home/$USER/Desktop ; then

#done
		
#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; 



# 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; 



# ver si sceptre esta en en /usr/local/bin o si esta en /usr/bin o en /home/$USER/bin
#BASEDIR=/usr/local
#LIBDIR=${BASEDIR}/lib
#ETCDIR=${BASEDIR}/etc
# VER SI ESTOS ARCHIVOS SON NECESARIOS
#if [ ! -d "$USRDIR/lib" ]; then
#	mkdir $USRDIR/lib
#fi
#cp -iv ${LIBDIR}/usrlib.a 	$USRDIR/lib
#cp -iv ${LIBDIR}/modellib.dat 	$USRDIR/lib

#if [ ! -d "$USRDIR/etc" ]; then
#	mkdir $USRDIR/etc
#fi
####### ojo, esto esta en /etc, ver de donde lo lee
#cp -iv ${ETCDIR}/ngp.rc 	$USRDIR/etc
