############################################################################
#
# 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 scripts extracts the contents of the boot.img from any folder
#

clear

date_str=`date '+%m%d%y_%H%M%S'`
boot_dir=bootimg_$date_str

echo
echo "Creating folder `pwd`/$boot_dir ..."
mkdir $boot_dir

echo
echo "---> Place boot.img/recovery.img into the folder mentioned above <--"

scripts/press_enter

cd $boot_dir

if [ -e boot.img ] || [ -e recovery.img ]
then

  if [ -e recovery.img ]
  then
    echo "Renaming recovery.img to boot.img (for convenience of scripts)"
    mv -f recovery.img boot.img
  fi

  cd ..
  scripts/check_bootimg_header $boot_dir
  scripts/check_kernel_offset $boot_dir
  res=$?

  if [ "$res" == "0" ]
  then
    kernel_file=extract-kernel.pl
    ramdisk_file=extract-ramdisk.pl
  else
    rm -rf $boot_dir
    exit 0
  fi

  cd $boot_dir
  cp ../tools/extract_boot_files/$kernel_file extract-kernel.pl
  cp ../tools/extract_boot_files/$ramdisk_file extract-ramdisk.pl

  echo
  echo Extracting kernel ...
  ./extract-kernel.pl boot.img 2>/dev/null
      
  if [ ! -e zImage ]
  then
    echo "Error: No zImage found!"
  else
    test_z=`od -A n -j 1 -N 4 zImage | sed 's/ //g'`
    if [ "$test_z" == "" ]
    then
      echo "Error: zImage is empty!"
    fi
  fi

  echo Extracting ramdisk ... 
  ./extract-ramdisk.pl boot.img 2>/dev/null
      
  if [ ! -d boot.img-ramdisk ]
  then
    echo "Error: No ramdisk folder found!"
  fi

  rm boot.img
  rm extract-*.pl
  cd ..
  echo
  echo "Contents of $boot_dir:"
  echo
  echo "`ls -l $boot_dir`"


else
  echo "Error: boot.img or recovery.img not found!"
  echo
  cd ..
  echo "Removing $boot_dir folder"
  rm -rf $boot_dir
  exit 0
fi      
  

