#!/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)
#/*****************************************************************************
#do_copy ()
#{
#    local readonly CP_ARGS=$1
#    set -- ${CP_ARGS}
#    local CP_SOURCE=$1
#    local CP_DEST=$2
#    debug "do_copy $CP_SOURCE"
#    debug "     to $CP_DEST"
#    ##cp -af $CP_SOURCE $CP_DEST
#    cp -dRpf $CP_SOURCE $CP_DEST
#    if [ $? == "0" ]; then
#        info "Migrating Success: $CP_DEST"
#    else
#        info "Error migrating: $CP_SOURCE"
#    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`
  #JPT Not loving this, but substitution below not working in ramfs
  #JPT 20 is length of /rootfs/opt/pkg/usr/
  local VARDIR=/rootfs/opt/pkg/var/${USRDIR:20}
  #local VARDIR=${USRDIR/opt\/pkg\/usr/opt\/pkg\/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.
  if [ -L ${PREVOEMSYMLINK} ]; then
    local RESTOREDIR=`pwd`
    cd ${PREVOEMSYMLINK}
    PREVOEMDIR=`pwd -P`
    cd ${RESTOREDIR}
    #debug "OEMDIR=${OEMDIR}"

    PREVVARDIR=/rootfs/opt/pkg/var/${PREVOEMDIR:20}
    #PREVVARDIR=${PREVOEMDIR/opt\/pkg\/usr/opt\/pkg\/var}
  fi

  # 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
    # first lets save the feature keys
    info "Saving feature keys..."
    cp -a ${VARDIR}/etc/sysconfig/features /tmp/
    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
    info "${VARDIR} directory does not yet exist, initializing it now..."
    mkdir -p ${VARDIR}

    info "cp -a $USRDIR/init/* ${VARDIR}"
    cp -a ${USRDIR}/init/* ${VARDIR}
    rm -f ${VARDIR}/initialize

    # make dev stuff
#    local RESTOREDIR=`pwd`
#    pushd ${VARDIR}
#   ./MAKEDEV
#   rm -f ${VARDIR}/MAKEDEV
#   popd

    local HW="unknown"
    # create the initialized name for the unit
    if [ $(grep -c serialnum /proc/cmdline) -eq 1 ]; then 
       HW=$(sed 's/.*serialnum=\([a-zA-Z0-9]*\)\s*.*/\1/' /proc/cmdline)
    fi
    if [ "$HW" == "unknown" ]; then
       HW=$(ifconfig eth0 | grep -o "..:..:..:..:..:.." | tr -d : | cut -c 7-12)
    fi
    if [ -e ${VARDIR}/etc/env.global ]; then
      . ${VARDIR}/etc/env.global
    fi
    echo "AFN-${HW}" > ${VARDIR}/etc/hostname
    #echo "${PRODUCT}-${HW}" > ${VARDIR}/etc/hostname

    info "done."

    touch /rootfs/tmp/nfcms_initialized

    info "${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 [ -e "${PREVVARDIR}/etc/sysconfig/network/ifcfg-eth${I}" ]; then
#            cp ${PREVVARDIR}/etc/sysconfig/network/ifcfg-eth${I} \
#              ${VARDIR}/etc/sysconfig/network/ifcfg-eth${I}
#            info "Migrated ${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} ] && [ -n "${PREVVARDIR}" ]; then
       info "Sourcing migrate script... prev ${PREVVARDIR} next ${VARDIR}"
       source ${MIGFILE}
       rm -rf ${MIGFILE}
    fi
  fi
#}
#create_init_vardir
