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


clear

echo
echo "----------------------------------------------------------------"
echo  
echo "Nano is a text editor that can be run from the ADB shell on your "
echo "computer or from a terminal emulator (e.g. ConnectBot) on your"
echo "Android device.  To run it from a shell, type: nano"
echo
echo "The scripts 'sysrw' and 'sysro' will also be included to make it"
echo "easier to mount the /system folder (as read-write or read-only)"
echo "for file editing purposes as root."
echo
echo "----------------------------------------------------------------"
echo

echo -n "Proceed with install (y/n)? (default: y): "
read do_install

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

echo 

scripts/add_busybox no_clear
res=$?

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


#
# sysro/sysrw require busybox to mount
# The regular 'mount' doesn't work for all devices
#

scripts/convert_to_unix tools/nano_files/sysrw
scripts/convert_to_unix tools/nano_files/sysro

cd WORKING_*
echo

sys_path=`find . -name sysrw`
if [ "$sys_path" == "" ] 
then
  cp -fv ../tools/nano_files/sysrw system/xbin/
else 
  echo "Found $sys_path"
fi

sys_path=`find . -name sysro`
if [ "$sys_path" == "" ] 
then
  cp -fv ../tools/nano_files/sysro system/xbin/ 
else 
  echo "Found $sys_path"
fi

cd ..
echo
scripts/add_sysrorw_to_update_script


scripts/check_galaxy_s_and_s2

if [ "$?" == "1" ]
then
  echo
  echo "Nano feature not supported by kitchen for this ROM"

else
  scripts/ensure_boot_extracted

  if [ -d BOOT-EXTRACTED ]
  then
    if [ -d WORKING_* ]
    then

      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

    else
      exit 0
    fi
  else
    exit 0
  fi

  echo

  if [ `grep -c TERMINFO init.rc` == 1 ]
  then
    echo "TERMINFO already defined in init.rc"
    cd ../..
    rm -rf BOOT-EXTRACTED

  else
    echo "Setting TERMINFO in init.rc ..."
    perl -pi -e 's/    export PATH /    export TERMINFO \/system\/etc\/terminfo\n    export PATH /g' init.rc

    if [ -e init.rc.bak ]
    then
      rm -f init.rc.bak
    fi

    if [ "`grep TERMINFO init.rc`" == "" ]
    then
      echo "Error: Could not update init.rc!"
      cd ../..
      rm -rf BOOT-EXTRACTED
      exit 0
    fi

    cd ../..
    scripts/build_boot_img

    echo
    echo "Adding /system/etc/terminfo ..."
    echo

    cd WORKING_*
    mkdir -p system/etc/terminfo
    cp ../tools/nano_files/terminfo.zip system/etc/terminfo/
    cd system/etc/terminfo
    unzip terminfo.zip
    rm terminfo.zip
    cd ../../../..
  fi

  echo

  scripts/nano_exists
  res=$?
  
  if [ "$res" == "0" ] 
  then
    cd WORKING_*
    cp -fv ../tools/nano_files/nano system/xbin/
    cd ..
  else 
    echo "Found nano binary"
  fi

  scripts/add_nano_to_update_script

fi

scripts/fix_update_script_blanks

