#!/bin/sh
fail() {
    /bin/echo "Failed"
    /bin/echo "$1"
    /bin/sync
    exec /bin/sh
}

/bin/echo "Welcome to Android Linux!"
/bin/mkdir -m 0755 /proc
/bin/mount -t proc proc /proc
/bin/mkdir -m 0755 /sys
/bin/mount -t sysfs sys /sys

#Configure Devices
/bin/MAKEDEVS

for i in 1 2 3 4 5 6 7 8 9 10 ; do
    if [ -d /sys/block/mmcblk0 ] ; then
        break;
    fi
    /bin/echo "Waiting for SD Card"
    /bin/sleep 1
done

nandparts=0
if [ -d /sys/block/mtdblock1 ] ; then
    nandparts=1
    RM_sysloc=0
    RM_dataloc=0
fi

partition=null

if [ -d /sys/block/mmcblk0 ] ; then
    partition=mmcblk0
fi

if [ -d /sys/block/mmcblk0/mmcblk0p1 ] ; then
    partition=mmcblk0p1
fi
/bin/mkdir -m 0777 /sdcard
chmod 0755 /system
mkdir -m 0755 /data
chown 1000:1000 /data
mkdir -m 0755 /tmp

if [ $partition != null ] ; then
    echo
    echo
    echo
    echo
    echo Hold down Volume Up or DPad
    echo Center to launch installer
    echo
    echo
    echo
    /bin/dosfsck -y /dev/block/$partition 2>&1 1>/bootlog.txt

    /bin/mount -t vfat -o fmask=0000,dmask=0000,rw,flush,noatime,nodiratime /dev/block/$partition /sdcard
    /bin/mkdir -p /sdcard/andboot
    /bin/dmesg > /sdcard/andboot/dmesg

    card=/sdcard/andboot

    PARTITIONED=0
    if [ -d /sys/block/mmcblk0/mmcblk0p2 ] ; then
        PARTITIONED=1
        if [ -d /sys/block/mmcblk0/mmcblk0p3 ] ; then
            PARTITIONED=2
        fi
    fi

    #set defaults
    if [ $nandparts -eq 1 ] ; then
        export RM_sysloc=0
        export RM_dataloc=0
    elif [ $PARTITIONED -eq 2 ] ; then
        export RM_sysloc=1
        export RM_dataloc=1
    elif [ $PARTITIONED -eq 1 ] ; then
        export RM_sysloc=1
        export RM_dataloc=2
    else
        export RM_sysloc=2
        export RM_dataloc=2
    fi

    if [ -f /sdcard/andboot/exports ] ; then
        /bin/sed -i s/^export\ RM_HIDE/#export\ RM_HIDE/ /sdcard/andboot/exports
        . /sdcard/andboot/exports
    fi

    if [ $nandparts -eq 0 ] ; then
        if [ $PARTITIONED -lt 2 ] ; then
            export RM_HIDE_dataloc=1
            export RM_dataloc=2
        fi
        if [ $PARTITIONED -eq 0 ] ; then
            export RM_HIDE_sysloc=1
            export RM_sysloc=2
        fi
    fi

    lastkey=`/bin/readkey -n`
    if [ $lastkey -eq 232 -o $lastkey -eq 115 ] ; then
        if [ ! [ -f $card/androidinstall.tar -o -f $card/androidinstall.tgz ] ] ; then
            export RM_HIDE_install=1
            export RM_HIDE_fixapps=1
        fi
        if [ ! [ -f $card/androidupdate.tar -o -f $card/androidupdate.tgz ] ] ; then
            export RM_HIDE_update=1
        fi
        /bin/replimenu -f /bin/instscripts/start.menu
    fi

    if [ -f /sdcard/andboot/exports ] ; then
        /bin/sed -i s/^export\ RM_HIDE/#export\ RM_HIDE/ /sdcard/andboot/exports
        . /sdcard/andboot/exports
    fi
fi

doLog() {
if [ $nandparts -eq 0 -a [ $RM_sysloc -eq 0 -o $RM_dataloc -eq 0 ] ] ; then
    fail "Can't boot from nand without nand"
fi

if [ $PARTITIONED -eq 0 -a $RM_sysloc -eq 1 ] ; then
    fail "Can't boot from SD without an SD partition"
fi

if [ $PARTITIONED -lt 2 -a $RM_dataloc -eq 1 ] ; then
    fail "Can't put data on SD without an SD partition"
fi

if [ $RM_sysloc -eq 2 ] ; then
    /bin/losetup /dev/block/loop1 $card/system.img
    [ $? -eq 0 ] || fail "Failed to reach system.img on SD Card"
    /bin/e2fsck -y /dev/block/loop1
    /bin/mount -t ext2 -o ro,noatime,nodiratime /dev/block/loop1 /system
    [ $? -eq 0 ] || fail "Failed to mount /system"
elif [ $RM_sysloc -eq 1 ] ; then
    /bin/echo "Using partitioned system"
    /bin/e2fsck -y /dev/block/mmcblk0p2
    /bin/mount -t ext2 -o ro,noatime,nodiratime /dev/block/mmcblk0p2 /system
    [ $? -eq 0 ] || fail "Failed to mount /system"
else
    /bin/mount -t yaffs2 -o ro /dev/block/mtdblock2 /system
fi

if [ $RM_dataloc -eq 2 ] ; then
    if [ ! -f $card/data.img ] ; then
        /bin/echo "Creating a new Data store"
        /bin/dd if=/dev/zero of=$card/data.img bs=1048576 count=256
        [ $? -eq 0 ] || fail "Failed to allocate the storage"
        /bin/mke2fs -F $card/data.img
        [ $? -eq 0 ] || fail "Failed to format the storage"
    fi

    /bin/losetup /dev/block/loop0 $card/data.img
    [ $? -eq 0 ] || fail "Failed to find data.img on SD Card"
    /bin/e2fsck -y /dev/block/loop0
    /bin/mount -t ext2 -o noatime,nodiratime,sync /dev/block/loop0 /data
elif [ $RM_dataloc -eq 1 ] ; then
    /bin/e2fsck -y /dev/block/mmcblk0p3
    /bin/mount -t ext2 -o noatime,nodiratime /dev/block/mmcblk0p3 /data
    [ $? -eq 0 ] || fail "Failed to mount /data"
else
    /bin/mount -t yaffs2 /dev/block/mtdblock3 /data
fi

/bin/mkdir -p -m 0777 /data/tmpcache
/bin/mkdir -m 0777 /cache
/bin/mount /data/tmpcache /cache

/bin/mkdir -p -m 0771 /data/shared_prefs
/bin/chmod 0771 /data/shared_prefs
/bin/chown 1000:1000 /data/shared_prefs
/bin/mkdir -m 0771 /shared_prefs
/bin/chown 1000:1000 /shared_prefs
/bin/mount /data/shared_prefs /shared_prefs

/bin/mount /data/tmpcache /tmp

### Can't bind mount onto an initrd, so must copy important files out to /data for bind mounting
/bin/rm -rf /data/sysfiles
/bin/mkdir -m 0775 /data/sysfiles

### Fix su...
/bin/cp /bin/su /data/sysfiles/su
/bin/chmod 4755 /data/sysfiles/su
/bin/mount --bind /data/sysfiles/su /system/bin/su
/bin/mount --bind /data/sysfiles/su /system/xbin/su
/bin/mount --bind /data/sysfiles/su /bin/su

### Fix ppp...
/bin/mknod -m 0777 /smodem/control p
/bin/cp -a /smodem/* /data/sysfiles
/bin/mount /data/sysfiles /system/etc/ppp
/bin/mount /data/sysfiles /smodem
/bin/chmod 4755 /bin/pppd

if [ -d /data/misc/keystore ] ; then
    /bin/chmod 0777 /data/misc/keystore
fi
}
doLog 2>&1 | /bin/tee -a /bootlog.txt

if [ $partition != null ] ; then
    /bin/cp /bootlog.txt $card
fi
exec /init.android
