#!/bin/sh -e
#
# Test if the smbadmin account is created

. /usr/share/debian-edu-config/testsuite-lib.sh

ADMINUSER=smbadmin
DOMAIN=SKOLELINUX

if test -r /etc/debian-edu/config ; then
    . /etc/debian-edu/config
fi

# Only Main-Server install samba
if echo "$PROFILE" | egrep -q 'Main-Server' ; then
    :
else
    exit 0
fi

if /usr/bin/ldapsearch -LLL -x \
    "(&(cn=$ADMINUSER)(objectClass=simpleSecurityObject))" \
    >/dev/null 2>&1 ; then
    echo "success: $0: smbadmin simpleSecurityObject exists in ldap"
else
    echo "error: $0: smbadmin simpleSecurityObject does not exists in ldap"
fi

if /usr/bin/ldapsearch -LLL -x \
    "(&(objectclass=sambadomain)(sambadomainname=$DOMAIN))" \
    > /dev/null 2>&1 ; then
    echo "success: $0: sambadomain object for domain $DOMAIN exists in ldap"
else
    echo "error: $0: sambadomain object for domain $DOMAIN does not exists in ldap"
fi

# Only test if samba is installed
if  deb_installed samba ; then
    :
else
    echo "info: $0: samba is not installed"
    exit 0
fi

if pidof smbd > /dev/null ; then
    echo "success: $0: samba is running."
else
    echo "error: $0: samba is not running."
    exit 1
fi

netstat_check netbios-ns udp "Netbios name"
netstat_check netbios-ssn tcp "Netbios session"

if which net >/dev/null 2>&1 ; then
    echo "success: $0: found net binary."
    if net groupmap list | grep -q '^Domain Admins ' ; then
	echo "success: $0: found Domain Admins in samba groupmap."
    else
	echo "error: $0: missing Domain Admins in samba groupmap."
    fi
    if net maxrid 2>&1 | grep -q '^Currently ' ; then
	echo "success: $0: maxrid is reported."
    else
	echo "error: $0: samba fails to report maxrid."
    fi
    exec 2>/dev/null
    if net time 2>&1 | grep -q $(date +%Y) ; then
	echo "success: $0: command 'net time' doesn't segfault."
    else
	echo "error: $0: command 'net time' segfaults (bug #760781)."
    fi
else
    echo "error: $0: missing net binary."
fi

exit 0
