############################################################################
#
# 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 four mandatory arguments:
#
# $1 = base directory of the kitchen
# $2 = path to system folder under working directory
# $3 = path to img file
# $4 = name of img file (e.g. hidden.img or tomb.img.ext4)
#


base_dir=$1
system_dir=$2
hidden_dir=$3
img_file=$4


######################################################################
# Check for symlinks to /preload/symlink/system/app under /system/app
######################################################################

cd $system_dir/app

# First check for *.apk and *.odex with symlink to preload/symlink/system/app
# and delete them (this works for Linux / OS X)

preload_links=( `find . -type l -exec ls -l {} \; | grep /preload/symlink/system/app | sed 's/.* \/preload\/symlink\/system\/app\/\(.*\)/\1/g'` )

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

  num_preload_links=${#preload_links[@]}

  echo 
  echo -n "Under /system/app, removing <<$num_preload_links>> symlinks to APK and ODEX files in /preload/symlink/system/app "
  for item in ${preload_links[@]} 
  do
    echo -n "."
    rm -f $item  
  done

  use_hidden=1

else

  # Check if the number of odex files outnumbers number of apks (this works for Cygwin)

  num_odex_app=`find . | grep -c "\.odex$"`
  num_apk_app=`find . | grep -c "\.apk$"`

  if [ $num_odex_app -gt $num_apk_app ]
  then
    echo
    echo "The /system/app folder is missing some APK (and maybe some ODEX) files."

    use_hidden=1

   elif [ `uname | grep CYGWIN` ]
   then

    # Check for versions higher than Gingerbread/ICS (this is a workaround for Cygwin)

    cd $system_dir
    if [ "`grep -c ro.build.version.release=2 build.prop`" == "0" ] && \
        [ "`grep -c ro.build.version.release=4.0 build.prop`" == "0" ]
    then
      echo
      echo "The /system/app folder is potentially missing some APK/ODEX files"

      use_hidden=1
    fi

   fi
fi

cd $base_dir

if [ "$use_hidden" == "1" ]
then
  
  echo
  echo "These are probably found in $img_file"

  if [ ! -e $hidden_dir/$img_file ]
  then
    echo
    echo "--> Place $img_file under the '$hidden_dir' folder <--"
    echo "    (You can also ignore this step)"
    echo
    
    scripts/press_enter

    if [ ! -e $hidden_dir/$img_file ]
    then
      echo "Skipping $img_file"
    fi
  fi


  #
  # $img_file unpacking
  #

  if [ -e $hidden_dir/$img_file ]
  then

    working_dir=$system_dir/..
    hidden_dir2=$working_dir/hidden
    mkdir $hidden_dir2
    cd $hidden_dir2
    hidden_dir2=`pwd`
    cd $base_dir

    echo
    echo "Converting $img_file into a format for unpacking ..."
    tools/samsung_files/simg2img_files/simg2img $hidden_dir/$img_file \
      $hidden_dir2/ext4_hidden.img

    if [ ! -e $hidden_dir2/ext4_hidden.img ]
    then
      echo "Unable to convert $img_file!"
      exit 1
    fi

    if [ "$hidden_dir" != "original_update" ]
    then
      rm -f $hidden_dir/$img_file
    fi

    #
    # Extract files from ext4_hidden.img
    #

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

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

    echo

    #
    # Check if preload files are found in $img_file
    #
    if [ -d $hidden_dir2/symlink/system/app ] 
    then
      num_preload_apk=`find $hidden_dir2/symlink/system/app | grep -c "\.apk$"`
      num_preload_odex=`find $hidden_dir2/symlink/system/app | grep -c "\.odex$"`

      num_preload_total=$(($num_preload_apk + $num_preload_odex))

      if [ $num_preload_total -gt 0 ]
      then
        echo "Found <<$num_preload_total>> APK and ODEX files under $img_file"
        mkdir -p $working_dir/preload/symlink/system
        mv $hidden_dir2/symlink/system/app $working_dir/preload/symlink/system/ 

        if [ "$num_preload_links" != "" ]
        then
          if [ $num_preload_links != $num_preload_total ]
          then
            echo "Warning: This number does not equal the number of symlinks found under /system/app earlier!"
          else
            echo "This number matches the number of symlinks found under /system/app earlier"
          fi
        fi


        #
        # Closing comments about /preload stuff
        #

        device_name=`scripts/get_samsung_variant`

        if [ "$device_name" == "" ]
        then
          echo
          echo "WARNING: Device name has not been defined under /tools/edify_defs!"
          echo
        else
          preload_mnt=`scripts/get_edify_def_val $device_name preload_mnt`

          if [ "$preload_mnt" != "UNKNOWN" ]
          then
            echo
            preload_mnt=`echo $preload_mnt | sed -e 's/\\\\//g'`

            echo "The mount point for /preload has already been defined for this"
            echo "device as $preload_mnt under /tools/edify_defs/$device_name. "
            echo "Good."
          else
            echo
            echo "Warning: No /preload mount point ('preload_mnt') was defined under"
            echo "tools/edify_defs/$device_name! Please ensure this is specified before"
            echo "proceeding to next stage (press Enter when done or if you want to"
            echo "skip this step)."
            echo
            scripts/press_enter
          fi
        fi

        echo 
        echo "NOTE: If you wish to de-odex this ROM, you will need to move the APK"
        echo "and ODEX files from the /preload apps folder to /system/app before you"
        echo "de-odex them. The kitchen will automatically handle this."
        echo

        scripts/press_enter

      else
        echo "Warning: No APK/ODEX files found in $img_file!"
      fi
    else
      echo "Warning: No /preload/symlink/system/app folder found in $img_file!"
    fi

    echo
    echo

    rm -rf $hidden_dir2
  fi

elif [ -e $hidden_dir/$img_file ]
then
  echo
  echo "Ignoring $img_file"

  if [ "$hidden_dir" != "original_update" ]
  then
    rm -f $hidden_dir/$img_file
  fi
fi




cd $base_dir


