# --- ROCK-COPYRIGHT-NOTE-BEGIN ---
# 
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
# Please add additional copyright information _after_ the line containing
# the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
# the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
# 
# ROCK Linux: rock-src/package/base/sysfiles/stone_mod_network.sh
# ROCK Linux is Copyright (C) 1998 - 2006 Clifford Wolf
# 
# 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. A copy of the GNU General Public
# License can be found at Documentation/COPYING.
# 
# Many people helped and are helping developing ROCK Linux. Please
# have a look at http://www.rocklinux.org/ and the Documentation/TEAM
# file for details.
# 
# --- ROCK-COPYRIGHT-NOTE-END ---
#
# [MAIN] 20 network Network Configuration

set_name() {
	old1="$HOSTNAME" old2="$HOSTNAME.$DOMAINNAME" old3="$DOMAINNAME"
	if [ $1 = HOSTNAME ] ; then
		gui_input "Set a new hostname (without domain part)" \
		          "${!1}" "$1"
	else
		gui_input "Set a new domainname (without host part)" \
		          "${!1}" "$1"
	fi
	new="$HOSTNAME.$DOMAINNAME $HOSTNAME"

	echo "$HOSTNAME" > /etc/HOSTNAME ; hostname "$HOSTNAME"

	#ip="`echo $IPADDR | sed 's,[/ ].*,,'`"
	#if grep -q "^$ip\\b" /etc/hosts ; then
	#	tmp="`mktemp`"
	#	sed -e "/^$ip\\b/ s,\\b$old2\\b[ 	]*,,g" \
	#	    -e "/^$ip\\b/ s,\\b$old1\\b[ 	]*,,g" \
	#	    -e "/^$ip\\b/ s,[ 	]\\+,&$new ," < /etc/hosts > $tmp
	#	cat $tmp > /etc/hosts ; rm -f $tmp
	#else
	#	echo -e "$ip\\t$new" >> /etc/hosts
	#fi

	if [ $1 = DOMAINNAME ] ; then
		tmp="`mktemp`"
		grep -vx "search $old3" /etc/resolv.conf > $tmp
		[ -n "$DOMAINNAME" ] && echo "search $DOMAINNAME" >> $tmp
		cat $tmp > /etc/resolv.conf
		rm -f $tmp
	fi
}

set_dns() {
	gui_input "Set a new (space seperated) list of DNS Servers" "$DNSSRV" "DNSSRV"
	DNSSRV="`echo $DNSSRV`" ; [ -z "$DNSSRV" ] && DNSSRV="none"

	tmp="`mktemp`" ; grep -v '^nameserver\b' /etc/resolv.conf > $tmp
	for x in $DNSSRV ; do
		[ "$x" != "none" ] && echo "nameserver $x" >> $tmp
	done
	cat $tmp > /etc/resolv.conf
	rm -f $tmp
}

HOSTNAME="`hostname`"
DOMAINNAME="`hostname -d 2> /dev/null`"

tmp="`mktemp`"
grep '^nameserver ' /etc/resolv.conf | tr '\t' ' ' | tr -s ' ' | \
    sed 's,^nameserver *\([^ ]*\),DNSSRV="$DNSSRV \1",' > $tmp
DNSSRV='' ; . $tmp ; DNSSRV="`echo $DNSSRV`"
[ -z "$DNSSRV" ] && DNSSRV="none" ; rm -f $tmp

main_detailed() {
    while
	cmd="gui_menu network 'Network Configuration'"

	cmd="$cmd 'Static hostname:   $HOSTNAME'   'set_name HOSTNAME'"
	cmd="$cmd 'Static domainname: $DOMAINNAME' 'set_name DOMAINNAME'"
	cmd="$cmd 'Static DNS-Server: $DNSSRV'     'set_dns' '' ''"

	cmd="$cmd 'Configure runlevels for network service'"
	cmd="$cmd '$STONE runlevel edit_srv network'"
	cmd="$cmd '(Re-)Start network init script'"
	cmd="$cmd '$STONE runlevel restart network'"
	cmd="$cmd '' ''"

	cmd="$cmd 'View/Edit /etc/resolv.conf file'    'gui_edit /etc/resolv.conf /etc/resolv.conf'"
	cmd="$cmd 'View/Edit /etc/hosts file'          'gui_edit /etc/hosts /etc/hosts'"
	cmd="$cmd 'View/Edit /etc/network/config file' 'gui_edit /etc/network/config /etc/network/config'"

	cmd="$cmd '' ''"

	cmd="$cmd 'Read the /etc/network/README file' 'gui_edit /etc/network/README /etc/network/README'"

	eval "$cmd"
    do : ; done
}

main() {
    while
	cmd="gui_menu network 'Network Configuration'"

	cmd="$cmd 'Just edit /etc/network/config'     'gui_edit /etc/network/config /etc/network/config'"
	cmd="$cmd 'Read the /etc/network/README file' 'gui_edit /etc/network/README /etc/network/README'"
	cmd="$cmd 'Detailed Networking configuration' 'exec $0 network main_detailed'"

	eval "$cmd"
    do : ; done
}

