{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE CPP                 #-}

-- |
-- Module      :  Distribution.Fedora.Release
-- Copyright   :  (C) 2020-2022,2024  Jens Petersen
--
-- Maintainer  :  Jens Petersen <petersen@fedoraproject.org>
--
-- Explanation: Fedora Release type and functions

-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.

module Distribution.Fedora.Release
  ( Release(..),
    getReleases,
    getFedoraReleases,
    getEPELReleases
  )
where

import Control.Monad (guard)
import Data.Aeson(Object)
import Data.Maybe (mapMaybe)
import Distribution.Fedora.BodhiReleases
import Fedora.Bodhi (lookupKey)

-- | Fedora Release data
data Release = Release {
    Release -> String
releaseName :: String,
    Release -> String
releaseVersion :: String,
    Release -> String
releaseIdPrefix :: String,
    Release -> String
releaseBranch :: String
  } deriving (Int -> Release -> ShowS
[Release] -> ShowS
Release -> String
(Int -> Release -> ShowS)
-> (Release -> String) -> ([Release] -> ShowS) -> Show Release
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Release -> ShowS
showsPrec :: Int -> Release -> ShowS
$cshow :: Release -> String
show :: Release -> String
$cshowList :: [Release] -> ShowS
showList :: [Release] -> ShowS
Show,Release -> Release -> Bool
(Release -> Release -> Bool)
-> (Release -> Release -> Bool) -> Eq Release
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Release -> Release -> Bool
== :: Release -> Release -> Bool
$c/= :: Release -> Release -> Bool
/= :: Release -> Release -> Bool
Eq)

instance Ord Release where
  compare :: Release -> Release -> Ordering
compare Release
r1 Release
r2 =
    String -> String -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Release -> String
releaseName Release
r1) (Release -> String
releaseName Release
r2)

readRelease :: Object -> Maybe Release
readRelease :: Object -> Maybe Release
readRelease Object
obj = do
  String
name <- Text -> Object -> Maybe String
forall a. FromJSON a => Text -> Object -> Maybe a
lookupKey Text
"name" Object
obj
  String
ver <- Text -> Object -> Maybe String
forall a. FromJSON a => Text -> Object -> Maybe a
lookupKey Text
"version" Object
obj
  String
idPref <- Text -> Object -> Maybe String
forall a. FromJSON a => Text -> Object -> Maybe a
lookupKey Text
"id_prefix" Object
obj
  Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (String
idPref String -> [String] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [String
"FEDORA-CONTAINER",String
"FEDORA-FLATPAK"])
  String
br <- Text -> Object -> Maybe String
forall a. FromJSON a => Text -> Object -> Maybe a
lookupKey Text
"branch" Object
obj
  Release -> Maybe Release
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return (Release -> Maybe Release) -> Release -> Maybe Release
forall a b. (a -> b) -> a -> b
$ String -> String -> String -> String -> Release
Release String
name String
ver String
idPref String
br

-- FIXME remove containers and flatpaks
-- | Get list of all current Fedora Project releases
getReleases :: IO [Release]
getReleases :: IO [Release]
getReleases =
  [Release] -> [Release]
forall a. [a] -> [a]
reverse ([Release] -> [Release])
-> ([Object] -> [Release]) -> [Object] -> [Release]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Object -> Maybe Release) -> [Object] -> [Release]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Object -> Maybe Release
readRelease ([Object] -> [Release]) -> IO [Object] -> IO [Release]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO [Object]
getBodhiReleases

getProductReleases :: String -> IO [Release]
getProductReleases :: String -> IO [Release]
getProductReleases String
name =
  (Release -> Bool) -> [Release] -> [Release]
forall a. (a -> Bool) -> [a] -> [a]
filter (\Release
p -> Release -> String
releaseIdPrefix Release
p String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
name) ([Release] -> [Release]) -> IO [Release] -> IO [Release]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO [Release]
getReleases

-- | Get list of current Fedora Linux releases
getFedoraReleases :: IO [Release]
getFedoraReleases :: IO [Release]
getFedoraReleases =
  String -> IO [Release]
getProductReleases String
"FEDORA"

-- | Get list of current Fedora EPEL releases
getEPELReleases :: IO [Release]
getEPELReleases :: IO [Release]
getEPELReleases =
  String -> IO [Release]
getProductReleases String
"FEDORA-EPEL"