#!/bin/sh

############################################################################
#			 Syntax check
############################################################################
#
# This checks IP address syntax.
# usage: syntax_check ADDRESS #-OF-EXPECTED-SEGMENTS (up to 4)
# example: syntax_check 123.22.43.1 4
# returns: 0=found correct  1=too many fields  2=non numeric field found
syntax_check_color() {
  RET_CODE=0 
  SCRATCH=$1
  SCRATCH=`echo $SCRATCH | tr "." "/"`
  INDEX=$2
  while [ ! "$INDEX" = "0" ]; do
    # OK, so I'm a LISP-head :^)
    FIELD=`basename $SCRATCH`
    SCRATCH=`dirname $SCRATCH`
    if expr $FIELD + 1 1> /dev/null 2> /dev/null; then
      GOOD=y
    else
      RET_CODE=2; # non-numeric field
    fi
    INDEX=`expr $INDEX - 1`
  done
  if [ ! "$SCRATCH" = "." ]; then
    RET_CODE=1; # too many arguments
  fi
  if [ "$3" = "WARN" -a ! "$RET_CODE" = "0" ]; then
    cat << EOF > /tmp/tmpmsg

Input address $1 doesn't seem to be a valid IP address.
We expect here dot-separated $2 numbers such as 127.0.0.1.
Please Input again.

EOF
    dialog --title "WARNING" --msgbox "`cat /tmp/tmpmsg`" 9 72
    rm -r /tmp/tmpmsg
  else
    if [ "$3" = "ECHO" ]; then
      echo $RET_CODE;
    fi
  fi
  return $RET_CODE;

}

############################################################################
#			 Question and answer.
############################################################################
#
firstdialog() {

cat << EOF > /tmp/tmpmsg

We are going to network configuration for this PC. 
Even if your PC doesn't have any network cards, you should
configure your hostname(or you should meet hangups when
you boot your system or apps).
hostname will be written to /etc/hosts and /etc/HOSTNAME,
network configurations to /etc/rc.d/rc.inet1, 
/etc/networks, and /etc/resolv.conf, so you can 
manually edit these files to modify your configurations. 

In case of NotePC, /etc/pcmcia.network.opts should 
be configured too.

You can modify your configurations with
  netconfig
command.

EOF
dialog --title "configure your network" --msgbox "`cat /tmp/tmpmsg`" 21 62

}


############################################################################
#		     Change permissions and exit.
############################################################################
#
last_dialog() {

dialog --title "finish network configuration" --msgbox \
"Network configuration finished.\n\
In case your kernel cannot recognize your network cards, \n\
you'll need to load device drivers for your cards. \n\
You can load module drivers with modprobe or insmod command, \n\
or rebuid your kernel with appropriate drivers built in. " 13 72
rm -f /tmp/tmpmsg

}

############################################################################
#		     ok.
############################################################################
#
ok_dialog() {

dialog --title " OK." --msgbox \
"\n\
      Configured.\n\
" 8 32
rm -f /tmp/tmpmsg

}

############################################################################
#		     Not found dhcpcd ?
############################################################################
#
dhcpcd_not_found_dialog() {

if [ ! "$DHCP" = "y" ] ; then
  return 0 ;
fi
if [ -x $DHCPCD ] ; then
  return 0 ;
fi

dialog --title "Dhcpcd not found" --msgbox \
"dhcpcd which is essential for DHCP connection seemd not to be installed.\n\
Plamo Linux provides dhcpcd.tgz packege for DHCP connection.\n\
You must install dhcpcd afterwards. \n\
You can install dhcpcd only with a short command like \n\
\n\
    # installpkg dhcpcd.tgz\n\
\n\
and that's all." 12 68
rm -f /tmp/tmpmsg

}

############################################################################
#			 Set hostname and domain.
############################################################################
#
set_hostname() {

HOSTNAME=""
while [ "$HOSTNAME" = "" ]; do
cat << EOF > /tmp/tmpmsg

At first, please input your PC's hostname. You should input
only hostname which doesn't include domainname.

What your PC's hostname ?:
EOF
 dialog --title "configure your hostname" --inputbox "`cat /tmp/tmpmsg`" 11 70 \
 2> /tmp/SeThost
 if [ $? = 1 -o $? = 255 ]; then
  rm -f /tmp/SeThost /tmp/tmpmsg
  exit
 fi
 HOSTNAME="`cat /tmp/SeThost`"
 rm -f /tmp/SeThost /tmp/tmpmsg
done


cat << EOF > /tmp/tmpmsg
Next, please input yout domainname. You shouldn't input first '.'
of your domainname(NOT .frog.or.jp but frog.or.jp).

If you use DHCP and you don't have fixed domainname, only
input [Enter] now.

What is the domainname for $HOSTNAME ?
EOF
 dialog --title "configure your domainname" --inputbox "`cat /tmp/tmpmsg`" \
14 72 2> /tmp/SeTdom
if [ $? = 1 -o $? = 255 ]; then
  rm -f /tmp/SeTdom /tmp/tmpmsg
  exit
fi
DOMAIN="`cat /tmp/SeTdom`"
rm -f /tmp/SeTdom /tmp/tmpmsg
if [ "x.$DOMAIN" = "x." ] ; then
  echo $HOSTNAME > etc/HOSTNAME
else
  echo $HOSTNAME.$DOMAIN > etc/HOSTNAME
fi

}

############################################################################
#			Select methond
############################################################################
#
select_dhcp_or_fixed() {

while [ 0 ] ; do
  dialog --title "Change the connect method" \
--menu "\nHow do you connect to your network ?\n\
\n" 17 76 3 \
"Fixed" "With fixed IP address(You need input network parameters afterwards)" \
"Dhcp" "With DHCP" \
"Loopback" "Don't connect to network" 2> /tmp/reply
  if [ $? = 1 -o $? = 255 ]; then
    rm -f /tmp/reply
    exit
  else
    break;
  fi
done
  REPLY="`cat /tmp/reply`"
  rm -f /tmp/reply
  LOOPBACK="n"
  if [ "$REPLY" != "Fixed" ] ; then
    LOOPBACK="y"
    if [ "$REPLY" = "Dhcp" ] ; then
      DHCP="y"
    fi
  fi
}



############################################################################
#			 TCP/IP Setting
############################################################################
#
set_tcpip() {

if [ -r etc/rc.d/rc.inet1 -a "$LOOPBACK" = "n" ]; then

 while [ 0 ]; do
  cat << EOF > /tmp/tmpmsg
Input the IP address of your PC. IP address is consisted from
4 digits separated by '.' , such as 111.112.113.114.
What is the IP address for $HOSTNAME ?(aaa.bbb.ccc.ddd):
EOF
  dialog --title "configure your IP address" --inputbox "`cat /tmp/tmpmsg`" \
10 72 2> /tmp/SeTlip
  if [ $? = 1 -o $? = 255 ]; then
   rm -f /tmp/SeTlip /tmp/tmpmsg
   exit
  fi
  IPADDR="`cat /tmp/SeTlip`"
  rm -f /tmp/SeTlip /tmp/tmpmsg
  if [ "x.$IPADDR" = "x." ]; then
    continue;
  fi
  syntax_check_color $IPADDR 4 WARN
  if [ $? = 0 ]; then
    break;
  fi
 done
 
 while [ 0 ]; do
  cat << EOF > /tmp/tmpmsg
Input your gateway address. Address is consisted from
4 digits separated by '.' like 111.112.113.1

If your network doesn't have gateway, push ENTER to go
ahead.

What is your gateway address?(aaa.bbb.ccc.ddd):
EOF
  dialog --title "configure your gateway address" --inputbox "`cat /tmp/tmpmsg`" \
13 72 2> /tmp/SeTgate
  if [ $? = 1 -o $? = 255 ]; then
   rm -f /tmp/SeTgate /tmp/tmpmsg
   exit
  fi
  GATEWAY="`cat /tmp/SeTgate`"
  rm -f /tmp/SeTgate /tmp/tmpmsg
  if [ "x.$GATEWAY" = "x." ]; then
    break;
  fi
  syntax_check_color $GATEWAY 4 WARN
  if [ $? = 0 ]; then
    break;
  fi
 done

 while [ 0 ]; do
  cat << EOF > /tmp/tmpmsg
Input netmask for your network. Netmask is 4 digits like
255.255.255.0.
What is your netmask ?(aaa.bbb.ccc.ddd):
EOF
  dialog --title "configure your netmask" --inputbox "`cat /tmp/tmpmsg`" \
10 72 2> /tmp/SeTnmask
  if [ $? = 1 -o $? = 255 ]; then
   rm -f /tmp/SeTnmask /tmp/tmpmsg
   exit
  fi
  NETMASK="`cat /tmp/SeTnmask`"
  rm -f /tmp/SeTnmask /tmp/tmpmsg
  if [ "x.$NETMASK" = "x." ]; then
    continue;
  fi
  syntax_check_color $NETMASK 4 WARN
  if [ $? = 0 ]; then
    break;
  fi
 done

# Set broadcast/network addresses automatially:

BROADCAST=`ipmask $NETMASK $IPADDR | cut -f 1 -d ' '`
NETWORK=`ipmask $NETMASK $IPADDR | cut -f 2 -d ' '`

# if [ -r etc/rc.d/rc.inet1 -a "$LOOPBACK" = "n" ]; then
else
 if [ ! -r etc/rc.d/rc.inet1 ]; then
  cat << EOF > /tmp/tmpmsg

As your system doesn't have TCP/IP packages installed, you can
only configure hostname and domainname now. In this case, 
you cannot connect to your network, but your hostname and domainname
will be used as prompter of your shell.

EOF
  dialog --title "skip almost configuration" \
--infobox "`cat /tmp/tmpmsg`" 10 72
 fi
fi

#
#'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#			  The networks file.
#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
#

if [ -f $ETCNETWORKS ]; then
 cp $ETCNETWORKS tmp/`basename $ETCNETWORKS`.OLD
fi
#echo "Creating /$ETCNETWORKS..."
/bin/cat <<EOF >$ETCNETWORKS
#
# networks	This file describes a number of netname-to-address
#		mappings for the TCP/IP subsystem.  It is mostly
#		used at boot time, when no name servers are running.
#

loopback	127.0.0.0
localnet	$NETWORK

# End of networks.
EOF
chmod 644 $ETCNETWORKS
#
#'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#			   The hosts file.
#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
#
#echo "Creating /$HOSTS..."
if [ -f $HOSTS ];then cp $HOSTS tmp/`basename $HOSTS`.OLD;fi
if [ "x.$DOMAIN" = "x." ] ; then
  LOOPIP="# 127.0.0.1"
  FULLDOMAIN="localhost"
else
  LOOPIP="127.0.0.1"
  FULLDOMAIN="$HOSTNAME.$DOMAIN"
fi

/bin/cat <<EOF >$HOSTS
#
# hosts		This file describes a number of hostname-to-address
#		mappings for the TCP/IP subsystem.  It is mostly
#		used at boot time, when no name servers are running.
#		On small systems, this file can be used instead of a
#		"named" name server.  Just add the names, addresses
#		and any aliases to this file...
#
# By the way, Arnt Gulbrandsen <agulbra@nvg.unit.no> says that 127.0.0.1
# should NEVER be named with the name of the machine.  It causes problems
# for some (stupid) programs, irc and reputedly talk. :^)
#

# For loopbacking.
$LOOPIP	localhost
$IPADDR	$FULLDOMAIN $HOSTNAME

# End of hosts.
EOF
chmod 644 $HOSTS

}

############################################################################
#			The resolv.conf file.
############################################################################
#
set_resolv() {

 if [ "$LOOPBACK" != "n" ] ; then
   if [ "$DHCP" != "y" ] ; then
     return 0 ;
   fi
 fi
 dialog --title "Specify nameserver ?" --yesno \
"Do you want to specify your nameserver's IP address ?" 5 72
 if [ $? = 0 ]; then
   while [ "x.$NAMESERVER" = "x." ]; do
     if [ "x.$DOMAIN" = "x." ] ; then
       FULLDOMAIN=""
     else
       FULLDOMAIN="$HOSTNAME.$DOMAIN"
     fi
     cat << EOF > /tmp/tmpmsg
Configured IP address and hostname is like this.
$IPADDR       $FULLDOMAIN    $HOSTNAME

Input your nameserver for name resolution services.
If you want to configure multiple nameservers, you
need to edit /$RESOLC file manually.

What is the IP address of your nameserver ?(aaa.bbb.ccc.ddd): 
EOF
     dialog --title "configure your nameserver" --inputbox \
"`cat /tmp/tmpmsg`" 14 72 2> /tmp/SeTns
     if [ $? = 1 -o $? = 255 ]; then
       rm -f /tmp/tmpmsg /tmp/SeTns 
       return 1
     fi
     NAMESERVER="`cat /tmp/SeTns`"
     rm -f /tmp/tmpmsg /tmp/SeTns 
     syntax_check_color $NAMESERVER 4 WARN
     if [ $? != 0 ]; then
       NAMESERVER=""
     fi
   done
   if [ -f $RESOLV ]; then cp $RESOLV tmp/`basename $RESOLV`.OLD;fi
   if [ ! "x.$DOMAIN" = "x." ] ; then
     echo "domain $DOMAIN" >$RESOLV
     echo "nameserver $NAMESERVER" >>$RESOLV
   else
     echo "nameserver $NAMESERVER" >$RESOLV
   fi
 else
   if [ ! "x.$DOMAIN" = "x." ] ; then
     echo "domain $DOMAIN" >$RESOLV
   else
     echo "" >$RESOLV
   fi
 fi
 if [ -f $PCMCIARC ] ; then
   cp $PCMCIARC "tmp/ptmp"$$
   sed -e "s/DNS_1=\"[^.]*.[^.]*.[^.]*.[^.]*\"/DNS_1=\"$NAMESERVER\"/g" "tmp/ptmp"$$ | sed -e "s/DNS_1=\"\"/DNS_1=\"$NAMESERVER\"/g" > $PCMCIARC
   rm "tmp/ptmp"$$
 fi
 return 0
}


############################################################################
#		PCMCIA?
############################################################################
#

if_pcmcia() {

if [ "$PCMCIA" = "y" ]; then

dialog --title "configure for PCMCIA-CS?" \
--yesno "This host seems to have PCMCIA system installed. \
Do you want to setup your network configuration for PCMCIA ? " 9 76
   if [ ! $? = 0 ]; then
     PCMCIA="n"
   else
     PCMCIA="Y"
   fi
else 
  if [ "$PCMCIA" != "Y" ] ; then
    PCMCIA="n"
  fi
fi

}

############################################################################
#		Make config. files
############################################################################
#
make_config_file() {

if [ "$PCMCIA" != "n" ];then
  if [ -f $PCMCIARC ]; then 
    cp $PCMCIARC tmp/`basename $PCMCIARC`.OLD
  fi

#''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#      make /etc/pcmcia/network.opts
#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

if [ "$LOOPBACK" = "n" ] ; then

 /bin/cat <<EOF >$PCMCIARC
# Network adapter configuration
#
# The address format is "scheme,socket,instance,hwaddr".
#
# Note: the "network address" here is NOT the same as the IP address.
# See the Networking HOWTO.  In short, the network address is the IP
# address masked by the netmask.
#
case "\$ADDRESS" in
dhcp,*,*,*)
    # Transceiver selection, for cards that need it -- see 'man ifport'
    IF_PORT=""
    # Use BOOTP [y/n]
    BOOTP="n"
    # Use DHCP [y/n]
    DHCP="y"
    # IP address
    IPADDR=""
    # Domain name
    DOMAIN="$DOMAIN"
    # Nameserver #1
    DNS_1="$NAMESERVER"
    # Nameserver #2
    DNS_2=""
    # Nameserver #3
    DNS_3=""
    ;;
fixed,*,*,*)
    # Transceiver selection, for cards that need it -- see 'man ifport'
    IF_PORT=""
    # Use BOOTP [y/n]
    BOOTP="n"
    # IP address
    IPADDR="$IPADDR"
    # Netmask
    NETMASK="$NETMASK"
    # Network address
    NETWORK="$NETWORK"
    # Broadcast address
    BROADCAST="$BROADCAST"
    # Gateway address
    GATEWAY="$GATEWAY"
    # Domain name
    DOMAIN="$DOMAIN"
    # Nameserver #1
    DNS_1="$NAMESERVER"
    # Nameserver #2
    DNS_2=""
    # Nameserver #3
    DNS_3=""
    # NFS mounts, should be listed in /etc/fstab
    MOUNTS=""
    ;;
esac

EOF

  SCHEME="`cat $PCMCIASCHEME`"
  if [ "$SCHEME" = "default" ] ; then
/bin/cat <<EOF >$PCMCIASCHEME
dhcp
EOF
    rm -rf $PCMCIASCHEMELN
    ln -sf /$PCMCIASCHEME $PCMCIASCHEMELN
  fi

else	# if [ "$LOOPBACK" = "n" ] ; then

 /bin/cat <<EOF >$PCMCIARC
# Network adapter configuration
#
# The address format is "scheme,socket,instance,hwaddr".
#
# Note: the "network address" here is NOT the same as the IP address.
# See the Networking HOWTO.  In short, the network address is the IP
# address masked by the netmask.
#
case "\$ADDRESS" in
default,*,*,*)
    # Transceiver selection, for cards that need it -- see 'man ifport'
    IF_PORT=""
    # Use BOOTP [y/n]
    BOOTP="n"
    # Use DHCP [y/n]
    DHCP="y"
    # IP address
    IPADDR=""
    # Domain name
    DOMAIN="$DOMAIN"
    # Nameserver #1
    DNS_1="$NAMESERVER"
    # Nameserver #2
    DNS_2=""
    # Nameserver #3
    DNS_3=""
    ;;
esac

EOF

fi	# if [ "$LOOPBACK" = "n" ] ; then




# if [ "$PCMCIA" = "yes" ];then
else
#
#'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#			  The rc.inet1 file.
#,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
#
if [ ! -h $RC ] ; then
  if [ -f $RC ]; then 
    cp $RC tmp/`basename $RC`.OLD
  fi
fi
# echo "Creating /$RC..."
/bin/cat <<EOF >$RC
#! /bin/sh
#
# rc.inet1	This shell script boots up the base INET system.
#
# Version:	@(#)/etc/rc.d/rc.inet1	1.01	05/27/93
#

HOSTNAME=\`cat /etc/HOSTNAME\`

# Attach the loopback device.
/$IFCONFIG lo 127.0.0.1
/$ROUTE add -net 127.0.0.0 netmask 255.0.0.0 lo

if [ -f /$INET1SCHEME ] ; then
  INET1_SCHEME=\`cat /$INET1SCHEME\`
else
  INET1_SCHEME="fixed"
fi

if [ "\$INET1_SCHEME" = "fixed" ] ; then

  # IF YOU HAVE AN ETHERNET CONNECTION, use these lines below to configure the 
  # eth0 interface. If you're only using loopback or SLIP, don't include the
  # rest of the lines in this file.

  # Edit for your setup.
  IPADDR="$IPADDR"		# REPLACE with YOUR IP address!
  NETMASK="$NETMASK"		# REPLACE with YOUR netmask!
  NETWORK="$NETWORK"		# REPLACE with YOUR network address!
  BROADCAST="$BROADCAST"	# REPLACE with YOUR broadcast address, if you
				# have one. If not, leave blank and edit below.
  GATEWAY="$GATEWAY"		# REPLACE with YOUR gateway address!

  # Uncomment the line below to configure your ethernet card.
  /$IFCONFIG eth0 \${IPADDR} broadcast \${BROADCAST} netmask \${NETMASK}

  # If the line above is uncommented, the code below can also be uncommented.
  # It sees if the ethernet was properly initialized, and gives the admin some
  # hints about what to do if it wasn't.
  if [ ! \$? = 0 ]; then
    cat << END
Your ethernet card was not initialized properly.  Here are some reasons why this
may have happened, and the solutions:
1. Your kernel does not contain support for your card.  Including all the 
   network drivers in a Linux kernel can make it too large to even boot, and
   sometimes including extra drivers can cause system hangs.  To support your
   ethernet, either edit /etc/rc.d/rc.modules to load the support at boottime,
   or compile and install a kernel that contains support.
2. You don't have an ethernet card, in which case you should comment out this
   section of /etc/rc.d/rc.inet1.  (Unless you don't mind seeing this error...)
END
  fi

  # Uncomment these to set up your IP routing table.
  # /$ROUTE add -net \${NETWORK} netmask \${NETMASK} eth0
  if [ ! "\$GATEWAY" = "" ]; then
   /$ROUTE add default gw \${GATEWAY} netmask 0.0.0.0 metric 1
  fi
elif [ "\$INET1_SCHEME" = "dhcp" -o "\$INET1_SCHEME" = "DHCP" ] ; then
  if [ -x /$DHCPCD ] ; then
    /$DHCPCD
  else
    cat << END
Dhcpcd was not found.
Please install dhcpcd.
END
  fi
fi

# End of rc.inet1
EOF
chmod 755 $RC

if [ "$LOOPBACK" = "n" ] ; then
  if [ -f $INET1SCHEME ] ; then
    INET1_SCHEME="`cat $INET1SCHEME`"
    if [ "$INET1_SCHEME" = "DHCP" ] ; then
/bin/cat <<EOF >$INET1SCHEME
dhcp
EOF
    elif [ "$INET1_SCHEME" = "LOOPBACK" ] ; then
/bin/cat <<EOF >$INET1SCHEME
loopback
EOF
    fi
  fi
fi

fi 

}

############################################################################
#		Set Scheme 
############################################################################
#
set_scheme() {
  touch $INET1SCHEME
  if [ "$PCMCIA" = "n" ] ; then
    if [ "$LOOPBACK" = "n" ] ; then
      /bin/cat <<EOF >$INET1SCHEME
fixed
EOF
    else
      FNA="y"
      INET1_SCHEME=`cat $INET1SCHEME`
      if [ "$INET1_SCHEME" = "fixed" -o "$INET1_SCHEME" = "dhcp" -o "$INET1_SCHEME" = "loopback" ] ; then
	FNA="n" ;
      fi
      if [ "$INITIAL" = "y" -o "$FNA" = "y" ] ; then
        if [ "$DHCP" = "y" ] ; then
          /bin/cat <<EOF >$INET1SCHEME
DHCP
EOF
        else
          /bin/cat <<EOF >$INET1SCHEME
LOOPBACK
EOF
	fi
      else
        if [ "$DHCP" = "y" ] ; then
          /bin/cat <<EOF >$INET1SCHEME
dhcp
EOF
        else
          /bin/cat <<EOF >$INET1SCHEME
loopback
EOF
	fi
      fi
    fi
 
  else		# notePC
    SCHEME="`cat $PCMCIASCHEME`"
    if [ "$LOOPBACK" = "n" ] ; then
 /bin/cat <<EOF >$PCMCIASCHEME
fixed
EOF
    else
# INITIAL="y" ΤȤloopback, dhcp λ default ˤ롣
# ϸ fixed äɤΥե饰ˤʤ롣
      if [ "$INITIAL" = "y" ] ; then
        /bin/cat <<EOF >$PCMCIASCHEME
default
EOF
      else 
	if [ "$SCHEME" = "fixed" ] ; then
	  /bin/cat <<EOF >$PCMCIASCHEME
dhcp
EOF
	fi
      fi
    fi
    rm -rf $PCMCIASCHEMELN
    ln -sf /$PCMCIASCHEME $PCMCIASCHEMELN
  fi

}


############################################################################
#		Select command ( if found network setting )
############################################################################
#
select_cmd() {

  dialog --title "modify network configuration" \
--menu "\nNetwork configuration seemed to have setup for this host already.\n\
\nWhich config do you want to modify ?\n" 17 78 6 \
"Scheme" "Modify connection scheme(DHCP/fixed IP/no connection)" \
"Network" "Modify network configuration for fixed IP network." \
"Dns" "Modify nameserver configuration" \
"Initialize" "abandon current configurations and reconfigure from begging" \
"Exit" "Exit" 2> /tmp/reply
  if [ $? = 1 -o $? = 255 ]; then
   rm -f /tmp/reply
   exit
  fi
  REPLY="`cat /tmp/reply`"
  rm -f /tmp/reply
}

############################################################################
#			Select methond
############################################################################
#
change_dhcp_or_fixed() {

  dialog --title "Change connection method" \
--menu "\nBy which method do you want to connect ?\n\
\n" 17 48 4 \
"Dhcp" "via DHCP" \
"Fixed" "via fixed IP address" \
"Loopback" "Don't connect" \
"Exit" "quit change" 2> /tmp/reply
  if [ $? = 1 -o $? = 255 ]; then
   rm -f /tmp/reply
   exit
  fi
  REPLY="`cat /tmp/reply`"
  rm -f /tmp/reply
  if [ "$REPLY" = "Exit" ] ; then
    exit
  fi
}

############################################################################
#			
############################################################################
#
if_modify_tcpip() {

 dialog --title "Not found TCP/IP setting" --yesno \
"This sytem doesn't have fixed IP configuration. \n\
Do you want to configure for fixed IP now ?\n\
(you can easily resume to DHCP connectio with  'netconfig' \n\
command)" 8 54
 
}

change_scheme() {

if [ "$PCMCIA" != "n" ] ; then 
  SCHEME="`cat $PCMCIASCHEME`"
  case "$REPLY" in
  "Dhcp")
    LOOPBACK="y"
    DHCP="y"
    dhcpcd_not_found_dialog
  ;;
  "Fixed")
    LOOPBACK="n"
    if [ "$SCHEME" != "fixed" ] ; then
      if [ "$SCHEME" != "dhcp" ] ; then
        if_modify_tcpip
	if [ $? = 0 ] ; then	# Yesʤ
	  DOMAIN=""
	  set_hostname
	  set_tcpip
	  set_resolv
	  make_config_file
	else
	  exit
        fi
      fi
    fi
  ;;
  esac

else	# if [ "$PCMCIA" != "n" ] ; then 
  # ǥȥåѤ
  case "$REPLY" in
  "Dhcp")
    LOOPBACK="y"
    DHCP="y"
    dhcpcd_not_found_dialog
  ;;
  "Loopback")
    LOOPBACK="y"
    DHCP="n"
  ;;
  "Fixed")
    LOOPBACK="n"
    FNA="y"
    if [ -f $INET1SCHEME ] ; then
      INET1_SCHEME=`cat $INET1SCHEME`
      if [ "$INET1_SCHEME" = "fixed" -o "$INET1_SCHEME" = "dhcp" -o "$INET1_SCHEME" = "loopback" ] ; then
        FNA="n" ;
      fi
    fi
    if [ "$FNA" = "y" ] ; then
      if_modify_tcpip
      if [ $? = 0 ] ; then	# Yesʤ
	DOMAIN=""
        set_hostname
	set_tcpip
	set_resolv
	make_config_file
      else
	exit
      fi
    fi
  ;;
  esac

fi	# if [ "$PCMCIA" != "n" ] ; then 

}

############################################################################
#			Ϥᤫʤ
############################################################################
#

cleanup_setting() {
  NETWORK=127.0.0.0
  IPADDR=127.0.0.1
  DOMAIN=""
  INITIAL="y"
  # ǽΥ
  firstdialog
  # ۥ̾
  set_hostname
  # ³ˡλ
  select_dhcp_or_fixed
  # dhcpcdĤʤdialogФ
  dhcpcd_not_found_dialog
  # ɬפʤIPʤɤ򤹤
  set_tcpip
  # ͡ॵФ
  set_resolv
  # PCMCIA?
  if_pcmcia
  # եؤν񤭽Ф
  set_scheme
  make_config_file
  # ǸΥ
  last_dialog
}

############################################################################
#				MAIN
############################################################################
#

if [ ! -d lost+found -a ! -d vmlinuz -a ! -d proc ]; then # cheap, but it works :^)
  cd /
fi;


# IMPORTANT!!! NO LEADING '/' in the paths below, or this script will not
# function from the bootdisk.
IFCONFIG=sbin/ifconfig			# Where ifconfig program is.
ROUTE=sbin/route			# Where route program is.
RC=etc/rc.d/rc.inet1			# Where rc.inet1 file is.
RESOLV=etc/resolv.conf			# Where resolv.conf file is.
HOSTS=etc/hosts			 	# Where hosts file is.
ETCNETWORKS=etc/networks		# Where networks file is.
PCMCIARC=etc/pcmcia/network.opts
PCMCIASCHEMEDIR=var/lib/pcmcia
PCMCIASCHEME=${PCMCIASCHEMEDIR}/scheme
PCMCIASCHEMELNDIR=var/run
PCMCIASCHEMELN=${PCMCIASCHEMELNDIR}/pcmcia-scheme
INET1SCHEME=var/run/inet1-scheme
DHCPCD=sbin/dhcpcd
#
/bin/mkdir -p $PCMCIASCHEMEDIR
/bin/mkdir -p $PCMCIASCHEMELNDIR
/bin/touch $PCMCIASCHEME
#
#
if [ -x etc/rc.d/rc.pcmcia -o -x etc/rc.d/init.d/pcmcia ] ; then
  PCMCIA="y"
else
  PCMCIA="n"
fi
if [ -f $INET1SCHEME ] ; then
  ALREADY="y"
fi

if [ ! "$ALREADY" = "y" ] ; then 
  cleanup_setting
else
  HOSTNAME=$(hostname)
  IPADDR=$(hostname -i)
  DOMAIN=$(hostname -d)
  INITIAL="n"
  if_pcmcia
  while [ 0 ] ; do 
  select_cmd
  case "$REPLY" in
  "Scheme")
    # ɤ³ˡ?
    change_dhcp_or_fixed
    change_scheme    
    set_scheme
    ok_dialog
  ;;
  "Hostname")
    DOMAIN=""
    set_hostname
    ok_dialog
  ;;
  "Network")
    LOOPBACK="n"
    DOMAIN=""
    set_hostname
    set_tcpip
    make_config_file
    ok_dialog
  ;;
  "Dns")
    LOOPBACK="n"
    set_resolv
    if [ $? = 0 ] ; then 
      ok_dialog
    fi
  ;;
  "Initialize")
    cleanup_setting
    exit
  ;;
  "Exit")
    exit
  ;;
  esac
  done
fi

