#!/bin/sh

source /usr/bin/utils.sh

actComp="/"

apiActive $actComp "Building CFG Bundle."

actComp="/bundle/cfg"

if [ -d /mnt/ramdisk/bundle/cfg ]; then
    rm -rf /mnt/ramdisk/bundle/cfg
fi

if [ ! -d /mnt/ramdisk/bundle ]
then
    mkdir /mnt/ramdisk/bundle
fi

if [ ! -f /mnt/ramdisk/bundle/capi.app ]
then
    cp /cfg/pfu/capi.app /mnt/ramdisk/bundle
fi


mkdir /mnt/ramdisk/bundle/cfg
mkdir /mnt/ramdisk/bundle/cfg/components
if [ -d "/cfg/brand" ]
then
    mkdir /mnt/ramdisk/bundle/cfg/components/brand
fi
mkdir /mnt/ramdisk/bundle/cfg/components/etc
if [ -d "/cfg/etc/autopool" ]
then
    mkdir /mnt/ramdisk/bundle/cfg/components/etc/autopool
fi
mkdir /mnt/ramdisk/bundle/cfg/components/etc/config
if [ -d "/cfg/etc/health" ]
then
    mkdir /mnt/ramdisk/bundle/cfg/components/etc/health
fi
mkdir /mnt/ramdisk/bundle/cfg/components/opt
mkdir /mnt/ramdisk/bundle/cfg/components/roles
mkdir /mnt/ramdisk/bundle/cfg/components/rs
mkdir /mnt/ramdisk/bundle/cfg/components/scheds
mkdir /mnt/ramdisk/bundle/cfg/components/sfcb
mkdir /mnt/ramdisk/bundle/cfg/components/ssh
mkdir /mnt/ramdisk/bundle/cfg/components/ssl
mkdir /mnt/ramdisk/bundle/cfg/components/sysinfo
mkdir /mnt/ramdisk/bundle/cfg/components/tasks
mkdir /mnt/ramdisk/bundle/cfg/components/users
# requires /mc/utils.sh to handle rebooting correctly
mkdir /mnt/ramdisk/bundle/mc

if [ ! -f /mnt/ramdisk/bundle/update.sh ]
then
    cp /cfg/pfu/update.sh /mnt/ramdisk/bundle
fi

if [ ! -f /mnt/ramdisk/bundle/utils.sh ]
then
    cp /cfg/pfu/utils.sh /mnt/ramdisk/bundle
fi

cp -R /cfg/pfu/cfg/* /mnt/ramdisk/bundle/cfg
if [ -d "/cfg/brand" ]
then
    cp -R /cfg/brand/* /mnt/ramdisk/bundle/cfg/components/brand
fi
cp /cfg/etc/snmpd.conf /mnt/ramdisk/bundle/cfg/components/etc
if [ -d "/cfg/etc/autopool" ]
then
    cp -R /cfg/etc/autopool/* /mnt/ramdisk/bundle/cfg/components/etc/autopool
fi
cp /cfg/etc/config/snmpd.conf /mnt/ramdisk/bundle/cfg/components/etc/config
if [ -d "/cfg/etc/health" ]
then
    cp -R /cfg/etc/health/* /mnt/ramdisk/bundle/cfg/components/etc/health
fi
cp -R /cfg/opt/* /mnt/ramdisk/bundle/cfg/components/opt
cp /cfg/roles/* /mnt/ramdisk/bundle/cfg/components/roles
cp /cfg/rs/* /mnt/ramdisk/bundle/cfg/components/rs
cp /cfg/scheds/* /mnt/ramdisk/bundle/cfg/components/scheds
cp -R /cfg/sfcb/* /mnt/ramdisk/bundle/cfg/components/sfcb
cp /cfg/ssh/sshd_config /mnt/ramdisk/bundle/cfg/components/ssh/sshd_config
cp /cfg/ssl/* /mnt/ramdisk/bundle/cfg/components/ssl
cp /cfg/sysinfo/system.xml /mnt/ramdisk/bundle/cfg/components/sysinfo
cp /cfg/tasks/* /mnt/ramdisk/bundle/cfg/components/tasks
cp /cfg/users/* /mnt/ramdisk/bundle/cfg/components/users
cp /cfg/pfu/mc/utils.sh /mnt/ramdisk/bundle/mc/utils.sh

# We need to grab the release.xml and platform.xml files from the filesystem
# to bring over just in case the receiver ever needs to use it.
# Note that buildmcbundle also does this. So the copies are redundant in the
# case where both buildmcbundle and buildcfgbundle are executed together.

# We want the installed XML files to be the ones that are pushed across,
# not the ones that are sitting in /cfg/pfu/mc.
rm -f /mnt/ramdisk/bundle/mc/release.xml
rm -f /mnt/ramdisk/bundle/mc/platform.xml

cp /app/relinfo/release.xml /mnt/ramdisk/bundle/mc
cp /etc/platform.xml /mnt/ramdisk/bundle/mc

bundleadditions.sh "/" "/mnt/ramdisk/bundle"

touch /mnt/ramdisk/bundle/do.reboot
touch /mnt/ramdisk/bundle/is.cfg
touch /mnt/ramdisk/bundle/is.spiboot

apiOK $actComp

exit 0
