#!/bin/bash

test -f /etc/samba/smb.conf || exit 0

test -n "$os_version" || source /etc/init.d/base
test -n "$os_debug" || source /etc/init.d/conf

has_net_conn || exit 0

start() {
    msg_begin "Setting smb admin password"
    
    mkdir -p /var/log/samba
    mkdir -p /var/lib/samba/private

    password=$(/etc/init.d/adminpw)
    echo -e "$password\n$password\n" | /usr/bin/smbpasswd -a admin -s > /dev/null
    test $? == 0 && msg_done || msg_fail

    msg_begin "Starting smbd"
    smbd -D
    test $? == 0 && msg_done || msg_fail

    msg_begin "Starting nmbd"
    nmbd -D
    test $? == 0 && msg_done || msg_fail
}

stop() {
    msg_begin "Stopping smbd"
    killall smbd &>/dev/null
    test $? == 0 && msg_done || msg_fail

    msg_begin "Stopping nmbd"
    killall nmbd &>/dev/null
    test $? == 0 && msg_done || msg_fail
}

case "$1" in
    start)
        start
        ;;

    stop)
        stop
        ;;

    restart)
        stop
        start
        ;;

    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
esac

exit $?

