############################################################################
#
# Copyright (c) 2012 - 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.
#
############################################################################


if [ "$1" == "show_help" ]
then

  clear
  echo
  echo "-----------------------------------------------------------------"
  echo  
  echo "The BusyBox Run-Parts feature allows the device to run scripts"
  echo "from the /system/etc/init.d folder during a reboot. "
  echo 
  echo "Normally the script names start with a number to indicate the "
  echo "order in which they will be executed."
  echo
  echo "-----------------------------------------------------------------"
  echo

  echo -n "Proceed with Run-Parts feature (y/n)? (default: y): "
  read do_runparts

  if [ "$do_runparts" == "n" ]
  then
    exit 0
  fi


elif [ "$1" == "for_apps2sd" ]
then

  echo
  echo "Optional: You can decide to add the BusyBox Run-Parts method for apps2sd."
  echo "          This option allows you to run scripts under /system/etc/init.d,"
  echo "          and not just those for a2sd."
  echo
  echo -n "Do you want to implement the BusyBox Run-Parts method (y/n)? (default: n): "
  read do_runparts

  if [ "$do_runparts" != "y" ]
  then
    exit 0
  fi

fi



cd WORKING_*

su_path=`find . -name su`

if [ "$su_path" == "" ]
then
  echo "'su' binary not found under working folder!"
  echo "You must root your ROM first."
  cd ..
  exit 0
fi



#
# Create the folder
#

if [ ! -d system/etc/init.d ]
then
  echo 
  echo "WARNING: You have not yet created the folder /system/etc/init.d"
  echo
  echo "Creating /system/etc/init.d folder ..."
  mkdir -p system/etc/init.d
  touch system/etc/init.d/placeholder
fi


cd ..


scripts/check_galaxy_s_and_s2
is_gs=$?


#
# Samsung Galaxy S2 or equivalent
#
if [ "$is_gs" == "1" ]
then

  device=`scripts/get_build_prop_val ro.product.device`

  cd WORKING_*
  test_initd=`grep -c "set_perm.*SYSTEM:etc/init.d" META-INF/com/google/android/update-script`
  cd ..

  if [ $test_initd != 0 ]
  then
    echo "Busybox Run-Parts already added!"
    exit 0
  fi

  cd WORKING_*
  if [ ! -d system/etc/init.d ]
  then
    echo "No /system/etc/init.d folder detected!"
    cd ..
    exit 0
  fi
  cd ..

  echo 
  echo "Because this device ($device) does not have a real boot.img with an init.rc,"
  echo "we have to instead use a custom kernel (zImage) that has busybox run-parts"
  echo "support built into it."
  echo
  echo "If you are using a custom kernel AND you know it supports busybox run-parts"
  echo "(/etc/init.d scripts), then all you need to do now is to set the appropriate"
  echo "permissions in the update-script.  If you are using a different kernel, then"
  echo "merely setting the permissions will not help."
  echo 
  echo -n "Do you wish to set the /system/etc/init.d permissions (y/n)? (default: y): "
  read do_runparts

  if [ "$do_runparts" == "n" ]
  then
    echo "Cancelled"
    exit 0
  fi

  scripts/add_initd_perm_to_update_script

else
  scripts/ensure_boot_extracted

  if [ -d BOOT-EXTRACTED ]
  then
    if [ -d WORKING_* ]
    then
      echo
    else
      exit 0
    fi
  else
    exit 0
  fi

  if [ -d BOOT-EXTRACTED/boot.img-ramdisk ]
  then
    cd BOOT-EXTRACTED/boot.img-ramdisk
  else
    echo "Error: BOOT-EXTRACTED/boot.img-ramdisk folder not found!"
    exit 0
  fi

  test_property=`grep -c "on property:cm.filesystem.ready=1" init.rc`
  test_start=`grep -c "start sysinit" init.rc`
  test_service=`grep -c "service sysinit" init.rc`
  test_cm_sysinit=`grep -c "exec /system/bin/sysinit" init.rc`

  if [ "$test_start" != "0" ] && [ "$test_service" != "0" ]
  then
    echo "Busybox Run-Parts already implemented in init.rc!"
    cd ../..
    echo
    echo "Removing BOOT-EXTRACTED folder ..."
    rm -rf BOOT-EXTRACTED
    scripts/add_initd_perm_to_update_script
    exit 0

  elif [ "$test_cm_sysinit" != "0" ]
  then
    echo "CyanogenMod sysinit for /etc/init.d is already enabled in init.rc!"
    cd ../..
    echo
    echo "Removing BOOT-EXTRACTED folder ..."
    rm -rf BOOT-EXTRACTED
    scripts/add_initd_perm_to_update_script
    exit 0

  else

    cd ../..
    scripts/add_busybox

    #cd WORKING_*
    #bb_path=`find . -name busybox | grep -m 1 busybox | sed -e 's/\.//g' -e 's/\//\\\\\//g'`
    #cd ..

    cd BOOT-EXTRACTED/boot.img-ramdisk


    if [ `grep -c "start a2sd" init.rc` != 0 ]
    then
      echo
      echo "Ensuring 'start sysinit' is in init.rc in ramdisk ..."

      sed -i -e 's/start a2sd/start sysinit/g' init.rc    

      echo
      echo "Ensuring 'service sysinit' is in init.rc in ramdisk ..."

      #sed -i -e 's/service a2sd[ ]*\/system\/bin\/logwrapper[ ]*\/system\/bin\/sh[ ]*\/system\/bin\/a2sd/service sysinit \/system\/bin\/logwrapper '"$bb_path"' run-parts \/system\/etc\/init.d/' init.rc
      sed -i -e 's/service a2sd[ ]*\/system\/bin\/logwrapper[ ]*\/system\/bin\/sh[ ]*\/system\/bin\/a2sd/service sysinit \/system\/bin\/logwrapper \/system\/xbin\/busybox run-parts \/system\/etc\/init.d/' init.rc

    else

      echo
      echo Modifying init.rc in ramdisk ...

      if [ "$test_start" == "0" ] && [ "$test_property" == "0" ]
      then
        sed -i -e '0,/class_start /s//start sysinit\n\n    class_start /' init.rc
     
      elif [ "$test_start" == "0" ] && [ "$test_property" != "0" ]
      then
        #
        # If a2sd was present before
        #
        sed -i -e 's/on property:cm.filesystem.ready=1/start sysinit\n    on property:cm.filesystem.ready=1/' init.rc 
      fi

      if [ "$test_service" == "0" ]
      then
        #sed -i -e 's/service media /service sysinit \/system\/bin\/logwrapper '"$bb_path"' run-parts \/system\/etc\/init.d\n    disabled\n    oneshot\n\nservice media /' init.rc
        sed -i -e 's/service media /service sysinit \/system\/bin\/logwrapper \/system\/xbin\/busybox run-parts \/system\/etc\/init.d\n    disabled\n    oneshot\n\nservice media /' init.rc
        
      fi

    fi

    
    if [ "`grep sysinit init.rc`" == "" ]
    then
      echo
      echo "Error: Unable to modify init.rc!"
    fi

    cd ../..
    scripts/add_initd_perm_to_update_script
    scripts/build_boot_img

  fi

  echo

fi


