#!/bin/sh
#
# Last modified:  Dec 5, 2006 by DWG
#

#/*****************************************************************************
# FUNCTION- do_copy
#
# Performs a copy (cp) from $1 to $2.   Allow a list of copied items to be abracted
# outside this script in a source-able file.  (./migrate)
#/*****************************************************************************
function do_copy ()
{
    local readonly CP_ARGS=$1
    set -- ${CP_ARGS}
    local CP_SOURCE=$1
    local CP_DEST=$2
    #echo "${RFSDIR}/bin/cp -dRf $CP_SOURCE" 
    #echo "    to $CP_DEST"
    #${RFSDIR}/bin/cp -af $CP_SOURCE $CP_DEST &>${RFSDIR}/dev/null
    ${RFSDIR}/bin/cp -dRpf $CP_SOURCE $CP_DEST &>${RFSDIR}/dev/null
    if [ $? == "0" ]; then
        echo "Migrating Success: $CP_DEST"
    else
        echo "Error migrating: $CP_SOURCE to $CP_DEST"
    fi
}


#/*****************************************************************************
# FUNCTION- create_init_var

# ENVIRONMENT VARIABLES EXPECTED INCLUDE:
#  PWD= The present working directory is the OEM selection to use.
#  RFSDIR= Path to RFS directory (where helper programs can be found)
#  The Ethernet configurations are held in these global arrays:
#    ETHEXISTS= Ethernet interface exists
#    IPADDR=    IP address
#    NETMASK=   Network Mask
#*****************************************************************************/
create_init_vardir ()
{
  local readonly USRDIR=`pwd -P`
  local readonly VARDIR=${USRDIR/${OPTPKG}\/usr/${OPTPKG}\/var}
  local PREVVARDIR=
  local PREVOEMDIR=
  #local MIGFILE=${SELECTED_OEMDIR}/init/migrate
  local MIGFILE=${VARDIR}/migrate

  # Dereference the OEM symbolic link and save the pointed to directory.
  pushd ${PREVOEMSYMLINK} &>${RFSDIR}/dev/null
  PREVOEMDIR=`pwd -P`
  popd &>${RFSDIR}/dev/null
  #debug "OEMDIR=${OEMDIR}"

  PREVVARDIR=${PREVOEMDIR/opt\/pkg\/usr/opt\/pkg\/var}
  
  # MPG:  Delete the existing VARDIR.  This allows us to pull this
  #       file back in from init to reset to factory settings.
  #       Note that prev_oem link will need to be deleted.
  # Note: there is a bug in "boot" sources /usr/../../initialize
  # instead of var/../../initialize so initialize ALWAYS runs.
  if [ -e "${VARDIR}/initialize" ]; then
    ${RFSDIR}/bin/rm -rf "${VARDIR}"
  fi

  # If the OEM selection does not yet have a corresponding VAR file,
  # Create it using the initial image in this directory.
  if [ ! -d ${VARDIR} ]; then
    echo -n "${VARDIR} directory does not yet exist, initializing it now..."
    ${RFSDIR}/bin/mkdir -p ${VARDIR}

    ${RFSDIR}/bin/cp -a ${USRDIR}/init/* ${VARDIR} &>${RFSDIR}/dev/null
    ${RFSDIR}/bin/rm -f ${VARDIR}/initialize

   # make dev stuff
   pushd ${VARDIR}
   ./MAKEDEV
	${RFSDIR}/bin/rm -f ${VARDIR}/MAKEDEV
   popd

    # create the initialized name for the unit
    local HW=$(${RFSDIR}/sbin/ifconfig eth0 | ${RFSDIR}/bin/grep -o "..:..:..:..:..:.." | ${RFSDIR}/usr/bin/tr -d : | ${RFSDIR}/usr/bin/cut -c 7-12)
    . ${VARDIR}/etc/env.global
    echo "${PRODUCT}-${HW}" > ${VARDIR}/etc/HOSTNAME
    
    echo "done."
    
    echo "${VARDIR} directory now exists."

    # Setup the network interface configuration files.
    # We only initialize for Ethernet interfaces that physically exist,
    # and then we only setup the parameters if they have been setup.
    declare -i I
    for ((I=0; I<4; I++)); do
      if [ -n "${ETHEXISTS[$I]}" ]; then
          if [ -z ${IPADDR[$I]} ]; then                  # likely new install?
              ${RFSDIR}/bin/cp ${PREVVARDIR}/etc/sysconfig/network/ifcfg-eth${I} \
                 ${VARDIR}/etc/sysconfig/network/ifcfg-eth${I} &>${RFSDIR}/dev/null
              if [ $? == "0" ]; then 
                  echo "Migrated ${VARDIR}/etc/sysconfig/network/ifcfg-eth${I}"
              else 
                  echo "Error reading ${PREVVARDIR}/etc/sysconfig/network/ifcfg-eth${I}"
              fi
          else
              echo "Creating ifcfg-eth${I} configuration file"
              echo -e "IPADDR=\"${IPADDR[$I]}\"\nNETMASK=\"${NETMASK[$I]}\"\nBROADCAST=\"${BROADCAST[$I]}\"\nNETWORK=\"${NETWORK[$I]}\"" \
              >${VARDIR}/etc/sysconfig/network/ifcfg-eth${I}
         fi
      fi
    done;

    # lets read the migrate file and copy any listed files in it
    if [ -e ${MIGFILE} ]; then
       . ${MIGFILE}    
       ${RFSDIR}/bin/rm -rf ${MIGFILE}
    fi
  fi
}
create_init_vardir
