#!/bin/bash
# Modificado para GALPon MiniNo
# 
# Make owned by current user - Makes the selected files owned by the current user with group
# being the user's primary group (the first in the output from the "groups" command)
#
# Installation:
# Put this script into the Nautilus script dir (~/.gnome2/nautilus-scripts) and make it executable.
#
# Usage:
# Right-click on files in Nautilus and choose Scripts -> Make owned by current user
#
# History:
# v20080131 - Fredrik Wollsén (this script is based on this guy's version you can find at : http://ubuntuforums.org/showthread.php?t=683945)
# This version did not work for me I dont know why... 
#
# So i made this version with additionnal functionnalities
# v20080919 - Emmanuel Morin (this script's current version)
#
# License GPL v3

# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

case $LANG in
    es*)
	MSG1="Vd. no puede ser el propietario del directorio"
	MSG2="ya que es probable que eso rompa su sistema."
	MSG3="Se requiere la contraseña del administrador para cambiar permisos y propietario:"
	MSG4="¿Tiene acceso como administrador?"
    ;;
    gl*)
	MSG1="Vostede non pde ser o propietario do directorio"
	MSG2="xa que é probable que iso rache o seu sistema."
	MSG3="Requirese o contrasinal do administrador para cambiar permisos e propietario:"
	MSG4="Ten acceso como administrador?"
    ;;
    *)
	MSG1="You cannot own the"
	MSG2="directory. This will most likely break your system."
	MSG3="It is necesary administrator password to change permissions."
	MSG4="Do you have administrator access?"
    ;;
esac

#Finds your current username
USER=`whoami`;

#Finds your user group name
GROUP=`groups | sed -r 's/ .*//g'`

#If no group... your group is your username
if [ "$GROUP" == "" ] ; then
GROUP=$USER;
fi

# This part ensure the directories you are trying to chown are not crucial directories
#echo $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS | zenity --text-info 
array=(/bin /boot /dev /etc /initrd /lib /lib32 /lib64 /lost+found /mnt /opt /proc /root /sbin /srv /sys /tmp /usr /var /initrd.img /tftpboot /vmlinuz)
len=${#array[*]}
i=0
while [ $i -lt $len ]; do
    filelist="$@"
    dir=${array[$i]}
    found=${filelist#*$dir *}
    if [ "$found" == "$filelist" ]; then 
        found=${filelist##$dir}
    fi
    if [ "$found" != "$filelist" ]; then 
        zenity --error --text="$MSG1 $dir $MSG2"
        exit 1
    fi
	let i++
done

gksudo -u root -k -m "$MSG3" /bin/echo "$MSG4"
quoted=$(echo -e "$@" | awk 'BEGIN {FS = "\n"} { printf "\"%s\" ", $1 }' | sed -e s#\"\"##)
eval sudo chown $USER:$GROUP -R -c $quoted
exit 0
