// This file is a simple script to configure SecondLife to use an Open Search page 
// that gives access to multiple search providers from within Second Life
// Just save this file to disk and then execute it (on windows)
// it modifies just two lines of the 
//     <user>\SecondLife\user_settings\settings*.xml files
// and saves a backup copy of the file
//
// Copyright (c) 2007-2008 Metaverse Ink. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

WshShell = WScript.CreateObject("WScript.Shell");
path = WshShell.SpecialFolders("AppData");

fso = new ActiveXObject("Scripting.FileSystemObject");
folder = fso.GetFolder(path + "\\SecondLife\\user_settings");

for (files = new Enumerator(folder.Files); !files.atEnd(); files.moveNext())

{ 
	var file = files.item(); 
	
	count = 0;
	prefix = "settings";
	suffix = ".xml";
	name = file.Name.toLowerCase();
	if (name.substring(0, prefix.length) == prefix
	    && name.substring(name.length - suffix.length, name.length) == suffix)
 	{
        backupPath =  file.ParentFolder.Path + "\\backup_" + file.Name;

        OpenFileForReading = 1;
        OpenFileForWriting = 2;

        WScript.Echo("Configuring " + file.Path);
        // back up the original file
        oldFile = fso.OpenTextFile(file.Path, OpenFileForReading, false);
        backupFile = fso.OpenTextFile(backupPath, OpenFileForWriting, true);
        while (!oldFile.AtEndOfStream) {
            line = oldFile.ReadLine();
            backupFile.WriteLine(line);
        }
        oldFile.Close();
        backupFile.Close();

        // copy the original file to the new file
        sDefault = "http://www.MetaverseInk.com/OpenSearch?m=[MATURE]";
        sQuery = "http://www.MetaverseInk.com/OpenSearch?q=[QUERY]&amp;s=[COLLECTION]&amp;";
        sExtra = "m=[MATURE]&amp;t=[TEEN]&amp;region=[REGION]&amp;x=[X]&amp;y=[Y]&amp;z=[Z]";
        backupFile = fso.OpenTextFile(backupPath, OpenFileForReading, false);
        newFile = fso.OpenTextFile(file.Path, OpenFileForWriting, true);
        while (!backupFile.AtEndOfStream) {
            line = backupFile.ReadLine();
            if (line.indexOf("<SearchURLDefault ") >= 0 && sDefault != null)
            {
	            line = "\t<SearchURLDefault value=\"" + sDefault + "\" />";
	            sDefault = null;
	        }
            else if (line.indexOf("<SearchURLQuery ") >= 0 && sQuery != null)
            {
	            line = "\t<SearchURLQuery value=\"" + sQuery + "\" />";
	            sQuery = null;
	        }
	        // previous version
            else if (line.indexOf("<SearchDefaultURL ") >= 0 && sDefault != null)
            {
	            line = "\t<SearchDefaultURL value=\"" + sDefault + "\" />";
	            sDefault = null;
	        }
            else if (line.indexOf("<SearchQueryURL ") >= 0 && sQuery != null)
            {
	            line = "\t<SearchQueryURL value=\"" + sQuery + sExtra + "\" />";
	            sQuery = null;
	        }
            else if (line.indexOf("</settings>") >= 0)
            {
                if (sDefault != null)
                {
                    newFile.WriteLine("\t<SearchDefaultURL value=\"" + sDefault + "\" />");
                    newFile.WriteLine("\t<SearchURLDefault value=\"" + sDefault + "\" />");
                }
                if (sQuery != null)
                {
                    newFile.WriteLine("\t<SearchURLQuery value=\"" + sQuery + "\" />");
                    newFile.WriteLine("\t<SearchQueryURL value=\"" + sQuery + sExtra + "\" />");
                }
	        }
            newFile.WriteLine(line);
        }
        backupFile.Close();
        newFile.Close();
        count++;
    }
}
if (count > 0)
    WScript.Echo("Second Life configured for Open Search");
else
    WScript.Echo("Could not locate configuration files. Please download the Second Life release candidate.");

