#!/bin/bash
# ---------------------------------------------------------------------------
# /var/install/bin/samba-deactivate-user - deactivate an existing samba user
#
# Copyright (c) 2002-2013 Thomas Bork, tom(at)eisfair(dot)net
# Copyright (c) 2014 the eisfair team <team@eisfair.org>
#
# usage: /var/install/bin/samba-deactivate-user
#    or: /var/install/bin/samba-deactivate-user "user"
#
# Creation   : 2002-11-11 tb
#
# 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.
# ---------------------------------------------------------------------------
. /var/install/include/eislib

debuglevel=`grep 'debug level =' /etc/smb.conf | cut -d'=' -f2 | sed 's# ##g'`
smbpasswdbin='/usr/bin/smbpasswd'
pdbeditbin='/usr/bin/pdbedit'
tdbsamfile='/var/lib/samba/private/passdb.tdb'

case $# in
0)
    interactive=true
    user=''
    ;;
1)
    interactive=false
    user="$1"
    "$smbpasswdbin" -D "$debuglevel" -d "$user"
    ;;
*)
    echo "usage: /var/install/bin/samba-deactivate-user" >&2
    echo "   or: /var/install/bin/samba-deactivate-user \" user\"" >&2
    exit 1
    ;;
esac

if ${interactive} ; then
    clrhome
    mecho --info "Deactivate existing Samba User"
    echo

    if [ -z "$user" ] ; then
        echo "Existing Samba User to deactivate (e.g. 'www'):"
        echo
        read user
    fi

    if [ -z "$user" ] ; then
        mecho --error "No user deactivated!"
        exit 1
    fi

    if ! `${pdbeditbin} -Lw | grep -q "^$user:"` ; then
        mecho --error "Failed to find entry for samba user $user in $tdbsamfile!"
        exit 1
    fi

    "$smbpasswdbin" -D "$debuglevel" -d "$user"

    anykey
fi
