############################################################################
#
# Copyright (c) 2013 - 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 two arguments:
#
# $1 = file name (mandatory)
# $2 = "kernel_only" or "name_given" (optional)
#


update_file=$1
 
echo "Selected $update_file"

# Trim spaces in file name
update_file2=`echo $update_file | tr ' ' '_'`

if [ "$update_file" != "$update_file2" ]
then
  echo "Renaming to $update_file2"
  mv "original_update/$update_file" original_update/$update_file2
  update_file=$update_file2
fi

#
# Working folder name
#
build_dir=`scripts/set_working_folder_name $2` 


#
# Creating folder structure
#

echo
echo "Creating working folder $build_dir ..."
mkdir $build_dir

echo
echo "Extracting ROM from TAR file ..."

tar xvf original_update/$update_file -C $build_dir -x factoryfs.img cache.img modem.bin \
      zImage factoryfs.rfs cache.rfs PDA.tar.md5 CSC.tar.md5 PHONE.tar.md5 PDA.tar CSC.tar \
      PHONE.tar system.img system.img.ext4 system.rfs cache.img.ext4 boot.img csc.rfs \
      system hidden.img tomb.img.ext4 2>/dev/null

cd $build_dir

echo

if [ -e factoryfs.img ]
then

  if [ -e zImage ]
  then
    cd ..
    scripts/show_rooted_kernel_msg
    cd $build_dir
  fi

  cd ..
  scripts/simg_files_to_working_folder no_create $2
  exit $?

elif [ -e system.img ]
then

  cd ..
  scripts/img_files_to_working_folder no_create $2
  exit $?

elif [ -e system.img.ext4 ]
then

  cd ..
  scripts/simg_ext4_files_to_working_folder no_create $2
  exit $?

elif [ -e factoryfs.rfs ]
then

  if [ -e zImage ]
  then
    cd ..
    scripts/show_rooted_kernel_msg
    cd $build_dir
  fi

  cd ..
  scripts/rfs_files_to_working_folder no_create $2
  exit $?


elif [ -e system.rfs ]
then

  cd ..
  scripts/rfs_files_to_working_folder no_create $2
  exit $?

elif [ -e PDA.tar ]
then

  cd ..
  scripts/sgs_tar_files_to_working_folder no_create $2
  exit $?

elif [ -e PDA.tar.md5 ]
then

  cd ..
  scripts/sgs_tarmd5_files_to_working_folder no_create $2
  exit $?


elif [ -d system ]
then

  mkdir -p META-INF/com/google/android
  echo
  echo "Adding an update-script ..."
  cp ../tools/update_files/update-script META-INF/com/google/android/

  cd ..
  if [ "$2" != "kernel_only" ] && [ "$2" != "name_given" ]
  then

    scripts/fix_update_script 

    # Modify the update-script with features found
    scripts/fix_update_script_with_features
    
    scripts/remove_recovery
  fi
    
  exit 0

else
  echo "Error: No image files identified in TAR archive!"
  echo 
  cd ..
  exit 1
fi


