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

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

check_root

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 a web browser"
  clean_exit 254
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 = "631" ]; then
    PORT="631"
    fi 
}

DIMENSION="12 54"
TITLE="CUPS CONFIGURATOR"
TEXT="\n
CUPS is the printing service for Linux. 
This configurator will launch a browser to set up
the CUPS. You better do this from GUI environment,
so I can launch GUI browser like Mozilla or Firefox.
Text based browser may not be suitable.\n
If asked, use root or sysadmin 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 ipp
if [ -z "$PORT" ]; then
    yesnobox "Hmmm ... CUPS is not running.\nLaunch it, shall we ?"
    [ $? != 0 ] && clean_exit
    clear
    service cups start
    echo -n "Waiting for CUPS ..."
    for II in 1 2 3 4 5; do
       sleep 1
       get_port ipp
       if [ -z "$PORT" ]; then 
         echo -n "$II "
         continue
       fi
       break
    done
    if [ -z "$PORT" ]; then
       errorbox "Sorry, cannot launch CUPS.\nPlease check your CUPS installation."
       clean_exit
    fi    
fi

clean_tmp
echo "Launching $WEBBROWSER http://127.0.0.1:$PORT"
exec $WEBBROWSER "http://127.0.0.1:$PORT"

