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

BASE=dc=skole,dc=skolelinux,dc=no
LDAPHOST="ldap"
BASEDN=cn=smbadmin,ou=People,$BASE

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

# Standalone Main-Server do not install xfs
if [ "$PROFILE" = Main-Server ] ; then
    exit 0
fi

# Check if a deb package is installed.  Return true if it is, and
# false if it isn't.
deb_installed() {
    RET=$( dpkg -s $1 2>/dev/null | awk '/Status\:/ {print $4}' )
    if [ "$RET" = "installed" ] ; then
        true
    else
        false
    fi
}

# Only test if xfs 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

port=netbios-ns 
proto=udp

if netstat -a 2>&1 | grep ":$port " | grep -q "^$proto" ; then
    echo "success: $0: Netbios name service is listening on $port/$proto."
else
    echo "error: $0: Netbios name service is not listening on $port/$proto."
    exit 1
fi

port=netbios-ssn
proto=tcp

if netstat -a 2>&1 | grep ":$port " | grep -q "^$proto" ; then
    echo "success: $0: Netbios session service is listening on $port/$proto."
else
    echo "error: $0: Netbios session service is not listening on $port/$proto."
    exit 1
fi

if /usr/bin/ldapsearch -LLL -h $LDAPHOST -x -b "$BASEDN" 1>/dev/null 2>&1 ; then
    echo "success: $0: smbadmin account exists in ldap"
else
    echo "error: $0: smbadmin account doesnt exists in ldap"
    exit 1
fi

exit 0
