#!/bin/bash

# sceptre_install user

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

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


# agregar archivos instalados a listado.ls en crealidas sceptre_simusol.ls

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/sceptre_simsuol ; then 
    SHAREDIR="/home/$USER/share/sceptre_simusol"
elif test -e /usr/local/share/sceptre_simusol ; then
    SHAREDIR="/usr/local/share/sceptre_simusol"
elif test -e /usr/share/sceptre_simusol ; then
    SHAREDIR=/usr/share/sceptre_simusol
else
    echo sceptre_simusol no está instalado
    exit;
fi    
#NOTA AHORA AL ejecutar simusol se crea un HOME/lib vacio


FILE=$SHAREDIR/launchers/sceptre_simusol-Manual.desktop



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; 
		 su $USER -c "mkdir -p /home/$USER/sceptre_ejemplos"; 
		 su $USER -c "cp -r ${SHAREDIR}/examples/* /home/$USER/sceptre_ejemplos";  
		 #chown -R $USER /home/$$USER/sceptre_ejemplos;
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; 
		 mkdir -p /home/$USER/sceptre_ejemplos; 
		 cp -r ${SHAREDIR}/examples/* /home/$USER/sceptre_ejemplos;  
		 #chown -R $USER /home/$$USER/sceptre_ejemplos;

    fi


echo "Installation Finished for $USER sceptre_simusol-install-  "

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