#!/bin/sh
#
# Gtkdialog box to manage network connections and servers.
# Please use tab to indent.
#
# (c) - SliTaz GNU/Linux 2009.
#
VERSION=20100315

. /usr/lib/slitaz/gui_functions

# Check if user is root.
check_root()
{
	if test $(id -u) != 0 ; then
		echo -e "
You must be root to run `basename $0`. Please type 'su' and
root password to become super-user.\n"
		exit 0
	fi
}


#
# Functions called by the connection tab to list interfaces.
#
############################################################

interface_status()
{
	if 	ifconfig | grep -A 1 $i | grep -q inet; then
		ip=`ifconfig | grep -A 1 $i | grep inet | awk '{ print $2 }' | cut -d ":" -f 2`
		echo "connected ($ip)"
	else
		echo "-"
	fi
}
# First column is for icon name.
detect_interfaces()
{
	for i in `ls /sys/class/net`
	do
		case $i in
			eth*)
				echo "network-wired | $i | Ethernet | `interface_status`" ;;
			wlan*|ath*|ra*)
				echo "network-wireless | $i | Wireless | `interface_status`" ;;
			lo)
				echo "gtk-network | $i | Loopback | `interface_status`" ;;
			*)
				continue ;;
		esac
		
	done
}

# When users double click on a connection.
interfaces_List_actions()
{
	echo "$INTERFACE_LIST"
}

# Netbox can be called with args.
case "$1" in
	detect_interfaces|interfaces_List_actions)
		$1
		exit 0 ;;
	*)
		continue ;;
esac

set_ipup()
{
	[ -f /etc/ppp/ip-up ] && return
	cat > /etc/ppp/ip-up <<EOT
#!/bin/sh
exec $0 call ipup \$@
EOT
	chmod +x /etc/ppp/ip-up
}

while true; do

# Detect WIFI_INTERFACE and update /etc/network.conf
. /etc/network.conf
if [ ! -d /sys/class/net/$WIFI_INTERFACE/wireless ]; then
	WIFI_INTERFACE=$(for i in /sys/class/net/*/wireless; do \
		[ -d $i ] && echo $(basename $(dirname $i)) || echo wlan0; \
		break; done)
	[ -n "$WIFI_INTERFACE" ] && sed -i "s/^WIFI_INTERFACE=.*/WIFI_INTERFACE=\"$WIFI_INTERFACE\"/" /etc/network.conf
fi

#
# Netbox internal calls
#
#########################################################

if [ "$1" = "call" ]; then
	ppp="pppd local lock notty"
	pppup=""
	sub=$2
	shift 2
	case "$sub" in
	sendsshkey)
		check_root
		( dropbearkey -y -f /etc/dropbear/dropbear_rsa_host_key ;
		  cat /etc/ssh/ssh_host_rsa_key.pub ) 2> /dev/null | \
		grep ^ssh | ssh $1 "mkdir .ssh 2> /dev/null ; \
while read key; do for i in authorized_keys authorized_keys2; do \
grep -qs '\$key' .ssh/\$i || echo '\$key' >> .ssh/\$i ; done ; done ; \
chmod 700 .ssh ; chmod 600 .ssh/authorized_keys*"
		exit 0;;
# OpenSSH VPN:
#   PermitTunnel=point-to-point   (or yes, ethernet, no)
#   Tunnel="4" => tun4
#   Local Client:
#     # ssh -f -w 0:1 $REMOTE true  => local tun0 [, remote tun1]
#     # ifconfig $TUN $IP1 $IP2 netmask 255.255.255.252
#     # route add $REMOTE_NETWORK $IP2
#   Remote Server:
#     # ifconfig $TUN $IP2 $IP1 netmask 255.255.255.252
#     # route add $LOCAL_NETWORK $IP1
	vpnssh)	check_root
		set_ipup
		ps ww | grep -q "$ppp $2:$3" && exit 1
		pipe="/tmp/ssh$$"
		mkfifo $pipe
		[ -n "$4" ] && pppup="ipparam 'addroutes,$(echo $4 | sed 's/ /,/g')'"
		cat $pipe | dbclient -i /etc/dropbear/dropbear_rsa_host_key \
		$1 "$ppp" | $ppp $2:$3 $pppup > $pipe
		rm -f $pipe
		exit 0;;
	killvpnssh)
		check_root
		kill $(ps x | grep dbclient | grep "$ppp" | awk '{ print $1 }')
		exit 0;;
	ipup)
#    Arg  Name                          Example
#    $1   Interface name                ppp0
#    $2   The tty                       ttyS1
#    $3   The link speed                38400
#    $4   Local IP number               12.34.56.78
#    $5   Peer  IP number               12.34.56.99
#    $6   Optional ``ipparam'' value    foo
		iface=$1
		# skip tty if present
		case "$2" in [0-9]*);; *) shift; esac
		peer=$4
		IFS=","; set -- $(eval echo $5); unset IFS
		set -- $1
		if [ "$1" = "addroutes" ]; then
			while [ -n "$2" ]; do
				eval $(ipcalc -n $2)
				eval $(ipcalc -m $2)
				route add -net $NETWORK netmask $NETMASK \
					gw $peer $iface
				shift
			done
		fi
		exit 0;;
	esac
	echo "call $sub unsupported."
	exit 1
fi



#
# Status wire interfaces
#
tab_status_iface()
{
local eth
eth="$( (cd /sys/class/net ; [ -d eth0 ] && ls -d eth* ) )"
which ethtool > /dev/null || eth=''
if [ -n "$eth" ]; then
    cat <<EOT
<notebook labels="Ifconfig|$(echo $eth | sed 's/ /|/g')">
EOT
fi
cat <<EOT
<frame Ifconfig>
	<text wrap="false" width-chars="58">
		<input>ifconfig</input>
	</text>
</frame>
EOT
if [ -n "$eth" ]; then
    local i
    for i in $eth ; do
        cat <<EOT
<frame $i>
	<text wrap="false" width-chars="58">
		<input>ethtool $i</input>
	</text>
</frame>
EOT
    done
    cat <<EOT
</notebook>
EOT
fi
}

#
# Status wifi interfaces
#
tab_status_wifi_iface()
{
cat <<EOT
<frame Iwconfig>
	<text wrap="false" width-chars="58">
		<input>iwconfig</input>
	</text>
</frame>
EOT
}

#
# Status wifi network
#
tab_status_wifi_net()
{
cat <<EOT
<frame Wireless networks>
	<text wrap="false" width-chars="58">
		<input>iwlist scan</input>
	</text>
</frame>
EOT
}

#
# Status routing
#
tab_status_route()
{
cat <<EOT
<frame Routing>
	<frame Nameservers>
		<text wrap="false" width-chars="58">
			<input>cat /etc/resolv.conf</input>
		</text>
	</frame>
	<frame Routing table>
		<text wrap="false" width-chars="58">
			<input>route</input>
		</text>
	</frame>
	<frame Arp table>
		<text wrap="false" width-chars="58">
			<input>arp</input>
		</text>
	</frame>
</frame>
EOT
}

data_status()
{
cat <<EOT
ifconfig    Network\ interfaces   tab_status_iface
iwconfig    Wireless\ interfaces  tab_status_wifi_iface
iwlist      Wireless\ networks    tab_status_wifi_net
route       Routing               tab_status_route
EOT
}

#
# Status
#
tab_status()
{
use_tab data_status
}

#
# Interfaces list from detect_interfaces()
#
tab_connections()
{
cat <<EOT
	<vbox>
		<tree>
			<width>520</width><height>120</height>
			<variable>INTERFACE_LIST</variable>
			<label>Interface|Type|Status</label>
			<input icon_column="0">$0 detect_interfaces</input>
			<action>refresh:INTERFACE_LIST</action>
		</tree>
	</vbox>
EOT
}

#
# DHCP
#
tab_udhcpc()
{
cat <<EOT
<frame Udhcpc (busybox)>
	<text width-chars="58">
		<label> "Ethernet (cable) default connection." </label>
	</text>
	<hbox>
		<text use-markup="true">
			<label>"<b>Interface:</b>"</label>
		</text>
		<entry>
			<input>. /etc/network.conf; echo "\$INTERFACE"</input>
			<variable>INTERFACE</variable>
		</entry>
	</hbox>
	<hbox>
		<text use-markup="true">
			<label>"<b>Options:  </b>"</label>
		</text>
		<entry>
			<default>-b</default>
			<variable>UDHCPC_OPTS</variable>
		</entry>
$(helpbutton udhcpc 80x30)
	</hbox>
	<hbox>
		<text use-markup="true">
			<label>"<b>Script:     </b>"</label>
		</text>
		<entry editable="false">
			<default>/usr/share/udhcpc/default.script</default>
			<variable>UDHCPC_SCRIPT</variable>
		</entry>
$(editbutton \$UDHCPC_SCRIPT)
	</hbox>
	<hbox>
		<button>
			<label>Start</label>
			<input file icon="forward"></input>
			<action>sed -i s/`cat /etc/network.conf | grep ^INTERFACE=`/INTERFACE="\$INTERFACE"/ /etc/network.conf</action>
			<action>sed -i s/DHCP="no"/DHCP="yes"/ /etc/network.conf</action>
			<action>sed -i s/STATIC="yes"/STATIC="no"/ /etc/network.conf</action>
			<action>udhcpc \$UDHCPC_OPTS -i \$INTERFACE -p /var/run/udhcpc.\$INTERFACE.pid</action>
			<action>refresh:INTERFACE_LIST</action>
		</button>
		<button>
			<label>Stop</label>
			<input file icon="stop"></input>
			<action>echo -n "Stopping interface : \$INTERFACE... "</action>
			<action>ifconfig \$INTERFACE down</action>
			<action>killall -q udhcpc; echo "done"</action>
			<action>refresh:INTERFACE_LIST</action>
		</button>
	</hbox>
</frame>
EOT
}

#
# Static/fixed IP settings.
#
tab_fixed_ip()
{
cat <<EOT
<frame Ethernet fixed IP>
	<hbox>
		<text use-markup="true">
			<label>"<b>Interface:   </b>"</label>
		</text>
		<entry>
			<input>. /etc/network.conf; echo "\$INTERFACE"</input>
			<variable>INTERFACE</variable>
		</entry>
	</hbox>
	<hbox>
		<text use-markup="true">
			<label>"<b>IP:                </b>"</label>
		</text>
		<entry>
			<input>. /etc/network.conf; echo "\$IP"</input>
			<variable>IP</variable>
		</entry>
	</hbox>
	<hbox>
		<text use-markup="true">
			<label>"<b>Netmask:    </b>"</label>
		</text>
		<entry>
			<input>. /etc/network.conf; echo "\$NETMASK"</input>
			<variable>NETMASK</variable>
		</entry>
	</hbox>
	<hbox>
		<text use-markup="true">
			<label>"<b>Gateway:    </b>"</label>
		</text>
		<entry>
			<input>. /etc/network.conf; echo "\$GATEWAY"</input>
			<variable>GATEWAY</variable>
		</entry>
	</hbox>
	<hbox>
		<text use-markup="true">
			<label>"<b>DNS server: </b>"</label>
		</text>
		<entry>
			<input>. /etc/network.conf; echo "\$DNS_SERVER"</input>
			<variable>DNS_SERVER</variable>
		</entry>
	</hbox>
	<hbox>
		<button>
		<label>Start</label>
			<input file icon="forward"></input>
			<action>ifconfig lo down</action>
			<action>ifconfig \$INTERFACE down</action>
			<action>sed -i s/`cat /etc/network.conf | grep ^INTERFACE=`/INTERFACE="\$INTERFACE"/ /etc/network.conf</action>
			<action>sed -i s/DHCP="yes"/DHCP="no"/ /etc/network.conf</action>
			<action>sed -i s/WIFI="yes"/WIFI="no"/ /etc/network.conf</action>
			<action>sed -i s/STATIC="no"/STATIC="yes"/ /etc/network.conf</action>
			<action>sed -i s/`cat /etc/network.conf | grep ^IP=`/IP="\$IP"/ /etc/network.conf</action>
			<action>sed -i s/`cat /etc/network.conf | grep ^NETMASK=`/NETMASK="\$NETMASK"/ /etc/network.conf</action>
			<action>sed -i s/`cat /etc/network.conf | grep ^GATEWAY=`/GATEWAY="\$GATEWAY"/ /etc/network.conf</action>
			<action>sed -i s/`cat /etc/network.conf | grep ^DNS_SERVER=`/DNS_SERVER="\$DNS_SERVER"/ /etc/network.conf</action>
			<action>/etc/init.d/network.sh</action>
			<action>refresh:INTERFACE_LIST</action>
		</button>
		<button>
			<label>Stop</label>
			<input file icon="stop"></input>
			<action>ifconfig \$INTERFACE down</action>
			<action>refresh:INTERFACE_LIST</action>
		</button>
	</hbox>
</frame>
EOT
}

#
# PPPoe settings.
#
tab_pppoe()
{
cat <<EOT
<frame PPPoE>
	<hbox>
		<text use-markup="true">
			<label>"<b>Name:       </b>"</label>
		</text>
		<entry>
			<input>NAME=\$(grep -s ^name /etc/ppp/options); echo "\${NAME#* }"</input>
			<variable>NAME</variable>
		</entry>
	</hbox>
	<hbox>
		<text use-markup="true">
			<label>"<b>Username:</b>"</label>
		</text>
		<entry>
			<variable>USER</variable>
		</entry>
	</hbox>
	<hbox>
		<text use-markup="true">
			<label>"<b>Password:</b>"</label>
		</text>
		<entry>
			<variable>PASS</variable>
		</entry>
	</hbox>
	<hbox>
$(helpbutton pppd 80x30)
$(manbutton 8 pppd)
$(webbutton ppp)
		<button>
			<input file icon="accessories-text-editor"></input>
			<label>Tune</label>
			<action>[ -n "\$NAME" ] && sed -i "s/^name .*/name \$NAME/" /etc/ppp/options</action>
			<action type="lauch">leafpad /etc/ppp/options</action>
		</button>
		<button>
			<label>Start</label>
			<input file icon="forward"></input>
			<action>[ -n "\$USER" ] && grep -qs "^\"\$USER\"" /etc/ppp/pap-secrets
			&& echo "\"\$USER\"	*	\"\$PASS\"" >> /etc/ppp/pap-secrets</action>
			<action>[ -n "\$USER" ] && grep -qs "^\"\$USER\"" /etc/ppp/chap-secrets
			&& echo "\"\$USER\"	*	\"\$PASS\"" >> /etc/ppp/chap-secrets</action>
			<action>[ -n "\$NAME" ] && sed -i "s/^name .*/name \$NAME/" /etc/ppp/options</action>
			<action>killall udhcpc</action>
			<action>sed -i "s/DHCP=\"yes\"/DHCP=\"no\"/" /etc/network.conf</action>
			<action>sed -i "s/PPPOE=\"no\"/PPPOE=\"yes\"/" /etc/network.conf</action>
			<action>pppd \$INTERFACE &</action>
		</button>
		<button>
			<label>Stop</label>
			<input file icon="stop"></input>
			<action>sed -i "s/PPPOE=\"yes\"/PPPOE=\"no\"/" /etc/network.conf</action>
			<action>killall pppd</action>
		</button>
	</hbox>
</frame>
EOT
}

#
# PPP settings.
#
tab_ppp()
{
cat <<EOT
<frame PPP>
	<hbox>
		<text use-markup="true">
			<label>"<b>Username: </b>"</label>
		</text>
		<entry>
			<input>USER=\$(grep -s ^ACCOUNT= /etc/ppp/scripts/ppp-on | cut -f1); echo "\${USER#*=}"</input>
			<variable>USER</variable>
		</entry>
	</hbox>
	<hbox>
		<text use-markup="true">
			<label>"<b>Password: </b>"</label>
		</text>
		<entry>
			<input>PASS=\$(grep -s ^PASSWORD= /etc/ppp/scripts/ppp-on | cut -f1); echo "\${PASS#*=}"</input>
			<variable>PASS</variable>
		</entry>
	</hbox>
	<hbox>
		<text use-markup="true">
			<label>"<b>Telephone:</b>"</label>
		</text>
		<entry>
			<input>PHONE=\$(grep -s ^TELEPHONE= /etc/ppp/scripts/ppp-on | cut -f1); echo "\${PHONE#*=}"</input>
			<variable>PHONE</variable>
		</entry>
	</hbox>
	<hbox>
$(helpbutton pppd 80x30)
$(manbutton 8 pppd)
$(webbutton ppp)
		<button>
			<input file icon="accessories-text-editor"></input>
			<label>Tune</label>
			<action>[ -n "\$NAME" ] && sed -i "s/^ACCOUNT=.*/ACCOUNT=\$NAME/" /etc/ppp/scripts/ppp-on</action>
			<action>[ -n "\$PASS" ] && sed -i "s/^PASSWORD=.*/PASSWORD=\$PASS/" /etc/ppp/scripts/ppp-on</action>
			<action>[ -n "\$PHONE" ] && sed -i "s/^TELEPHONE=.*/TELEPHONE=\$PHONE/" /etc/ppp/scripts/ppp-on</action>
			<action type="lauch">leafpad /etc/ppp/scripts/ppp-on</action>
		</button>
		<button>
			<label>Start</label>
			<input file icon="forward"></input>
			<action>[ -n "\$USER" ] && grep -qs "^\"\$USER\"" /etc/ppp/pap-secrets
			&& echo "\"\$USER\"	*	\"\$PASS\"" >> /etc/ppp/pap-secrets</action>
			<action>[ -n "\$USER" ] && grep -qs "^\"\$USER\"" /etc/ppp/chap-secrets
			&& echo "\"\$USER\"	*	\"\$PASS\"" >> /etc/ppp/chap-secrets</action>
			<action>[ -n "\$NAME" ] && sed -i "s/^name .*/name \$NAME/" /etc/ppp/options</action>
			<action>/etc/ppp/scripts/ppp-off</action>
			<action>/etc/ppp/scripts/ppp-on &</action>
		</button>
		<button>
			<label>Stop</label>
			<input file icon="stop"></input>
			<action>/etc/ppp/scripts/ppp-off</action>
		</button>
	</hbox>
</frame>
EOT
}

#
# System Wide configuration.
#
tab_system()
{
cat <<EOT
<frame Configuration files>
      <hbox>
        <text use-markup="true">
          <label>"<b>Hosts            :</b>"</label>
        </text>
        <entry editable="false">
          <default>/etc/hosts</default>
          <variable>HOSTS</variable>
        </entry>
$(editbutton \$HOSTS)
      </hbox>
      <hbox>
        <text use-markup="true">
          <label>"<b>Host name    :</b>"</label>
        </text>
        <entry editable="false">
          <default>/etc/hostname</default>
          <variable>HOSTNAME</variable>
        </entry>
$(editbutton \$HOSTNAME)
      </hbox>
      <hbox>
        <text use-markup="true">
          <label>"<b>Network       :</b>"</label>
        </text>
        <entry editable="false">
          <default>/etc/network.conf</default>
          <variable>CONFIG_FILE</variable>
        </entry>
$(editbutton \$CONFIG_FILE)
      </hbox>
      <hbox>
        <button>
          <label>Restart</label>
          <input file icon="reload"></input>
          <action>echo -n "Stopping interface : \$INTERFACE... "</action>
          <action>ifconfig \$INTERFACE down</action>
          <action>killall -q udhcpc; echo "done"</action>
          <action>/etc/init.d/network.sh restart</action>
        </button>
      </hbox>
</frame>
EOT
}

#
# ssh/ppp
#
tab_sshppp()
{
cat <<EOT
    <frame Virtual Private Network with PPP/SSH>
      <hbox>
        <text use-markup="true">
          <label>"<b>Peer          :</b>"</label>
        </text>
        <entry>
          <variable>DROPBEAR_PEERVPN</variable>
	  <default>user@elsewhere</default>
        </entry>
      </hbox>
      <hbox>
        <text use-markup="true">
          <label>"<b>Local IP     :</b>"</label>
        </text>
        <entry>
          <variable>DROPBEAR_LOCAL</variable>
	  <default>192.168.254.1</default>
        </entry>
      </hbox>
      <hbox>
        <text use-markup="true">
          <label>"<b>Remote IP :</b>"</label>
        </text>
        <entry>
          <variable>DROPBEAR_REMOTE</variable>
	  <default>192.168.254.2</default>
        </entry>
      </hbox>
      <hbox>
        <text use-markup="true">
          <label>"<b>Route(s)    :</b>"</label>
        </text>
        <entry>
          <variable>DROPBEAR_ROUTE</variable>
	  <default>192.168.10.0/24 192.168.20.0/28</default>
        </entry>
      </hbox>
      <hbox>
$(wikibutton http://wiki.slitaz.org/doku.php?id=quickstart:vpn)
        <button>
          <input file icon="forward"></input>
	  <label>Connect</label>
	  <action>$0 call vpnssh \$DROPBEAR_PEERVPN \$DROPBEAR_LOCAL \$DROPBEAR_REMOTE "\$DROPBEAR_ROUTE" &</action>
        </button>
        <button>
          <input file icon="stop"></input>
	  <label>Disconnect</label>
	  <action>$0 call killvpnssh</action>
        </button>
        <button>
          <input file icon="go-up"></input>
	  <label>Send key</label>
	  <action>$xterm -geometry 80x10 -title "Send key" -e "$0 call sendsshkey \$DROPBEAR_PEERVPN; echo -e \"----\nENTER to continue...\" && read close"</action>
        </button>
      </hbox>
    </frame>
EOT
}

#
# PPTP client
#
#FIXME
#cat > /etc/ppp/peers/$TUNNEL <<EOT
#pty "pptp $SERVER --nolaunchpppd"
#lock
#noauth
#nobsdcomp
#nodeflate
#name $DOMAIN\\$USERNAME
#remotename $TUNNEL
#ipparam $TUNNEL
#$(encryption && echo "require-mppe-128")
#EOT
#pppd call $TUNNEL updetach

tab_pptp()
{
[ -f /etc/ppp/options.pptp ] || cat >> /etc/ppp/options.pptp <<EOT
lock noauth nobsdcomp nodeflate remotename PPTP
EOT
[ -f /etc/ppp/pptp-servers ] || cat >> /etc/ppp/pptp-servers <<EOT
# PPTP servers list
EOT
cat <<EOT
    <frame Virtual Private Network with PPTP>
      <hbox>
        <text use-markup="true">
          <label>"<b>Server:</b>"</label>
        </text>
        <combobox>
          <variable>PPTP_SERVER</variable>
EOT
grep -v ^\# /etc/ppp/pptp-servers | \
awk '{ printf "          <item>%s</item>\n",$1 }'
cat <<EOT
          <item>SERVER</item>
        </combobox>
$(editbutton /etc/ppp/pptp-servers)
      </hbox>
      <hbox>
        <text use-markup="true">
          <label>"<b>   User:</b>"</label>
        </text>
        <combobox>
          <variable>PPTP_USER</variable>
EOT
grep '\\' /etc/ppp/chap-secrets | grep -v ^\# | \
awk '{ printf "          <item>%s</item>\n",$1 }'
cat <<EOT
          <item>DOMAIN\USERNAME</item>
        </combobox>
$(editbutton /etc/ppp/chap-secrets)
      </hbox>
      <hbox>
        <text use-markup="true">
          <label>"<b>Password:</b>"</label>
        </text>
        <entry>
          <variable>PPTP_PASS</variable>
        </entry>
      </hbox>
      <hbox>
      	<checkbox>
	  <label>Encryption</label>
	  <variable>PPTP_ENC</variable>
	  <default>true</default>
      	</checkbox>
$(helpbutton pptp 80x35)
	<button>
	  <input file icon="accessories-text-editor"></input>
	  <label>Options</label>
	  <action type="lauch">leafpad /etc/ppp/options.pptp</action>
	</button>
$(webbutton pptpclient)
        <button>
          <input file icon="forward"></input>
	  <label>Connect</label>
	  <action>grep -qs ^\$PPTP_USER /etc/ppp/chap-secrets || \
echo "\$PPTP_USER PPTP \"$PPTP_PASS\" *" >> /etc/ppp/chap-secrets</action>
	  <action>grep -qs ^\$PPTP_SERVER /etc/ppp/pptp-servers || \
echo "\$PPTP_SERVER" >> /etc/ppp/pptp-servers</action>
	  <action>PASS=""; [ -n "\$PPTP_PASS" ] && PASS="password \$PASS"; \
ENC=""; [ x\$PPTP_ENC == xtrue ] && ENC="require-mppe-128"; \
/usr/sbin/pptp \$PPTP_SERVER file /etc/ppp/options.pptp \$ENC user \$PPTP_USER \$PASS &
	  </action>
        </button>
        <button>
          <input file icon="stop"></input>
	  <label>Disconnect</label>
	  <action>killall pptp</action>
        </button>
      </hbox>
    </frame>
EOT
}

#
# Cisco EasyVPN
#
tab_easyvpn()
{
cat <<EOT
    <frame Cisco EasyVPN>
<hbox>
	<text use-markup="true">
		<label>"<b>VPNC_OPTIONS</b>"</label>
	</text>
	<entry editable="false">
EOT
[ -n "$VPNC_OPTIONS" ] && cat << EOT
		<default>$tmp</default>
EOT
	cat << EOT
		<variable>VPNC_OPTS</variable>
	</entry>
$(helpbutton vpnc 80x30)
<button>
	<input file icon="help"></input>
	<label>more</label>
	<action>$xterm -geometry 80x40 -title "vpnc help (q to quit)" -e "$(which vpnc) --long-help 2>&1 | less"</action>
</button>
$(editbutton /etc/daemons.conf)
</hbox>
$(configfile /etc/vpnc/default.conf VPNC_CONF)
$(datafile "/etc/vpnc/vpnc-script" VPNC_SCRIPT 'Script:')
      <hbox>
	$(startstopdaemon vpnc)
      </hbox>
     </frame>
EOT
}

#
# OpenVPN
#
tab_openvpn()
{
local i
local n
cat <<EOT
<frame OpenVPN>
EOT
n=1
for i in /etc/openvpn/*.conf ; do
	[ -f $i ] || continue
	configfile $i OPENVPN_CONF$n
	n=$(($n + 1))
done
cat <<EOT
      <hbox>
	$(helpbutton openvpn 80x40)
	$(startstopdaemon openvpn)
      </hbox>
</frame>
EOT
}

data_vpn()
{
cat <<EOT
$([ -x /usr/sbin/dropbear -o -x /usr/sbin/sshd ] && echo "pppd" ||
echo "#")   PPP/SSH       tab_sshppp
pptp        PPTP          tab_pptp
vpnc        EasyVPN       tab_easyvpn
openvpn     OpenVPN       tab_openvpn
EOT
}

#
# VPN
#
tab_vpn()
{
use_tab data_vpn
}

#
# ether-wake
#
tab_etherwake()
{
cat <<EOT
    <frame Ether-wake (busybox)>
    <hbox>
      <text use-markup="true">
        <label>"<b>Machines :</b>"</label>
      </text>
      <entry editable="false">
        <default>/etc/ethers</default>
        <variable>ETHERS</variable>
      </entry>
      <button>
        <input file icon="accessories-text-editor"></input>
        <action>[ -s \$ETHERS ] || echo "#00:01:02:03:04:05 mystation" >\$ETHERS</action>
        <action type="lauch">leafpad \$ETHERS</action>
      </button>
    </hbox>
    <hbox>
      <text use-markup="true">
        <label>"<b>Options : </b>"</label>
      </text>
      <entry editable="true">
        <variable>ETHERWAKE_OPTS</variable>
      </entry>
$(helpbutton ether-wake 80x15)
	<button>
		<label>Start</label>
		<input file icon="forward"></input>
		<action>ether-wake \$ETHERWAKE_OPTS</action>
	</button>
    </hbox>
    <frame>
    <hbox>
EOT
if which ethtool > /dev/null; then
cat <<EOT
        <text use-markup="true">
          <label>"<b>Interface:</b>"</label>
        </text>
        <combobox>
          <variable>ETHERWAKE_ETH</variable>
EOT
(cd /sys/class/net ; [ -d eth0 ] && ls -d eth* ) | \
awk '{ printf "          <item>%s</item>\n",$1 }'
cat <<EOT
        </combobox>
	<button>
		<label>Enable Wake On LAN</label>
		<input file icon="forward"></input>
		<action>ethtool -s \$ETHERWAKE_ETH wok g</action>
	</button>
EOT
else
cat <<EOT
    <vbox>
	<text wrap="true" width-chars="58">
			<label>
"Ethtool can enable the Wake-on-lan 
feature on many Ethernet cards.
"
			</label>
	</text>
$(installbox ethtool)
    </vbox>
EOT
fi
cat <<EOT
    </hbox>
    </frame>
    </frame>
EOT
}

data_main()
{
cat <<EOT
#program    tab name      function
ifconfig    Connections   tab_connections
udhcpc      DHCP          tab_udhcpc
ifconfig    Static\ IP    tab_fixed_ip
pppoe       PPPoE         tab_pppoe
pppd        PPP           tab_ppp
true        VPN           tab_vpn
ether-wake  Etherwake     tab_etherwake
true        Configuration tab_system
EOT
}

tab_main()
{
use_tab data_main
}

######################################################
#
# Netbox GUI
#
######################################################

# English/French help dialog.
export HELP='
<window title="Network status" icon-name="network-wire">
<vbox>
	<text use-markup="true">
		<label>
"
<b>SliTaz - Netbox</b>"
		</label>
	</text>
	<frame English>
		<text wrap="true" width-chars="58">
			<label>
"Netbox lets you manage network connections by getting a dynamic IP by 
DHCP or a static IP and setup servers. Netbox can start or stop
networking, configure network interfaces or directly edit files."
			</label>
		</text>
	</frame>
	<frame Francais>
		<text wrap="true" width-chars="58">
			<label>
"Netbox vous permet de gerer les connexions reseau avec une IP
statique ou en obtenant une IP dynamique par DHCP, et de parametrer
les serveurs. Netbox peut demarrer ou arreter le reseau, configurer
les interfaces reseau ou editer directement les fichiers."
			</label>
		</text>
	</frame>
</vbox>
</window>
'

# Interface status with ifconfig without arguments to show all
# active connections.
#
export IFCONFIG="
<window title=\"Network status\" icon-name=\"network-wire\">
<vbox>
$(tab_status)
	<hbox>
		<button>
			<input file icon=\"gtk-close\"></input>
			<action type=\"closewindow\">IFCONFIG</action>
		</button>
	</hbox>
</vbox>
</window>"

# The main dialog with notebook, start/stop buttons and all options.
# Note that /etc/network.conf is seded when an interface is activated.
#
head='
<window title="SliTaz Netbox Manager" icon-name="network-wired">
	<vbox>

		<hbox>
			<text use-markup="true">
				<label>"<b>Network Manager</b>"</label>
			</text>
			<pixmap>
				<input file>/usr/share/pixmaps/netbox.png</input>
			</pixmap>
		</hbox>
'
bottom='
		<hbox>
			<button>
				<label>Wireless manager</label>
				<input file icon="network-wireless"></input>
				<action>wifibox &</action>
			</button>
			<button>
				<label>Refresh list</label>
				<input file icon="reload"></input>
				<action>refresh:INTERFACE_LIST</action>
			</button>
			<button>
			<label>Full status</label>
				<input file icon="dialog-information"></input>
				<action type="launch">IFCONFIG</action>
			</button>
			<button help>
				<label>Help</label>
				<action type="launch">HELP</action>
			</button>
			<button>
				<label>Quit</label>
				<input file icon="exit"></input>
				<action type="exit">Exit</action>
			</button>
		</hbox>

	</vbox>
</window>
'

NET_BOX="${head}$(tab_main)${bottom}"

export NET_BOX

# TODO:  Modules(Network kernel modules)

# Only root can configure network.
check_root

# Configure and connect if button Connect was pressed.
if ! grep -qs ^name /etc/ppp/options ; then
	# Generate /etc/ppp/options
	cat > /etc/ppp/options << _EOT_
plugin rp-pppoe.so
name provider-ID
noipdefault
defaultroute
mtu 1492
mru 1492
lock
_EOT_
	# Generate /etc/ppp/pap-secrets
	cat > /etc/ppp/pap-secrets << _EOT_
# Secrets for authentication using PAP
# client	server	secret			IP addresses
_EOT_
	# Generate /etc/ppp/chap-secrets
	cat > /etc/ppp/chap-secrets << _EOT_
# Secrets for authentication using CHAP
# client	server	secret			IP addresses
_EOT_
fi
gtkdialog --center --program=NET_BOX | grep -a 'EXIT="restart"' && continue
exit 0
done
