#!/bin/sh

IFACE_COUNTER="0"
CONFIGFILE_NAME="freesbie_settings.conf"
PARTITION=""

set_defaultrouter() {
  #Set the default router if different from rc.conf setting
  echo "Saving the defaultrouter setting"
  DEFAULTROUTER=`netstat -rn | grep default | awk '{print $2;}'`
  DEF_ROUTER_RC=`grep defaultrouter /etc/rc.conf | cut -d\" -f 2`
  [ "${DEFAULTROUTER}" ] &&                           \
  [ "${DEFAULTROUTER}" != "${DEF_ROUTER_RC}" ] &&     \
  echo "DEFAULTROUTER=\"$DEFAULTROUTER\"" >>          \
                        $PARTITION/$CONFIGFILE_NAME
}

set_hostname() {
  #Set the hostname if different from rc.conf setting
  echo "Saving the hostname setting"
  HOSTNAME=`hostname`
  [ "${HOSTNAME}" ] && echo "HOSTNAME=\"$HOSTNAME\"" >> \
                                    $PARTITION/$CONFIGFILE_NAME
}
write_iface_config() {
  #$i is the iface id
  IFACE_ARGS=`ifconfig $i | grep inet\ | awk '  \
    {                                           \
      for (i=1; i<= NF; i++)                    \
        print $i;                               \
    }                                           \
  ' | xargs`
  echo "NETWORK_IFACE${IFACE_COUNTER}=\"${i},${IFACE_ARGS}\"" >>  \
  $PARTITION/$CONFIGFILE_NAME
  IFACE_COUNTER=`expr $IFACE_COUNTER + 1`
}

save_iface_config() {
  #Set the nic informations if not dhcp'ed
  echo "Saving network interfaces settings"
  IFACES_NAME=`ifconfig -a | grep flags | cut -d: -f 1`
  for i in $IFACES_NAME
  do
    IFACE_DHCP_TMP=`ps -x | grep "dhclient -nw $i"`
    if [ "${IFACE_DHCP_TMP}" ]; then
      IFACE_DHCP=`echo ${IFACE_DHCP_TMP} | awk '{if ($5 == "dhclient") print $0;}'`
      if [ ! "${IFACE_DHCP}" ]; then
        write_iface_config
      fi
    fi
  done
}

check_new_or_append() {
  #if the file exists, ask for action
  if [ -f "$PARTITION/$CONFIGFILE_NAME" ]; then
    echo "I found an existing FreeSBIE config file,"
    echo -n "what do you want to do: [O]verwrite, [A]ppend, [Q]uit? "
    read ANS
    if [ "${ANS}" = "" ]; then
      echo "------------------"
      check_new_or_append
    fi
    if [ "${ANS}" = "O" -o "${ANS}" = "O" ]; then
      rm ${PARTITION}/${CONFIGFILE_NAME}
      touch ${PARTITION}/${CONFIGFILE_NAME}
    elif [ "${ANS}" = "Q" -o "${ANS}" = "q" ]; then
      exit 0
    fi
  fi
  echo -n "#FreeSBIE save_settings.sh script launched at: " \
      >> ${PARTITION}/${CONFIGFILE_NAME}
  date  >> ${PARTITION}/${CONFIGFILE_NAME}
}

check_location() {
  echo "FreeSBIE save_settings.sh"
  echo "-------------------------"
  echo "Available partitions:"
  GOOD_PARTS=`mount | grep mnt | egrep "ext2fs|msdosfs|ntfs|ufs" | \
              cut -d\  -f 1`
  MOUNTED_PARTS=`mount | grep mnt | egrep "ext2fs|msdosfs|ntfs|ufs" \
                 | cut -d\  -f 3`
  df -h $GOOD_PARTS | sed "s/Filesystem/Partition /" | \
                      sed "s/Mounted\ on/FreeSBIE/"
  echo $DETAILS
  echo "-------------------------"
  echo "To save your data, specify a partition identifier"
  echo -n "using the string in the FreeSBIE column ([Q]uit): "
  read ANS_LOCATION
  echo "risposta: $ANS_LOCATION"
  [ ! "${ANS_LOCATION}" ] && check_location
  [ "${ANS_LOCATION}" = "Q" -o "${ANS_LOCATION}" = "q" ] && exit 0
  #Check for syntax errors
  IS_OK=1
  for k in $MOUNTED_PARTS
  do
    [ "${k}" = "${ANS_LOCATION}" ] && IS_OK=0
  done
  if [ ! -d ${ANS_LOCATION} -o "${IS_OK}" = "1" ]; then 
    echo "I think you made a syntax error (e.g. mnt -> nmt),"
    echo -n "do you want to retry or exit ([R]etry or a key to exit)? "
    read ANS_RETRY
    if [ "${ANS_RETRY}" = "R" -o "${ANS_RETRY}" = "r" ]; then
      check_location
    else
      exit 0
    fi
  fi
  echo "You selected this partition:"
  df -h $ANS_LOCATION
  echo -n "[C]onfirm, [R]etry or a key to quit: "
  read LAST_ANS
  if [ "$LAST_ANS" = "R" -o "${LAST_ANS}" = "r" ]; then
    check_location
  elif [ "$LAST_ANS" = "C" -o "${LAST_ANS}" = "c" ]; then
    PARTITION=`echo $ANS_LOCATION`
  else
    exit 0
  fi
}

check_location
check_new_or_append
set_defaultrouter
set_hostname
save_iface_config
