#!/sbin/sh

OUTFD=$2
ZIP=$3

# Borrowed from SuperSU package
ui_print() {
  echo -n -e "ui_print $1\n" > /proc/self/fd/$OUTFD
  echo -n -e "ui_print\n" > /proc/self/fd/$OUTFD
}

ch_con() {
  LD_LIBRARY_PATH=$SYSTEMLIB /system/toolbox chcon -h u:object_r:system_file:s0 $1
  LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/toolbox chcon -h u:object_r:system_file:s0 $1
  chcon -h u:object_r:system_file:s0 $1
  LD_LIBRARY_PATH=$SYSTEMLIB /system/toolbox chcon u:object_r:system_file:s0 $1
  LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/toolbox chcon u:object_r:system_file:s0 $1
  chcon u:object_r:system_file:s0 $1
}

ch_con_ext() {
  LD_LIBRARY_PATH=$SYSTEMLIB /system/toolbox chcon $2 $1
  LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/toolbox chcon $2 $1
  chcon $2 $1
}

set_perm() {
  chown $1.$2 $4
  chown $1:$2 $4
  chmod $3 $4
  ch_con $4
  ch_con_ext $4 $5
}


ui_print " "
ui_print "*******************************"
ui_print "  SDCARD PERMISSION FIX v1.9b"
ui_print "    by       RW"
ui_print "*******************************"



ui_print "- Mounting /system"
mount /system
mount -o rw,remount /system
mount -o rw,remount /system /system


ui_print "- Detecting current API version"
API=$(cat /system/build.prop | grep "ro.build.version.sdk=" | dd bs=1 skip=21 count=2)
VALID_API=0
if [ "${API}" -ge "19" ]; then
   if [ "${API}" -le "22" ]; then
      VALID_API=1
   elif [ "${API}" -eq "23" ]; then
      VALID_API=2
   fi
fi
ui_print "      API v${API} detected"

if [ "${VALID_API}" -eq "1" ]; then
   fpath_ori="/system/etc/permissions/platform.xml"
   fpath_bak="${fpath_ori}-RW-$(date -u +%Y%m%d%H%M%S).bak"
   fpath_tmp="/tmp/platform-RW.xml"

   ui_print "- Backing up file to ${fpath_bak}"
   # backups the original file
   cp "${fpath_ori}" "${fpath_bak}"

   HasPerm=$(awk -e '/<permission[^<>]*name="android\.permission\.WRITE_EXTERNAL_STORAGE"[^<>]*>/ {print "1";} 0 ;' "${fpath_ori}")
   HasGid=$(awk -e '/<permission[^<>]*name="android\.permission\.WRITE_EXTERNAL_STORAGE"[^<>]*>/ {a=1;} ; a&&/media_rw/ {print "1";} ; a&&/<\/permission>/ {a=0;} 0 ;' "${fpath_ori}")

   if [ "${VALID_API}" -eq "2" ]; then

      if [ "${HasPerm}" -eq "1" ]; then
         ui_print "- Removing WRITE_EXTERNAL_STORAGE"
         # removes permission "WRITE_EXTERNAL_STORAGE"
         awk -e 'BEGIN {a=0;} ; /<permission[^<>]*name="android\.permission\.WRITE_EXTERNAL_STORAGE"[^<>]*>/ {a=1;} ; (1-a) {print $0;} ; a&&/<\/permission>/ {a=0;} 0 ;' "${fpath_ori}" > "${fpath_tmp}"

      else
         ui_print "- Adding WRITE_EXTERNAL_STORAGE"
         # adds permission "WRITE_EXTERNAL_STORAGE"
         awk -e '/<permission[^<>]*name="android\.permission\.ACCESS_MTP"[^<>]*>/ {print "    <permission name=\"android.permission.WRITE_EXTERNAL_STORAGE\">"; print "        <group gid=\"sdcard_r\"/>"; print "        <group gid=\"sdcard_rw\"/>"; print "    </permission>"; print "";} 1 ;' "${fpath_ori}" > "${fpath_tmp}"
      fi
      
      # overwrites the changes
      cat "${fpath_tmp}" > "${fpath_ori}"
      
   fi


   if [ "${HasGid}" -eq "1" ]; then
      ui_print "- Removing media_rw from WRITE_EXTERNAL_STORAGE"
      # removes permission to "media_rw" for "WRITE_EXTERNAL_STORAGE"
      awk -e 'BEGIN {a=0;} ; /<permission[^<>]*name="android\.permission\.WRITE_EXTERNAL_STORAGE"[^<>]*>/ {a=1;} ; a&&!/media_rw/ {print $0;} ; (1-a) {print $0;} 0 ; a&&/<\/permission>/ {a=0;} 0 ;' "${fpath_ori}" > "${fpath_tmp}"

   else
      ui_print "- Inserting media_rw from WRITE_EXTERNAL_STORAGE"
      # adds permission to "media_rw" for "WRITE_EXTERNAL_STORAGE"
      awk -e '/<permission[^<>]*name="android\.permission\.WRITE_EXTERNAL_STORAGE"[^<>]*>/ {p=1} ; p&&/media_rw/{p=0} ; p&&/<\/permission>/{print "        <group gid=\"media_rw\" />";p=0;} 1 ;' "${fpath_ori}" > "${fpath_tmp}"
      
   fi
   
   # feeds back into the original file
   cat "${fpath_tmp}" > "${fpath_ori}"

   # removes temporary file
   rm "${fpath_tmp}"
   

   ui_print "- Fixing permissions"
   # changes permissions
   set_perm 0 0 0644 "${fpath_ori}"
   set_perm 0 0 0644 "${fpath_bak}"
   
else
   fpath_ori="/data/system/packages.xml"
   fpath_bak="${fpath_ori}-RW-$(date -u +%Y%m%d%H%M%S).bak"
   fpath_tmp="/tmp/packages-RW.xml"

   ui_print "- Backing up file to ${fpath_bak}"
   # backups the original file
   cp "${fpath_ori}" "${fpath_bak}"

   awk -e '/<perms>/ {p=1} ;
            p&&/WRITE_MEDIA_STORAGE/{p=0} ;
            p&&/<\/perms>/{print "            <item name=\"android.permission.WRITE_MEDIA_STORAGE\" granted=\"true\" flags=\"0\" />";p=0;} 1 ;' "${fpath_ori}" > "${fpath_tmp}"
   
   # feeds back into the original file
   cat "${fpath_tmp}" > "${fpath_ori}"

   # removes temporary file
   rm "${fpath_tmp}"
   

   ui_print "- Fixing permissions"
   # changes permissions
   set_perm 0 0 0644 "${fpath_ori}"
   set_perm 0 0 0644 "${fpath_bak}"
   
fi


ui_print "- Unmounting /system"
umount /system



ui_print "- Done !"
ui_print " "
exit 0
