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


cd WORKING_*
working_folder=`pwd`
cd META-INF/com/google/android
update_dir=`pwd`

if [ ! -e updater-script ]
then
  echo
  echo "Error: No updater-script found!"
  cd ../../../../..
  exit 0
fi

if [ -e update-binary.orig ]
then
  echo
  echo "Copying original update-binary"
  cp -vf update-binary.orig update-binary

elif [ -e update-binary ]
then
  echo
  echo "Using existing update-binary"
  echo

else

  cd ../../../../..
  scripts/check_galaxy_s2
  sgs2=$?

  if [ "$sgs2" != "1" ]
  then
    scripts/check_galaxy_s2x
    sgs2x=$?
    
    if [ "$sgs2x" != "1" ]
    then
      scripts/check_galaxy_s3_common
      sgs3_common=$?

      if [ "$sgs3_common" != "1" ]
      then
        scripts/check_galaxy_s4_common
        sgs4_common=$?

        if [ "$sgs4_common" != "1" ] && [ -e $working_folder/boot.img ]
        then
          scripts/check_mt65xx_bootimg
          mt65xx=$?
        fi
      fi
    fi
  fi
  
  device_name=`scripts/get_device_name`

  cd $update_dir

  echo
  echo "Copying update-binary ..."

  if [ "$sgs2" == "1" ] || [ "$sgs2x" = "1" ]
  then
    
    cp -vf ../../../../../tools/update_files/sgs2-update-binary update-binary

  elif [ "$sgs3_common" == "1" ] || [ "$sgs4_common" == "1" ]
  then

    cp -vf ../../../../../tools/update_files/sgs3-update-binary update-binary

  elif [ "$device_name" == "GT-I9103" ]
  then
    cp -vf ../../../../../tools/update_files/emmc-not-neon-update-binary update-binary

  else

    grep_emmc=`grep -c "mount(.*,.*,.*,.*)" updater-script`

    if [ $grep_emmc -gt 0 ]
    then

      #
      # Four args for mount
      #
      # e.g. mount("ext3", "EMMC", "/dev/block/mmcblk0p26", "/data");
      # e.g. mount("yaffs2", "MTD", "userdata", "/data");
      #

      if [ "$mt65xx" == "1" ]
      then
        cp -vf ../../../../../tools/update_files/emmc-mt65xx-update-binary update-binary
      else
        cp -vf ../../../../../tools/update_files/emmc-update-binary update-binary
      fi
   
    else

      #
      # Three args for mount, but no MTD or assert
      #
      # mount("ext3", "/dev/block/mmcblk0p25", "/system");
      #
      # OR
      #
      # Three arguments, can use MTD and assert 
      #
      # e.g. mount("MTD", "userdata", "/data");
      #

      cp -vf ../../../../../tools/update_files/mtd-update-binary update-binary

    fi
  fi

fi

cd ../../../../..
