############################################################################
#
# Copyright (c) 2012 - dsixda (dislam@rocketmail.com)
#
# Android Kitchen is 100% free.  This script file is intended for personal
# and/or educational use only.  It may not be duplicated for monetary
# benefit or any other purpose without the permission of the developer.
#
############################################################################

#
# This script has one optional parameter:
#
# $1 = folder containing boot.img
#


base_dir=`pwd`

if [ "$1" == "" ]
then
  if [ ! -d WORKING_* ]
  then
    echo Working folder not found!
    exit 0
  fi

  cd WORKING_*
  working_folder=`pwd`

else
  cd $1
  working_folder=`pwd`
fi


echo

if [ -e boot.img ] 
then

  cd $base_dir
  dec_offset=`scripts/get_boot_img_page_size $working_folder`

  if [ "$dec_offset" != "" ]
  then

    if [ "$dec_offset" == "0" ]
    then
      echo "Error: kernel offset (page size) is 0"
      exit 1
    fi

    echo "Kernel found at offset $dec_offset in boot.img"

    # Check for MT65XX
    scripts/check_mt65xx_bootimg $working_folder $dec_offset
    mt65xx=$?

    if [ "$mt65xx" == "1" ]
    then
      type=mt65xx
    else
      type=""
    fi

    scripts/set_kernel_offset_files $dec_offset $type
    exit $?

  else
    echo "Error: kernel offset (page size) not found in boot.img!"
    exit 1
  fi

else
  echo "boot.img not found!"
  cd $base_dir
  exit 1
fi

