deviceTreeInsert :: Text -> DeviceTree -> DeviceTree -> STM DeviceTree
deviceTreeInsert path node root = do
   let cs = deviceNodeChildren root

   cs' <- case bkPath path of
      (x,xs)
         | Text.null x && Text.null xs -> error "deviceTreeInsert: empty path"
         | Text.null xs                -> return (Map.insert x node cs)
         | otherwise                   -> 
            case Map.lookup x cs of
               Just p  -> do
                  node' <- deviceTreeInsert xs node p
                  return (Map.insert x node' cs)
               -- the parent doesn't exist yet. Add it. As it should not be a
               -- real device, we don't look for subsystem and device id (i.e.,
               -- we don't call deviceAdd)
               Nothing -> do
                  p <- deviceTreeCreate' Nothing Nothing Map.empty
                  node' <- deviceTreeInsert xs node p
                  return (Map.insert x node' cs)

   return (root { deviceNodeChildren = cs' })

-------------------------------------------------------------------------------
-- Various
-------------------------------------------------------------------------------


-- | Create a new thread reading kernel events and putting them in a TChan
deviceTreeInsert :: Text -> DeviceTree -> DeviceTree -> STM DeviceTree
deviceTreeInsert path node root = do
   let cs = deviceNodeChildren root

   cs' <- case bkPath path of
      (x,xs)
         | Text.null x && Text.null xs -> error "deviceTreeInsert: empty path"
         | Text.null xs                -> return (Map.insert x node cs)
         | otherwise                   -> 
            case Map.lookup x cs of
               Just p  -> do
                  node' <- deviceTreeInsert xs node p
                  return (Map.insert x node' cs)
               -- the parent doesn't exist yet. Add it. As it should not be a
               -- real device, we don't look for subsystem and device id (i.e.,
               -- we don't call deviceAdd)
               Nothing -> do
                  p <- deviceTreeCreate' Nothing Nothing Map.empty
                  node' <- deviceTreeInsert xs node p
                  return (Map.insert x node' cs)

   return (root { deviceNodeChildren = cs' })

-------------------------------------------------------------------------------
-- Various
-------------------------------------------------------------------------------


-- | Create a new thread reading kernel events and putting them in a TChan
deviceTreeInsert path node root = do
   let cs = deviceNodeChildren root

   cs' <- case bkPath path of
      (x,xs)
         | Text.null x && Text.null xs -> error "deviceTreeInsert: empty path"
         | Text.null xs                -> return (Map.insert x node cs)
         | otherwise                   -> 
            case Map.lookup x cs of
               Just p  -> do
                  node' <- deviceTreeInsert xs node p
                  return (Map.insert x node' cs)
               -- the parent doesn't exist yet. Add it. As it should not be a
               -- real device, we don't look for subsystem and device id (i.e.,
               -- we don't call deviceAdd)
               Nothing -> do
                  p <- deviceTreeCreate' Nothing Nothing Map.empty
                  node' <- deviceTreeInsert xs node p
                  return (Map.insert x node' cs)

   return (root { deviceNodeChildren = cs' })

-------------------------------------------------------------------------------
-- Various
-------------------------------------------------------------------------------


-- | Create a new thread reading kernel events and putting them in a TChan