#!/bin/bash


# dia_simusol_install user
# se lo pone aqui porque nuevos usaurios deberan instalarlo si no se pone en el skeleton 

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

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


if  test x"$1" = x ; then
    USERID=`id -u`;
    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, $ESUSER ";
       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/*


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


if  test  x"${UBUNTU}" != x ; then 
			APPS=`gsettings get com.canonical.Unity.Launcher favorites`; 
			echo APPS UNITY $APPS; 
			NAPPS="$ICON";
			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;

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
