#!/system/bin/sh

echo "updateperms v0.0.5"
echo ""

while [ -n "$1" ];
do
   case "$1" in
     -v) verbose=1; shift ;;
     -p) pmode=1; shift ;;
     -u) umode=1; shift ;;
     -c) cmode=1; shift ;;
     *) echo "usage: $0 [-p] [-u] [-v]" 
         echo "searches for media not owned by cheogram in app-specific storage directories"
         echo "       -p: print mode: print all files not owned by app and exit [default]"
         echo "       -u: update mode: search for files not owned by app and prompt to update"
         echo "       -v: verbose: show commands used to search and/or update ownership"
         echo "       -c: search for conversations files instead of cheogram"
         exit ;;
   esac
done
if [ -z "$pmode" ] && [ -z "$umode" ]; then
   pmode=1
fi

users=$(pm list users|grep UserInfo|cut -d: -f1,2)
nusers=$(echo "$users"|wc -l)
if [ "$nusers" = "1" ]; then
   prof=0
else
   echo -n "+ multiple profiles [$(echo "$users"|cut -d{ -f2|tr -s '\n' ',')]; enter the profile numerical id: "
   read prof
   profname=$(echo "$users"|grep "UserInfo{${prof}:"|cut -d: -f2)
   if [ -z "$profname" ]; then echo "- invalid profile id"; exit; fi
   if [ "$prof" != "0" ]; then
     echo "+ non-owner selected; profile needs to be active or errors may result. hit [enter]"
     read dfaf
   fi
fi

app=com.cheogram.android
basedir=Cheogram

if [ -n "$cmode" ]; then
   # UNCOMMENT next two lines for Conversations
   app=eu.siacs.conversations
   basedir=Conversations
fi

echo "+ search limited to the following:"
echo "  *********************************"
echo "         user: $profname"
echo "   profile id: $prof"
echo "          app: $app"
echo "    directory: $basedir"
echo "  *********************************"
echo ""
echo ""

mtypes="images/media video/media audio/media downloads file"

if [ -n "$pmode" ]; then
   echo "+ print mode enabled"
   echo ""
   for m in $mtypes
   do
      echo "+ searching for files of type $m: "
      res=$(content query --user ${prof} --uri content://media/external/$m --where "(owner_package_name is NULL or owner_package_name <> '${app}') and _data like '%/${basedir}%'" |
         tr -s ',' '\n' | grep -E '^ (owner_package_name|_data)' | sed -e 's/owner_package_name=/app=/g; s/_data=/file=/g; s/^/   /g; /^    app/i \ ')
      nres=$(echo "$res"|grep "file="|wc -l)
      if [ $nres -gt 0 ]; then
          echo "  + $nres files:"
          echo "$res"
      fi
      echo ""
   done
fi

if [ -n "$umode" ]; then
   echo "+ update mode enabled"
   echo ""

   for m in $mtypes
   do
      echo "+ searching for files of type ${m}:"
      if [ -n "$verbose" ]; then
          echo "+ search command:"
          echo "  content query --user ${prof} --uri content://media/external/$m --where \"(owner_package_name is NULL or owner_package_name <> '${app}')  and _data like '%/${basedir}%'\""
      fi

      res=$(content query --user ${prof} --uri content://media/external/$m --where "(owner_package_name is NULL or owner_package_name <> '${app}') and _data like '%/${basedir}%'"|sed -n 's/^.*_data=\([^,]*\),.*/\1/p')
      if [ -n "$res" ]; then
         nres=$(echo "$res"|wc -l)
         echo "  + $nres files:"
         echo "$res"
         echo ""
         echo -n "** press y to update the above to $app [yN] **"
         if read doit && [[ "$doit" = [yY] ]]; then
            if [ -n "$verbose" ]; then
               echo "+ update command:"
               echo "  content update --user ${prof} --uri content://media/external/$m --bind \"owner_package_name:s:${app}\" --where \"(owner_package_name is NULL or owner_package_name <> '${app}') and _data like '%/${basedir}%'\""
            fi
            echo -n "+ updating..."
            content update --user ${prof} --uri content://media/external/$m --bind "owner_package_name:s:${app}" --where "(owner_package_name is NULL or owner_package_name <> '${app}') and _data like '%/${basedir}%'"
            echo "done."
         else
            echo "+ skipping"
         fi
      else
         echo "  + no results for $m"
      fi
      echo ""
   done
fi

echo "updateperms done."

