#!/bin/zsh -f

# shell script name  acl_adduser

# An acl nuclear option to give access to user $1 to the directory tree rooted at $2.

version="0.0.1"


###############################################################################
 
#  Created by wgscott  on 1 August 2009.
#  Copyright (c) . All rights reserved.


#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
#    USA
#    
#    cf. URL:   http://www.fsf.org/licensing/licenses/gpl.html
#
###############################################################################





scriptname="$0"

function desc_use {
    print ""
    print "\e[1m Give a single username followed by a single directory, eg: \e[0m $scriptname $USERNAME /Users/$USERNAME/Pictures/iPhoto\\\ Library \e[0m "
    print "\e[1m This example command gives the named user complete access to the iPhoto Library \e[0m "
    print ""
}

# Sanity checks:

if [[ $# != 2 ]]; then
    desc_use
    return 1
fi

if [[ ! -d "$2" ]]; then
    print ""
    print "\e[1m The directory \e[0m $2 \e[1m does not exist. Please create it or double-check the directory path. \e[0m "
    print ""
    return 10
fi




if [[ $(  command ls -1 /Users | grep $1 )  != $1 ]]; then
    print ""
    print "\e[1m Warning:  The username you have specified does not appear to exist. \e[0m "
    print ""
    print -n "\e[1m Are you sure you want to continue? \e[0m [y/N] "

    user_task_yes=( true   )
    user_task_no=( return 42  )

    timeout=30 # second until default answer takes effect

    function querry_user {
            answer=""
            read -t $timeout -A answer

            if [[ $answer[1] == (y|Y) ]]; then
                $user_task_yes
                answer=""
            else
                $user_task_no
                answer=""
               
            fi
    }    
     
    # Execute the command:
    querry_user
    querry_result="$?"
    if [[ $querry_result != 0 ]]; then
        return $querry_result
    fi
fi

TheACLcommandArray=( chmod -R +a "$1 allow list,add_file,search,delete,add_subdirectory,delete_child,chown,file_inherit,directory_inherit" "$2" )

if [[ -O "$2" ]]; then
    
    print " "
    print "\e[1m Issuing the command \e[0m \"$TheACLcommandArray\" " in 3 seconds ...
    print " "
    sleep 5
    set -x
    $TheACLcommandArray
    return $?
    
else  
    print " "
    print "\e[1m Issuing the command \e[0m \"$TheACLcommandArray\" " in 3 seconds ...
    print " "
    sleep 5
    set -x
    sudo $TheACLcommandArray
    return $?
    
fi







