#!/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

Ϥ줿ɥ쥹 $1 ϡIP ɥ쥹Τ褦ˤϸޤ
ǴԤƤΤϡ127.0.0.1 Τ褦˥ɥåȤǤ줿
$2 ĤοǤ⤦١ϤľƤ

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

줫 ƥΥͥåȥ˴ؤԤʤޤ
ͥåȥɤΤʤޥǤ⡢ۥ̾Ϥ
ɬפޤ(ʤȵư䥢ץꥱ
¹Ի˰ưʤʤ뤳Ȥޤ

ۥ̾ϡ/etc/hosts, /etc/HOSTNAME ˡͥå
˴ؤ /etc/rc.d/rc.inet1, /etc/networks, 
/etc/resolv.conf  ˹ԤʤޤΤǡɬפʤľܥե
Ƥ
ΡPCξϡ/etc/pcmcia/network.opts ꤷޤ
ޤ
  
  netconfig

ޥɤ¹Ԥ뤳ȤǡĤǤѹǤޤ

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

}


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

dialog --title "finish network configuration" --msgbox \
"ͥåȥեȥ꤬λޤ\n\
ͥ뤬ͥåȥɤǧʤ硢 \n\
ɬפʥɥ饤ФȤ߹ޤƤʤǽޤ \n\
ξ硢⥸塼ȤѰդƤɥ饤Ф \n\
Ȥ߹फɬפʥɥ饤ФȤ߹褦˥ͥ \n\
ƹۤƤ" 13 72
rm -f /tmp/tmpmsg

}

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

dialog --title " OK." --msgbox \
"\n\
ꤷޤ\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 \
"DHCP³ɬפ dhcpcd ޤ󥹥ȡ뤵Ƥʤ褦Ǥ\n\
Plamo Linux Ǥϥѥå dhcpcd.tgz 󶡤Ƥޤθ塢\n\
dhcpcd  󥹥ȡɬԤäƤ\n\
dhcpcd Υ󥹥ȡñˡ\n\
\n\
    # installpkg dhcpcd.tgz\n\
\n\
ȤǴλޤ" 12 68
rm -f /tmp/tmpmsg

}

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

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

ǽˤȤΥۥȤ̾ꤷƤǻꤹΤ
ۥ̾ǡɥᥤ̾ϴޤʤǤ

ۥ̾ϡ:
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
˥ɥᥤ̾Ԥʤޤǽ '.' Ϥʤ
(.frog.or.jp ǤϤʤ frog.or.jp ȤƤ)

DHCPˤ³ǡɥᥤޤäƤʤϡñ
[Enter] 򲡤Ƥ

$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 "³ˡѹ" \
--menu "\nɤˡǥͥåȥ³ޤ\n\
\n" 17 76 3 \
"Fixed" "IP³(θ塢ͥåȥѥ᡼򤷤ޤ)" \
"Dhcp" "DHCP³" \
"Loopback" "³˻" 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
ۥȤ IP ɥ쥹ꤷƤIP ɥ쥹ϡ
㤨 111.112.113.114 Τ褦ˤʤޤ
$HOSTNAME  IP ɥ쥹ϡ(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
ȥΥɥ쥹ꤷƤɥ쥹
111.112.113.1 Τ褦˻ꤷޤ

ȥ̵硢ENTER 򲡤ơؿʤߤޤ

ȥɥ쥹ϡ(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
ȤΥͥåȥΥͥåȥޥꤷƤͥå
ޥ255.255.255.0 Τ褦ʿǤ
ͥåȥޥϡ(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

TCP/IP ѥå󥹥ȡ뤵Ƥʤ褦ʤΤǡ
ǤΤϥۥ̾/ɥᥤ̾Ǥξ硢ͥåȥ
³ǤʤΤǤΥץץȤ˻ꤷ̾
ɽ褦ˤʤޤ

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 \
"ͥåȥΥ͡ॵФΥɥ쥹ꤷޤ" 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
ꤷ IP ɥ쥹ȥۥ̾ϰʲ̤Ǥ
$IPADDR       $FULLDOMAIN    $HOSTNAME

ȤΥ͡ॵФΥɥ쥹ꤷƤ
ʣΥ͡ॵФȤ /$RESOLV ե
ԽƤ

͡ॵФIPɥ쥹ϡ(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 "ȤΥƥˤ PCMCIA 󥹥ȡ뤵Ƥ褦Ǥ \
ͥåȥ 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 "ͥåȥѹ" \
--menu "\nǤ˥ͥåȥ꤬Ƥ褦Ǥ\n\
\nɤѹ򤷤ޤ\n" 17 78 6 \
"Scheme" "³ˡ(DHCP/IP/³)ѹ" \
"Network" "IP³Υͥåȥѹ(³ˡѹʤ)" \
"Dns" "͡ॵФѹ" \
"Initialize" "ߤ˴Ϥᤫꤷľ" \
"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 "³ˡѹ" \
--menu "\nɤˡǥͥåȥ³ޤ\n\
\n" 17 48 4 \
"Dhcp" "DHCP³" \
"Fixed" "IP³" \
"Loopback" "³ʤ" \
"Exit" "ѹʤ" 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 \
"ޤIP³Ѥ꤬ Ƥʤ褦Ǥ\n\
IPѤΥͥåȥԤޤ\n\
(¹ԤƤ⡢netconfig ȤС\n\
ñDHCP ᤻ޤ)" 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

