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

#
# This script has two optional arguments: 
#
# $1 = "no_info"      - specify this when porting or extracting kernel
# $2 = "kernel_only"  - if this is specified, then $1 is mandatory
#

echo
echo "CREATE WORKING FOLDER FOR ROM"
echo "=============================="
echo
echo "Ensure there is at least one ROM under the 'original_update' folder!"
echo
echo "Select an option:"
echo
echo " s - Show supported formats"
echo " x - Abort, don't create working folder"
echo ""
echo "Or press Enter to continue"
echo
echo -n "? "
read proceed
  
if [ "$proceed" == "x" ]
then
  echo
  echo "Cancelled"
  exit 1

elif [ "$proceed" == "s" ]
then
  
  more tools/formats.txt

  echo
  echo -n "Press Enter to continue, or type 'x' to abort: "
  read proceed
  
  if [ "$proceed" == "x" ]
  then
    exit 1
  fi

fi

scripts/make_backup_working

#
# Check if the working folder was actually backed up
#
result=$?

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

  echo
  echo "Error: Old working folder is in use. Ensure its files or folders are not open."
  scripts/press_enter
  exit 1

else

  scripts/choose_rom $2
  res=$?

  if [ "$res" == "1" ] && [ "$2" == "kernel_only" ]
  then
    scripts/press_enter
  fi

  if [ `ls | grep -m 1 WORKING_` ] && [ "$res" != "1" ] && [ "$1" == "" ]
  then

    #
    # Check for radio.img
    #

    echo
    cd WORKING*
    if [ -e radio.img ]
    then
      echo
      echo "NOTE: radio.img was found in the working folder. If you wish to"
      echo "remove it, you may do so at any time before building the ROM."
    else

      if [ -e META-INF/com/google/android/update-script ]
      then
        cd ..
        scripts/update_script_should_convert_back ignore_msg
        res=$?
        cd WORKING_*
      fi
    fi

    if [ -e boot.img ] 
    then
      boot_found=yes
    elif [ -e boot/zImage ] && [ -e boot/initrd.gz ]
    then
      boot_found=yes
    else
      boot_found=no
    fi

    if [ "$boot_found"=="yes" ]
    then

      #
      # Prompt to show ROM info
      # 

      if [ "$1" == "" ]
      then
        cd ..
        scripts/prompt_show_rom_info
      else
        cd ..
      fi

    else
      echo
      echo "No boot image found!"
      cd ..
    fi
  fi
fi

if [ "$1" == "" ]
then
  scripts/press_enter
fi

exit $res
