<?xml version="1.0" encoding="utf-8"?>
<PackageClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Version>2.0</Version>
  <Groups>
    <Items>
      <GroupItem Name="Default">
        <ParentGroup />
        <DisplayName>Default</DisplayName>
        <DefaulChecked>true</DefaulChecked>
        <Description>Default</Description>
        <Files>
          <Items>
            <FileItem InstallType="CopyFile" SystemFile="false" Modified="true">
              <Param1 />
              <UpdateOption>AlwaysOverwrite</UpdateOption>
              <LocalFileName>EPGCleanerSetup.exe</LocalFileName>
              <ZipFileName>Installer{CopyFile}\{6c85d720-cef0-4d44-89a3-93cc881cec82}-EPGCleanerSetup.exe</ZipFileName>
              <DestinationFilename>%Temp%\EPGCleanerSetup.exe</DestinationFilename>
            </FileItem>
          </Items>
        </Files>
      </GroupItem>
    </Items>
  </Groups>
  <Sections>
    <Items>
      <SectionItem Guid="c13d0858-4dc7-4074-b913-4eb93b1891ff" Name="[Group] Set state" ConditionGroup="">
        <Params>
          <Items>
            <SectionParam Name="State">
              <Value>NO</Value>
              <ValueType>Bool</ValueType>
              <Description>All included groups will have this state</Description>
            </SectionParam>
          </Items>
        </Params>
        <Actions>
          <Items>
            <ActionItem Name="Script" ActionType="Script" ConditionGroup="Default">
              <Params>
                <Items>
                  <SectionParam Name="Script">
                    <Value>//css_reference "MpeCore.dll";
//css_reference "Ionic.Zip.dll";

using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using Ionic.Zip;
using MpeCore;
using MpeCore.Classes;

public class Script
{
  public static void Main(PackageClass packageClass, ActionItem actionItem)
  {
    string mpePath = packageClass.GeneralInfo.Location;
    string setupPath = Path.Combine(Path.GetTempPath(), "EPGCleanerSetup.exe");

    if (!File.Exists(mpePath))
    {
      MessageBox.Show(String.Format(
        "Unable to find local mpe1 file.{0}" +
        "Please install using the mpe1 package: EPGCleaner_x.x.x.x.mpe1", Environment.NewLine),
        "EPG Cleaner Setup Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
      return;
    }

    try
    {
      using (ZipFile zip = ZipFile.Read(mpePath))
      {
        foreach (ZipEntry entry in zip.Entries)
        {
          if (entry.FileName.EndsWith("EPGCleanerSetup.exe"))
          {
            using (FileStream fs = new FileStream(setupPath, FileMode.Create))
            {
              entry.Extract(fs);
            }
            break;
          }
        }
      }
    }
    catch (Exception ex)
    {
      MessageBox.Show(String.Format(
        "Error extracting setup file: {0}{1}", Environment.NewLine, ex.Message),
        "EPG Cleaner Setup Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
      return;
    }

    try
    {
      if (File.Exists(setupPath))
      {
        Process.Start(setupPath).WaitForExit();
        //File.Delete(setupPath); // moved to post install script
      }
    }
    catch (Exception ex)
    {
      MessageBox.Show(String.Format(
        "Error starting setup wizard:{0}{1}", Environment.NewLine, ex.Message),
        "EPG Cleaner Setup Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
  }
}</Value>
                    <ValueType>Script</ValueType>
                    <Description />
                  </SectionParam>
                  <SectionParam Name="Uninstall Script">
                    <Value>//css_reference "MpeCore.dll";
//css_reference "Ionic.Zip.dll";

using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using Ionic.Zip;
using MpeCore;
using MpeCore.Classes;

public class Script
{
  public static void Main(PackageClass packageClass, UnInstallItem item)
  {
    string mpePath = packageClass.GeneralInfo.Location;
    string setupPath = Path.Combine(Path.GetTempPath(), "EPGCleanerSetup.exe");

    if (!File.Exists(mpePath))
    {
      MessageBox.Show(String.Format(
        "Unable to find local mpe1 file.{0}" +
        "Please uninstall using the mpe1 package: EPGCleaner_x.x.x.x.mpe1", Environment.NewLine),
        "EPG Cleaner Setup Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
      return;
    }

    try
    {
      using (ZipFile zip = ZipFile.Read(mpePath))
      {
        foreach (ZipEntry entry in zip.Entries)
        {
          if (entry.FileName.EndsWith("EPGCleanerSetup.exe"))
          {
            using (FileStream fs = new FileStream(setupPath, FileMode.Create))
            {
              entry.Extract(fs);
            }
            break;
          }
        }
      }
    }
    catch (Exception ex)
    {
      MessageBox.Show(String.Format(
        "Error extracting setup file: {0}{1}", Environment.NewLine, ex.Message),
        "EPG Cleaner Setup Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
      return;
    }

    try
    {
      if (File.Exists(setupPath))
      {
        Process.Start(setupPath, "uninstall").WaitForExit();
        //File.Delete(setupPath); // moved to post uninstall script
      }
    }
    catch (Exception ex)
    {
      MessageBox.Show(String.Format(
        "Error starting setup wizard:{0}{1}", Environment.NewLine, ex.Message), 
        "EPG Cleaner Setup Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
  }
}</Value>
                    <ValueType>Script</ValueType>
                    <Description />
                  </SectionParam>
                </Items>
              </Params>
              <ExecuteLocation>BeforPanelShow</ExecuteLocation>
            </ActionItem>
            <ActionItem Name="Script" ActionType="Script" ConditionGroup="Default">
              <Params>
                <Items>
                  <SectionParam Name="Script">
                    <Value>//css_reference "MpeCore.dll";

using System;
using System.Diagnostics;
using System.IO;
using System.Management;
using System.Windows.Forms;
using MpeCore.Classes;
using MpeCore;

public class Script
{
  public static void Main(PackageClass packageClass, ActionItem actionItem)
  {
    string setupPath = Path.Combine(Path.GetTempPath(), "EPGCleanerSetup.exe");

    try
    {
      if (File.Exists(setupPath))
      {
        File.Delete(setupPath);
      }
    }
    catch (Exception ex)
    {
      MessageBox.Show(String.Format(
        "Error deleting setup file: {0}{1}", Environment.NewLine, ex.Message),
        "EPG Cleaner Setup Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

    // Work-around for uninstall initiated from mpe1 package: [This must be in post-install script]
    // ------------------------------------------------------
    // if mpe1 was launched from outside MPEM then MPEM will assume an install was completed.
    // If the plugin setup performed an uninstall or a cancelled install we must explicitly
    // remove the plugin's entry in InstalledExtensions list and forcibly close MPEM otherwise
    // it will add the plugin details again to InstalledExtensions.xml after script has completed.
    // Check for existance of plugin dll and if not found, remove from InstalledExtensions list.
    // Note: This work-around is not needed in uninstall script because that will only be called
    // by the 'uninstall' option from within MPEM gui, in which case plugin will be removed from 
    // InstalledExtensions list by MPEM.
    // ** I don't like this especially using Kill() but it's the only workable approach to resolve this case.

    string pluginPath = Path.Combine(MpeInstaller.TransformInRealPath("%TvServerPlugins%"), "EPGCleaner.dll");

    if (!File.Exists(pluginPath))
    {
      try
      {
        MpeInstaller.InstalledExtensions.Remove(packageClass);
        MpeInstaller.Save();
        foreach (Process p in Process.GetProcessesByName("MpeInstaller"))
        {
          // extra check here for hidden window incase user has 2nd instance of MpeInstaller open
          if (p.MainWindowHandle == IntPtr.Zero)
          {
              string args = GetCommandLineArgs(p); // confirm MPEM was launched from mpe1 package
              if (args == null || args == packageClass.GeneralInfo.Location)
              {
                p.Kill();
              }
          }
        }
      }
      catch (Exception ex)
      {
        MessageBox.Show(String.Format(
          "Error removing plugin from extensions list.{0}{1}", Environment.NewLine, ex.Message),
          "EPG Cleaner Setup Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
    }
  }

  // get path to mpe1 file which launched MpeInstaller
  // empty if wasn't launched by mpe1 or null if error
  private static string GetCommandLineArgs(Process process)
  {
    try
    {
      string wmiQuery = "SELECT CommandLine FROM Win32_Process WHERE ProcessId = " + process.Id;
      ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiQuery);
      foreach (ManagementObject mObj in searcher.Get())
      {
        string args = mObj["CommandLine"].ToString().Trim('\"');
        if (args.EndsWith(".mpe1", StringComparison.OrdinalIgnoreCase))
        {
          int idx = args.LastIndexOf('\"') + 1;
          if (idx &gt; 0 &amp;&amp; idx &lt; args.Length)
          {
            return args.Substring(idx);
          }
        }
        else { break; }
      }
    }
    catch (Exception)
    {
      return null;
    }
    return String.Empty;
  }
}
</Value>
                    <ValueType>Script</ValueType>
                    <Description />
                  </SectionParam>
                  <SectionParam Name="Uninstall Script">
                    <Value>//css_reference "MpeCore.dll";

using System;
using System.IO;
using System.Windows.Forms;
using MpeCore.Classes;
using MpeCore;

public class Script
{
  public static void Main(PackageClass packageClass, UnInstallItem item)
  {
    string setupPath = Path.Combine(Path.GetTempPath(), "EPGCleanerSetup.exe");

    try
    {
      if (File.Exists(setupPath))
      {
        File.Delete(setupPath);
      }
    }
    catch (Exception ex)
    {
      MessageBox.Show(String.Format(
        "Error deleting setup file: {0}{1}", Environment.NewLine, ex.Message),
        "EPG Cleaner Setup Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
  }
}
</Value>
                    <ValueType>Script</ValueType>
                    <Description />
                  </SectionParam>
                </Items>
              </Params>
              <ExecuteLocation>AfterPanelHide</ExecuteLocation>
            </ActionItem>
          </Items>
        </Actions>
        <IncludedGroups />
        <PanelName>[Group] Set state</PanelName>
        <WizardButtonsEnum>BackNextCancel</WizardButtonsEnum>
      </SectionItem>
    </Items>
  </Sections>
  <Dependencies>
    <Items>
      <DependencyItem>
        <Type>MediaPortal</Type>
        <Id />
        <MinVersion>
          <Major>1</Major>
          <Minor>1</Minor>
          <Build>6</Build>
          <Revision>27644</Revision>
        </MinVersion>
        <MaxVersion>
          <Major>1</Major>
          <Minor>1</Minor>
          <Build>6</Build>
          <Revision>27644</Revision>
        </MaxVersion>
        <WarnOnly>true</WarnOnly>
        <Message>requires MediaPortal version 1.1.6.27644 to 1.1.6.27644.</Message>
        <Name>MediaPortal</Name>
      </DependencyItem>
    </Items>
  </Dependencies>
  <PluginDependencies>
    <Items />
  </PluginDependencies>
  <GeneralInfo>
    <Name>EPG Cleaner</Name>
    <Id>e2588542-34cc-4114-8326-eaf9247d782c</Id>
    <Author>hurley</Author>
    <HomePage>http://www.team-mediaportal.com/extensions/television/epg-cleaner-schedule-repair</HomePage>
    <ForumPage>http://forum.team-mediaportal.com/threads/plugin-epg-cleaner-schedule-repair.133416/</ForumPage>
    <UpdateUrl>http://www.team-mediaportal.com/index.php?option=com_mtree&amp;task=att_download&amp;link_id=358&amp;cf_id=52</UpdateUrl>
    <Version>
      <Major>1</Major>
      <Minor>0</Minor>
      <Build>0</Build>
      <Revision>0</Revision>
    </Version>
    <ExtensionDescription>EPG Cleaner allows EPG data to be processed using regular expression filters.
Remove spam or words like "New" or "Repeat" for a clean, easy to read TV guide and more reliable scheduling. Schedule Repair improves on MediaPortal's scheduling by tracking EPG changes and repairing broken schedules.</ExtensionDescription>
    <VersionDescription>EPG Cleaner plus Schedule Repair version 1.0.0.0</VersionDescription>
    <DevelopmentStatus>Stable</DevelopmentStatus>
    <OnlineLocation>http://www.team-mediaportal.com/index.php?option=com_mtree&amp;task=att_download&amp;link_id=358&amp;cf_id=24</OnlineLocation>
    <ReleaseDate>2016-01-12T19:53:24</ReleaseDate>
    <Tags>epg, tv guide, tv server, schedules, clean, repair, filter, regex</Tags>
    <Location>D:\_VISUAL STUDIO PROJECTS\_MEDIAPORTAL\_EPGCleaner\MPE Installer\EPGCleaner_1.0.0.0.mpe1</Location>
    <Params>
      <Items>
        <SectionParam Name="Icon">
          <Value>LocalIcon\EPGCleaner_Icon_64.png</Value>
          <ValueType>File</ValueType>
          <Description>The icon file of the package (jpg,png,bmp)</Description>
        </SectionParam>
        <SectionParam Name="Online Icon">
          <Value>http://forum.team-mediaportal.com/attachments/epgcleaner_icon_64-png.175045/</Value>
          <ValueType>String</ValueType>
          <Description>The icon file of the package stored online (jpg,png,bmp)</Description>
        </SectionParam>
        <SectionParam Name="Configuration file">
          <Value>%TvServerBase%\SetupTv.exe</Value>
          <ValueType>Template</ValueType>
          <Description>The file used to configure the extension.
 If it has .exe extension the will be executed.
 If it has .dll extension it's started like MP plugin configuration.</Description>
        </SectionParam>
        <SectionParam Name="Online Screenshots">
          <Value>http://www.team-mediaportal.com/media/com_mtree/images/listings/o/2695.png;
http://www.team-mediaportal.com/media/com_mtree/images/listings/o/2696.png;
http://www.team-mediaportal.com/media/com_mtree/images/listings/o/2697.png;
http://www.team-mediaportal.com/media/com_mtree/images/listings/o/2698.png;</Value>
          <ValueType>String</ValueType>
          <Description>Online stored screenshot urls separated by ; </Description>
        </SectionParam>
        <SectionParam Name="Force to uninstall on update">
          <Value>NO</Value>
          <ValueType>Bool</ValueType>
          <Description>Show dialog and force to uninstall previous version when updating an extension. Should only be disabled if you are using an NSIS/MSI installer.</Description>
        </SectionParam>
      </Items>
    </Params>
  </GeneralInfo>
  <UniqueFileList>
    <Items>
      <FileItem InstallType="CopyFile" SystemFile="false" Modified="true">
        <Param1 />
        <UpdateOption>AlwaysOverwrite</UpdateOption>
        <LocalFileName>EPGCleanerSetup.exe</LocalFileName>
        <ZipFileName>Installer{CopyFile}\{6c85d720-cef0-4d44-89a3-93cc881cec82}-EPGCleanerSetup.exe</ZipFileName>
        <DestinationFilename>%Temp%\EPGCleanerSetup.exe</DestinationFilename>
      </FileItem>
      <FileItem InstallType="CopyFile" SystemFile="true" Modified="true">
        <Param1 />
        <UpdateOption>OverwriteIfOlder</UpdateOption>
        <LocalFileName>LocalIcon\EPGCleaner_Icon_64.png</LocalFileName>
        <ZipFileName>Installer{CopyFile}\{04f952e2-501b-4233-9ded-64db426af7d2}-EPGCleaner_Icon_64.png</ZipFileName>
        <DestinationFilename />
      </FileItem>
    </Items>
  </UniqueFileList>
  <ProjectSettings>
    <FolderGroups />
    <ProjectFilename>EPGCleaner.xmp2</ProjectFilename>
    <UpdatePath1>D:\_VISUAL STUDIO PROJECTS\_MEDIAPORTAL\_EPGCleaner\MPE Installer\EPGCleaner.update.xml</UpdatePath1>
    <UpdatePath2 />
    <UpdatePath3 />
  </ProjectSettings>
  <IsSkin>false</IsSkin>
</PackageClass>