#!/bin/bash
#
# Program: Remove the unionfs system from an eee PC
# Author:  John McCreesh jpmcc@users.sf.net
# Version: 26-Dec-07
# Notes:   This has only been tested from Puppy Linux
#          Copyright (c) John McCreesh 2007 and licenced under
#          the GPL V2.
#
SOURCE_DEVICE="/dev/hdc"
SYSTEM_DISK=$SOURCE_DEVICE"1"
SYSTEM_DRIVE="/mnt-system"
USER_DISK=$SOURCE_DEVICE"2"
USER_DRIVE="/mnt-user"
NEW_RAMFS="initramfs-eeepc-kibobo.img"
OLD_RAMFS="initramfs-eeepc.img"
BOOT_DIR=$SYSTEM_DRIVE"/boot"

echo "

eee PC disk conversion utility v1.0

This utility should be run from an eee PC running Puppy Linux.
DO NOT RUN THIS SCRIPT if you have any valuable data on your eee,
OR if you cannot restore the factory software from an external
drive in case of disaster.

If you really want to run this utility, 
enter \"yes\" to continue, anything else to abort."

read ans
if [ "$ans" != "yes" ]
then 
  echo "Aborted at user request"
  exit 1
fi

echo "
Estimated time required 2 minutes.
"

if [ "$(id -u)" != "0" ]; then
   echo "This script must be run as root"
   exit 1
fi

if ! [ -d "$SYSTEM_DRIVE" ]
then
  mkdir $SYSTEM_DRIVE
fi
mount $SYSTEM_DISK $SYSTEM_DRIVE

if ! [ -d "$USER_DRIVE" ]
then
  mkdir $USER_DRIVE
fi
mount $USER_DISK $USER_DRIVE

if ! [ -r "$BOOT_DIR/$OLD_RAMFS" ]
then
  echo "Cannot continue - file $BOOT_DIR/$OLD_RAMFS not found"
  exit 1
fi

work_dir=`mktemp -d`
if [ $? -ne 0 ]
then
  echo "Could not create a temporary work directory"
  exit 1
fi

echo "
Working ... please be patient...
"
cd $work_dir
gunzip < $BOOT_DIR/$OLD_RAMFS | cpio -i

# Start to create the replacement img file
echo "#!/bin/sh
mount -t proc proc /proc

if [ -n \"\$XANDROSBOOTDEBUG\" ]; then
    /bin/busybox sh
    set -x
fi

ROOT=\`cat /proc/cmdline | sed 's/.*root=// ; s/ .*//'\`
#VERSION=\`cat /proc/version | cut -f3 -d\" \"\`

mount -t ext3 -o rw,noatime \$ROOT /mnt
if [ \$? -ne 0 ] ; then
    echo Could not mount OS on \$ROOT. Starting debugging shell....
    /bin/busybox sh
fi

#if [ -n \"\$XANDROSSCAN\" ]; then
#    exec switch_root /mnt-system /sbin/scanuser.sh
#fi

#if [ -n \"\$XANDROSRESTORE\" ]; then
#    exec switch_root /mnt-system /sbin/formatuser.sh
#fi

#if [ -z \"\`grep nosplash /proc/cmdline\`\" ]; then
#    echo -n \"\"
#    cp /mnt-system/boot/startup.fb /dev/fb/0
#fi

#if ! mount -t ext3 -o rw /dev/sda2 /mnt-user; then
#    echo Error mounting user partition. Must run filesystem scan!
#    exec switch_root /mnt-system /sbin/scanuser.sh
#fi

# Factory auto-format functionality
#if [ -f /mnt-user/.autoformat ]; then
#    umount /mnt-user
#    exec switch_root /mnt-system /sbin/formatuser.sh -- --auto
#fi

#insmod /mnt-system/lib/modules/\$VERSION/kernel/fs/unionfs/unionfs.ko > /dev/null

#mount -t unionfs -o dirs=/mnt-user=rw:/mnt-system=ro unionfs /mnt
#if [ \$? -ne 0 ]; then
#    echo Could not mount unionfs. Starting debugging shell....
#    /bin/busybox sh
#fi

#mount --move /mnt-system /mnt/mnt
#umount -l /mnt-user

umount /proc

if [ -n \"\$INIT\" ]; then
    if [ -n \"\$XANDROSBOOTDEBUG\" ]; then
    exec switch_root /mnt \$INIT </mnt/dev/console >/mnt/dev/console
    else
    exec switch_root /mnt \$INIT </mnt/dev/null >/mnt/dev/null
    fi
else
    exec switch_root /mnt /sbin/fastinit \"\$@\" </mnt/dev/console >/mnt/dev/console
fi

echo
echo Init Failed. Starting emergency shell....
/bin/busybox sh" > init

echo "
Working ... please be patient...
"

# Finish creating the replacement ramfs file
find | cpio -H newc -o | gzip -9 > $BOOT_DIR/$NEW_RAMFS
cd ~

rm -rf $work_dir

# Change the grub boot file to point to the new image
sed -i s/$OLD_RAMFS/$NEW_RAMFS/ $BOOT_DIR/grub/menu.lst 

echo "
Working ... please be patient...
"

# Copy the users stuff from partition 2 to 1
cp -a $USER_DRIVE/* $SYSTEM_DRIVE > /dev/null 2>&1

echo "
Working ... please be patient...
"
umount $SYSTEM_DRIVE
umount $USER_DRIVE

# now remove partition 2 and expand partition 1
echo -e 'd
1
d
2
n
p
1


w' | fdisk $SOURCE_DEVICE

echo "
Working ... please be patient...
"

e2fsck -pf $SYSTEM_DISK
resize2fs $SYSTEM_DISK

# Change partition 1 to ext3
echo "
Working ... please be patient...
"
tune2fs -j $SYSTEM_DISK

echo "
Working ... please be patient...
"

tune2fs -c 0 -i 0 $SYSTEM_DISK

echo "
Script finished. Cross your fingers and reboot your eee...
"
exit 0
