#!/bin/sh
# info: script for Android-on-Palm on Pre
# version: 0.3.9
# author: k3dar7@gmail.com

#aop_url="http://192.168.100.50"
aop_url="http://android-on-pre.googlecode.com/files"
aop_dir="/media/internal/android"
aop_file="mix.tar.gz"
aop_file1="mix.tar.00"
aop_file2="mix.tar.01"
kernel="uImage.usbnet"

#remix_url="http://192.168.100.50"
remix_url="http://android-on-pre.googlecode.com/files"
remix_file="remixfiles.tar.gz"
mix=$aop_dir/mix

#bootr_url="http://192.168.100.50"
bootr_url="http://cloud.github.com/downloads/k3dar/bootr-nofob"
bootr_file="bootr-0.3-nofob3-5.tar.gz"

if [ "`cat /proc/cpuinfo | grep Hardware | sed 's/.*: //'`" != "Sirloin OMAP3630 board" ]; then
    echo -e "\nOnly Palm Pre is supported. Exiting..."
    exit
fi

mkdir -p $mix $aop_dir/install

check_patch_cmd(){
    patchcmd=/usr/bin/patch
    if [ ! -x $patchcmd ]; then patchcmd=/media/cryptofs/apps/usr/bin/patch; fi
    if [ ! -x $patchcmd ]; then echo "   [ERROR] Command 'patch' not found, install package 'GNU Patch' using Preware or WOSQI and try again. Exiting..."; exit; fi
}

download_bootr(){
    echo -e "\nDownload Bootr..."
    wget $bootr_url/$bootr_file
}

install_bootr(){
        echo " "
        mount -o remount,rw /boot
        tar -xzf $aop_dir/$bootr_file -C /boot
        sh /boot/bootr/bin/install.sh install
}

uninstall_bootr(){
    if [ -x /boot/bootr/bin/install.sh ]; then
        sh /boot/bootr/bin/install.sh uninstall
        mount -o remount,rw /boot
        rm -rf /boot/bootr/
    else
        echo -e "\nBootr is not installed"
    fi
}

download_android(){
    echo -e "\nDownload Android kernel $kernel..."
    wget $aop_url/$kernel
    if [ "`wget -s $aop_url/$aop_file 2>/dev/null||echo error`" != "error" ]; then
        echo -e "\nDownload Android rootfs $aop_file..."
        wget $aop_url/$aop_file
    else
        echo -e "\nDownload Android rootfs part $aop_file1..."
        wget $aop_url/$aop_file1
        echo -e "\nDownload Android rootfs part $aop_file2..."
        wget $aop_url/$aop_file2
    fi
    echo -e "\nTry download $remix_file for remixing..."
    wget $remix_url/$remix_file
}

install_kernel(){
    echo -e "\nInstall $kernel to /boot"
    mount -o remount,rw /boot
    cp $aop_dir/$kernel /boot/$kernel
}

uninstall_kernel(){
    if [ -f /boot/$kernel ]; then
        mount -o remount,rw /boot
        echo -e "\nDelete $kernel from /boot"
        rm /boot/$kernel
    else
        echo -e "\nKernel is not installed"
    fi
}

prepare_ext3(){
    echo -e "\nPrepare $1 for store $3"
    if [ ! -f $aop_dir/$1 ]; then
        dd if=/dev/full of=$aop_dir/$1 bs=1048576 count=$2 2>/dev/null #bs=32768 count=320 ?
        mkfs.ext3 -qF $aop_dir/$1
        mount -o loop $aop_dir/$1 $mix
        umount -d $mix
    else
        echo -e "\n Image $1 exist. Skip creating..."
    fi
}

extract_mix(){
    if [ -f $aop_file ]; then
        echo -e "\nExtract files from $aop_file to $aop_dir..."
        tar -xzvf $aop_dir/$aop_file --no-same-owner
    else
        if [ ! -f mix.tar -a -f $aop_file1 -a -f $aop_file2 ]; then
            echo -e "\nJoin $aop_file1 and $aop_file2 to mix.tar..."
            cat mix.tar.00 mix.tar.01 > mix.tar
        fi
        if [ -f mix.tar ]; then
            echo -e "\nExtract system.ext3 from mix.tar..."
            tar -xf $aop_dir/mix.tar mix/system.ext3 --no-same-owner
            mv $mix/system.ext3 $aop_dir
            prepare_ext3 boot.ext3 10 "Android Boot"
            mount -o loop $aop_dir/boot.ext3 $mix
            mkdir $mix/boot 2>/dev/null
            echo -e "\nExtract other files from mix.tar to boot.ext3..."
            tar -xf mix.tar --exclude system.ext3
            umount -d $mix
        fi
    fi
}

remix_mix(){
    if [ -f $aop_dir/$remix_file ]; then
        echo -e "\nFound $remix_file, unpack to mounted boot and system images..."
        mount_android_ext3
        tar -xzvf $aop_dir/$remix_file
        umount_android_ext3
    else
        echo -e "\n [I] Not found $remix_file, skip unpacking..."
    fi
}

edit_boot_for_sd(){
    mount_android_ext3
    mv -f $mix/init.rc.orig $mix/init.rc 2>/dev/null
    mkdir -p $mix/mnt/sdcard
    ln -sf /mnt/sdcard $mix/sdcard
    ln -sf /sdcard/android/system.ext3 $mix/system.ext3
    ln -sf /sdcard/android/data.ext3 $mix/data.ext3
 mount -o remount,rw /boot
 sed -i 's/#mount/mount/' /boot/sbin/init.android.bootr
 sed -i 's#android/mnt#android/mix#g' /boot/sbin/init.android.bootr
 mount -o remount,ro /boot
    umount_android_ext3
}

mount_android_ext3(){
    if [ ! -d "$mix/system" ]; then
        mount -o loop $aop_dir/boot.ext3 $mix
        mount -o loop $aop_dir/system.ext3 $mix/system
        mount -o loop $aop_dir/data.ext3 $mix/data
    fi
}

umount_android_ext3(){
    if [ -d "$mix/system" ]; then
        umount -d $mix/data
        umount -d $mix/system
        umount -d $mix
    fi
}

delete_useless(){
    rm $aop_dir/$kernel 2>/dev/null
    rm $aop_dir/$bootr_file 2>/dev/null
    rm $aop_dir/$aop_file 2>/dev/null
    rm $aop_dir/$aop_file1 2>/dev/null
    rm $aop_dir/$aop_file2 2>/dev/null
    rm $aop_dir/mix.tar 2>/dev/null
    rm $aop_dir/$remix_file 2>/dev/null
}

delete_android(){
    are_you_sure "Delete Android ext3 images"
    rm $aop_dir/boot.ext3 2>/dev/null
    rm $aop_dir/system.ext3 2>/dev/null
}

delete_data(){
    are_you_sure "Delete data.ext3 with user saved data"
    rm $aop_dir/data.ext3 2>/dev/null
}

are_you_sure(){
    unset answer
    echo -e "\n$1 ? [y/N]"
    read answer
    if [ "$answer" != "y" ]; then
        echo -e "Aborting..."
        exit
    fi
}

tolog(){
    echo -e "$1"
    echo -e "\n$1:" >>$logfile
    $2 >>$logfile
}

install_apk(){
    if [ "`ls $aop_dir/install|grep '.apk'`" != "" ]; then
        echo -e "\nInstall application from $aop_dir/install into Android..."
        mount_android_ext3
        mkdir -p $mix/data/app 2>/dev/null
        mv $aop_dir/install/*.apk $mix/data/app
        umount_android_ext3
    else
        echo -e "\n[I] application for install in $aop_dir/install not found..."
    fi
}

backup_apk(){
    mount_android_ext3
    if [ "`ls $mix/data/app|grep '.apk'`" != "" ]; then
        echo -e "\nBackup application from Android to $aop_dir/backup..."
        mkdir -p $aop_dir/backup 2>/dev/null
        cp $mix/data/app/*.apk $aop_dir/backup
    else
        echo -e "\n[I] User installed application not found..."
    fi
    umount_android_ext3
}

debug_install(){
    logfile=$aop_dir/AoP-debug.txt; rm $logfile
    echo -e "Collect next information for debug to $logfile\n"
    mount_android_ext3
    tolog "Version of AoP.sh" "grep $0 -e version -m1"
    tolog "Palm Build Info" "cat /etc/palm-build-info"
    tolog "Memory size" "grep /proc/meminfo -e MemTotal"
    tolog "CPU board and revision" "grep /proc/cpuinfo -e Hardware -e Revision"
    tolog "CPU frequency and governor" "cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq"
    tolog "MD5 of Android kernel" "md5sum /boot/uImage.usbnet"
    tolog "List of boot.ext3" "ls -l $mix/"
    tolog "List of system.ext3/framework/" "ls -l $mix/system/framework/"
    tolog "List of /boot/uImage*" "ls -l /boot/uImage*"
    tolog "List of /boot/sbin/init*" "ls -l /boot/sbin/init*"
    tolog "List of /media/internal/android/" "ls -l /media/internal/android/"
    tolog "List of /boot/bootr/bin/" "ls -l /boot/bootr/bin/"
    umount_android_ext3
    echo -e "\nPlease attach $logfile to post on Precentral Android-on-Pre thread..."
}

show_help(){
        echo ""
        echo "AoP.sh - simple script for Android-on-Pre on Pre..."
        echo "*** USE ONLY AT YOUR RISK ***"
        echo "Usage: AoP.sh [option]"
        echo "option:"
        echo "  all  - download,remix,install Android and Bootr (to /boot) + apk"
        echo ""
        echo "  download  - download files (to $aop_dir)"
        echo "  install   - install Android kernel and Bootr (to /boot)"
        echo "  uninstall - uninstall Android kernel and Bootr (from /boot)"
        echo "  delete    - delete all Android related files (on $aop_dir)" 
        echo "  clean     - delete only useless files for booting Android (on $aop_dir)" 
        echo ""
        echo "  remix     - unpack $remix_file to mounted Android ext3 images"
        echo "  eboot     - edit $aop_dir/boot.ext3 for sdcard"
        echo ""
        echo "  apk       - install applications from $aop_dir/install into Android"
        echo "  bapk      - backup user applications from Android to $aop_dir/backup"
        echo "  wroot     - connect Android images on $mix for webOS and/or Novacom"
        echo "  uwroot   - disconnect Android images on $mix for webOS and/or Novacom"
        echo ""
        echo ""
        echo "  in-bootr  - install only Bootr (to /boot)"
        echo "  un-bootr  - uninstall only Bootr (from /boot)"
        echo ""
}


case $1 in
    all)
        check_patch_cmd
        download_bootr
        download_android
        prepare_ext3 data.ext3 1024 "Android Data"
        extract_mix
        remix_mix
        install_apk
        install_kernel
        install_bootr
        edit_boot_for_sd
#        delete_useless
        ;;
    download)
        download_bootr
        download_android
        ;;
    install)
        check_patch_cmd
        install_kernel
        install_bootr
        ;;
    uninstall)
        uninstall_bootr
        uninstall_kernel
        ;;
    delete)
        delete_android
        delete_useless
        delete_data
        ;;
    clean)
        delete_useless
        ;;
    remix)
        remix_mix
        ;;
    eboot)
        edit_boot_for_sd
        ;;
    in-bootr)
        check_patch_cmd
        download_bootr
        install_bootr
        ;;
    un-bootr)
        uninstall_bootr
        ;;
    debug)
        debug_install
        ;;
    wroot)
        mount_android_ext3
        ;;
    uwroot)
        umount_android_ext3
        ;;
    apk)
        install_apk
        ;;
    bapk)
        backup_apk
        ;;
    *)
        show_help
        ;;
esac
