#!/bin/sh
#*=====================================================================*/
#*    serrano/prgm/project/bigloo/autoconf/androidmkdir                */
#*    -------------------------------------------------------------    */
#*    Author      :  Manuel Serrano                                    */
#*    Creation    :  Thu Sep 23 14:28:53 2010                          */
#*    Last change :  Fri Dec 29 06:20:38 2017 (serrano)                */
#*    Copyright   :  2010-20 Manuel Serrano                            */
#*    -------------------------------------------------------------    */
#*    Create a directory on the Android device/emulator                */
#*=====================================================================*/

adb=$1
dir=$2
ok=0

# test the dir exists
if $adb shell ls $dir 2>&1 | grep -iq 'no such'; then
   # no; try to create it
   $adb shell mkdir $dir 2> /dev/null > /dev/null

   if $adb shell ls $dir 2>&1 | grep -iq 'no such'; then
      ok=1
   fi
fi

if [ $ok -eq 0 ]; then
   # the directory exists
   # try to create an exec
   if $adb shell "echo 'echo yep' > $dir/foo" 2>&1 | grep -iq 'read-only'; then
      # read only
      ok=1
   else
      $adb shell chmod 755 $dir/foo 2> /dev/null > /dev/null
      # set it executable and run it
      if [ "$?" != "0" ]; then
        # can't chmod
        ok=1;
      else
        $adb shell $dir/foo 2>&1 | grep "yep" 2> /dev/null > /dev/null
	
	if [ "$?" != "0" ]; then
          # can't execute
          ok=1
        fi
      fi

      # cleanup
      $adb shell rm -r $dir/foo 2> /dev/null > /dev/null
      if [ "$?" != "0" ]; then
        ok=1
      fi
   fi
fi

exit $ok

