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

device=`scripts/get_variant_or_device_name`

if [ ! -e "tools/edify_defs/$device" ] && [ "$device" != "UNKNOWN" ]
then

  cd WORKING_*

  out=(`grep ^mount META-INF/com/google/android/updater-script | grep block`)
  if [ "$out" == "" ]
  then
    out=(`grep "^run_program(\"/sbin/mount\"" META-INF/com/google/android/updater-script | grep block`)
  fi

  if [ "$out" != "" ]
  then
    echo
    echo "======================================================================"
    echo
    echo "WARNING! Device '$device' is not defined under tools/edify_defs"
    echo
    echo "The kitchen will now try to create a very basic definition file based"
    echo "on the mount points specified in your updater-script; however, it may"
    echo "not be 100% accurate ..."

    file_path=../tools/edify_defs/$device

    echo "change_mnt=yes" >> $file_path
    echo >> $file_path

    mnt_name_list=( system cache data sdcard )
    var_name_list=( sys_mnt cache_mnt data_mnt sdcard_mnt )
    got_block_id=0
    found_param1=0

    for item in ${out[@]}
    do
      
      # Look for ext3/ext4 etc.
      if [ "`echo $item | grep -c \"mount(\\\"ext\"`" != 0 ] && [ $found_param1 == 0 ]
      then
        param1=`echo $item | sed -e 's/mount(\"\(.*\)\",/\1/g'`
        echo >> $file_path
        echo "param1=$param1" >> $file_path 
        echo "param2=EMMC" >> $file_path 
        echo >> $file_path
        found_param1=1

        echo "param1_sdcard=vfat" >> $file_path
        echo "param2_sdcard=MTD" >> $file_path
        echo >> $file_path

      # Look for /dev/block/mmcblock etc.
      elif [ "`echo $item | grep -c block`" != 0 ]
      then
        got_block_id=1
        slash=\\\\
        block_id=`echo $item | sed -e 's/\"//g' -e 's/,//g' -e 's/\(\/\)/'$slash'\1/g'`


      elif [ $got_block_id == 1 ]
      then

        # Look for mount name
        if [ "`echo $item | grep -c /`" == "1" ]
        then
          for (( i = 0 ; i < ${#mnt_name_list[@]} ; i++ ))
          do
            mnt=${mnt_name_list[$i]}
            
            if [ "`echo $item | grep $mnt`" != "" ]
            then
              var=${var_name_list[$i]}
              echo "$var=$block_id" >> $file_path 
              got_block_id=0
            fi                  
          done
        fi            
      fi

    done

    # Default ext type
    if [ $found_param1 == 0 ]
    then
      echo >> $file_path
      echo "param1=ext4" >> $file_path
      echo "param2=EMMC" >> $file_path 
      echo >> $file_path

      echo "param1_sdcard=vfat" >> $file_path
      echo "param2_sdcard=MTD" >> $file_path
      echo >> $file_path      
    fi


    # Look for boot image mount - for the boot_mnt and fix_boot2 parameters
    boot_out=(`grep write_raw_image META-INF/com/google/android/updater-script \
        | grep boot.img | grep block`)
 
    if [ "$boot_out" != "" ]
    then
      slash=\\\\
      boot_mnt=`echo ${boot_out[1]} | sed -e 's/\"\(.*\)\"),/\1/g' -e 's/\(\/\)/'$slash'\1/g'`
      
      if [ "`echo $boot_mnt | grep block`" != "" ]
      then
        echo "boot_mnt=$boot_mnt" >> $file_path
        echo "fix_boot2=yes" >> $file_path 
      fi
    fi

    for (( i = 0 ; i < ${#var_name_list[@]} ; i++ ))
    do
      var=${var_name_list[$i]}
      if [ "`grep $var $file_path`" == "" ]
      then
        echo "$var=???" >> $file_path
      fi
    done

    echo
    echo "tools/edify_defs/$device:"
    echo "-------------------------------------------------"
    echo
    cat $file_path
    echo
    echo "-------------------------------------------------"
    echo
    echo
  fi

  cd ..
fi

