#!/bin/sh

# define this variable if you want to keep the tmploc directory
# for debugging purposes
# export KEEPTEMPDIR=1

home_dir=${PWD}

project_name="ProjectTest"
version="1.0"
module_name="ModuleTest"

# Create unique directory
tmploc=$(mktemp -d -t temp1.XXXXXX)

# Clean and exit
clean_and_exit() {
  cd ${home_dir}
  if [ -z "${KEEPTEMPDIR}" ]; then
    rm -rf ${tmploc}
  fi
  exit $1
}

# Set to the User_area
export User_area=${tmploc}

cd ${tmploc}

# We need to create first a project
CreateElementsProject ${project_name} ${version} --yes

# if error stop and clean up
if [ $? -ne 0 ]; then
   echo "Error: <CreateElementsProject ${project_name} ${version}> command failed!" 1>&2
   clean_and_exit 1
fi

# Move to the project
cd $User_area/$project_name/$version/

# We need to create a module
AddElementsModule $module_name --yes

# if error stop and clean up
if [ $? -ne 0 ]; then
   echo "Error: <AddElementsModule $module_name> command failed!" 1>&2
   clean_and_exit 1
fi

if [ "$(basename $0)" = "createCommonStructure_test" ]; then
  # If I have been called standalone, I do a clean.
  clean_and_exit 0
fi

# Variables to propagate
export TEMPORARY_LOCATION=${User_area}
export MODULE_NAME_PATH=${User_area}/${project_name}/${version}/${module_name}
export MODULE_NAME=${module_name}
