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


clear

echo 

if [ ! -d scripts/plugins ] 
then
  echo "Error: /scripts/plugins folder not found!"
  exit 0
fi

cd scripts/plugins
grep_cmd=`find . | grep "\.plugin\$" | sed -e 's/.\///'`
grep_cmd2=`find . | grep "\.plugin\$" | sed -e 's/.\///' -e 's/.plugin//g'`
cd ../..


if [ "$grep_cmd" == "" ]
then
  echo "No *.plugin files found under /scripts/plugins!"
  echo
  echo "Press Enter to continue"
  read enterKey
  exit 0

else

  echo -n "Please wait "

  long_line="--------------------------------------"
  count=0

  for plugin_file in $grep_cmd
  do
    echo -n "."
    # Converting end-of-line character in files created in DOS 
    scripts/convert_to_unix scripts/plugins/$plugin_file
  done

  echo

  #
  # Show the menu
  #

  while :
  do

    clear
    count=0
    refresh_names=0

    echo
    echo "PLUGIN SCRIPTS"
    echo $long_line
    echo

    for plugin_file in $grep_cmd2
    do
      count=$(($count+1))

      # Store file names in an array
      file_array[$count]=$plugin_file

      echo "  ($count) $plugin_file"

    done

    echo
    echo $long_line
    echo
    echo "  0 - Exit    r - Refresh list"
    echo
    echo
    echo -n "Enter an option: "
    read enterOption

    if [ "$enterOption" == "0" ]
    then
      exit 0
    fi

    if [ "$enterOption" == "r" ]
    then
      refresh_names=1
    
    # Verify input is a number
    elif [ "`echo $enterOption | sed 's/[0-9]*//'`" == "" ]
    then

      file_chosen=${file_array[$enterOption]}.plugin
    
      if [ "$file_chosen" == "" ]
      then
        continue
      fi

      #
      # Run script
      #
      if [ -e scripts/plugins/$file_chosen ]
      then
        clear
        chmod 755 scripts/plugins/$file_chosen
        scripts/plugins/$file_chosen
      else
        echo "Error: File not found!"
        refresh_names=1 
      fi

      scripts/press_enter

    else
      continue
    fi

    if [ $refresh_names == 1 ]
    then
      cd scripts/plugins
      grep_cmd=`find . | grep "\.plugin\$" | sed 's/.\///'`
      cd ../..  
    fi

  done
fi

