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


cd WORKING_*

if [ ! -d system ]
then
  echo
  echo "Error: No /system folder found!"
  cd ..
  exit 0
fi


if [ -e META-INF/com/google/android/update-script ]
then

  #
  # Check for symbolic links in /system/bin and add symlinks to update-script
  #

  # Don't go all the way into system/bin, because some of the binaries in
  # there may collide with your operating system's file operation commands
  # (e.g. ls, rm)!
  cd system

  echo
  echo -n "Looking for symbolic links under /system/bin ..."
  
  symlinks=`find bin -type l -exec ls -l {} \; | sed 's/.* bin\/\([^ ]*\).*/\1/g'`
  source_files=`find bin -type l -exec ls -l {} \; | sed 's/.*-> //g'`

  #
  # Fix for newer RFS-based ROMs (e.g. on Samsung Infuse 4G)
  # The symlink files contain a hex-encoded reference to 'toolbox'
  #
  if [ "$symlinks" == "" ]
  then

    if [ -e bin/cat ]
    then

      toolbox_hex="0074006f006f006c0062006f0078"
      test_toolbox=`od -A n -h -j 8 -N 13 bin/cat | sed 's/ //g'`
   
      if [ "$test_toolbox" == "$toolbox_hex" ]
      then

        file_list=`ls bin`

        for curr_file in $file_list
        do
          check_toolbox=`od -A n -h -j 8 -N 13 bin/$curr_file | sed 's/ //g'`
        
          if [ "$check_toolbox" == "$toolbox_hex" ]
          then
            echo -n "."
            symlinks="$symlinks $curr_file"
          fi
        done
      fi
    fi
  fi

  echo

  symlinks=( $symlinks )
  source_files=( $source_files )

  #
  # Check if no symlinks found in ROM and update-script has no symlink entries
  #
  if [ "$symlinks" == "" ] && [ "`grep "symlink toolbox" ../META-INF/com/google/android/update-script`" == "" ]
  then

    echo "None detected"

    #
    # Build a set of default symlinks in update-script if none found in /system/bin
    #

    if [ "`grep "symlink toolbox" ../META-INF/com/google/android/update-script`" == ""  ] && [ -e bin/toolbox ]
    then
      echo -n "Adding a default set of toolbox symlinks to update-script "

      cp ../../tools/update_files/default_symlinks .
      sed -i -e '/copy_dir PACKAGE:system SYSTEM:/r default_symlinks' ../META-INF/com/google/android/update-script
      rm default_symlinks

      file_list=`ls bin`

      # 
      # If binary already exists, then remove symlink in update-script
      #
      for curr_file in $file_list
      do
        echo -n "."
        check_line="symlink toolbox SYSTEM:bin\\/$curr_file"
        if [ `grep -c "$check_line" ../META-INF/com/google/android/update-script` != 0 ]
        then
          sed -i -e '/'"$check_line"'[ ]*$/d' ../META-INF/com/google/android/update-script
        fi
      done

      echo
    fi

    if [ "`grep "symlink dumpstate" ../META-INF/com/google/android/update-script`" == "" ] && [ -e bin/dumpstate ]
    then

      if [ ! -e bin/bugreport ]
      then
        echo "Adding bugreport symlink ..."
        sed -i -e 's/copy_dir PACKAGE:system SYSTEM:/copy_dir PACKAGE:system SYSTEM:\nsymlink dumpstate SYSTEM:bin\/bugreport\n/' ../META-INF/com/google/android/update-script 
      fi

      if [ ! -e bin/dumpcrash ]
      then
      echo "Adding dumpcrash symlink ..."
        sed -i -e 's/copy_dir PACKAGE:system SYSTEM:/copy_dir PACKAGE:system SYSTEM:\nsymlink dumpstate SYSTEM:bin\/dumpcrash\n/' ../META-INF/com/google/android/update-script 
      fi
    fi


  #
  # Check if symlinks found in ROM
  #

  elif [ "$symlinks" != "" ]
  then

    echo -n "They will now be added to the update-script and removed from system/bin "

    for (( i=0 ; i < ${#symlinks[@]} ; i++ ))
    do

      echo -n "."

      link=${symlinks[$i]}
      src=${source_files[$i]}

      if [ "$src" == "" ]
      then
        src=toolbox
      else

        #
        #  Remove full paths for some binaries
        #
        if [ `echo $src | grep -c /busybox$` == 1 ]
        then
          src=busybox
        elif [ `echo $src | grep -c /toolbox$` == 1 ]
        then
          src=toolbox
        elif [ `echo $src | grep -c /dumpstate$` == 1 ]
        then
          src=dumpstate
        elif [ `echo $src | grep -c /iwmulticall$` == 1 ]
        then
          src=iwmulticall
        fi

      fi

      # Deleting the symbolic link
      rm -f bin/$link

      export LINK=$link
      export SRC=$src

      perl -pi -e 's/set_perm_recursive 0 0 0755 0644 SYSTEM:[ ]*$/symlink $ENV{"SRC"} SYSTEM:bin\/$ENV{"LINK"}\nset_perm_recursive 0 0 0755 0644 SYSTEM:/' ../META-INF/com/google/android/update-script 
    done

    echo

  else
    echo "No symbolic links in ROM; they are found in update-script"
  fi

  cd ..



  #
  # Modify the update-script if certain files are missing under /system
  #

  if [ ! -e system/bin/debuggerd ] 
  then
    sed -i -e '/symlink debuggerd /d' META-INF/com/google/android/update-script
  fi

  if [ ! -e system/bin/mksh ] 
  then
    sed -i -e '/symlink mksh /d' META-INF/com/google/android/update-script
  fi

  if [ ! -e system/bin/wiperiface_v02 ] 
  then
    sed -i -e '/symlink wiperiface_v02 /d' META-INF/com/google/android/update-script
  fi

  if [ ! -e system/bin/ip ] 
  then
    perl -pi -e 's/set_perm 0 3003 06755 SYSTEM:bin\/ip\n//g' META-INF/com/google/android/update-script
  fi

  if [ ! -d system/etc/ppp ] 
  then
    perl -pi -e 's/set_perm_recursive 0 0 0755 0555 SYSTEM:etc\/ppp[ ]*\n//g' META-INF/com/google/android/update-script
  fi

  if [ -d system/etc/bluetooth ]
  then
   perl -pi -e 's/set_perm_recursive 1002 1002 0755 0440 SYSTEM:etc\/bluez\n//g' META-INF/com/google/android/update-script
   perl -pi -e 's/set_perm 0 0 0755 SYSTEM:etc\/bluez\n//g' META-INF/com/google/android/update-script
   
  else
    perl -pi -e 's/set_perm_recursive 1002 1002 0755 0440 SYSTEM:etc\/bluetooth\n//g' META-INF/com/google/android/update-script
    perl -pi -e 's/set_perm 0 0 0755 SYSTEM:etc\/bluetooth\n//g' META-INF/com/google/android/update-script
  fi


  if [ ! -e system/etc/bluetooth/auto_pairing.conf ]
  then
    perl -pi -e 's/set_perm 1000 1000 0640 SYSTEM:etc\/bluetooth\/auto_pairing.conf\n//g' META-INF/com/google/android/update-script
  fi

  if [ ! -e system/etc/bluetooth/blacklist.conf ]
  then
    perl -pi -e 's/set_perm 3002 3002 0444 SYSTEM:etc\/bluetooth\/blacklist.conf\n//g' META-INF/com/google/android/update-script
  fi

   if [ ! -e system/etc/bluetooth/auto_pair_devlist.conf ]
  then
    perl -pi -e 's/set_perm 1000 1000 0640 SYSTEM:etc\/bluetooth\/auto_pair_devlist.conf\n//g' META-INF/com/google/android/update-script
  fi


  if [ ! -e system/bin/run-as ]
  then
    perl -pi -e 's/set_perm 0 2000 06750 SYSTEM:bin\/run-as\n//g' META-INF/com/google/android/update-script
  fi

  if [ ! -d system/vendor ]
  then
    perl -pi -e 's/set_perm_recursive 0 2000 0755 0644 SYSTEM:vendor[ ]*$//g' META-INF/com/google/android/update-script
  fi

  if [ ! -d system/vendor/firmware ]
  then
    perl -pi -e 's/set_perm_recursive 0 0 0755 0644 SYSTEM:vendor\/firmware[ ]*$//g' META-INF/com/google/android/update-script
    perl -pi -e 's/set_perm 0 2000 0755 SYSTEM:vendor\/firmware[ ]*$//g' META-INF/com/google/android/update-script    
  fi

  if [ ! -d system/vendor/bin ]
  then
    perl -pi -e 's/set_perm_recursive 0 2000 0755 0755 SYSTEM:vendor\/bin[ ]*$//g' META-INF/com/google/android/update-script
  fi 

  if [ ! -d system/vendor/etc ]
  then
    perl -pi -e 's/set_perm_recursive 0 2000 0755 0644 SYSTEM:vendor\/etc[ ]*$//g' META-INF/com/google/android/update-script
  fi 

  if [ ! -d system/vendor/lib ]
  then
    perl -pi -e 's/set_perm 0 2000 0755 SYSTEM:vendor\/lib[ ]*$//g' META-INF/com/google/android/update-script    
  fi

  if [ ! -d system/vendor/lib/egl ]
  then
    perl -pi -e 's/set_perm 0 2000 0755 SYSTEM:vendor\/lib\/egl[ ]*$//g' META-INF/com/google/android/update-script    
  fi

  if [ ! -d system/vendor/lib/hw ]
  then
    perl -pi -e 's/set_perm 0 2000 0755 SYSTEM:vendor\/lib\/hw[ ]*$//g' META-INF/com/google/android/update-script    
  fi

  if [ ! -e system/vendor/etc/gps.xml ]
  then
    perl -pi -e 's/set_perm 0 0 0644 SYSTEM:vendor\/etc\/gps.xml//g' META-INF/com/google/android/update-script    
  fi

  if [ ! -d system/etc/bluez ]
  then
    perl -pi -e 's/set_perm_recursive 1002 1002 0755 0440 SYSTEM:etc\/bluez//g' META-INF/com/google/android/update-script
    perl -pi -e 's/set_perm 0 0 0755 SYSTEM:etc\/bluez//g' META-INF/com/google/android/update-script
  fi

  if [ ! -d system/xbin ]
  then
    perl -pi -e 's/set_perm_recursive 0 2000 0755 0755 SYSTEM:xbin[ ]*$//g' META-INF/com/google/android/update-script
  fi


  if [ ! -e system/etc/install-recovery.sh ]
  then
    perl -pi -e 's/set_perm 0 0 0544 SYSTEM:etc\/install-recovery.sh\n//g' META-INF/com/google/android/update-script
  fi

  cd ..
  scripts/fix_update_script_blanks


else
  cd ..
fi  

