using System;

using System.IO;

using System.Diagnostics;

using IWshRuntimeLibrary;

using Colligo.Properties;

using Colligo.WML;

 

namespace Colligo.Extensions

{

    public class ContributorCreateShortcut

    {

        const string DEVELOPER_KEY = "0K1Q8-075YJ-Q8FEN-GS64P-45JXG";

 

        static int Main (String[] args)

        {

            Trace.Listeners.Add(new ConsoleTraceListener());

            try

            {

                if (args.Length > 0)

                {

                    String folder = args[0];

                    if (Directory.Exists(folder))

                    {

                        return (new ContributorCreateShortcut()).Run(folder);

                    }

                    else

                    {

                        Trace.WriteLine(TraceLevel.Error, "Directory not found.");

                        return -3;

                    }

                }

                else

                {

                    Trace.WriteLine(TraceLevel.Error, "No Directory supplied.");

                    return -2;

                }

            }

            catch (Exception ex)

            {

                Trace.WriteLine(TraceLevel.Error, ex.ToString());

                return -1;

            }

        }

 

        public int Run(String folderPath)

        {

            IWebManager webMgr = null;

 

            try

            {

                const String COLLIGO_KEY = @"SOFTWARE\ColligoOfflineClient\";

                String storageRoot = String.Empty;

                using (Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(COLLIGO_KEY + "General"))

                {

                    storageRoot = key.GetValue("StorageRoot").ToString();

                }

                if (storageRoot.Length == 0)

                {

                    using (Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(COLLIGO_KEY + "General"))

                    {

                        storageRoot = key.GetValue("StorageRoot").ToString();

                    }

                }

                if (storageRoot.Length == 0)

                {

                    Trace.WriteLine(TraceLevel.Error, "Failed to find StorageRoot.");

                    return -5;

                }

 

                WshShell shell = new WshShellClass();

                if (!folderPath.EndsWith(@"\")) folderPath += @"\";

 

                webMgr = WMLApp.GetWebManager();

                webMgr.Initialize(DEVELOPER_KEY);

 

                foreach (IWeb web in webMgr.GetTopLevelWebs())

                {

                    if (web.WebHandle == ulong.MaxValue - 1) continue;

 

                    Trace.WriteLine(TraceLevel.Verbose, "Processing Web. Name='" + web.Name + "'");

                    String shortcutPath = folderPath + web.Name + ".lnk";

                    if (!System.IO.File.Exists(shortcutPath))

                    {

                        WshShortcut shortcut = (WshShortcut)shell.CreateShortcut(shortcutPath);

                        shortcut.TargetPath = storageRoot + @"\Files\" + web.StoragePath;

                        shortcut.Description = web.Name;

                        shortcut.Save();

                    }

                }

 

            }

            catch (Exception ex)

            {

                Trace.WriteLine(TraceLevel.Error, ex.ToString());

                return -4;

            }

            finally

            {

                webMgr.Shutdown();

            }

 

            return 0;

        }

 

    }

}