Index: CMT/HEAD/ChangeLog
===================================================================
--- CMT/HEAD/ChangeLog	(revision 654)
+++ CMT/HEAD/ChangeLog	(revision 655)
@@ -1,2 +1,20 @@
+2013-11-13    <rybkin@lal.in2p3.fr> 514
+
+	* source/cmt_use.cxx: In class Use get_strategy function, make use of now
+	fast get_project function rather than compute, in fill_standard_macros,
+	get_package_name, get_project functions, simplify code
+	* source/cmt_parser.cxx: In class Cmt install_scripts functions, make
+	generated setup and cleanup scrips call CMT executable directly rather than
+	via shell script wrapper
+	* src/setup.sh: Reduce usage of external shell utilities by using POSIX
+	shell features instead
+	* src/cleanup.sh: idem, unset everything set by setup.sh
+	* src/setup.csh: Reduce usage of external shell utilities by using C shell
+	features instead
+	* src/cleanup.csh: idem, unset everything set by setup.csh
+	* src/cleanup.tcsh: Add script to unset TCSH specific environment setup
+	* src/cleanup.zsh: Add script to unset Z shell specific environment setup
+	* src/cleanup.bash: Add script to unset BASH specific environment setup
+	
 2013-11-08    <rybkin@lal.in2p3.fr> 513
 
Index: CMT/HEAD/source/cmt_parser.cxx
===================================================================
--- CMT/HEAD/source/cmt_parser.cxx	(revision 654)
+++ CMT/HEAD/source/cmt_parser.cxx	(revision 655)
@@ -6262,8 +6262,7 @@
               fprintf (f, "endif\n");
               fprintf (f, "source ${CMTROOT}/mgr/setup.csh\n");
-              fprintf (f, "set cmt%stempfile=`${CMTROOT}/mgr/cmt -quiet build temporary_name`\n", mangled_package.c_str ());
+              fprintf (f, "set cmt%stempfile=`${CMTROOT}/${CMTBIN}/cmt.exe -quiet build temporary_name`\n", mangled_package.c_str ());
               fprintf (f, "if $status != 0 then\n  set cmt%stempfile=/tmp/cmt.$$\nendif\n", mangled_package.c_str ());
-              //fprintf (f, "${CMTROOT}/mgr/cmt setup -%s "
-              fprintf (f, "${CMTROOT}/mgr/cmt %s -%s "
+              fprintf (f, "${CMTROOT}/${CMTBIN}/cmt.exe %s -%s "
                        "-pack=%s -version=%s -path=%s %s $* >${cmt%stempfile}\n",
 		       action.c_str (),
@@ -6276,5 +6275,5 @@
 	      fprintf (f,
 		       "if ( $status != 0 ) then\n"
-		       "  echo \"${CMTROOT}/mgr/cmt %s -%s "
+		       "  echo \"${CMTROOT}/${CMTBIN}/cmt.exe %s -%s "
                        "-pack=%s -version=%s -path=%s %s $* >${cmt%stempfile}\"\n"
 		       "  set cmt%sstatus=2\n"
@@ -6322,8 +6321,7 @@
               fprintf (f, "fi\n");
               fprintf (f, ". ${CMTROOT}/mgr/setup.sh\n");
-              fprintf (f, "cmt%stempfile=`${CMTROOT}/mgr/cmt -quiet build temporary_name`\n", mangled_package.c_str ());
+              fprintf (f, "cmt%stempfile=`${CMTROOT}/${CMTBIN}/cmt.exe -quiet build temporary_name`\n", mangled_package.c_str ());
               fprintf (f, "if test ! $? = 0 ; then cmt%stempfile=/tmp/cmt.$$; fi\n", mangled_package.c_str ());
-              //fprintf (f, "${CMTROOT}/mgr/cmt setup -%s "
-              fprintf (f, "${CMTROOT}/mgr/cmt %s -%s "
+              fprintf (f, "${CMTROOT}/${CMTBIN}/cmt.exe %s -%s "
                        "-pack=%s -version=%s -path=%s %s $* >${cmt%stempfile}\n",
 		       action.c_str (),
@@ -6336,5 +6334,5 @@
               fprintf (f,
 		       "if test $? != 0 ; then\n"
-		       "  echo >&2 \"${CMTROOT}/mgr/cmt %s -%s "
+		       "  echo >&2 \"${CMTROOT}/${CMTBIN}/cmt.exe %s -%s "
                        "-pack=%s -version=%s -path=%s %s $* >${cmt%stempfile}\"\n"
 		       "  cmt%sstatus=2\n"
@@ -6370,5 +6368,4 @@
           else if (mode[i] == Bat)
             {
-              //fprintf (f, "rem Setting %s %s in %%~d0%%~p0\n",
               fprintf (f, "rem %s %s %s in %%~d0%%~p0\n",
 		       action.c_str (),
Index: CMT/HEAD/source/cmt_use.cxx
===================================================================
--- CMT/HEAD/source/cmt_use.cxx	(revision 654)
+++ CMT/HEAD/source/cmt_use.cxx	(revision 655)
@@ -3653,10 +3653,11 @@
 bool Use::get_strategy (const cmt_string& name) const
 {
-  const Project* p =
+  const Project* p = get_project ();
+    /*
     Project::find_by_cmtpath (m_realpath.size ()
 			      ? Project::find_in_cmt_paths (m_realpath, true)
 			      : Project::find_in_cmt_paths (real_path)
 			      );
-  //  const Project* p = Project::find_by_cmtpath (Project::find_in_cmt_paths (real_path));
+    */
 
   bool strategy;
@@ -3798,7 +3799,13 @@
   buffer += "\n";
   
+  const Project* p = m_project
+    ? m_project
+    : Project::find_by_cmtpath (cmtpath)
+    ;
+  /*
   const Project* p (m_project);
   if (!p)
     p = Project::find_by_cmtpath (cmtpath);
+  */
 
   buffer += "macro ";
@@ -4556,8 +4563,11 @@
   static const cmt_string null = "";
 
+  return m_package ? m_package->get_name () : null ;
+  /*
   Package* p = m_package;
   if (p == 0) return (null);
 
   return (p->get_name ());
+  */
 }
 
@@ -5771,4 +5781,9 @@
     return m_project;
 
+  return Project::find_by_cmtpath (m_realpath.size ()
+				   ? Project::find_in_cmt_paths (m_realpath, true)
+				   : Project::find_in_cmt_paths (real_path)
+				   );
+  /*
   cmt_string cmtpath = m_realpath.size ()
     ? Project::find_in_cmt_paths (m_realpath, true)
@@ -5776,4 +5791,5 @@
     ;
   return Project::find_by_cmtpath (cmtpath);
+  */
   /*
   Project* p;
Index: CMT/HEAD/src/cleanup.bash
===================================================================
--- CMT/HEAD/src/cleanup.bash	(revision 655)
+++ CMT/HEAD/src/cleanup.bash	(revision 655)
@@ -0,0 +1,16 @@
+#-----------------------------------------------------------
+# Copyright Christian Arnault LAL-Orsay CNRS
+# arnault@lal.in2p3.fr
+# Written by Grigory Rybkin
+# See the complete license in cmt_license.txt "http://www.cecill.info". 
+#-----------------------------------------------------------
+
+if complete -p cmt >/dev/null 2>&1; then
+  complete -r cmt
+fi
+
+unset -f _cmt
+
+unset -f _cmt_escape_compreply
+
+shopt -u extglob
Index: CMT/HEAD/src/cleanup.csh
===================================================================
--- CMT/HEAD/src/cleanup.csh	(revision 654)
+++ CMT/HEAD/src/cleanup.csh	(revision 655)
@@ -2,23 +2,74 @@
 # Copyright Christian Arnault LAL-Orsay CNRS
 # arnault@lal.in2p3.fr
+# Modified by Grigory Rybkin
 # See the complete license in cmt_license.txt "http://www.cecill.info". 
 #-----------------------------------------------------------
 
-set newpath=
+unsetenv CMTBIN
+unsetenv CMTCONFIG
 
-foreach p ( `echo ${PATH} | sed 's#:# #g'` )
-  echo ${p} | egrep '/CMT/' >& /dev/null
-  set s=${status}
-  if ( "${s}" != 0 ) then
-    if ( ${newpath} == "" ) then
-      set newpath=${p}
+if ( $?CMTROOT == 1 ) then
+if ( $?0 == 0 ) then
+if ( $?tcsh == 1 ) then
+  source ${CMTROOT}/src/cleanup.tcsh
+else if ( "${shell:t}" == "tcsh" ) then
+  source ${CMTROOT}/src/cleanup.tcsh
+endif
+endif
+endif
+
+unsetenv CMTROOT
+
+# Remove CMT entries, if any
+set newpath=()
+foreach p (${path:q})
+  if ( "${p}" !~ "*/CMT/*" ) then
+    if ( ${#newpath} != 0 ) then
+      set newpath=(${newpath:q} ${p:q})
     else
-      set newpath=${newpath}:${p}
+      set newpath=(${p:q})
     endif
   endif
 end
 
-setenv PATH ${newpath}
-unsetenv CMTCONFIG
-unsetenv CMTROOT
+set path=(${newpath:q})
+unset newpath
 
+unalias cmt
+
+if ( $?CLASSPATH == 1 ) then
+if ( "${CLASSPATH}" =~ "*/CMT/*" ) then
+# Remove CMT entries
+  set _p="${CLASSPATH}"
+  while ( "$_p" =~ "*:*" )
+  set _p="${_p:s/:/ /}"
+  end
+
+  set newpath=
+  foreach p (${_p})
+    if ( "${p}" !~ "*/CMT/*" ) then
+      if ( "${newpath}" != "" ) then
+        set newpath=${newpath}:${p}
+      else
+        set newpath=${p}
+      endif
+    endif
+  end
+
+  if ( "${newpath}" != "" ) then
+    setenv CLASSPATH ${newpath}
+  else
+    unsetenv CLASSPATH
+  endif
+  unset _p newpath
+endif
+endif
+
+unalias jcmt
+
+# Unset MAKEFLAGS, if looks set by CMT
+if ( $?MAKEFLAGS == 1 ) then
+if ( "$MAKEFLAGS" =~ "*QUICK=1" ) then
+  unsetenv MAKEFLAGS
+endif
+endif
Index: CMT/HEAD/src/cleanup.sh
===================================================================
--- CMT/HEAD/src/cleanup.sh	(revision 654)
+++ CMT/HEAD/src/cleanup.sh	(revision 655)
@@ -2,20 +2,80 @@
 # Copyright Christian Arnault LAL-Orsay CNRS
 # arnault@lal.in2p3.fr
+# Modified by Grigory Rybkin
 # See the complete license in cmt_license.txt "http://www.cecill.info". 
 #-----------------------------------------------------------
 
-newpath=""
-for p in `echo ${PATH} | sed 's/:/ /g'`; do
-  if test ! "`echo ${p} | egrep CMT`" ; then
-    if test "${newpath}" = "" ; then
-      newpath=${p}
-    else
-      newpath=${newpath}:${p}
+[ -z ${CMTBIN+CMT} ] || unset CMTBIN
+[ -z ${CMTCONFIG+CMT} ] || unset CMTCONFIG
+
+if [ "${CMTROOT+CMT}" = CMT ]; then
+if [ -n "$-" ] && [ "${-#*i}" != "$-" ]; then
+if [ -n "${ZSH_NAME-}" ]; then
+  . ${CMTROOT}/src/cleanup.zsh
+elif [ -n "${BASH-}" ]; then
+  . ${CMTROOT}/src/cleanup.bash
+fi
+fi
+fi
+
+[ -z ${CMTROOT+CMT} ] || unset CMTROOT
+
+cmt_clean_path()
+{
+# Remove CMT entries, if any
+  _np=
+  _p="$1"
+  while [ -n "$_p" ]; do
+    __p="${_p#:}"
+    while [ "$__p" != "$_p" ]; do
+      _p="${__p}"
+      __p="${_p#:}"
+    done
+    _e="${_p%%:*}"
+    _p="${_p#${_e}}"
+    if [ "${_e%/CMT/*}" = "${_e}" ] && [ -n "${_e}" ]; then
+      if [ -n "${_np}" ]; then
+        _np="${_np}:${_e}"
+      else
+        _np="${_e}"
+      fi
     fi
+  done
+  echo "$_np"
+  unset _np _p __p _e
+}
+
+if [ "${PATH%/CMT/*}" != "${PATH}" ]; then
+  newpath=`cmt_clean_path "${PATH}"`
+  PATH=${newpath}
+  unset newpath
+fi
+
+if alias cmt >/dev/null 2>&1; then
+  unalias cmt
+fi
+
+if [ -n "${CLASSPATH-}" ]; then
+if [ "${CLASSPATH%/CMT/*}" != "${CLASSPATH}" ]; then
+  newpath=`cmt_clean_path "${CLASSPATH}"`
+  if [ -n "${newpath}" ]; then
+    CLASSPATH=${newpath}
+  else
+    unset CLASSPATH
   fi
-done
+  unset newpath
+fi
+fi
 
-PATH=${newpath}; export PATH
+if alias jcmt >/dev/null 2>&1; then
+  unalias jcmt
+fi
 
-CMTCONFIG=; export CMTCONFIG
-CMTROOT=; export CMTROOT
+unset -f cmt_clean_path
+
+# Unset MAKEFLAGS, if looks set by CMT
+if [ "${MAKEFLAGS+CMT}" = CMT ]; then
+if [ "${MAKEFLAGS%QUICK=1}" != "${MAKEFLAGS}" ]; then
+  unset MAKEFLAGS
+fi
+fi
Index: CMT/HEAD/src/cleanup.tcsh
===================================================================
--- CMT/HEAD/src/cleanup.tcsh	(revision 655)
+++ CMT/HEAD/src/cleanup.tcsh	(revision 655)
@@ -0,0 +1,8 @@
+#-----------------------------------------------------------
+# Copyright Christian Arnault LAL-Orsay CNRS
+# arnault@lal.in2p3.fr
+# Written by Grigory Rybkin
+# See the complete license in cmt_license.txt "http://www.cecill.info". 
+#-----------------------------------------------------------
+
+uncomplete cmt
Index: CMT/HEAD/src/cleanup.zsh
===================================================================
--- CMT/HEAD/src/cleanup.zsh	(revision 655)
+++ CMT/HEAD/src/cleanup.zsh	(revision 655)
@@ -0,0 +1,14 @@
+#-----------------------------------------------------------
+# Copyright Christian Arnault LAL-Orsay CNRS
+# arnault@lal.in2p3.fr
+# Written by Grigory Rybkin
+# See the complete license in cmt_license.txt "http://www.cecill.info". 
+#-----------------------------------------------------------
+
+[ -z "${ARGC-}" ] && return
+
+compctl + cmt.exe
+
+for fn in $(functions -m + 'cmt_*'); do
+  unset -f $fn
+done
Index: CMT/HEAD/src/setup.csh
===================================================================
--- CMT/HEAD/src/setup.csh	(revision 654)
+++ CMT/HEAD/src/setup.csh	(revision 655)
@@ -6,5 +6,11 @@
 #-----------------------------------------------------------
 
-setenv CMTBIN `uname`-`uname -m | tr -d ' '`
+# Remove spaces, if any
+set _m="`uname -m`"
+while ( "$_m" =~ "* *" )
+set _m="${_m:s/ //}"
+end
+setenv CMTBIN `uname`-"$_m"
+unset _m
 
 if ( $?CMTCONFIG == 0 ) then
@@ -15,23 +21,25 @@
 if ( $?tcsh == 1 ) then
   source ${CMTROOT}/src/setup.tcsh
-else if ( `basename "$shell"` == "tcsh" ) then
+else if ( "${shell:t}" == "tcsh" ) then
   source ${CMTROOT}/src/setup.tcsh
 endif
 endif
 
+# Remove CMT entries, if any
 set newpath=()
-foreach p (${path})
+foreach p (${path:q})
   if ( "${p}" !~ "*/CMT/*" ) then
     if ( ${#newpath} != 0 ) then
-      set newpath=(${newpath} ${p})
+      set newpath=(${newpath:q} ${p:q})
     else
-      set newpath=(${p})
+      set newpath=(${p:q})
     endif
   endif
 end
 
-set path=(${newpath} "${CMTROOT}/${CMTBIN}")
+set path=(${newpath:q} "${CMTROOT}/${CMTBIN}")
+unset newpath
+
 alias cmt '${CMTROOT}/${CMTBIN}/cmt.exe'
-unset newpath
 
 if ( $?CLASSPATH == 0 ) then
@@ -42,6 +50,12 @@
   setenv CLASSPATH ${CLASSPATH}:${CMTROOT}/java/cmt.jar
 else
+# Remove CMT entries
+  set _p="${CLASSPATH}"
+  while ( "$_p" =~ "*:*" )
+  set _p="${_p:s/:/ /}"
+  end
+
   set newpath=
-  foreach p ( `echo ${CLASSPATH} | tr ':' ' '` )
+  foreach p (${_p})
     if ( "${p}" !~ "*/CMT/*" ) then
       if ( "${newpath}" != "" ) then
@@ -58,5 +72,5 @@
     setenv CLASSPATH ${CMTROOT}/java/cmt.jar
   endif
-  unset newpath
+  unset _p newpath
 endif
 
Index: CMT/HEAD/src/setup.sh
===================================================================
--- CMT/HEAD/src/setup.sh	(revision 654)
+++ CMT/HEAD/src/setup.sh	(revision 655)
@@ -6,5 +6,17 @@
 #-----------------------------------------------------------
 
-CMTBIN=`uname`-`uname -m | tr -d ' '`; export CMTBIN
+# Remove spaces, if any
+_m="`uname -m`"
+_nm=
+while [ -n "$_m" ]; do
+  _t="${_m#?}"
+  _a="${_m%${_t}}"
+  if [ "${_a}" != " " ]; then
+    _nm="${_nm}${_a}"
+  fi
+  _m="$_t"
+done
+CMTBIN=`uname`-"$_nm"; export CMTBIN
+unset _m _nm _t _a
 
 if test "${CMTCONFIG}" = "" ; then
@@ -20,23 +32,38 @@
 fi
 
+cmt_clean_path()
+{
+# Remove CMT entries, if any
+  _np=
+  _p="$1"
+  while [ -n "$_p" ]; do
+    __p="${_p#:}"
+    while [ "$__p" != "$_p" ]; do
+      _p="${__p}"
+      __p="${_p#:}"
+    done
+    _e="${_p%%:*}"
+    _p="${_p#${_e}}"
+    if [ "${_e%/CMT/*}" = "${_e}" ] && [ -n "${_e}" ]; then
+      if [ -n "${_np}" ]; then
+        _np="${_np}:${_e}"
+      else
+        _np="${_e}"
+      fi
+    fi
+  done
+  echo "$_np"
+  unset _np _p __p _e
+}
+
 if [ "${PATH%/CMT/*}" = "${PATH}" ]; then
   PATH=${PATH}:${CMTROOT}/${CMTBIN}; export PATH
 else
-  newpath=
-  for p in `echo ${PATH} | tr ':' ' '`; do
-    if [ "${p%/CMT/*}" = "${p}" ]; then
-      if [ -n "${newpath}" ]; then
-        newpath=${newpath}:${p}
-      else
-        newpath=${p}
-      fi
-    fi
-  done
-
+  newpath=`cmt_clean_path "${PATH}"`
   PATH=${newpath}:${CMTROOT}/${CMTBIN}; export PATH
   unset newpath
 fi
 
-alias cmt='${CMTROOT}/${CMTBIN}/cmt.exe'; export cmt
+alias cmt='${CMTROOT}/${CMTBIN}/cmt.exe'
 
 if [ -z "${CLASSPATH-}" ]; then
@@ -45,15 +72,5 @@
   CLASSPATH=${CLASSPATH}:${CMTROOT}/java/cmt.jar; export CLASSPATH
 else
-  newpath=
-  for p in `echo ${CLASSPATH-} | tr ':' ' '`; do
-    if [ "${p%/CMT/*}" = "${p}" ]; then
-      if [ -n "${newpath}" ]; then
-        newpath=${newpath}:${p}
-      else
-        newpath=${p}
-      fi
-    fi
-  done
-
+  newpath=`cmt_clean_path "${CLASSPATH}"`
   if [ -n "${newpath}" ]; then
     CLASSPATH=${newpath}:${CMTROOT}/java/cmt.jar; export CLASSPATH
@@ -64,5 +81,7 @@
 fi
 
-alias jcmt='(java cmt_parser)'; export jcmt
+alias jcmt='(java cmt_parser)'
+
+unset -f cmt_clean_path
 
 # Set MAKEFLAGS, if unset (similar to NPROC with Plan 9 mk utility)
