// http://akelpad.sourceforge.net/forum/viewtopic.php?p=24839#24839
// Version: 2014-05-25
// Author: KDJ
//
// *** AkelPad restart without additional tools ***
//
// Usage:
//   Call("Scripts::Main", 1, "AkelPadRestart.js")

if (typeof AkelPad == "object")
{
  if (WScript.Arguments.length != 2)
    CloseAkelPad();
  else
    OpenFiles(WScript.Arguments(0), parseInt(WScript.Arguments(1)));
}
else if (WScript.Arguments.length == 4)
  RunAkelPad(WScript.Arguments(0), WScript.Arguments(1), parseInt(WScript.Arguments(2)), parseInt(WScript.Arguments(3)));

function CloseAkelPad()
{
  var oFSO        = new ActiveXObject("Scripting.FileSystemObject");
  var sWinDir     = new ActiveXObject("WScript.Shell").ExpandEnvironmentStrings("%WinDir%");
  var sWscriptExe = sWinDir + "\\Sysnative\\wscript.exe";
  var hMainWnd = AkelPad.GetMainWnd();
  var nMDI     = AkelPad.IsMDI();
  var nPID        = AkelPad.SystemFunction().Call("Kernel32::GetCurrentProcessId");
  var MAX_PATH    = 260;
  var lpBuffer    = AkelPad.MemAlloc(MAX_PATH * _TSIZE);
  var sAkelExe;
  var nTabs;
  var i;

  GetLangStrings();

  //on Win7 x64 no file %WinDir%\SysWOW64\qprocess.exe, therefore if AkelPd is 32bit, we run wscript.exe x64
  if (! oFSO.FileExists(sWscriptExe))
    sWscriptExe = sWinDir + "\\System32\\wscript.exe";

  AkelPad.SystemFunction().Call("Kernel32::GetModuleFileName" + _TCHAR, 0, lpBuffer, MAX_PATH);
  sAkelExe = AkelPad.MemRead(lpBuffer, _TSTR)
  AkelPad.MemFree(lpBuffer);

  if (AkelPad.GetEditModified(0) || AkelPad.SendMessage(hMainWnd, 1291 /*AKD_FRAMESTATS*/, 1 /*FWS_COUNTMODIFIED*/, 0))
  {
    switch (AkelPad.MessageBox(hMainWnd, sTxtSaveAll, WScript.ScriptBaseName, 0x23 /*MB_ICONQUESTION|MB_YESNOCANCEL*/))
    {
      case 6 /*IDYES*/:
        if (! AkelPad.Command(nMDI ? 4110 /*IDM_FILE_SAVEALL*/ : 4105 /*IDM_FILE_SAVE*/)) return;
        break;
      case 7 /*IDNO*/:
        if (nMDI)
        {
          nTabs = AkelPad.SendMessage(hMainWnd, 1291 /*AKD_FRAMESTATS*/, 0 /*FWS_COUNTALL*/, 0);
          for (i = 0; i < nTabs; ++i)
            AkelPad.SendMessage(hMainWnd, 1229 /*AKD_SETMODIFY*/, AkelPad.SendMessage(hMainWnd, 1223 /*AKD_GETFRAMEINFO*/, 2 /*FI_WNDEDIT*/, AkelPad.SendMessage(hMainWnd, 1288 /*AKD_FRAMEFIND*/, 8 /*FWF_BYTABINDEX*/, i)), 0);
        }
        else
          AkelPad.SendMessage(hMainWnd, 1229 /*AKD_SETMODIFY*/, 0, 0);
        break;
      case 2 /*IDCANCEL*/:
        return;
    }
  }

  if (nMDI)
  {
    if (oFSO.FileExists(AkelPad.GetAkelDir(4 /*ADTYPE_PLUGS*/) + "\\Sessions.dll"))
    {
      AkelPad.Call("Sessions::Main", 2, "OnExit");
      if (AkelPad.IsPluginRunning("Sessions::Main"))
        AkelPad.Call("Sessions::Main", 10); //stop plugin
    }
    else if (AkelPad.SendMessage(hMainWnd, 1291 /*AKD_FRAMESTATS*/, 0 /*FWS_COUNTALL*/, 0) > 1)
      AkelPad.MessageBox(hMainWnd, sTxtToRestore, WScript.ScriptBaseName, 0x30 /*MB_ICONWARNING*/);
  }

  if (AkelPad.IsPluginRunning("Exit::Main"))
    AkelPad.Call("Exit::Main"); //stop plugin

  AkelPad.Exec('"' + sWscriptExe + '" "' + WScript.ScriptFullName + '" "' + sAkelExe + '" "' + AkelPad.GetEditFile(0) + '" ' + nMDI + ' ' + nPID);
  AkelPad.Command(4109 /*IDM_FILE_EXIT*/);
}

function RunAkelPad(sAkelExe, sEditFile, nMDI, nPID)
{
  var oWshShell = new ActiveXObject("WScript.Shell");

  while (! oWshShell.Run('qprocess.exe ' + nPID, 0 /*hide cmd window*/, 1 /*return errorlevel*/))
    WScript.Sleep(20);

  oWshShell.Run('"' + sAkelExe + '" /Call("Scripts::Main",1,"' + WScript.ScriptName + '",`"' + sEditFile + '" ' + nMDI + '`)');
}

function OpenFiles(sEditFile, nMDI)
{
  if (nMDI && AkelPad.IsMDI() && (new ActiveXObject("Scripting.FileSystemObject").FileExists(AkelPad.GetAkelDir(4 /*ADTYPE_PLUGS*/) + "\\Sessions.dll")))
    AkelPad.Call("Sessions::Main", 1, "OnExit");
  else if (sEditFile)
    AkelPad.OpenFile(sEditFile, 0x002 /*OD_ADT_REG_CODEPAGE*/);
}

function GetLangStrings()
{
  if (AkelPad.GetLangId(0 /*LANGID_FULL*/) == 2052 /*Chinese Simplified*/)
  {
    sTxtSaveAll   = "要在重启之前保存所有已修改文档吗？";
    sTxtToRestore = "若要在重启之后恢复所有标签页，必须安装有插件 Sessions。";
  }
  else
  {
    sTxtSaveAll   = "Save all modified documents before restart?";
    sTxtToRestore = "To restore all tabs after restart, you should install Sessions plugin.";
  }
}
