#!/bin/sh 
#
# things which might be done already somewhere else
# if used in combination with other scripts
#
PATH=/bin:/usr/bin:/sbin:/usr/sbin
export PATH
mount -t proc none /proc
mount -t sysfs none /sys
#
# /dev is expected to be minimal populated here
# rootdev, fb0 (if colored feedback is wanted)
# should be available here
#
# switch off vusb out if it might go on accidently
# because of some charger with id pin grounded
#
echo off > /sys/bus/platform/devices/twl4030_usb/vbus_out

#
# wait with further boot until battery voltage is high
# 
if [ `cat /sys/class/power_supply/bq27000-battery/voltage_now` -lt 3450000 ] ; then
# background to red
  fillfb /dev/fb0 0xff0000 1228800
# required to start charging at low voltages
# otherwise automatic charging might switch the usb current 
# (not the charging current!) to 100mA, so there is no
# charging done
  echo on >/sys/bus/platform/devices/twl4030_bci/lin
  while [ `cat /sys/class/power_supply/bq27000-battery/voltage_now` -lt 3450000 ] 
    do
#  pulse the backlight to have some live sign
#  and operate in some low-power mode, so battery
#  is charged as fast as possible

      echo 100 > /sys/class/backlight/pwm-backlight/brightness 
      sleep 0.3
      echo 0 > /sys/class/backlight/pwm-backlight/brightness 
      sleep 2
    done
# green background
  fillfb /dev/fb0 0x00ff00 1228800
# switch to automatic charging, now the battery voltage
# is high enough
    echo auto >/sys/bus/platform/devices/twl4030_bci/lin
    echo off > /sys/bus/platform/devices/twl4030_usb/vbus_out
fi

echo 100 > /sys/class/backlight/pwm-backlight/brightness 
mkdir /mnt

# wait for mmc
sleep 4
# mount rootfs 
# should be done more sophisticated
# with other scripts
ROOT=/dev/mmmcblkk
INIT=/sbin/init
ROOTFSTYPE=ext3
for arg in `cat /proc/cmdline`
do
  case $arg in 
  root=*)
    ROOT=${arg#root=}
    ;;
  init=*)
    INIT=${arg#init=}
    ;;
  rootfstype=*)
    ROOTFSTYPE=${arg#rootfstype=}
    ;;
  esac
done


while [ ! -e /mnt/$INIT ] 
do 
mount -o ro -t "$ROOTFSTYPE" "$ROOT" /mnt 
sleep 1
done
umount /sys
umount /proc
exec run-init /mnt $INIT "$@" </mnt/dev/console >/mnt/dev/console

# exec chroot /mnt /sbin/init
