# HG changeset patch
# User Vincent Hatakeyama <vincent.hatakeyama@xcg-consulting.fr>
# Date 1522239320 -7200
#      Wed Mar 28 14:15:20 2018 +0200
# Node ID 82efd72a5f4861ba07dd04e67eee31296bd93007
# Parent  aa6b483722ca15623466a134834a33ee1fa6aee9
fix docker build copy/clean and cleanup on error too

diff --git a/docker_build b/docker_build
--- a/docker_build
+++ b/docker_build
@@ -13,19 +13,24 @@
 image=$(python -B -c "import ConfigParser ; c = ConfigParser.SafeConfigParser() ; c.read('${project_home}/setup.cfg') ; print (c.has_section('odoo_scripts') and c.has_option('odoo_scripts', 'image') and c.get('odoo_scripts', 'image')) or '$image_default'")
 
 ignore_git=$(python -B -c "import ConfigParser ; c = ConfigParser.SafeConfigParser() ; c.read('${project_home}/setup.cfg') ; print (c.has_section('odoo_scripts') and c.has_option('odoo_scripts', 'ignore_git') and c.get('odoo_scripts', 'ignore_git')) or 'False'")
-docker_module_dir=
+
+pushd $project_home
 
 # Ensureconf 
-pushd $project_home
-if [ "$ignore_git" = "True" ];
+# TODO handle confnest
+if [[ -d ".hg" ]];
 then
-    hg cfensureconf || echo "ignore all cfensureconf errors to bypass bad error code with git repositories"
-else
     hg cfensureconf || { echo 'hg cfensureconf failed' ; popd ; exit 1; }
 fi
-popd
+
+# TODO only conditionaly do that
+# Conditions could be: detect odoo_modules in Dockerfile.
+$here/docker_build_copy
 
-$here/docker_build_copy
+function clean_on_exit() {
+    $here/docker_build_clean
+}
+trap clean_on_exit INT TERM EXIT
 
 docker build --pull=true --rm=true -t $registry/$image $project_home || { echo 'Build failed' ; exit 1; }
 
@@ -51,5 +56,4 @@
   docker push $registry/$image || { echo 'Push failed' ; exit 1; }
 fi
 
-$here/docker_build_clean
-
+popd
diff --git a/docker_build_clean b/docker_build_clean
--- a/docker_build_clean
+++ b/docker_build_clean
@@ -8,11 +8,11 @@
 # TODO add a way to bypass the value, maybe with a key in the setup.cfg file
 here=$(dirname $0)
 project_home=$(cd $here && cd .. && echo $PWD)
-odoo_modules=$(python -B -c "import ConfigParser ; c = ConfigParser.SafeConfigParser() ; c.read('${project_home}/setup.cfg') ; print (c.has_option('odoo_scripts', 'modules') and c.get('odoo_scripts', 'modules') or '')")
+odoo_modules="$(python -B -c "import ConfigParser ; c = ConfigParser.SafeConfigParser() ; c.read('${project_home}/setup.cfg') ; print (c.has_option('odoo_scripts', 'modules') and ' '.join(c.get('odoo_scripts', 'modules').split()) or '')")"
 
 # Delete odoo modules temp directory
 if [[ -n $odoo_modules ]];
 then
-    echo rm -rf $project_home/odoo_modules
+    rm -rf $project_home/odoo_modules
 fi
 
diff --git a/docker_build_copy b/docker_build_copy
--- a/docker_build_copy
+++ b/docker_build_copy
@@ -10,11 +10,10 @@
 project_home=$(cd $here && cd .. && echo $PWD)
 odoo_modules="$(python -B -c "import ConfigParser ; c = ConfigParser.SafeConfigParser() ; c.read('${project_home}/setup.cfg') ; print (c.has_option('odoo_scripts', 'modules') and ' '.join(c.get('odoo_scripts', 'modules').split()) or '')")"
 
-mkdir -p $project_home/odoo_modules
-
 # Copy modules when specified
 if [[ -n "$odoo_modules" ]];
 then
+    mkdir -p $project_home/odoo_modules
     rsync --delete -C --exclude='.hg*' --exclude='.git*' --links --exclude='*.pyc' -r --times $(eval echo $odoo_modules) $project_home/odoo_modules
 fi