#!/sbin/sh
export PATH=/sbin:/system/sbin:/system/bin
/sbin/busybox mount -t sysfs sysfs /sys
/sbin/busybox mount -t proc proc /proc
/sbin/busybox chmod -R 755 /system/sbin
/sbin/busybox mv /sbin/busybox /system/sbin
/sbin/busybox chmod 755 /system/sbin/busybox
/system/sbin/busybox --install -s /system/sbin
rm /sbin/sh
chown 0.0 /system/sbin/su
chmod 6755 /system/sbin/su
mount -t tmpfs tmpfs /dev
mkdir /dev/pts
mkdir /dev/log
mkdir /dev/input
mount -t devpts devpts /dev/pts
mount -t debugfs debugfs /sys/kernel/debug
mknod -m a=rw /dev/null c 1 3
mknod -m a=rw /dev/zero c 1 5
mknod -m a=rw /dev/full c 1 7
mknod -m a=rw /dev/random c 1 8
mknod -m a=rw /dev/urandom c 1 9
mknod -m a=rw /dev/kmsg c 1 11
mknod -m a=rw /dev/tty0 c 4 0
mknod -m a=rw /dev/tty1 c 4 1
mknod -m a=rw /dev/tty c 5 0
mknod -m a=rw /dev/ptmx c 5 2
mknod -m a=rw /dev/log/system c 10 19
mknod -m a=rw /dev/log/radio c 10 20
mknod -m a=rw /dev/log/events c 10 21
mknod -m a=rw /dev/log/main c 10 22
mknod -m a=rw /dev/input/event0 c 13 64
mknod -m a=rw /dev/input/event1 c 13 65
mknod -m a=rw /dev/input/event2 c 13 66
mknod -m a=rw /dev/input/event3 c 13 67
mknod -m a=rw /dev/input/event4 c 13 68
mknod -m a=rw /dev/input/event5 c 13 69
mknod -m a=rw /dev/input/event6 c 13 70
mknod -m a=rw /dev/input/event7 c 13 71

/system/sbin/ge0 &
GE0_PID=$!
/system/sbin/ge4 &
GE4_PID=$!
for i in 1 2 3 4 5 ; do
  if [ -e /.pressed_pwr ] ; then
    break
  fi
  if [ -e /.pressed_home ] ; then
    touch /recovery
    break
  fi
  sleep 1
done
kill $GE0_PID
kill $GE4_PID
kill `ps | grep "busybox hexdump" | awk '{print $1}'`
rm -f /.pressed_*

umount -l /sys/kernel/debug
umount -l /sys
umount -l /dev/pts
umount -l /dev
umount -l /proc
if [ -e /recovery ] ; then
  rm /default.prop
  mv /default.prop.recovery /default.prop
  rm /init.rc
  mv /init.rc.recovery /init.rc
  rm /init.org
  rm /init.goldfish.rc
  rm /init.qcom.rc
  rm /init.qcom-pre.sh
  rm /init.target.rc
  rm /sbin/_su_
  rm /init
  mv /init.org.recovery /init
else
  rm /default.prop.recovery
  rm /init.rc.recovery
  rm /init.org.recovery
  rm -Rf /etc
  rm -Rf /res
  rm -Rf /system/bin /system/lib
  rm /sbin/recovery
  rm /sbin/recovery_kit
  mv /sbin/_su_ /sbin/su
  rm /init
  mv /init.org /init
  rm -Rf /system/sbin
fi
exec /init
