#!/bin/bash

#root level functions requiring password for mx-samba-config

#disable/enable on both systemd and sysV

enablesamba()
{
if [ -x /usr/sbin/update-rc.d ]; then
    update-rc.d smbd remove
    update-rc.d nmbd remove
    update-rc.d smbd defaults
    update-rc.d nmbd defaults
fi
if [ -x /usr/bin/systemctl ]; then
    systemctl unmask smbd
    systemctl unmask nmbd
    systemctl enable smbd
    systemctl enable nmbd	
fi
}


disablesamba()
{
if [ -x /usr/sbin/update-rc.d ]; then
    update-rc.d smbd remove
    update-rc.d nmbd remove
fi
if [ -x /usr/bin/systemctl ]; then    
    systemctl disable smbd
    systemctl disable nmbd
    systemctl mask smbd
    systemctl mask nmbd
fi
}

addsambauser()
{
	
echo -ne "$pass\n$pass" | smbpasswd -as "$user"
exit $?

}

removesambauser()
{

pdbedit --delete "$user"
exit $?
	
}

changesambapasswd()
{
	
echo -ne "$pass\n$pass" | smbpasswd -U "$user"
exit $?
	
}


#service works for starting and stopping on both sysV and systemd
startsamba()
{

if [ -x /usr/bin/systemctl ]; then
    MASKED=$(systemctl is-enabled smbd)
    systemctl unmask smbd
    systemctl unmask nmbd
fi
service smbd start
service nmbd start	
if [[ -x /usr/bin/systemctl && $MASKED == "masked" ]]; then
    systemctl mask smbd
    systemctl mask nmbd
fi 

exit $?
	
}

stopsamba()
{
service smbd stop
service nmbd stop
exit $?

}


main()
{
case $1 in
    startsamba)  startsamba
                 ;;
    stopsamba)  stopsamba
                 ;;
    enablesamba)  enablesamba
                 ;;      
    disablesamba)  disablesamba
                 ;;
    addsambauser)  user="$3"
                   pass="$2"
                   addsambauser   
                   ;;  
    removesambauser)  user="$2"
                      removesambauser   
                   ;;    
    changesambapasswd)  user="$3"
                   pass="$2"
                   changesambapasswd   
                   ;;  
esac
}

main "$@"
