<?php

	global $arModule;

	include dirname(__FILE__).'/module.php';
	include dirname(__FILE__).'/conf.php';
	include dirname(__FILE__).'/functions.inc.php';
			
	$version = $_SERVER["argv"][1];
	 
	$src_dir = dirname(__FILE__)."/../../wpshopgermany_svn/";
	$targ_dir = dirname(__FILE__)."/../../wpshopgermany/";
	$targ_module_dir = dirname(__FILE__).'/../../wpsg_module/';
 	
	rec_rmdir($targ_dir);
	mkdir($targ_dir);
	  	
	function obfusFile($file)
	{
		
		global $src_dir, $targ_dir, $version;
		
		$target = str_replace($src_dir, $targ_dir, $file);
		 
 		$content = php_strip_whitespace($file);
  		
		if (preg_match("/wpshopgermany\.php/", $file))
		{			
			
			$content = preg_replace("/\<\?php/is", "<?php

	/*
	Plugin Name: wpShopGermany
	Text Domain: wpsg
	Domain Path: /lang
	Plugin URI: http://wpshopgermany.maennchen1.de/
	Description: The first real Wordpress shop plugin for Germany!
	Author: maennchen1.de
	Version: ".$version."
	Author URI: http://maennchen1.de/
	*/

", $content);
	
			$content = preg_replace("/define\(\'WPSG_VERSION\'\,\040\'9\.9\.9\'\)\;/is", "define('WPSG_VERSION', '".$version."');", $content);
				
		}
		
		file_put_contents($target, $content);
 
	}
		
	function copydirr($source, $dest)
	{
		
		if (!is_dir($source))
    	return false; 
        
		$shellBefehl = "cp -Rv $source $dest";
		exec($shellBefehl, $var);
	    
	  return $var;
   
	}
	
	function obfusDir($dir)
	{
						
		global $src_dir, $targ_dir, $arExclude, $arExclude_copy;
						
		$handle = opendir($dir);	
		
		while($file = readdir($handle))
	  	{
	  	
	  		if ($file != ".." && $file != ".")
	   		{	   		

	   			$test_path = str_replace('/scr/wpsg/wpshopgermany_svn/', '', realpath($dir.'/'.$file));
					   			
	   			if (in_array($file, $arExclude_copy))
		  		{
		  		
		  			$target = str_replace($src_dir, $targ_dir, $dir.'/'.$file);
		  		
		  			if (is_file($dir.'/'.$file))
		  			{
		  				copy($dir.'/'.$file, $target);
		  			}	
		  			else
		  			{
		  				copydirr($dir.'/'.$file, $target); 
		  			}
		  		
		  		}	   		
	   			else if (
	   					!preg_match("/\.svn/", $file) && 
	   					!preg_match("/svn/", $file) && 
	   					!in_array($file, $arExclude) &&
	   					!in_array($test_path, $arExclude) &&
	   					!in_array($test_path.'/', $arExclude)
	   			)
	   			{	   			
	   			
		  			if (is_file($dir."/".$file) && preg_match("/(.*)\.php$/", $file))
			  		{ 			  			
			  			obfusFile($dir."/".$file);
			  		}		
		  			else if (is_file($dir."/".$file))
		  			{
		  				$target = str_replace($src_dir, $targ_dir, $dir."/".$file);
			  			copy($dir."/".$file, $target);
			  		}
			  		else if (is_dir($dir."/".$file))
		  			{
		  				mkdir(str_replace($src_dir, $targ_dir, $dir."/".$file));
		  				obfusDir($dir."/".$file);
			  		}
		  		
			  	}
		  	
		   	}
		   	
	  	}
		
		closedir($handle);    
		
	}
	 
	obfusDir($src_dir);
 
	foreach ($arModule as $m)
	{
	 
		// Verzeichnis für das Modul erstellen 
		mkdir($targ_module_dir.'/'.$m['modulname'].'/wpshopgermany', 0777, true);
		
		foreach ($m['files'] as $f)
		{
			
			obfusDirFile($src_dir.'/'.$f, $targ_module_dir.'/'.$m['modulname'].'/wpshopgermany/'.$f, array(), array('/var\040\$version\040=\040\"9.9.9\"\;/' => 'var $version = "'.$version.'";'));
				
		}
				
		if ($m['modulname'] == 'wpsg_mod_paypalapi')
		{
		
			mkdir($targ_module_dir.'/'.$m['modulname'].'/wpshopgermany/mods/mod_paypalapi/');
			
			copydirr(
				$src_dir.'/mods/mod_paypalapi/PayPal/',
				$targ_module_dir.'/'.$m['modulname'].'/wpshopgermany/mods/mod_paypalapi/PayPal/'
			);
				
		}
		
		// Packen
		chdir($targ_module_dir.'/'.$m['modulname']);
		exec('zip -qrT ./../'.$m['modulname'].'_'.$version.'.zip wpshopgermany/*');
		 
		// Verzeichnis anlegen
		if (!file_exists('/var/home/htdocs/dl.maennchen1.de/www/wpsg/'.$m['modulname'].'/')) mkdir('/var/home/htdocs/dl.maennchen1.de/www/wpsg/'.$m['modulname'].'/', 0777, true);
				
		// Versionsdatei setzen
		if (!isset($_SERVER["argv"][2]))
		{
			
			// Live
			file_put_contents(dirname(__FILE__).'/../../wpsg_module_version/'.$m['modulname'], $version.'|'.$version.'|'.$m['url'].'|'.((isset($m['free']) && $m['free'] === true)?'1':'0').'|'.((isset($m['hide']) && $m['hide'] === true)?'1':'0'));
		
			// Archiv kopieren 
			copy(dirname(__FILE__).'/../../wpsg_module/'.$m['modulname'].'_'.$version.'.zip', '/var/home/htdocs/dl.maennchen1.de/www/wpsg/'.$m['modulname'].'/'.$m['modulname'].'_'.$version.'.zip');

			// Symlink Latest anlegen
			if (file_exists('/var/home/htdocs/dl.maennchen1.de/www/wpsg/'.$m['modulname'].'/'.$m['modulname'].'_latest.zip')) unlink('/var/home/htdocs/dl.maennchen1.de/www/wpsg/'.$m['modulname'].'/'.$m['modulname'].'_latest.zip');
			exec('ln -s /var/home/htdocs/dl.maennchen1.de/www/wpsg/'.$m['modulname'].'/'.$m['modulname'].'_'.$version.'.zip /var/home/htdocs/dl.maennchen1.de/www/wpsg/'.$m['modulname'].'/'.$m['modulname'].'_latest.zip'); 

			echo "Modul ".$m['modulname']." Version ".$version." installiert ...\r\n";
			
		}
		else
		{
			
			// Test
			file_put_contents(dirname(__FILE__).'/../../wpsg_module_version/'.$m['modulname'].'_dev', $version.'|'.$version.'|'.$m['url'].'|'.((isset($m['free']) && $m['free'] === true)?'1':'0').'|'.((isset($m['hide']) && $m['hide'] === true)?'1':'0'));
			
			// Archiv kopieren			
			copy(dirname(__FILE__).'/../../wpsg_module/'.$m['modulname'].'_'.$version.'.zip', '/var/home/htdocs/dl.maennchen1.de/www/wpsg/'.$m['modulname'].'/'.$m['modulname'].'_dev.zip');
			
			// Symlinks nutze ich hier nicht, da sonst unnötige Versionen rumliegen
			
			echo "Modul ".$m['modulname']." Version ".$version." installiert ...\r\n";
			
		}
				
	}
	 
?>
