-- | Maintainer: Sean Whitton <spwhitton@spwhitton.name>

module Propellor.Property.Ccache (
	hasCache,
	hasLimits,
	Limit(..),
	DataSize,
) where

import Propellor.Base
import qualified Propellor.Property.File as File
import qualified Propellor.Property.Apt as Apt

import Utility.DataUnits
import System.Posix.Files
import qualified Data.Semigroup as Sem

-- | Limits on the size of a ccache
data Limit
	-- | The maximum size of the cache, as a string such as "4G"
	= MaxSize DataSize
	-- | The maximum number of files in the cache
	| MaxFiles Integer
	-- | A cache with no limit specified
	| NoLimit
	| Limit :+ Limit

instance Sem.Semigroup Limit where
	<> :: Limit -> Limit -> Limit
(<>) = Limit -> Limit -> Limit
(:+)

instance Monoid Limit where
	mempty :: Limit
mempty  = Limit
NoLimit
	mappend :: Limit -> Limit -> Limit
mappend = Limit -> Limit -> Limit
forall a. Semigroup a => a -> a -> a
(Sem.<>)

-- | A string that will be parsed to get a data size.
--
-- Examples: "100 megabytes" or "0.5tb"
type DataSize = String

maxSizeParam :: DataSize -> Maybe String
maxSizeParam :: String -> Maybe String
maxSizeParam String
s = [Unit] -> String -> Maybe Integer
readSize [Unit]
dataUnits String
s
	Maybe Integer -> (Integer -> Maybe String) -> Maybe String
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Integer
sz -> String -> Maybe String
forall a. a -> Maybe a
Just (String -> Maybe String) -> String -> Maybe String
forall a b. (a -> b) -> a -> b
$ String
"--max-size=" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Integer -> String
ccacheSizeUnits Integer
sz

-- Generates size units as used in ccache.conf.  The smallest unit we can
-- specify in a ccache config files is a kilobyte