Index: trunk/xpressme_integration_kit/include/onupdate.php
===================================================================
--- trunk/xpressme_integration_kit/include/onupdate.php	(revision 456)
+++ trunk/xpressme_integration_kit/include/onupdate.php	(revision 462)
@@ -50,6 +50,10 @@
 // make templates
 	include_once XOOPS_ROOT_PATH . '/modules/' . $mydirname . '/include/xpress_templates_make.php' ;
+	$mod_version = $module->getVar('version') ;
+
+	$t_mess = xpress_clean_templates_file($mydirname,$mod_version);
+	$msgs = array_merge($msgs,$t_mess);
+	
 	$t_mess = xpress_templates_make($mid,$mydirname);
-	
 	$msgs = array_merge($msgs,$t_mess);
 
Index: trunk/xpressme_integration_kit/include/xpress_templates_make.php
===================================================================
--- trunk/xpressme_integration_kit/include/xpress_templates_make.php	(revision 456)
+++ trunk/xpressme_integration_kit/include/xpress_templates_make.php	(revision 462)
@@ -7,4 +7,6 @@
 	$tplfile_handler =& xoops_gethandler( 'tplfile' ) ;
 	$tpl_path = XOOPS_ROOT_PATH . '/modules/' . $mydirname . '/templates' ;
+	
+	//copy  template file from source
 	if( $handler = @opendir( $tpl_path . '/source/' ) ) {
 		while( ( $file = readdir( $handler ) ) !== false ) {
@@ -26,34 +28,19 @@
 					$msgs[] = '<span style="color:#ff0000;">ERROR: Could not copy template <b>'.htmlspecialchars($target_file_name).'</b> from ' . htmlspecialchars($file). '(check templates directory permision (777))</span><br />';
 				}					
-				$tplfile =& $tplfile_handler->create() ;
-				if ($rcd) {
-					$mtime = intval( @filemtime( $target_file_path ) ) ;
-					$tplfile->setVar( 'tpl_source' , file_get_contents( $target_file_path ) , true ) ;
-				} else {
-					$mtime = intval( @filemtime( $file_path ) ) ;
-					$tplfile->setVar( 'tpl_source' , file_get_contents( $file_path ) , true ) ;
-				}
-
-				$tplfile->setVar( 'tpl_refid' , $mid ) ;
-				$tplfile->setVar( 'tpl_tplset' , 'default' ) ;
-				$tplfile->setVar( 'tpl_file' , $target_file_name ) ;
-				$tplfile->setVar( 'tpl_desc' , '' , true ) ;
-				$tplfile->setVar( 'tpl_module' , $mydirname ) ;
-				$tplfile->setVar( 'tpl_lastmodified' , $mtime ) ;
-				$tplfile->setVar( 'tpl_lastimported' , 0 ) ;
-				$tplfile->setVar( 'tpl_type' , 'module' ) ;
-				if( ! $tplfile_handler->insert( $tplfile ) ) {
-					$msgs[] = '<span style="color:#ff0000;">ERROR: Could not insert template <b>'.htmlspecialchars($target_file_name).'</b> to the database.</span><br />';
-				} else {
-					$tplid = $tplfile->getVar( 'tpl_id' ) ;
-					$msgs[] = 'Template <b>'.htmlspecialchars($target_file_name) .'</b> added to the database. (ID: <b>'.$tplid.'</b>)<br />';
-					// generate compiled file
-					include_once XOOPS_ROOT_PATH.'/class/xoopsblock.php' ;
-					include_once XOOPS_ROOT_PATH.'/class/template.php' ;
-					if( ! xoops_template_touch( $tplid ) ) {
-						$msgs[] = '<span style="color:#ff0000;">ERROR: Failed compiling template <b>'.htmlspecialchars($target_file_name).'</b>.</span><br />';
-					} else {
-						$msgs[] = 'Template <b>'.htmlspecialchars($target_file_name).'</b> compiled.</span><br />';
-					}
+			}
+		}
+		closedir( $handler ) ;
+	}
+	
+	// template added to the database.
+	if( $handler = @opendir( $tpl_path . '/' ) ) {
+		while( ( $file = readdir( $handler ) ) !== false ) {
+			if( substr( $file , 0 , 1 ) == '.' ) continue ;
+			$file_path = $tpl_path . '/' . $file ;
+			$file_name = $file ;
+			$pattern = '^' . $mydirname . '_';
+			if (preg_match('/' . $pattern . '/' , $file_name, $match)){
+				if( is_file( $file_path ) ) {
+					$msgs[] = xpress_create_db_template($file_name,$file_path,$mydirname,$mid);
 				}
 			}
@@ -61,4 +48,5 @@
 		closedir( $handler ) ;
 	}
+	
 	include_once XOOPS_ROOT_PATH.'/class/xoopsblock.php' ;
 	include_once XOOPS_ROOT_PATH.'/class/template.php' ;
@@ -68,4 +56,35 @@
 }
 
+function xpress_create_db_template($file_name,$file_path,$mydirname,$mid){
+	if( is_file( $file_path ) ) {
+		$tplfile_handler =& xoops_gethandler( 'tplfile' ) ;
+		$tplfile =& $tplfile_handler->create() ;
+		$mtime = intval( @filemtime( $file_path ) ) ;
+		$tplfile->setVar( 'tpl_source' , file_get_contents( $file_path ) , true ) ;
+		$tplfile->setVar( 'tpl_refid' , $mid ) ;
+		$tplfile->setVar( 'tpl_tplset' , 'default' ) ;
+		$tplfile->setVar( 'tpl_file' ,  $file_name ) ;
+		$tplfile->setVar( 'tpl_desc' , '' , true ) ;
+		$tplfile->setVar( 'tpl_module' , $mydirname ) ;
+		$tplfile->setVar( 'tpl_lastmodified' , $mtime ) ;
+		$tplfile->setVar( 'tpl_lastimported' , 0 ) ;
+		$tplfile->setVar( 'tpl_type' , 'module' ) ;
+		if( ! $tplfile_handler->insert( $tplfile ) ) {
+			$msg = '<span style="color:#ff0000;">ERROR: Template Could not added to the database. <b>'.htmlspecialchars($file_name).'</b> to the database.</span><br />';
+		} else {
+			$tplid = $tplfile->getVar( 'tpl_id' ) ;
+			$msg = 'Template <b>'.htmlspecialchars($file_name) .'</b> added to the database. (ID: <b>'.$tplid.'</b>)<br />';
+			// generate compiled file
+			include_once XOOPS_ROOT_PATH.'/class/xoopsblock.php' ;
+			include_once XOOPS_ROOT_PATH.'/class/template.php' ;
+			if( ! xoops_template_touch( $tplid ) ) {
+				$msg = '<span style="color:#ff0000;">ERROR: Failed compiling template <b>'.htmlspecialchars($file_name).'</b>.</span><br />';
+			} else {
+				$msg = 'Template <b>'.htmlspecialchars($file_name).'</b> compiled.</span><br />';
+			}
+		}
+	}
+	return $msg;
+}
 
 function is_template($file_name){
@@ -96,4 +115,46 @@
 }
 
+function xpress_clean_templates_file($mydirname,$mod_version)
+{
+	$tpl_path = XOOPS_ROOT_PATH . '/modules/' . $mydirname . '/templates/' ;
+	$msgs = array();
+	if( $handler = @opendir( $tpl_path) ) {
+		while( ( $file = readdir( $handler ) ) !== false ) {
+			if( substr( $file , 0 , 1 ) == '.' ) continue ;
+			if ( strcmp($file,'source')==0 ) continue ;
+			if ( strcmp($file,'index.html')==0 ) continue ;
+			$target_file_path = $tpl_path . $file;
+			if (is_dir($target_file_path)){
+				rmDirectory($target_file_path);
+				$msgs[] = 'Template <b>'.htmlspecialchars($file).'</b> directory deleted.</span><br />';
+			} else {
+				if ($mod_version >= 200){
+					$pattern = '^' . $mydirname . '_';
+					if (preg_match('/' . $pattern . '/' , $file, $match))  continue ;
+				}
+				$rcd = @unlink($target_file_path);
+				$msgs[] = 'Template <b>'.htmlspecialchars($file).'</b> file deleted.</span><br />';
+			}
+		}
+		closedir( $handler ) ;
+	}
+	return $msgs;
+}
+
+function rmDirectory($dir){
+	if ($handle = opendir("$dir")){
+		while (false !== ($item = readdir($handle))){
+			if ($item != "." && $item != ".."){
+				if (is_dir("$dir/$item")){
+					rmDirectory("$dir/$item");
+				}else{
+					unlink("$dir/$item");
+				}
+			}
+		}
+		closedir($handle);
+		rmdir($dir);
+	}
+}
 
 ?>
