module Propellor.Property.Laptop where

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

-- | Makes powertop auto-tune the system for optimal power consumption on
-- boot.
powertopAutoTuneOnBoot :: RevertableProperty DebianLike DebianLike
powertopAutoTuneOnBoot :: RevertableProperty
  (MetaTypes '[ 'Targeting 'OSDebian, 'Targeting 'OSBuntish])
  (MetaTypes '[ 'Targeting 'OSDebian, 'Targeting 'OSBuntish])
powertopAutoTuneOnBoot = Property
  (MetaTypes '[ 'Targeting 'OSDebian, 'Targeting 'OSBuntish])
setup Property
  (MetaTypes '[ 'Targeting 'OSDebian, 'Targeting 'OSBuntish])
-> Property
     (MetaTypes '[ 'Targeting 'OSDebian, 'Targeting 'OSBuntish])
-> RevertableProperty
     (MetaTypes '[ 'Targeting 'OSDebian, 'Targeting 'OSBuntish])
     (MetaTypes '[ 'Targeting 'OSDebian, 'Targeting 'OSBuntish])
forall setupmetatypes undometatypes.
Property setupmetatypes
-> Property undometatypes
-> RevertableProperty setupmetatypes undometatypes
<!> Property
  (MetaTypes '[ 'Targeting 'OSDebian, 'Targeting 'OSBuntish])
undo
	RevertableProperty
  (MetaTypes '[ 'Targeting 'OSDebian, 'Targeting 'OSBuntish])
  (MetaTypes '[ 'Targeting 'OSDebian, 'Targeting 'OSBuntish])
-> Desc
-> RevertableProperty
     (MetaTypes '[ 'Targeting 'OSDebian, 'Targeting 'OSBuntish])
     (MetaTypes '[ 'Targeting 'OSDebian, 'Targeting 'OSBuntish])
forall p. IsProp p => p -> Desc -> p
`describe` Desc
"powertop auto-tune on boot"
  where
	setup :: CombinedType
  (Property
     (MetaTypes '[ 'Targeting 'OSDebian, 'Targeting 'OSBuntish]))
  (Property UnixLike)
setup = Desc -> Property Linux
Systemd.enabled Desc
"powertop"
		Property Linux
-> Property
     (MetaTypes '[ 'Targeting 'OSDebian, 'Targeting 'OSBuntish])
-> CombinedType
     (Property Linux)
     (Property
        (MetaTypes '[ 'Targeting 'OSDebian, 'Targeting 'OSBuntish]))
forall x y. Combines x y => x -> y -> CombinedType x y
`requires` [Desc]
-> Property
     (MetaTypes '[ 'Targeting 'OSDebian, 'Targeting 'OSBuntish])
Apt.installed [Desc
"powertop"]
		Property
  (MetaTypes '[ 'Targeting 'OSDebian, 'Targeting 'OSBuntish])
-> Property UnixLike
-> CombinedType
     (Property
        (MetaTypes '[ 'Targeting 'OSDebian, 'Targeting 'OSBuntish]))
     (Property UnixLike)
forall x y. Combines x y => x -> y -> CombinedType x y
`requires` Desc -> [Desc] -> Property UnixLike
File.hasContent Desc
servicefile
			[ Desc
"[Unit]"
			, Desc
"Description=Powertop tunings"
			, Desc
"[Service]"
			, Desc
"ExecStart=/usr/sbin/powertop --auto-tune"
			, Desc
"RemainAfterExit=true"
			, Desc
"[Install]"
			, Desc
"WantedBy=multi-user.target"
			]
	undo :: Property
  (MetaTypes '[ 'Targeting 'OSDebian, 'Targeting 'OSBuntish])
undo = Property Linux
-> Property
     (MetaTypes '[ 'Targeting 'OSDebian, 'Targeting 'OSBuntish])
forall (p :: * -> *) (untightened :: [MetaType])
       (tightened :: [MetaType]).
(TightenTargets p, TightenTargetsAllowed untightened tightened,
 SingI tightened) =>
p (MetaTypes untightened) -> p (MetaTypes tightened)
tightenTargets (Property Linux
 -> Property
      (MetaTypes '[ 'Targeting 'OSDebian, 'Targeting 'OSBuntish]))
-> Property Linux
-> Property
     (MetaTypes '[ 'Targeting 'OSDebian, 'Targeting 'OSBuntish])
forall a b. (a -> b) -> a -> b
$ Desc -> Property UnixLike
File.notPresent Desc
servicefile
		Property UnixLike
-> Property Linux
-> CombinedType (Property UnixLike) (Property Linux)
forall x y. Combines x y => x -> y -> CombinedType x y
`requires` IO Bool -> Property Linux -> Property Linux
forall (p :: * -> *) i (m :: * -> *).
(Checkable p i, LiftPropellor m) =>
m Bool -> p i -> Property i
check (Desc -> IO Bool
doesFileExist Desc
servicefile)
			(Desc -> Property Linux
Systemd.disabled Desc
"powertop")
	servicefile :: Desc
servicefile = Desc
"/etc/systemd/system/powertop.service"

-- | Enables weekly TRIM for SSDs, using systemd's fstrim.timer,
trimSSD :: Property Linux
trimSSD :: Property Linux
trimSSD = Desc -> Property Linux
Systemd.enabled Desc
"fstrim.timer"