#!/bin/bash
#----------------------------------------------------------------------------
# /var/install/bin/samba-delayed-stop - shutdown samba delayed with message
#
# Copyright (c) 2002-2013 Thomas Bork, tom(at)eisfair(dot)net
#
# usage: /var/install/bin/samba-delayed-stop
#    or: /var/install/bin/samba-delayed-stop {"all"|"active"|"host1 host2"} "message" "time"
#
# Creation   : 2001-11-04 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.
#----------------------------------------------------------------------------
# set -x
. /var/install/include/eislib

info='/var/tmp/samba-delayed-message'
samba_netsend='/var/install/bin/samba-netsend'
samba_lookup_active='/var/install/bin/samba-lookup-active'
sambastatus='/var/install/bin/samba-check-status'

rm -f "$info"

do_interactive ()
{
    if [ "$interactive" = "true" ]
    then
        clrhome
        mecho --info "Stop Samba delayed"
        echo
    fi
}

case $# in
0)
    interactive='true'
    hosts=''
    message=''
    time=''
    broadcast=''
    ;;
3)
    interactive='false'
    hosts="$1"
    message="$2"
    time="$3"
    broadcast=''
    ;;
*)
    echo "usage: /var/install/bin/samba-delayed-stop" >&2
    echo "   or: /var/install/bin/samba-delayed-stop {\"all\"|\"active\"|\"host1 host2\"} \"message\" \"time\"" >&2
    exit 1
    ;;
esac

do_interactive

. $sambastatus
if [ "$smbdstatus" = "dead" ]
then
    mecho --error "Samba is not running!"
    exit 1
fi

if [ -z "$time" ]
then
    echo "After which time in minutes Samba should go down?"
    echo "If you only press ENTER, 3 minutes will be used:"
    echo
    read time

    if [ -z "$time" ]
    then
        time='3'
    fi
fi

do_interactive
if [ -z "$hosts" ]
then
    echo "To which SMB Hosts shutdown message should be send?"
    echo
    echo "Choice 1"
    echo "--------"
    echo "All SMB Hosts on configured Subnets on eisfair - type 'all'."
    echo
    echo "Choice 2"
    echo "--------"
    echo "eisfair Samba Clients with active connections - type 'active'."
    echo
    echo "Choice 3"
    echo "--------"
    echo "One ore more active SMB Hosts, type NETBIOS Names"
    echo "separated with a blank, for instance 'client1 client2':"
    echo
    echo "Choice 4"
    echo "--------"
    echo "If you only press ENTER, active hosts will be used."
    echo
    read hosts

    if [ -z "$hosts" ]
    then
        hosts='active'
    fi
fi

do_interactive
if [ -z "$message" ]
then
    if [ "$interactive" = "true" ]
    then
        echo "Which Message should be send? For instance:"
        echo
        echo "###############################################"
        echo "Attention!"
        echo
        echo "eisfair Samba Server is going down in $time minute(s)."
        echo "Please save your data and disconnect now!"
        echo "###############################################"
        echo
        echo "If you only press ENTER, above message will be used:"
        echo
        read message
    fi

    if [ -z "$message" ]
    then
       {
        echo "###############################################"
        echo "Attention!"
        echo
        echo "eisfair Samba Server is going down in $time minute(s)."
        echo "Please save your data and disconnect now!"
        echo "###############################################"
       } >"$info"
    else
        echo "$message" >"$info"
    fi
else
    echo "$message" >"$info"
fi

do_interactive
message=`cat $info`
if [ -n "$hosts" -a -n "$message" ]
then
    case "$hosts" in
    all)
        "$samba_netsend" all "$message"
        ;;
    active)
        "$samba_netsend" active "$message"
        ;;
    *)
        "$samba_netsend" "$hosts" "$message"
        ;;
    esac
fi

counter=0
seconds=`expr $time \* 60`
echo
echo "Shutting down in $seconds seconds"
echo -n "remaining: "
while [ "$seconds" -ge "$counter" ]
do
    echo -n "$seconds "
    len=`expr length $seconds`
    len=`expr $len + 1`
    seconds=`expr $seconds - 1`
    sleep 1
    lencounter=0
    while [ "$lencounter" != "$len" ]
    do
        echo -e "\b\c"
        lencounter=`expr $lencounter + 1`
    done
done

echo
if [ -n "`$samba_lookup_active`" ]
then
    if [ "$interactive" = "true" ]
    then
        echo
        mecho --warn "There are always clients active!"

        /usr/bin/smbstatus -b

        echo
        if /var/install/bin/ask "Shut down anyway" "yes"
        then
            /etc/init.d/smbfs stop
            /etc/init.d/samba stop
        else
            echo "Shutdown canceled."
        fi
    else
        /etc/init.d/smbfs stop
        /etc/init.d/samba stop
    fi
else
    /etc/init.d/smbfs stop
    /etc/init.d/samba stop
fi

rm -f "$info"
if [ "$interactive" = "true" ]
then
    anykey
fi
