############################################################################
#
# Copyright (c) 2011 - 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_*

if [ ! -e META-INF/com/google/android/update-script ]
then
  echo "Error: update-script not found!"
  cd ..
  exit 0
fi

cd ..
scripts/data_app_exists

if [ "$?" == 1 ]
then
  echo "update-script already has /data/app content"
  exit 0

else

  cd WORKING_*
  cd META-INF/com/google/android

  echo "Updating update-script with /data/app entries"

  if [ `grep -c "delete DATA:app[ ]*" update-script` == 0 ]
  then
    if [ `grep -c "format .*DATA:" update-script` == 0 ]
    then
      if [ `grep -c "format .*SYSTEM:" update-script` == 0 ]
      then
        sed -i -e '1i delete DATA:app\n' update-script
      else
        sed -i -e 's/format\(.*\)SYSTEM:[ ]*[\n]*/format\1SYSTEM:\n\ndelete DATA:app\n/' update-script
      fi
    else
      #
      # The 'delete DATA:app' command is redundant because you're formatting the DATA partition,
      # but we'll do that in case the user decides later to remove the format command. 
      #
      sed -i -e 's/format\(.*\)DATA:[ ]*[\n]*/format\1DATA:\n\ndelete DATA:app\n/' update-script
    fi
  fi    

  if [ `grep -c "copy_dir PACKAGE:data DATA:" update-script` == 0 ]
  then
    sed -i -e 's/delete DATA:app[ ]*[\n]*/delete DATA:app\ncopy_dir PACKAGE:data DATA:\n/' update-script
  fi

  sed -i -e 's/copy_dir PACKAGE:data DATA:[ ]*[\n]*/copy_dir PACKAGE:data DATA:\nset_perm_recursive 1000 1000 0771 0644 DATA:app\n/' update-script

fi

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

scripts/fix_update_script_blanks
