From d7f0c4fe4c1d622e029e2961f7168441e89da87f Mon Sep 17 00:00:00 2001
From: Jean-Christophe Fillion-Robin <jchris.fillionr@kitware.com>
Date: Tue, 19 Apr 2016 13:40:17 -0400
Subject: [PATCH] utils/aosp: Refactor code to display status message and
 remove chmod call

Now directories are owned by aosp user, there is not need to explicitly
chmod to 777. Current user will create and own the folder.
---
 utils/aosp | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/utils/aosp b/utils/aosp
index 965c1f8..29c8e5d 100755
--- a/utils/aosp
+++ b/utils/aosp
@@ -19,15 +19,30 @@ AOSP_VOL_AOSP=${AOSP_VOL_AOSP%/} # Trim trailing slash if needed
 AOSP_VOL_CCACHE=${AOSP_VOL_CCACHE:-$AOSP_VOL/ccache}
 AOSP_VOL_CCACHE=${AOSP_VOL_CCACHE%/} # Trim trailing slash if needed
 
-if [ ! -d "$AOSP_VOL_AOSP" -o ! -d "$AOSP_VOL_CCACHE" ]; then
-    sudo mkdir -p $AOSP_VOL_AOSP $AOSP_VOL_CCACHE
-    sudo chmod 777 $AOSP_VOL_AOSP $AOSP_VOL_CCACHE
-fi
+# Convenience function
+function aosp_create_dir_if_needed {
+  directory=$1
+  msg="Checking if $directory exists"
+  echo "$msg"
+  if [ ! -d "$directory" ]; then
+    echo "$msg - unexistent"
+    msg="Creating $directory"
+    echo "$msg"
+    mkdir -p $directory
+  fi
+  echo "$msg - ok"
+}
+
+# Create AOSP_VOL_AOSP
+aosp_create_dir_if_needed $AOSP_VOL_AOSP
+aosp_create_dir_if_needed $AOSP_VOL_CCACHE
 
 if [ -n "$SSH_AUTH_SOCK" ]; then
     SSH_AUTH_ARGS="-v $SSH_AUTH_SOCK:/tmp/ssh_auth -e SSH_AUTH_SOCK=/tmp/ssh_auth"
 fi
 
+echo ""
+
 docker run $AOSP_ARGS $SSH_AUTH_ARGS $AOSP_EXTRA_ARGS \
     -v "$AOSP_VOL_AOSP:/aosp" -v "$AOSP_VOL_CCACHE:/tmp/ccache" \
     $AOSP_IMAGE $@