############################################################################
#
# 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/system.img.ext4 ]
  then
    echo "Error: No $build_dir/system.img.ext4 found!"
    exit 1
  fi

  if [ ! -d $build_dir/system ]
  then
    mkdir $build_dir/system
  fi

  mv $build_dir/system.img.ext4 $build_dir/system

  if [ -e $build_dir/cache.img ]
  then
    mv $build_dir/cache.img -fv $build_dir/cache.img.ext4
  fi

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

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

else

  #
  # Check for system.img.ext4, tomb.img.ext4, cache.img.ext4 and boot.img in original_update  
  #
  if [ -e original_update/system.img.ext4 ]
  then
    echo "Found original_update/system.img.ext4"

    if [ -e original_update/tomb.img.ext4 ]
    then
      echo "Found original_update/tomb.img.ext4"
    fi

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

    if [ -e original_update/cache.img.ext4 ]
    then
      echo "Found original_update/cache.img.ext4"
      echo "Error: No original_update/system.img.ext4 found"
    else
      echo
      echo "Error: No ROMs found under original_update folder!"
    fi

    exit 1
  fi

  if [ -e original_update/boot.img ]
  then
    echo "Found original_update/boot.img"
  else
    echo "Warning: No original_update/boot.img 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/boot.img ]
  then
    echo Copying boot.img ...
    cp original_update/boot.img $build_dir
  fi

fi

echo



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


#
# Compile simg2img if necessary
#

echo
echo "Analyzing system.img.ext4 ..."
sys_bytes=`od -A n -h -j 0 -N 6 $src_dir/system.img.ext4 | sed 's/ //g'`

if [ -e $src_dir/cache.img.ext4 ]
then
  echo "Analyzing cache.img.ext4 ..."
  cache_bytes=`od -A n -h -j 0 -N 6 $src_dir/cache.img.ext4 | sed 's/ //g'`
else
  cache_bytes="000000000000"
fi

if [ -e $hidden_dir/tomb.img.ext4 ]
then
  echo "Analyzing tomb.img.ext4 ..."
  tomb_bytes=`od -A n -h -j 0 -N 6 $hidden_dir/tomb.img.ext4 | sed 's/ //g'`
else
  tomb_bytes="000000000000"
fi




if [ "$sys_bytes" != "000000000000" ] || [ "$cache_bytes" != "000000000000" ] || [ "$tomb_bytes" == "000000000000" ]
then
  echo
  scripts/compile_simg2img
  if [ "$?" == "1" ]
  then
    exit 1
  fi
fi



cd $system_dir

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


#
# Convert system.img.ext4 to ext4
#

if [ "$sys_bytes" == "000000000000" ]
then
  cp $src_dir/system.img.ext4 $system_dir/ext4_system.img

else 

  echo
  echo "Converting system.img.ext4 into a format for unpacking ..."
  tools/samsung_files/simg2img_files/simg2img $src_dir/system.img.ext4 \
    $system_dir/ext4_system.img 

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

rm -rf $system_dir/system.img.ext4


#
# Directory tree
#

if [ ! -e $build_dir/META-INF/com/google/android/update-script ]
then
  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/
fi


#
# Extract files from ext4_system.img
#

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

cd $system_dir

rm -f ext4_system.img 2>/dev/null
if [ "$?" == "1" ]
then
  echo
  echo "WARNING: ext4_system.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_system.img has been fully unpacked into:"
echo "  $system_dir"

base_dir=`pwd`



scripts/check_galaxy_s2x

if [ "$?" == "1" ]
then
  # Check for /preload
  scripts/check_preload_symlinks $base_dir $system_dir $hidden_dir tomb.img.ext4
fi


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

  read do_extract
  echo

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

    if [ "$cache_bytes" == "000000000000" ]
    then
      cp -v $src_dir/cache.img.ext4 $system_dir/ext4_cache.img

    else

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

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

    rm -rf $system_dir/cache.img.ext4


    #
    # 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



cd $build_dir

if [ ! -e boot.img ]
then
  cd ..
  scripts/check_no_bootimg
else
  cd ..
fi



#
# 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!"

