#!/bin/sh

. ./config.sh

MODULES="ipfw4 ipfw6 ipsec netgraph usb"

KERN_FILE="/usr/src/sys/i386/conf/${KERNEL_FILE}"

ask_for_ipfw4() {
  IS_PRESENT=`grep IPFIREWALL ${KERN_FILE}`
  if [ "${IS_PRESENT}" != "" ]; then
    echo "IPv4 firewall options are present."
  else
    #Checking ipv4 option
    INET4_PRESENT=`grep INET$ ${KERN_FILE}`
    if [ "${INET4_PRESENT}" = "" ]; then
      echo "options  INET" >> ${KERN_FILE}
    fi
    echo "options  IPFIREWALL" >> ${KERN_FILE}
    echo "options  IPFIREWALL_FORWARD" >> ${KERN_FILE}
    echo "options  IPFIREWALL_DEFAULT_TO_ACCEPT" >> ${KERN_FILE}
    echo "IPv4 firewall options added."
  fi
}

ask_for_ipfw6() {
  IS_PRESENT=`grep IPV6FIREWALL ${KERN_FILE}`
  if [ "${IS_PRESENT}" != "" ]; then
    echo "IPv6 firewall options are present."
  else
    #Checking ipv6 option
    INET6_PRESENT=`grep INET6 ${KERN_FILE}`
    if [ "${INET6_PRESENT}" = "" ]; then
      echo "options  INET6" >> ${KERN_FILE}
    fi
    echo "options  IPV6FIREWALL" >> ${KERN_FILE}
    echo "options  IPV6FIREWALL_DEFAULT_TO_ACCEPT" >> ${KERN_FILE}
    echo "IPv4 firewall options added."
  fi
}

ask_for_ipsec() {
  IS_PRESENT=`grep IPSEC ${KERN_FILE}`
  if [ "${IS_PRESENT}" != "" ]; then
    echo "IPSec options are present."
  else
    echo "# IPSEC support" >> ${KERN_FILE}
    echo "options  IPSEC" >> ${KERN_FILE}
    echo "options  IPSEC_ESP" >> ${KERN_FILE}
    echo "IPSec options added."
  fi
}

ask_for_netgraph() {
  IS_PRESENT=`grep NETGRAPH ${KERN_FILE}`
  if [ "${IS_PRESENT}" != "" ]; then
    echo "NETGRAPH options are present."
  else
    echo "# NETGRAPH support" >> ${KERN_FILE}
    echo "options  NETGRAPH" >> ${KERN_FILE}
    echo "options  NETGRAPH_ASYNC" >> ${KERN_FILE}
    echo "options  NETGRAPH_BPF" >> ${KERN_FILE}
    echo "options  NETGRAPH_BRIDGE" >> ${KERN_FILE}
    echo "options  NETGRAPH_CISCO" >> ${KERN_FILE}
    echo "options  NETGRAPH_ECHO" >> ${KERN_FILE}
    echo "options  NETGRAPH_ETHER" >> ${KERN_FILE}
    echo "options  NETGRAPH_FRAME_RELAY" >> ${KERN_FILE}
    echo "options  NETGRAPH_GIF" >> ${KERN_FILE}
    echo "options  NETGRAPH_GIF_DEMUX" >> ${KERN_FILE}
    echo "options  NETGRAPH_HOLE" >> ${KERN_FILE}
    echo "options  NETGRAPH_IFACE" >> ${KERN_FILE}
    echo "options  NETGRAPH_IP_INPUT" >> ${KERN_FILE}
    echo "options  NETGRAPH_KSOCKET" >> ${KERN_FILE}
    echo "options  NETGRAPH_L2TP" >> ${KERN_FILE}
    echo "options  NETGRAPH_LMI" >> ${KERN_FILE}
    echo "options  NETGRAPH_MPPC_ENCRYPTION" >> ${KERN_FILE}
    echo "options  NETGRAPH_ONE2MANY" >> ${KERN_FILE}
    echo "options  NETGRAPH_PPP" >> ${KERN_FILE}
    echo "options  NETGRAPH_PPPOE" >> ${KERN_FILE}
    echo "options  NETGRAPH_PPTPGRE" >> ${KERN_FILE}
    echo "options  NETGRAPH_RFC1490" >> ${KERN_FILE}
    echo "options  NETGRAPH_SOCKET" >> ${KERN_FILE}
    echo "options  NETGRAPH_SPLIT" >> ${KERN_FILE}
    echo "options  NETGRAPH_TEE" >> ${KERN_FILE}
    echo "options  NETGRAPH_TTY" >> ${KERN_FILE}
    echo "options  NETGRAPH_UI" >> ${KERN_FILE}
    echo "options  NETGRAPH_VJC" >> ${KERN_FILE}
    echo "Netgraph options added."
  fi
}

ask_for_usb() {
  IS_PRESENT=`grep USB ${KERN_FILE}`
  if [ ! "${IS_PRESENT}" = "" ]; then
    echo "USB options are present."
  else
    echo "# USB support" >> ${KERN_FILE}
    echo "device    ohci" >> ${KERN_FILE}
    echo "device    uhci" >> ${KERN_FILE}
    echo "device    usb" >> ${KERN_FILE}
    echo "device    ugen" >> ${KERN_FILE}
    echo "device    uhid" >> ${KERN_FILE}
    echo "device    ukbd" >> ${KERN_FILE}
    echo "device    umass" >> ${KERN_FILE}
    echo "device    ums" >> ${KERN_FILE}
    echo "#SCSI peripheral" >> ${KERN_FILE}
    echo "#Could be USB related :-)" >> ${KERN_FILE}
    echo "device    scbus" >> ${KERN_FILE}
    echo "device    da" >> ${KERN_FILE}
  fi
}

for i in ${MODULES}
do
  echo -n "Do you want ${i} in your kernel (y/n) [y]: "
  read ANSWER
  if [ "${ANSWER}" = "" -o "${ANSWER}" = "y" ]; then
    ask_for_${i}
  fi
done
