var fso = new ActiveXObject("Scripting.FileSystemObject");
var wsh = new ActiveXObject("WScript.Shell");

var objArgs = WScript.Arguments;
if (objArgs.length <= 0) {
  try {
    wsh.RegRead("HKCR\\.b$s\\");
    wsh.RegRead("HKCR\\b$sfile\\");
  }
  catch (e) {
    if (wsh.Popup("Install \"New -> ImajSite Build Script\"?", 0, "Install", 4 + 32) != 6)
      WScript.Quit();
    var scriptPath = WScript.ScriptFullName;
    wsh.RegWrite("HKCR\\.b$s\\", "b$sfile");
    wsh.RegWrite("HKCR\\.b$s\\ShellNew\\Command", scriptPath + ' "%1" "%2"');
    wsh.RegWrite("HKCR\\b$sfile\\", "ImajSite Build Script");
    wsh.RegWrite("HKCR\\b$sfile\\shell\\open\\command\\", 'NOTEPAD.EXE "%1"');
    wsh.Popup("\"New -> ImajSite Build Script\"\nhas been installed\n\n" +
              "To uninstall, run this script again.", 0, "Install", 64);
    WScript.Quit();
  }
  if (wsh.Popup("Remove \"New -> ImajSite Build Script\"?", 0, "Uninstall", 4 + 32) != 6)
    WScript.Quit();
  wsh.RegDelete("HKCR\\.b$s\\ShellNew\\Command");
  wsh.RegDelete("HKCR\\.b$s\\ShellNew\\");
  wsh.RegDelete("HKCR\\.b$s\\");
  wsh.RegDelete("HKCR\\b$sfile\\shell\\open\\command\\");
  wsh.RegDelete("HKCR\\b$sfile\\shell\\open\\");
  wsh.RegDelete("HKCR\\b$sfile\\shell\\");
  wsh.RegDelete("HKCR\\b$sfile\\");
  wsh.Popup("\"New -> ImajSite Build Script\"\nhas been removed.", 0, "Uninstall", 64);
  WScript.Quit();
}

var path = "";
if (objArgs.length >= 1)
  path = objArgs.Item(0);
if (!path && objArgs.length >= 2)
  path = objArgs.Item(1);
if (!path) {
  WScript.Echo("Error:  The file was not specified");
  WScript.Quit();
}

if (fso.FileExists(path))
  fso.DeleteFile(path);
var pathLen = path.lastIndexOf('\\');
if (pathLen >= 0)
  path = path.substr(0, pathLen + 1);
else {
  WScript.Echo("Error:  Invalid path \"" + path + "\"");
  WScript.Quit();
}

f = fso.CreateTextFile(path + "makeindx.bat", true);
f.WriteLine("@mkhtmset");

//*****************************************************************************
// This section may be customized to add specific lines to the makeindx.bat
// file as needed:

if (fso.FileExists(path + "bigthumb.jpg"))
  f.WriteLine("template=bigthumb.htt");

//*****************************************************************************

f.Close();
