#!/bin/bash

#network stuff
function network_screen() {
  need_dependency disk          &&
  need_dependency install_start &&
  nw_main
}

nw_ethernet()  {

# To be considered: Do these need to be here? We have the optional spell menu,
# after all. Maybe semi-intelligently queue those needed? (as in "wlan0" or
# "MODE=dynamic")
  si_queue $(run_dialog --single-quoted --checklist \
             "Here is a list of spells you may need. Select those wanted."    \
             0 0 0                                                            \
 "dhcpcd" "DHCP is a protocol to automatically get IP and routing info" "off" \
 "pcmcia-cs"                    "Drivers for PCMCIA or PC Card support" "off" \
 "wireless_tools"             "Tools to configure wireless connections" "off" \
 "ndiswrapper"       "support for wireless cards with win-only drivers" "off" )

  # configure netconf on the CD (requires a writable /etc) and next
  # copy the configuration to the target.

  netconf &&
  cp -af --parents /etc/sysconfig/network "${TARGET}" &&
  debug_log "network" 2 "Running netconf"               &&

  #TODO make this more intelligent! not quite right question
  if confirm "Do you want to start networking now?"; then

    local ETHDEV
    pushd /etc/sysconfig/network >/dev/null
    for ETHDEV in *.dev ;do
      try_run ifup ${ETHDEV%.dev}
    done                                                        &&

    cp -f /etc/resolv.conf  ${TARGET}/etc/resolv.conf           &&
    debug_log "network" 1 "starting ethernet interfaces"          &&
    fill_dependency network on                                  ||
    fill_dependency network off
    # Okay, we configured it, couldn't start it - mark it as configured anyway

    popd >/dev/null
  else
    debug_log "network" 1 "declined to start ethernet interface"
    fill_dependency network off
  fi
}

nw_ppp()  {

  si_queue $(run_dialog --single-quoted --checklist \
             "Here is a list of spells you may need. Select those wanted."    \
             0 0 0                                                            \
 "dhcpcd" "DHCP is a protocol to automatically get IP and routing info" "off" \
 "pcmcia-cs"                    "Drivers for PCMCIA or PC Card support" "off" \
 "wireless_tools"             "Tools to configure wireless connections" "off" \
 "ndiswrapper"       "support for wireless cards with win-only drivers" "off" \
 "rp-pppoe"     "Roaring Penguin for PPP-over-Ethernet, common for DSL" "off" )
  # TODO: Check why PPPoE is here

  local USERNAME PASSWORD NUMBER PORT
  USERNAME=`inputbox  "Please enter your ISP username"` &&
  PASSWORD=`inputbox  "Please enter your ISP password"` &&
    NUMBER=`inputbox  "Enter ISP phone number without dashes, spaces and parenthesis" "5555555"` &&
      PORT=`inputbox  "Enter serial port device (ttyS0 is the first COM port)" "/dev/ttyS0"` &&

  sed -i $'s\a5555555\a'"$NUMBER"$'\a'        /etc/ppp/chat/isp     &&
  sed -i $'s\a: username\a'": $USERNAME"$'\a' /etc/ppp/chat/isp     &&
  sed -i $'s\aqpassword\a'"q$PASSWORD"$'\a'   /etc/ppp/chat/isp     &&
  sed -i $'s\a/dev/modem\a'"$PORT"$'\a'       /etc/ppp/peers/isp    &&
  cp --parents /etc/ppp/{peers,chat}/isp $FINALFILES                &&


  if confirm "Should a connection be started now?"; then
    try_run pon isp                                &&
    cp -f /etc/resolv.conf  ${TARGET}/etc          &&
    debug_log "network" 1 "starting ppp interface" &&
    fill_dependency network on ||
    fill_dependency network off
  else
    debug_log "network" 1 "declined to start ppp interface"
    fill_dependency network off
  fi

}

nw_pppoe()  {
# FIXME: This is complete bunk. Where does this ever install the settings to the final system?
#        Need to have someone who knows adsl-setup look over this (Bear, 2005-05-21)

  si_queue $(run_dialog --single-quoted --checklist \
             "Here is a list of spells you may need. Select those wanted."    \
             0 0 0                                                            \
 "dhcpcd" "DHCP is a protocol to automatically get IP and routing info" "off" \
 "pcmcia-cs"                    "Drivers for PCMCIA or PC Card support" "off" \
 "wireless_tools"             "Tools to configure wireless connections" "off" \
 "ndiswrapper"       "support for wireless cards with win-only drivers" "off" )

  adsl-setup &&
  debug_log "network" 2 "Setting up adsl via adsl-setup" &&

  if confirm "Start that interface now ?"; then
    try_run adsl-start                              &&
    cp -f /etc/resolv.conf  ${TARGET}/etc           &&
    debug_log "network" 1 "starting adsl interface" &&
    fill_dependency network on ||
    fill_dependency network off
  else
    debug_log "network" 1 "declined to start adsl interface"
    fill_dependency network off
  fi


}

nw_none() {
  run_dialog --msgbox "Source Mage is a source-based distribution
trying to stay pretty up-to-date with the newest versions.
This CD is not intended to be just installed as a static
system, but rather as a starting point for a system
that should be regularly updated. It is advised that you
get sorcery and grimoire updates regularly and download
the package sources. Please check the sorcery manpage for a
description on how to do this manually." 0 0
  fill_dependency network none
}

nw_main()  {

  # netconf complains without this directory, just create it
  mkdir -p /etc/sysconfig/network
  mkdir -p ${TARGET}/etc/sysconfig/network

  # get a hostname (remove /etc/hostname to force reconfigure)
  rm -f /etc/hostname
  netconf --check_hostname

  debug_log "network" 2 "hostname is `cat /etc/hostname`"

  cp -f /etc/hostname ${TARGET}/etc/hostname

  local COMMAND

  COMMAND=$(memory_menu network "Networking Setup" "--nocancel"              \
             "Please select the way this box will connect to the internet"   \
             "Ethernet"                                                      \
             "Ethernet" "By plain network, using e.g. a gateway"             \
             "PPP" "Point-to-Point Protocol is used for modem connections"   \
             "PPPoE" "PPP over Ethernet, usually used for A-DSL and similar" \
             "None" "No internet connection available")                      &&

  case  $COMMAND  in
    Ethernet)  nw_ethernet  ;;
    PPP)  nw_ppp       ;;
    PPPoE)  nw_pppoe     ;;
    None)  nw_none      ;;
  esac &&

  # reset hostname to user specified
  hostname $(cat /etc/hostname)
  [[ -e $TARGET/etc/hosts ]] && # If netkit-base installed it yet
  sed -i "s/^127.0.0.1.*/127.0.0.1	localhost	$(cat /etc/hostname)/" \
    $TARGET/etc/hosts
  true
}
