//==============================================================//
//             MGFloatingLicence Shared Procedures
//==============================================================//
//Author: Miguel (Wenfeng Gao) 
//website: http://mgland.com
//E-mail: mgtoolspro@gmail.com 
//CopyRight Miguel @ mgland animation studio.
//The script is shared between MGTools and MG-PickerStudio, etc.
//==============source codes if necessary=========================
proc initGlobalVars()
{
    global string $gMG_LicenseConnectorDir_ov;
    $gMG_LicenseConnectorDir_ov = "MG_LicenceConnectorDir";
    
    global string $gMG_NewsProgramDir_ov;
    $gMG_NewsProgramDir_ov = "MGLAND_NewsDir";    
    
    global string $gMG_Connector_APPName;
    $gMG_Connector_APPName = "MG-LicenceConnector";
    
    global string $gMG_News_APPName;
    $gMG_News_APPName = "MG-News";
    
    global string $gMG_ConnectorInfoServerFileName = "MG_LicenceConnector.txt";
    global string $gMG_ConnectorInfoServerFile_Dir_Key = "ConnectorDirectory";
}
initGlobalVars;

proc string mg_getProgramExt()
{    
    global int $mgtoolsOStype;
    string $ext = ".exe";
    if($mgtoolsOStype)  //macos && linux
    {
        $ext = "";
    }
    return $ext;
}
global proc string MG_GetConnectorProgramFileName()
{    
    global string $gMG_Connector_APPName;  
    return   ($gMG_Connector_APPName + `mg_getProgramExt`);    
}
global proc string MG_GetNewsProgramFileName()
{       
    global string $gMG_News_APPName;
    return   ($gMG_News_APPName + `mg_getProgramExt`);
    
}

//for getting programes from optionvar and server define  ------------------------------

proc string mg_getServerConnectionFile(string $serverConfigDir)
{
    global string $gMG_ConnectorInfoServerFileName;
    string $file = $serverConfigDir + $gMG_ConnectorInfoServerFileName;
    return $file;
}
proc string mg_getSubfolderAbsolutePath(string $serverConfigDir, string $path)
{
    string $pwd = $serverConfigDir;
    $path = `substring $path 3 (size($path))`;
    return $pwd + $path;
}
proc string mg_getParentfolderAbsolutePath(string $serverConfigDir, string $path)
{
    string $pwd = $serverConfigDir;
    string $pathParts[] = `stringToStringArray $path "/"`;
    string $nonDotParts[];
    for($p in $pathParts)
    {
        if($p == "..")
        {
            $pwd = `dirname $pwd`;
        }
        else
        {
            $nonDotParts[size($nonDotParts)] = $p;
        }
    }
    string $leftoverPath = `stringArrayToString $nonDotParts "/"`;
    if(!`endsWith $pwd "/"`)
    {
        $pwd += "/";
    }
    return $pwd + $leftoverPath;
}
proc string mg_parseServerConnectorPath(string $serverConfigDir, string $path, int $fullPath)
{
    $path = strip($path);
    $path = `substituteAllString $path "\\" "/"`;
    if(`gmatch $path "./*"`)
    {
        $path = `mg_getSubfolderAbsolutePath $serverConfigDir $path`;
    }
    else if(`gmatch $path "../*"`)
    {
        $path = `mg_getParentfolderAbsolutePath $serverConfigDir $path`;
    }
    
    string $programFileName = `MG_GetConnectorProgramFileName`;
    int $isFullPath = `endsWith $path $programFileName`;
    if(!$fullPath && $isFullPath)
    {
        $path = `dirname $path` + "/";
    }
    if($fullPath && !$isFullPath)
    {
        if(!`endsWith $path "/"`)
        {
            $path += "/";
        }
        $path += $programFileName;
    }
    return $path;
}
global proc string MG_GetServerConnector(string $serverConfigDir, int $fullPath)
{
    global string $gMG_ConnectorInfoServerFile_Dir_Key;
    string $file = `mg_getServerConnectionFile $serverConfigDir`;
    if(!`filetest -f $file`)
    {
        return "";
    }
    $readFileID=`fopen $file "r"`;
	while(!`feof $readFileID`)
	{
		$nextLine=strip(`fgetline $readFileID`);
		if (size($nextLine) && !`startsWith $nextLine "#"`)
		{
		    if(`startsWith $nextLine $gMG_ConnectorInfoServerFile_Dir_Key`)
		    {
		        string $datas[]=`stringToStringArray $nextLine "="`;
		        if(size($datas)!=2)
		        {
	                fclose $readFileID;
		            return "";
		        } 
	            fclose $readFileID;
		        return `mg_parseServerConnectorPath $serverConfigDir $datas[1] $fullPath`;
		    }
		}
	}
	fclose $readFileID;
	return "";
}

global proc string MG_getLicenseConnectorDir(string $serverConfigDir, int $fullPath)
{
    global string $gMG_LicenseConnectorDir_ov;  
    int $hasRecord = 0;
    string $dir;
    if(`optionVar -ex $gMG_LicenseConnectorDir_ov`)
    {
        $dir = `optionVar -q $gMG_LicenseConnectorDir_ov`;
        if(`filetest -d $dir`)
        {           
            if($fullPath)
            {
                string $programFileName = `MG_GetConnectorProgramFileName`;
                $dir += $programFileName;
                $hasRecord =`filetest -f $dir`;
            }
            else
            {
                $hasRecord = 1;
            }
        }
    }
    //if no record from optionvar, we check out the server data file:
    if(!$hasRecord)
    {
        string $pathInServerFile = `MG_GetServerConnector $serverConfigDir $fullPath`;
        if(size($pathInServerFile))
        {
            if(!$fullPath)
            {
                if(`filetest -d $pathInServerFile`)
                {
                    return $pathInServerFile;
                }
    		}
    		else
            {
                if(`filetest -f $pathInServerFile`)
                {
                    return $pathInServerFile;
                }
    		}
        }
    }
    //if no record found, we check out the news program:
    if(!$hasRecord)
    {
        global string $gMG_NewsProgramDir_ov;  
        if(!`optionVar -ex $gMG_NewsProgramDir_ov`)
        {
            return "";
        }
        $dir = `optionVar -q $gMG_NewsProgramDir_ov`;
        if(!`filetest -d $dir`)
        {
            return "";
        }
        if($fullPath)
        {
            string $programFileName = `MG_GetConnectorProgramFileName`;
            $dir += $programFileName;
        }
    }
    return $dir;
}
global proc string MG_getNewsProgramDir(string $serverConfigDir, int $fullPath)
{
    global string $gMG_NewsProgramDir_ov;  
    
    string $dir;
    int $hasRecord = 0;
    if(`optionVar -ex $gMG_NewsProgramDir_ov`)
    {
        $dir = `optionVar -q $gMG_NewsProgramDir_ov`;
        if(`filetest -d $dir`)
        {           
            if($fullPath)
            {
                string $programFileName = `MG_GetNewsProgramFileName`;
                $dir += $programFileName;
                $hasRecord = 1;
            }
        }
    }
    //if no record found, we check out the connector program:
    if(!$hasRecord)
    {
        $dir = `MG_getLicenseConnectorDir $serverConfigDir 0`;
        if(!`filetest -d $dir`)
        {
            return "";
        }
        if($fullPath)
        {
            string $programFileName = `MG_GetNewsProgramFileName`;
            $dir += $programFileName;
        }
    }
    return $dir;
}

// save the connector and news to optionvar.------------------------
proc string processDirToSave(string $filePath)
{
    if(!size($filePath))
    {
        return "";
    }
    string $dir = $filePath;
    if(`filetest -f $filePath`)
    {
       $dir = `dirname $filePath`;   
    }
    else
    {
        if(!`filetest -d $filePath`)
        {
            return "";
        }
    }
    if(!`endsWith $dir "/"`)
    {
        $dir += "/";
    }
    return $dir;
}
global proc int MG_SaveLicenseConnectorDir(string $filePath)
{
    string $dir = `processDirToSave $filePath`;
    if(!size($dir))
    {
        return 0;
    }
    global string $gMG_LicenseConnectorDir_ov;  
    optionVar -sv $gMG_LicenseConnectorDir_ov $dir; 
    return 1;          
}

global proc int MG_SaveNewsProgramDir(string $filePath)
{
    
    string $dir = `processDirToSave $filePath`;
    if(!size($dir))
    {
        return 0;
    }
    global string $gMG_NewsProgramDir_ov;  
    optionVar -sv $gMG_NewsProgramDir_ov $dir;
    return 1;            
}

//=========lazy source marker proc=========================
global proc MGFloatLicence_SharedProcs()
{
}