//.. 解压安装
import fsys;
import process.popen; 
import inet.downBox;
import inet.url;
namespace fsys.cab;

var dll = ..raw.loadDll("Setupapi.dll") 
SetupIterateCabinet =  dll.api("SetupIterateCabinetA","bool(string cabinetFile,INT reserved,pointer msgHandler,pointer context)")

class FILE_IN_CABINET_INFO {
    pointer NameInCabinet;
    INT FileSize;
    INT Win32Error;
    WORD  DosDate;
    WORD  DosTime;
    WORD  DosAttribs;
    BYTE  FullTargetName[0x104/*_MAX_PATH*/];
}  
class FILEPATHS {
    string  Target;
    string  Source;  
    INT   Win32Error;
    INT  Flags;  
} 
 
var callback = ..raw.tostdcall(  
	function(targetDir,notification,param1,param2){
		var lRetVal = 0x0/*_NO_ERROR*/; 
		
 		select(notification) {
 			case 0x11/*_SPFILENOTIFY_FILEINCABINET*/{
  				var info =  ..raw.convert( param1,FILE_IN_CABINET_INFO() ) 
  				info.FullTargetName =  ..fsys.joinpath(targetDir,..raw.tostring(info.NameInCabinet) );
  				..raw.mixin( param1, info );
  				lRetVal = 1/*_FILEOP_DOIT*/;  // Extract the file.
  			}
 			//case 0x13/*_SPFILENOTIFY_FILEEXTRACTED*/{  
  				/*
				var paths = ..raw.convert(param1,FILEPATHS() ) 
  				if( ..string.endWith( paths.Target,".cab",true) ){ 
  					if( extract( paths.Target ) ){
  						..io.remove( paths.Target )
  					} 
  				}
				*/
  			//}
 			//case 0x12/*_SPFILENOTIFY_NEEDNEWCABINET*/{
			//} 
 		}
 		return lRetVal;

	},"INT(string context,INT notification,pointer param1,pointer param2)"
)
	
extract = function(path,targetDir){  
	if(! ..io.exist(path,4) )
		error("cab文件路径错误",2)
	
	if(!targetDir){
		targetDir = ..string.match(path,"(.+)\.[^.]+$")
	}	
	if(!targetDir)
		error("解压目录错误",2)
	 
	..fsys.createDir(targetDir); 
	
	if( SetupIterateCabinet(..io.fullpath(path),0,callback,..io.fullpath(targetDir)) ){
		var ret = true;
		..fsys.enum(  targetDir,  "*.cab", 
			function(dir,file,fullpath,findData){ 
				if(file){ 
	           		if( extract(fullpath) ) {
	           			 ..io.remove(fullpath) ;
	           		}
	           		else {
	           			ret = false;
	           			return false;
	           		}
	           		
				} 
			}  
		)
		return ret;
	}
}

download = function(url,title="自动更新程序",downloadDir){
	var tUrl = ..inet.url.split(url);
	if(!tUrl){
		error("错误的URL参数",2)
	}
	if(!downloadDir){
		downloadDir = ..fsys.joinpath(..io._exedir,"\download\" )
	}
	else {
		downloadDir = ..io.fullpath(downloadDir)
	}
	..fsys.createDir(downloadDir);
	
	//download file
	var tPath = ..io.splitpath(tUrl.path)
	var pathCabinet = ..fsys.joinpath(downloadDir,tPath.file)
	var downBox = ..inet.downBox(,title,true) 
	downBox.download(url,pathCabinet ) 
	
	//download failed
	if(!downBox.complete){ 
		return;
	}
		
	//extract file
	var extraDir = ..fsys.joinpath(downloadDir,tPath.name)
	if( ! ..io.exist(pathCabinet,4) ){  
		return false; 
	}
	if( ! ..fsys.cab.extract(pathCabinet,extraDir ) ) { 
		return false;
	}
 
	var setupFile = ..fsys.joinpath(extraDir,"setup.aau")

	//setup file
	if( ..io.exist(setupFile) ){
		var handle = ..thread.create( setupFile ); 
		if(..win.closed ){
			..thread.wait(handle) 
		}
		else {
			..thread.waitOne(handle) 
		}  
	}
	return true; 		
}
  

/**intellisense(fsys.cab)
extract("\filename->cab","\tagetDir") = 解压cab文件,参数一指定cab文件,\n如果cab文件中包含cab文件,自动展开为子文件夹,\n参数二为可选参数,指定解压目录
download(.(URL,下载对话框标题,下载目录) = 下载cab文件,\n如果cab文件包含setup.aau,自动执行该文件安装.\n参数2,参数3 为可选参数
end intellisense**/

