#!/bin/bash
set -e
# Check if we can do it
echo Checking environment
if [ "$(whoami)" != "root" ]; then
	echo You must run this script as root
	exit 0
fi

if [ "$(cat /proc/device-tree/banner-name | grep 'OLPC 4')" != "" ]; then
	XO=xo4
elif [ "$(cat /proc/device-tree/banner-name | grep 'OLPC 1')" != "" ]; then
	XO=xo175
else
	echo This script will only run on an ARM XO laptop
	exit 0
fi

if [ "$XO" = "xo4" ]; then
    FW1="$(cat /proc/device-tree/openprom/model| awk '{print $2}' | cut -c 1-3)"
    FW2="$(cat /proc/device-tree/openprom/model| awk '{print $2}' | cut -c 4-5)"
    [ "$FW1" != "Q7C" -o "$FW2" -lt 04 ] && \
    echo You need to be runing a SMP compatible firmware, q7c04 or newer. \
    && exit 0
fi

OSVER=$(cat /etc/olpc-release | cut -f 1 -d ' ' | tr -d '.')
if [ "$OSVER" -lt 1323 -a "$XO" = "xo4" ]; then
	echo You need to be running an OLPC build, 13.2.3 or newer
	exit 0
fi
if [ "$OSVER" -lt 1320 -a "$XO" = "xo175" ]; then
    echo You need to be running an OLPC build, 13.2.0 or newer
    exit 0
fi

if [ -d /bootpart/boot/alt ]; then 
	echo You already have an alternative OS
	echo Will not install FatDog-ARM_XO
	exit 0
fi

if [ ! -f fd-arm.sfs ]; then
	echo Downloading the fd-arm.sfs file
	wget -c http://ftp.nluug.nl/ibiblio/distributions/fatdog/arm/releases/beta3/fd-arm.sfs
	sync
fi

echo Checking fd-arm.sfs integrity
if [ "$(md5sum fd-arm.sfs | cut -f1 -d ' ')" != "6cebaf377e0e8cf87ce43eadc80a2dc3" ]; then
	echo Please re-download  the fd-arm.sfs file
	echo It is corrupted and deleted
	rm -f fd-arm.sfs
	exit 0
fi

echo Installing SFS ...
mkdir -p DEV
[ "$XO" = "xo4" ] && mount /dev/disk/mmc/mmc1p2 DEV/
[ "$XO" = "xo175" ] && mount /dev/disk/mmc/mmc2p2 DEV/
rm -f DEV/fd-arm.sfs
mv fd-arm.sfs DEV/
sync
umount DEV
rm -rf DEV

echo Getting the kernel and initrd
if [ ! -f "$XO"-kernel.tar.gz ]; then
	echo Downloading the "$XO" kernel
	wget -c http://ftp.nluug.nl/ibiblio/distributions/fatdog/arm/releases/beta3/"$XO"-kernel.tar.gz
	sync
fi
mkdir -p /bootpart/boot/alt
tar -C /bootpart/boot/alt/ -xf "$XO"-kernel.tar.gz --strip-components 2 "$XO"/boot/vmlinuz.* 
tar -C /bootpart/boot/alt/ -xf "$XO"-kernel.tar.gz --strip-components 2 "$XO"/boot/initrd.*
mv /bootpart/boot/alt/vmlinuz.* /bootpart/boot/alt/vmlinuz
mv /bootpart/boot/alt/initrd.* /bootpart/boot/alt/initrd.img
sync

for B in /bootpart/boot /boot  
do
 sed '/" "  to boot-file/c\
   alternate?  if\
      visible\
      " waitdev=3 basesfs=device:mmcblk0p2:\/fd-arm.sfs"  to boot-file\
   else\
      " "  to boot-file\
   then'  $B/olpc.fth > $B/olpc.fth.new
 sed -i '/for\ android/d' $B/olpc.fth.new
 mv $B/olpc.fth $B/olpc.fth.orig
 mv $B/olpc.fth.new $B/olpc.fth
 if [ "$(diff $B/olpc.fth.orig $B/olpc.fth)" = "" ]; then
     sed '/ramdisk/ 	a\
	alternate?  if\
	visible\
	" waitdev=3 basesfs=device:mmcblk0p2:\/fd-arm.sfs"  to boot-file\
	then' $B/olpc.fth > $B/olpc.fth.new
     mv $B/olpc.fth.new $B/olpc.fth
 fi
done
echo
echo Done!
echo Restart your XO holding the \"O\" gamekey
echo to boot FatDog-ARM
