#!/bin/sh

# autorun.sh
#
# WonderMedia Linux BSP auto installatopn script 
#
# This script is derived from VT8430 Linux BSP installation script.
#
# This shell script is for ash in busybox. Ash is not as smart as bash.
# Therefore, only the basic shell syntax can be used in ash.
# Please note that a bash shell script may not work in ash.
#
# Written by Vincent Chen <vincentchen@wondermedia.com.tw>
#
# Copyright 2008-2009 WonderMedia Corporation. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY WONDERMEDIA CORPORATION ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL WONDERMEDIA CORPORATION OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.

# get_dir - get file location
# @src_dir
# @filename
# @search_dirs

get_dir ()
{
	for d in $@ ; do
		if [ -e "$d/$2" ]; then
			eval "$1=$d"
			break
		fi
	done
}

src_dir=

source /etc/profile

get_dir src_dir bspinst_api.sh `find $PWD -type d -maxdepth 3` \
	`find /mnt -type d -maxdepth 3`

src_dir=`echo $src_dir | sed 's/\/bin//g'`

if [ -z $src_dir ]; then
	echo "Can't find install source. Abort!"
	exit 0
fi

source $src_dir/bin/bspinst_api.sh
source $src_dir/shellui/runui.sh
source $src_dir/bin/bspinst_addons.sh

bsp_ver_check ()
{
	FILE_VER_1=$1
	FILE_VER_2=$2

	if [ ! -e "$FILE_VER_1" ] || [ ! -e "$FILE_VER_2" ]; then
		echo "bsp_ver_check ... skip"
		return 0
	fi

	MAJOR_1=`cat $FILE_VER_1 | cut -d. -f1`
	MINOR_1=`cat $FILE_VER_1 | cut -d. -f2`
	BUILD_1=`cat $FILE_VER_1 | cut -d. -f3`

	MAJOR_2=`cat $FILE_VER_2 | cut -d. -f1`
	MINOR_2=`cat $FILE_VER_2 | cut -d. -f2`
	BUILD_2=`cat $FILE_VER_2 | cut -d. -f3`

	echo $MAJOR_1 $MINOR_1 $BUILD_1
	echo $MAJOR_2 $MINOR_2 $BUILD_2

	if [ $MAJOR_1 -le $MAJOR_2 ] && [ $MINOR_1 -le $MINOR_2 ] \
		&& [ $BUILD_1 -le $BUILD_2 ]; then
		echo "$FILE_VER_1 <= $FILE_VER_2"
		echo "bsp_ver_check ... ok"
		return 0
	else
		echo "$FILE_VER_1 > $FILE_VER_2"
		echo "bsp_ver_check ... failed"
		exit 0
	fi
}

echo "Using $src_dir/autorun.sh"

export PATH=.:$src_dir/bin:$PATH
envlog.sh $src_dir/env/ > $src_dir/env/envlog.txt

mdev -s

get_var $@

load_config
load_version_config



scenario=$DEFAULT_SCENARIO

bsp_ver_check /VERSION $src_dir/VERSION

#Quiet modprobe fusion
Quiet mount -t proc none /proc
Quiet mount -t tmpfs none /dev/shm

fstype=`cat /proc/mounts |grep \/dev\/root|cut -d\  -f3| head -n 1`
echo "fstype = $fstype"

#add_missing_files
tar zxf $src_dir/tools/mtd-utils*.tgz -C / >/dev/null 
tar zxf $src_dir/tools/zlib*.tgz -C / >/dev/null 

if [ "$fstype" == "yaffs2" ]; then
	return 0
	#pre_install $scenario
else
	#Rem by Jody to remove select_scenario by user, scenario is default
	#select_scenario scenario $TIMEOUT_MENU
	#config_menu_01 $TIMEOUT_MENU
	echo "Install Scenario NO.$scenario"
	#wait_install $TIMEOUT_STARTUP
	run_ui ${src_dir} ${TYPE_UPDATE}
	
	if [ ! -f "$src_dir/$WLOAD_IMAGE" ]; then
    msg_title "CONFIG ERROR!"
    msg_info "Error in VERSION_CONFIG.txt"
    msg "Specified WLOAD_IMAGE not found!"
    msg "value: $WLOAD_IMAGE"
  fi
	if [ ! -f "$src_dir/$KERNEL_IMAGE" ]; then
    msg_title "CONFIG ERROR!"
    msg_info "Error in VERSION_CONFIG.txt"
    msg "Specified KERNEL_IMAGE not found!"
    msg "value: $KERNEL_IMAGE"
  fi
  
	msg_title "WM8650 Upgrading (Loaders & Kernel)"
	
	msg_info "Please VERIFY Settings!"
	
	print_recommend_wload_version
	sleep 10
	msg "WLOAD_IMAGE: $WLOAD_IMAGE"
	msg "KERNEL_IMAGE: $KERNEL_IMAGE"
	msg "if you're really want to upgrade to the versions above"
	msg "just wait; otherwise TURN YOUR DEVICE OFF!"
	msg "Installation will begin in 25 Seconds."
	sleep 22
	
	msg "4..."
	sleep 1
	msg "3..."
	sleep 1
	msg "2..."
	sleep 1
	msg "1..."
	sleep 1
	
	msg_info "DON'T REMOVE INSTALLATION MEDIA..."
	do_install $scenario
fi


netconfig
mkscriptcmd

#if [ -e "$src_dir/bin/bspinst_addons.sh" ]; then
#	source $src_dir/bin/bspinst_addons.sh
	ratio 100
#fi

echo "WonderMedia Android FW installation is complete."

sync

msg_info "PLEASE REMOVE INSTALLATION MEDIA..."
while [ -e $0 ]; do
	read -t 1 key
	if [ $? -eq 0 ]; then
		break
	fi
done

#Quiet wait_reboot $TIMEOUT_EXIT
msg_info "System will reboot in $t seconds"
exit_ui "OK"
reboot -f -n

