From 74ed25108f5a5c4771e001aa9921f175872c076e Mon Sep 17 00:00:00 2001
From: Stefan Saraev <stefan@saraev.ca>
Date: Fri, 24 Apr 2015 19:45:20 +0300
Subject: [PATCH 09/13] use a wrapper to setup systemd services

---
 xbmc/addons/Addon.cpp | 30 ++++++++++++++++++++++++++++++
 xbmc/addons/Addon.h   |  2 ++
 2 files changed, 32 insertions(+)

diff --git a/xbmc/addons/Addon.cpp b/xbmc/addons/Addon.cpp
index 377a49c..2fda194 100644
--- a/xbmc/addons/Addon.cpp
+++ b/xbmc/addons/Addon.cpp
@@ -638,6 +638,13 @@ AddonVersion CAddon::GetDependencyVersion(const std::string &dependencyID) const
   return AddonVersion("0.0.0");
 }
 
+void CallOEWrapper(const std::string& ID, bool disable)
+{
+  char cmd[255];
+  snprintf(cmd, sizeof(cmd), "/usr/lib/libreelec/systemd-addon-wrapper %s %d %d", ID.c_str(), disable);
+  system(cmd);
+}
+
 void OnEnabled(const std::string& id)
 {
   // If the addon is a special, call enabled handler
@@ -646,6 +653,11 @@ void OnEnabled(const std::string& id)
       CAddonMgr::GetInstance().GetAddon(id, addon, ADDON_ADSPDLL))
     return addon->OnEnabled();
 
+  // OE: systemctl enable & start on addon enable
+  if (CAddonMgr::GetInstance().GetAddon(id, addon, ADDON_SERVICE))
+    CallOEWrapper(addon->ID(), false);
+  // OE
+
   if (CAddonMgr::GetInstance().GetAddon(id, addon, ADDON_SERVICE))
     std::static_pointer_cast<CService>(addon)->Start();
 
@@ -663,6 +675,11 @@ void OnDisabled(const std::string& id)
   if (CAddonMgr::GetInstance().GetAddon(id, addon, ADDON_SERVICE, false))
     std::static_pointer_cast<CService>(addon)->Stop();
 
+  // OE: systemctl stop & disable on addon disable
+  if (CAddonMgr::GetInstance().GetAddon(id, addon, ADDON_SERVICE, false))
+    CallOEWrapper(addon->ID(), true);
+  // OE
+
   if (CAddonMgr::GetInstance().GetAddon(id, addon, ADDON_CONTEXT_ITEM, false))
     CContextMenuManager::GetInstance().Unregister(std::static_pointer_cast<CContextMenuAddon>(addon));
 }
@@ -686,6 +703,14 @@ void OnPreInstall(const AddonPtr& addon)
 void OnPostInstall(const AddonPtr& addon, bool update, bool modal)
 {
   AddonPtr localAddon;
+  // OE: systemctl stop & disable / enable & start on addon upgrade
+  if (CAddonMgr::GetInstance().GetAddon(addon->ID(), localAddon, ADDON_SERVICE))
+  {
+    CallOEWrapper(addon->ID(), true);
+    CallOEWrapper(addon->ID(), false);
+  }
+  // OE
+
   if (CAddonMgr::GetInstance().GetAddon(addon->ID(), localAddon, ADDON_SERVICE))
     std::static_pointer_cast<CService>(localAddon)->Start();
 
@@ -720,6 +745,11 @@ void OnPostInstall(const AddonPtr& addon, bool update, bool modal)
 void OnPreUnInstall(const AddonPtr& addon)
 {
   AddonPtr localAddon;
+  // OE: systemctl stop & disable on addon ininstall
+  if (CAddonMgr::GetInstance().GetAddon(addon->ID(), localAddon, ADDON_SERVICE))
+    CallOEWrapper(addon->ID(), true);
+  // OE
+
   if (CAddonMgr::GetInstance().GetAddon(addon->ID(), localAddon, ADDON_SERVICE))
     std::static_pointer_cast<CService>(localAddon)->Stop();
 
diff --git a/xbmc/addons/Addon.h b/xbmc/addons/Addon.h
index d0a40d3..0742374 100644
--- a/xbmc/addons/Addon.h
+++ b/xbmc/addons/Addon.h
@@ -173,6 +173,8 @@ public:
   const InfoMap &ExtraInfo() const { return m_props.extrainfo; }
   const ADDONDEPS &GetDeps() const { return m_props.dependencies; }
 
+  void CallOEWrapper(const std::string& ID, bool disable);
+
   /*! \brief get the required version of a dependency.
    \param dependencyID the addon ID of the dependency.
    \return the version this addon requires.
-- 
2.5.0

