#!/bin/sh

CFG_PATH=/mnt/fs/cfg
CFG_HTTP=$CFG_PATH/http
BOOT_ERR_LOG=/var/log/bootlog.err
#
# Welcome
#
if [ -e $CFG_PATH/product ]; then
  PRODUCT=`cat $CFG_PATH/product`
fi

if [ -z $PRODUCT ]; then
  PRODUCT=default
fi

echo
echo "/mnt/fs/init.d/rcInit:"
echo -e "Productinfo: \t\t\t\t["$PRODUCT"]"


echo -en "Setting up hostname...\t\t\t"
hostname galep5m 2>>$BOOT_ERR_LOG
echo "[`hostname`]"

#
# Patch the root fs with files provided in /mnt/fs/patch-root.
#
echo -en "Patching /...\t\t"
if [ -e /mnt/fs/patch-root ]; then
  cp -rf /mnt/fs/patch-root/* / && rm -rf /mnt/fs/patch-root
fi
echo "[done]"

#
# Move all interesting files to /app
#
echo -en "Linking configurable application...\t"
if mv /mnt/fs/app / 2>>$BOOT_ERR_LOG; then
  if [ ! -e /app/init.d/rcNormal ]; then
    echo "[done]"
    echo "* Warning: no /app/init.d/rcNormal provided. (This may cause problems later.)"
    echo
  else
    echo "[done]"
  fi
else
  echo "[error (May cause problems later]"
fi

#
# Link the configuration to /etc/cfg
#
echo -en "Linking configuration...\t\t"
if ln -s $CFG_PATH /etc/cfg 2>>$BOOT_ERR_LOG; then
  echo "[done]"
else
  echo "[error (May cause problems later]"
fi

# Merging libraries...
echo -en "Merging libraries...\t\t\t"
if [ -e /app/sw/lib ]; then
  if ln -s /app/sw/lib/* /lib 2>/dev/null; then
    echo "[done]"
  else
    echo "[skipped: no libs ?]"
  fi
else
  echo "[skipped: no /app/sw/lib]"
fi

export PRODUCT
export CFG_PATH

echo -en "Product specific startup...\t\t"
if [ -e /mnt/fs/init.d/rcInit.$PRODUCT ]; then
  echo "[/mnt/fs/init.d/rcInit.$PRODUCT]"
  /mnt/fs/init.d/rcInit.$PRODUCT
else
  echo "[skipped]"
fi

echo -en "Production...\t\t"
if [ -e /mnt/fs/init.d/rcProduction.$PRODUCT ]; then
  echo "[/mnt/fs/init.d/rcProduction.$PRODUCT]"
  /mnt/fs/init.d/rcProduction.$PRODUCT
else
  echo "[skipped]"
fi
