--
-- Electronica-Tune.lua
--
PLUGIN_NAME = "Electronica-Tune"
--
-- Copyright (C) 2015 Yamaha Corporation
--

PROC_INTVL = 1000 -- Recover (msec)
PROC_LEN   = 60   -- Tolerance (msec) 

--
-- プラグインマニフェスト関数.
--
function manifest()
    myManifest = {
        name          = PLUGIN_NAME,
        comment       = "Change to \"Electronica-Style\".",
        author        = "Yamaha Corporation",
        pluginID      = "{ECE04958-46AE-4d9b-A623-40B077E7FEC0}",
        pluginVersion = "1.0.0.1",
        apiVersion    = "4.2.0.1"
    }
	return myManifest
end

--
-- スクリプトのエントリポイント関数.
--
function main(processParam, envParam)

	local retCode = 0
	-- 実行時に渡された実行環境パラメータを取得する.
	local OSType = checkOS(envParam.tempDir)
	if( OSType == "Mac") then
		retCode,envParam.scriptDir,envParam.scriptName,envParam.tempDir = modifyPath(envParam.scriptDir,envParam.scriptName,envParam.tempDir)
	end
	if( retCode ~= 0 ) then
		return retCode
	end
	
		
	local scriptDir  = envParam.scriptDir	-- Luaスクリプトが配置されているディレクトリパス（末尾にデリミタ "\" を含む）.
	local scriptName = envParam.scriptName	-- Luaスクリプトのファイル名.
	local tempDir    = envParam.tempDir		-- Luaプラグインが利用可能なテンポラリディレクトリパス（末尾にデリミタ "\" を含む）.
	local workPath   = tempDir .. PLUGIN_NAME -- プラグイン専用の作業ディレクトリ（末尾のデリミタ "\" は含めない）.

	if( OSType == "Mac") then
		package.path = scriptDir .. "bin/?.lub;" .. package.path
	else
		package.path = ".\\bin/?.lub;" .. package.path
	end
	--VSMessageBox("package.path=" .. package.path,0)

	retCode = pcall(require,"ET")
	if( retCode == false ) then
		require("ET64")
	end
	
	envParam['workPath'] = workPath
	envParam['OSType']   = OSType

	local endStatus = ETmain(processParam,envParam)

	if( endStatus == 0) then
		VSMessageBox("パートをピッチスナップモードに変更しました\nTurned on Pitch Snap Mode for this musical part.",0)
	end

	return endStatus
end
	
-- //--------------------------
function checkOS(tempDir)
	if(string.find(tempDir,"/VOCALOID%d* Editor for Cubase/JOB_PLUGINS") ~= nil) then
		return "Mac"
	elseif(string.find(tempDir,"\\VOCALOID%d* Editor for Cubase\\JOB_PLUGINS\\") ~= nil) then
		return "Win" --VOCALOID Editor for Cubase
	elseif(string.find(tempDir,"(\\VOCALOID%d*\\JOB_PLUGINS)") ~= nil) then
		return "Win" --Std. VOCALOID Editor
	end
	return "Unknown"
end

function modifyPath(scriptDir,scriptName,tempDir)
	local FH
	local message 

	scriptName = scriptName:match("(.*.lua)")

	local FH
	local message 
	local scriptDirTmp = scriptDir
	while string.len(scriptDirTmp) > 0 do
		local scriptFile = scriptDirTmp .. "/" .. scriptName
		FH,message = io.open(scriptFile,"r")
		if( FH ~= nil ) then
			FH:close()
			break
		end
		scriptDirTmp = string.sub(scriptDirTmp,1,string.len(scriptDirTmp)-1)
	end
	
	if( string.len(scriptDirTmp) == 0 ) then
		VSMessageBox("Cannot find scriptDir!",0)
		return 1,"","",""
	end
	
	if( scriptDir:match("/$") == nil ) then
		scriptDir = scriptDirTmp .. "/"
	end
	package.path = package.path .. ";" .. scriptDir .. "?.lua"
	tempDir = tempDir:gsub("\\$","/")
	
	return 0 , scriptDir, scriptName , tempDir
end
