############################################################################
#
# 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 one optional parameter:
#
# $1 = 'override' - add the busybox info to the update-script no matter what
#    = 'amend'    - use the installbusybox file, which is supported by update-script
#                   - use this only when ROM is being built with update-script
#

#
# Don't add 'installbusybox' script if going to convert to updater-script when 
# buiding ROM; it's too much clutter -- use the installation command directly.
#
# The installation command uses 'run_program' and a set of arguments, which 
# would only work with an updater-script.  On the other hand, the update-
# script uses the 'installbusybox' script, which is executed by run_program 
# but requires no arguments.
#


if [ "$1" == "amend" ]
then
  convert_to_edify=0
else
  convert_to_edify=1
fi

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

echo

if [ -e update-script ]
then

  if [ "$convert_to_edify" == "0" ]
  then

    # Exit if all busybox symlinks already exist in update-script, or a
    # busybox installer script exists in base dir of working folder
    if [ "`grep -c \"symlink busybox\" update-script`" != "0" ] || \
      [ "`grep -m 1 \"busybox ln\" ../../../../*`" != "" ] || \
      [ "`grep -m 1 \"busybox --install\" ../../../../*`" != "" ] 
    then
      cd ../../../../..
      exit 0
    fi
 
    #
    # Remove existing busybox references in update-script
    #
    search_run=`grep -c "run_program.*xbin/busybox --install" update-script`
    search_perm=`grep -c "set_perm.*xbin/busybox" update-script`
    search_symlinks=`grep -c "symlink.*xbin/busybox " update-script` 

    if [ $search_run -gt 0 ] || [ $search_perm -gt 0 ] || [ $search_symlinks -gt 0 ]
    then
      echo "Removing existing references to /system/xbin/busybox in update-script"
      sed -i \
        -e '/set_perm.*xbin\/busybox/d' \
        -e '/symlink.*xbin\/busybox/d' \
        -e '/run_program.*xbin\/busybox --install/d' \
      update-script 
    fi


    echo "Adding installbusybox to working folder"
    cp ../../../../../tools/busybox_files/installbusybox ../../../../

    echo "Adding installbusybox to update-script"

    if [ `grep -c "set_perm_recursive.*SYSTEM:xbin[ ]*$" update-script` != 0 ]
    then
      sed -i -e 's/set_perm_recursive\(.*\)SYSTEM:xbin[ ]*$/set_perm_recursive\1SYSTEM:xbin\nset_perm 0 1000 0755 SYSTEM:xbin\/busybox\nrun_program PACKAGE:installbusybox\n/' update-script

    elif [ `grep -c "init.goldfish.sh" update-script` != 0 ]
    then
      sed -i -e 's/set_perm 0 2000 0550 SYSTEM:etc\/init.goldfish.sh/set_perm 0 2000 0550 SYSTEM:etc\/init.goldfish.sh\nset_perm 0 1000 0755 SYSTEM:xbin\/busybox\nrun_program PACKAGE:installbusybox/g' update-script

    elif [ `grep -c "write_raw_image .*:boot.img BOOT:" update-script` != 0 ]
    then
      sed -i -e 's/write_raw_image \(.*:boot.img\) BOOT:/set_perm 0 1000 0755 SYSTEM:xbin\/busybox\nrun_program PACKAGE:installbusybox\nwrite_raw_image \1 BOOT:/' update-script
    
    else
      echo "set_perm 0 1000 0755 SYSTEM:xbin/busybox" >> update-script      
      echo "run_program PACKAGE:installbusybox" >> update-script
    fi

    if [ `grep -c installbusybox update-script` == 0 ]
    then
      echo "Error: installbusybox not added to update-script!"
    else

      if [ ! -e ../../../../system/bin/busybox ]
      then
        echo "Adding symlink to /system/bin/busybox"
        sed -i -e 's/set_perm 0 1000 0755 SYSTEM:xbin\/busybox/set_perm 0 1000 0755 SYSTEM:xbin\/busybox\nsymlink \/system\/xbin\/busybox SYSTEM:bin\/busybox/g' update-script
      fi
    fi


  ######################################################################
  # The update-script will be converted to updater-script when built
  ######################################################################
  elif [ "$convert_to_edify" == "1" ]
  then

    # Remove the installbusybox script with just a single call in the updater-script (which
    # is not compatible with update-scripts)
    if [ -e ../../../../installbusybox ]
    then
      echo "Removing existing references to BusyBox and installbusybox in update-script"
      sed -i \
        -e '/set_perm.*bin\/busybox/d' \
        -e '/symlink.*bin\/busybox/d' \
        -e '/run_program.*installbusybox/d' \
      update-script 

      if [ -e ../../../../installbusybox ]
      then
        echo "Removing installbusybox"
        rm -f ../../../../installbusybox
      fi

      echo

    elif [[ "$1" != "override" ]] && [[ `grep -c "symlink busybox" update-script` != 0 || `grep -c "symlink /system/xbin/busybox " update-script` != 0 ]]
    then
      echo "update-script already has BusyBox content"

    elif [[ "$1" != "override" ]] && [[ "`grep -m 1 \"busybox ln\" ../../../../* 2>/dev/null`" != "" || "`grep -m 1 \"busybox --install\" ../../../../* 2>/dev/null`" != "" ]] 
    then

      echo "A script containing BusyBox symlinks was detected"
      echo "No changes to update-script required"
   
    else

      echo "Adding BusyBox install command to update-script"

      if [ `grep -c "set_perm_recursive.*SYSTEM:xbin[ ]*$" update-script` != 0 ]
      then
        sed -i -e 's/set_perm_recursive\(.*\)SYSTEM:xbin[ ]*$/set_perm_recursive\1SYSTEM:xbin\nset_perm 0 1000 0755 SYSTEM:xbin\/busybox\nrun_program PACKAGE:system\/xbin\/busybox --install -s SYSTEM:xbin\n/' update-script

      elif [ `grep -c "init.goldfish.sh" update-script` != 0 ]
      then
        sed -i -e 's/set_perm 0 2000 0550 SYSTEM:etc\/init.goldfish.sh/set_perm 0 2000 0550 SYSTEM:etc\/init.goldfish.sh\nset_perm 0 1000 0755 SYSTEM:xbin\/busybox\nrun_program PACKAGE:system\/xbin\/busybox --install -s SYSTEM:xbin\n/g' update-script

      elif [ `grep -c "write_raw_image .*:boot.img BOOT:" update-script` != 0 ]
      then
        sed -i -e 's/write_raw_image \(.*:boot.img\) BOOT:/set_perm 0 1000 0755 SYSTEM:xbin\/busybox\nrun_program PACKAGE:system\/xbin\/busybox --install -s SYSTEM:xbin\nwrite_raw_image \1 BOOT:/' update-script
      
      else
        echo "set_perm 0 1000 0755 SYSTEM:xbin/busybox" >> update-script      
        echo "run_program PACKAGE:system\/xbin\/busybox --install -s SYSTEM:xbin" >> update-script
      fi

      if [ `grep -c "busybox --install -s" update-script` == 0 ]
      then
        echo "Error: BusyBox not added to update-script!"
      else

        if [ ! -e ../../../../system/bin/busybox ]
        then
          echo "Adding symlink to /system/bin/busybox"
          sed -i -e 's/set_perm 0 1000 0755 SYSTEM:xbin\/busybox/set_perm 0 1000 0755 SYSTEM:xbin\/busybox\nsymlink \/system\/xbin\/busybox SYSTEM:bin\/busybox/g' update-script
        fi
      fi
    fi

  fi

else
  echo "Error: No update-script found!"
fi

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

scripts/fix_update_script_blanks

