##@cooked comments # -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
## Outils pour MacOS X
##@cooked nocomments
##@require base
##@require sysinfos
##@require compat
uprovide macosx
urequire base sysinfos compat

################################################################################
# Gestion des utilisateurs

function local_shellfix() {
    # Modifier le compte local $1 pour qu'il utilise bash au lieu de sh
    local username="${1:-root}"
    local shell="$(dscl localhost read "/Local/Default/Users/$username" UserShell | sed 's/^UserShell: //g')"
    if [ "$shell" != "/bin/bash" ]; then
        dscl localhost change "/Local/Default/Users/$username" UserShell "$shell" /bin/bash
    fi
}

function local_usercheck() {
    # Vérifier si le user local $1 existe
    if check_sysinfos -s macosx -d 10.5+; then
        [ -n "$(dscl localhost list /Local/Default/Users | grep "$username")" ]
    elif check_sysinfos -s macosx -d 10.4; then
        [ -n "$(nicl . -list /users | grep "$username")" ]
    else
        return 123
    fi
}

function local_useradd() {
    # Créer le user local $1
    # USAGE: local_useradd username [gecos [passwd]]
    # OPTIONS
    #     -s  Créer l'utilisateur avec les droits d'administrateur
    #     -m  Créer le home directory
    local -a args
    local admin create_home
    parse_opts "${PRETTYOPTS[@]}" \
        -s,--admin admin=1 \
        -m,--create-home create_home=1 \
        @ args -- "$@" && set -- "${args[@]}" || {
        eerror "$args"
        return 1
    }
    local uid gid
    local username="$1" gecos="$2" passwd="$3"
    [ -n "$passwd" ] || passwd="${username:0:3}+def"
    local_usercheck "$username" && {
        eerror "$username: ce compte existe déjà"
        return 1
    }

    if check_sysinfos -s macosx -d 10.5+; then
        [ -n "$uid" ] || uid=$(($(dscl localhost list /Local/Default/Users UniqueID | awk '{print $2}' | sort -rn | head -n 1) + 1))
        [ -n "$gid" ] || gid=20
        echo "\
cd /Local/Default/Users
create $username UniqueID $uid
create $username PrimaryGroupID $gid
create $username RealName \"$gecos\"
create $username UserShell /bin/bash
create $username NFSHomeDirectory /Users/$username
create $username _writers_passwd $username
passwd $username $passwd
${admin:+
cd /Local/Default/Groups
merge admin GroupMembership $username
}" | dscl -q localhost

    elif check_sysinfos -s macosx -d 10.4; then
        local create_group
        [ -n "$uid" ] || uid="$(($(nicl . -list /users uid | awk '{print $2}' | sort -rn | head -n 1) + 1))"

        if [ -z "$gid" ]; then
            gid="$(($(nicl . -list /groups gid | awk '{print $2}' | sort -rn | head -n 1) + 1))"
            [ "$gid" -lt 501 ] && gid=501
        fi
        [ -z "$(nicl . -list /groups gid | awk '{print $2}' | grep "$gid")" ] && create_group=1 || create_group=

        echo "\
${create_group:+cd /groups
create $username gid $gid
create $username passwd *
}cd /users
create $username uid $uid
create $username gid $gid
${gecos:+create $username realname \"$gecos\"
}create $username shell /bin/bash
create $username home /Users/$username
create $username _writers_passwd $username
${admin:+
cd /groups
merge admin users $username
}" | nicl -q .
        echo 'spawn passwd '"$username"'
expect "password:"
send "'"$passwd"'\r"
expect "password:"
send "'"$passwd"'\r"
expect eof
' | expect

    else
        return 123
    fi

    if [ -n "$create_home" ]; then
        local homedir="/Users/$username"
        ditto "/System/Library/User Template/French.lproj" "$homedir"
        chown -R $uid:$gid "$homedir"
        chmod 755 "$homedir"
    fi

    return 0
}
