module BDCS.Label.FileLabels(apply)
where
import Control.Monad.IO.Class(MonadIO)
import Database.Esqueleto(Key, SqlPersistT)
import Data.Maybe(mapMaybe)
import BDCS.DB(Files(..), FileKeyValues(..))
import qualified BDCS.Label.Docs as Docs
import qualified BDCS.Label.Fonts as Fonts
import qualified BDCS.Label.InfoPage as Info
import qualified BDCS.Label.Library as Library
import qualified BDCS.Label.License as License
import qualified BDCS.Label.ManPage as Man
import qualified BDCS.Label.Service as Service
import qualified BDCS.Label.Translation as Xlat
import BDCS.Label.Types(Label(..))
import BDCS.Label.Utils(addLabelKey)
import BDCS.Utils.Monad(concatForM)
checks :: [(Files -> Bool, Files -> Maybe Label)]
checks = [(Docs.matches, Docs.mkLabel),
(Fonts.matches, Fonts.mkLabel),
(Info.matches, Info.mkLabel),
(License.matches, License.mkLabel),
(Library.matches, Library.mkLabel),
(Man.matches, Man.mkLabel),
(Service.matches, Service.mkLabel),
(Xlat.matches, Xlat.mkLabel)]
apply :: MonadIO m => [(Files, Key Files)] -> SqlPersistT m [Key FileKeyValues]
apply lst =
concatForM lst $ \(f, ndx) -> do
let successfulChecks = filter (\(matches, _) -> matches f) checks
let labels = mapMaybe (\(_, maker) -> maker f) successfulChecks
mapM (\lbl -> addLabelKey ndx lbl Nothing Nothing)
labels