############################################################################
#
# 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 optional arguments:
#
# $1 = "no_create" or "create" or "create_working 
#         - must specify no_create if working folder already exists
# $2 = "kernel_only" or "name_given" - if this is specified then $1 is mandatory
#

if [ "$1" == "no_create" ] 
then
  build_dir=`ls | grep -m 1 ^WORKING_`

  if [ ! -e $build_dir/factoryfs.img ]
  then
    echo "Error: No $build_dir/factoryfs.img found!"
    exit 1
  fi

  mkdir $build_dir/system
  mv $build_dir/factoryfs.img $build_dir/system

  if [ -e $build_dir/cache.img ]
  then
    mv $build_dir/cache.img $build_dir/system
  else
    scripts/show_cache_warning cache.img 
  fi

  # Sometimes images have zero permissions set when extracted from TAR archive,
  # making them unreadable!
  chmod -R 777 $build_dir

else

  #
  # Check for factoryfs.img, hidden.img, cache.img and zImage in original_update  
  #
  if [ -e original_update/factoryfs.img ]
  then
    echo "Found original_update/factoryfs.img"
    
    if [ -e original_update/hidden.img ]
    then
      echo "Found original_update/hidden.img"
    fi

    if [ -e original_update/cache.img ]
    then
      echo "Found original_update/cache.img"
    else
      scripts/show_cache_warning original_update/cache.img
    fi

  else
    echo
    echo "Error: No factoryfs.img found under original_update folder!"
    exit 1
  fi

  if [ -e original_update/zImage ]
  then
    echo "Found original_update/zImage"
    scripts/show_rooted_kernel_msg
  else
    echo
    echo "Warning: No original_update/zImage found"
  fi


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

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

  # Kernel is optional
  if [ -e original_update/zImage ]
  then
    echo Copying zImage ...
    cp original_update/zImage $build_dir
  fi

fi

echo


#
# Compile sigm2img
#

scripts/compile_simg2img
if [ "$?" == "1" ]
then
  exit 1
fi



if [ "$1" == "no_create" ]
then
  src_dir=$build_dir/system
  hidden_dir=$build_dir
else
  src_dir=original_update
  hidden_dir=$src_dir
fi

system_dir=$build_dir/system
cd $system_dir

# Get full path
system_dir=`pwd`
cd ../..


#
# Convert factoryfs.img to ext4
#

echo
echo "Converting factoryfs.img into a format for unpacking ..."
tools/samsung_files/simg2img_files/simg2img $src_dir/factoryfs.img \
  $system_dir/ext4_factoryfs.img

if [ ! -e $system_dir/ext4_factoryfs.img ]
then
  echo "Unable to convert factoryfs.img!"
  exit 1
fi

rm -rf $system_dir/factoryfs.img



#
# Directory tree
#

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



#
# Extract files from ext4_factoryfs.img
#

if [ `uname | grep CYGWIN` ]
then
  scripts/show_ext2explore $system_dir ext4_factoryfs.img
else
  scripts/extract_ext3_img $system_dir ext4_factoryfs.img
fi

cd $system_dir

rm -f ext4_factoryfs.img 2>/dev/null
if [ "$?" == "1" ]
then
  echo
  echo "WARNING: ext4_factoryfs.img is in use.  Remove file manually after application is closed."
  echo
fi

cd ..

if [ ! -d system ]
then
  echo
  echo "Error:  No /system folder found!"
  exit 1
fi

if [ ! -d system/framework ]
then
  echo
  echo "Error: No /system/framework folder found!"
  exit 1
fi

if [ ! -d system/app ]
then
  echo
  echo "Error: No /system/app folder found!"
  exit 1
fi

if [ "`ls system`" == "" ]
then
  echo 
  echo "Error: /system folder is empty!"
  echo
  exit 1
fi


cd ..

echo
echo "The file ext4_factoryfs.img has been fully unpacked into:"
echo "  $system_dir"



base_dir=`pwd`


# Check for /preload
scripts/check_preload_symlinks $base_dir $system_dir $hidden_dir hidden.img



# Check for CSC
if [ -e $src_dir/cache.img ]
then
  echo
  echo "Do you wish to extract the Customer Software Customization (CSC)"
  echo -n "from the cache.img (y/n)? (default: y): "

  read do_extract
  echo

  if [ "$do_extract" == "n" ]
  then
    rm -vf $src_dir/cache.img
    echo
  else

    #
    # Convert cache.img to ext4
    #
    echo "Converting cache.img into a format for unpacking ..."
    tools/samsung_files/simg2img_files/simg2img $src_dir/cache.img \
      $system_dir/ext4_cache.img

    if [ ! -e $system_dir/ext4_cache.img ]
    then
      echo "Unable to convert cache.img!"
      exit 1
    fi

    rm -rf $system_dir/cache.img


    #
    # Extract files from ext4_cache.img
    #
    if [ `uname | grep CYGWIN` ]
    then
      scripts/show_linux_reader $system_dir ext4_cache.img
    else
      scripts/extract_ext3_img $system_dir ext4_cache.img
    fi

    cd $system_dir 

    rm -f ext4_cache.img 2>/dev/null
    if [ "$?" == "1" ]
    then
      echo
      echo "WARNING: ext4_cache.img is in use.  Remove file manually after application is closed."
      echo
    fi

    cd ../..
    scripts/unzip_csc $build_dir
  fi
fi



#
# Insert fake boot.img
#
scripts/check_no_bootimg


#
# update-script and other adjustments
#
if [ "$2" != "kernel_only" ] && [ "$2" != "name_given" ]
then
  scripts/fix_update_script 

  # Modify the update-script with features found in Nandroid backup
  scripts/fix_update_script_with_features

  scripts/remove_recovery
fi


echo
echo Listing of $build_dir:
echo
ls -l $build_dir

echo
echo "Finished setting up working folder!"

