#!/bin/sh
# 
if [ "$1" = "prereqs" ]; then
  exit 0
fi


. /scripts/functions
. /conf/tcos.conf
. /conf/tcos-run-functions

quiet=n

# if break=make_init STOP here
maybe_break make_init

# read /tmp/less_ram
if [ "$(cat /tmp/less_ram)" = "0" ]; then
  # have more than TCOS_MIN_RAM
  exit 0
fi


if grep -q bootchartd /proc/cmdline ; then
  # stop bootchart
  /sbin/tcos-bootchartd stop
  mkdir -p /root/tmp/bootchart
  mv /tmp/bootchart/* /root/tmp/bootchart
fi

rm -f /root/sbin/init
cat << EOF > /root/sbin/init
#!/bin/sh

. /scripts/functions

. /conf/initramfs.conf
. /conf/tcos.conf

quiet=n

if grep -q bootchartd /proc/cmdline ; then
 /sbin/daemonize.sh /sbin/tcos-bootchartd start
fi


/scripts/tcos-top/01busybox >/dev/null 2>&1

# start udev again
log_begin_msg "Re-Discovering hardware"
/scripts/init-premount/udev
log_end_msg \$?


run_scripts /scripts/tcos-bottom/

EOF
chmod +x /root/sbin/init




move_file() {
# $1 orig
# $2 dest
 mkdir -p ${2}/$(dirname ${1})
 if [ -f $1 ]; then
  mv ${1} ${2}/$(dirname ${1})
  ln -s ${2}/${1} ${1}
 else
  touch $2/$1
 fi
}

move_file /var/lib/dhcp/dhclient.leases /root
move_file /etc/hostname     /root
move_file /etc/hosts        /root
move_file /etc/resolv.conf  /root
move_file /tmp/progress     /root
move_file /tmp/initramfs.debug /root

move_file /var/log/messages /root
move_file /tmp/less_ram     /root

touch /etc/mtab
move_file /etc/mtab         /root

mkdir -p /root/proc
mkdir -p /root/dev
mkdir -p /root/sys



exit 0
