#!/usr/bin/sh

# Simplified usage of chmod and chown
# Created: 2016.05.10.08:49
# Author: Kārlis Kalviškis <eko@lanet.lv>
# License: GPLv3


# For translation
export TEXTDOMAIN="karlo-scripts"
. gettext.sh

# Should be the root
if [[ $UID -ne 0 ]]; then
    echo
    echo `eval_gettext 'root privileges required'`
    echo `eval_gettext 'Use'` su
    echo     `eval_gettext 'or'`
    echo sudo $0 `eval_gettext '[file_or_directory] [user[:group]]'` [0]
    echo
    exit 126
fi




# Is there a file or drectory?
if [ ! $1 ] ; then
	echo 
	echo `eval_gettext 'Changes ownership and permissions'`
	echo 
	echo `eval_gettext 'Missing parameters. Usage:'`
	echo $0 `eval_gettext '[file_or_directory] [user[:group]]'` [0]
	echo
	echo `eval_gettext 'The default user is'` root:root.
	echo `eval_gettext 'Change permissions to'` rw-r--r-- 
	echo  , `eval_gettext 'or'` rw-r--- , `eval_gettext 'if the third parameter is'` 40
	echo  , `eval_gettext 'or'` rw-rw-r-- , `eval_gettext 'if the third parameter is'` 64
	echo 
	exit 2
fi

# Is there a user?
if [ $# -gt 1 ] ; then
	lietotajs=$2
else
	lietotajs='root:root'
fi


# Is there the third parameter?
if [ $# -gt 2 ]
then
    pilnvaras="6$3"
else
    pilnvaras='644'
fi
 
chown -R $lietotajs $1 && chmod -R $pilnvaras $1 && chmod -R +X $1 &&  echo `eval_gettext 'Permissions are changed'`
