#!/bin/bash

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

version=0.224


if [ ! -e menu ]
then
  echo
  echo "Ensure you run this file from the SAME folder as where it was"
  echo "installed, otherwise the kitchen will have problems running the"
  echo "scripts.  After you 'cd' to the correct folder, start the kitchen"
  echo "with the ./menu command, NOT with any other command or method!"
  exit 0
fi

dir_list=( original_update scripts tools )
error_found=0

for check_dir in ${dir_list[@]}
do
  if [ ! -e $check_dir ]
  then
    echo "Error: Folder '$check_dir' not found"
    error_found=1
  fi
done

if [ $error_found == 1 ]
then
  echo
  echo "Ensure you have installed the folders for the kitchen correctly."
  exit 0
fi


chmod 755 scripts/*
scripts/check_install_dir

if [ "$?" == "1" ]
then
  exit 0
fi

scripts/init_kitchen
scripts/check_binaries

if [ "$?" == "1" ]
then
  exit 0
fi


while :
do

  clear

  echo 
  echo "============================================================"
  echo " Android Kitchen $version - by dsixda (xda-developers.com)"
  echo "============================================================"
  echo 
  echo "> MAIN MENU"
  echo
  echo "  1 - Set up working folder from ROM"
  echo "  2 - Add root permissions"
  echo "  3 - Add BusyBox"
  echo "  4 - Disable boot screen sounds"
  echo "  5 - Zipalign all *.apk files to optimize RAM usage"
  echo "  6 - Change wipe status of ROM"
  echo "  7 - Change name of ROM"
  echo "  8 - Show working folder information"
  echo 
  echo "  0 - ADVANCED OPTIONS"
  echo " 00 - LEGACY OPTIONS (old and rarely used)"
  echo " 99 - Build ROM from working folder"
  echo
  echo "  u - Kitchen Info/Update/Donate"
  echo "  x - Exit"
  echo
  echo 
  echo -n "Enter option: "
  read opt

  if [ "$opt" != "x" ] && [ "$opt" != "u" ] && [ "$opt" != "1" ]
  then
    scripts/check_multiple_working
  fi
  
  if [ "$?" != "1" ]
  then
    case $opt in
      1) clear; scripts/check_rom; continue;;
      2) scripts/do_root; continue;;
      3) scripts/add_busybox show_help;;
      4) scripts/disable_sounds;;
      5) scripts/do_zipalign;;
      6) scripts/change_wipe;;
      7) scripts/change_rom_name;;
      8) scripts/show_rom_info;;
      0) scripts/menu_advanced $version; continue;;
      00) scripts/menu_legacy $version; continue;;
      99) scripts/build_rom;;
      u) scripts/about $version; continue;;
      x) scripts/init_kitchen; clear; echo; echo "Goodbye."; echo; exit 1;;
      *) echo "Invalid option"; continue;;
    esac
  fi

  scripts/press_enter

done

