#!/bin/sh
# @vasm : vsamba
# @level: root
# @description: samba administration
# 
# (c) Eko M. Budi, 2003
# (c) VLocity Linux, 2003
#
# Released under GNU GPL

if [ ! "$VASM_FUNCTIONS" ]; then
  . /sbin/vasm-functions
fi

if [ "$DISPLAY" ]; then
  for WEBBROWSER in browser firefox mozilla konqueror dillo; do
     if which $WEBBROWSER &> /dev/null; then
        break
     fi  
  done
else
  WEBBROWSER=lynx
fi
if [ -z "$WEBBROWSER" ]; then
   errorbox "Cannot find web browser"
   clean_exit 1
fi

for NSCANNER in nmap knocker; do
    if which $NSCANNER &> /dev/null; then
        break
    fi  
done
if [ -z "$NSCANNER" ]; then
  errorbox "Cannot find a network scanner"
  clean_exit 254
fi

get_port() {
    case $NSCANNER in
	nmap)
	    PORT=`nmap 127.0.0.1 | grep -m 1 "open.*$1" | cut -f1 -d '/'` 
	    ;;
	knocker)
	    PORT=`knocker -nf -nc -H 127.0.0.1 | grep -m 1 "$1.*open" | 
	    cut -f1 -d '/' | sed 's/[^[0-9]]//g'`
	    ;;
    esac
    if [ ! $PORT = "901" ]; then
    PORT="901"
    fi 
}

launch_swat() {
    if [ ! -f /etc/inetd.conf ]; then
	return 1
    fi
    if cat /etc/inetd.conf | sed 's/^#*swat/swat/' > /etc/inetd.conf.new; then
	mv /etc/inetd.conf.new /etc/inetd.conf
    fi
    service inetd restart
}

stop_swat() {
    if [ ! -f /etc/inetd.conf ]; then
	return 1
    fi
    if cat /etc/inetd.conf | sed 's/^swat/#swat/' > /etc/inetd.conf.new; then
	mv /etc/inetd.conf.new /etc/inetd.conf
    fi
    service inetd restart
}


DIMENSION="12 60"
TITLE="SAMBA ADMINISTRATION"
TEXT="\n
SAMBA server allows file and printer sharing\n
between Windows as well as Linux computers.\n
This configurator will launch a browser to set up the\n
SAMBA through SWAT. It is recommended to run this\n
from GUI environment, so I can launch GUI browser like\n
Mozilla or Firefox. TUI browser may not good enough.\n
If asked, use your root account to login.\n"

$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --msgbox "$TEXT" $DIMENSION 2> $freply
[ ! $? = 0 ] && clean_exit

## Check if swat is already running
get_port swat
if [ -z "$PORT" ]; then
    yesnobox "Hmmm ... Samba SWAT is not running.\nLaunch it, shall we ?"
    [ $? != 0 ] && clean_exit 0
    clear
    if launch_swat; then
	echo -n "Waiting for SWAT ..."
	for II in 1 2 3 4 5; do
    	    sleep 1
	    get_port swat
    	    [ "$PORT" ] && break
    	    echo -n "$II "
    	    continue
	done
    fi
    if [ -z "$PORT" ]; then
       errorbox "Sorry, cannot launch SWAT.\nPlease check your SAMBA installation"
       clean_exit 0
    fi    
fi

clean_tmp
$WEBBROWSER http://127.0.0.1:$PORT
stop_swat &> /dev/null

