2013-04-23 15:38:52 +00:00
|
|
|
{- git-annex extra config files
|
2012-08-02 04:42:33 +00:00
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2013-04-23 15:38:52 +00:00
|
|
|
module Config.Files where
|
2012-08-02 04:42:33 +00:00
|
|
|
|
2012-08-27 17:43:03 +00:00
|
|
|
import Common
|
2013-05-12 23:19:28 +00:00
|
|
|
import Utility.Tmp
|
2012-08-02 04:42:33 +00:00
|
|
|
import Utility.FreeDesktop
|
|
|
|
|
|
|
|
{- ~/.config/git-annex/file -}
|
|
|
|
userConfigFile :: FilePath -> IO FilePath
|
|
|
|
userConfigFile file = do
|
|
|
|
dir <- userConfigDir
|
|
|
|
return $ dir </> "git-annex" </> file
|
|
|
|
|
|
|
|
autoStartFile :: IO FilePath
|
|
|
|
autoStartFile = userConfigFile "autostart"
|
|
|
|
|
2013-03-03 21:07:27 +00:00
|
|
|
{- Returns anything listed in the autostart file (which may not exist). -}
|
|
|
|
readAutoStartFile :: IO [FilePath]
|
|
|
|
readAutoStartFile = do
|
|
|
|
f <- autoStartFile
|
2013-04-23 16:36:37 +00:00
|
|
|
nub . map dropTrailingPathSeparator . lines
|
|
|
|
<$> catchDefaultIO "" (readFile f)
|
2013-03-03 21:07:27 +00:00
|
|
|
|
2013-04-23 16:36:37 +00:00
|
|
|
modifyAutoStartFile :: ([FilePath] -> [FilePath]) -> IO ()
|
|
|
|
modifyAutoStartFile func = do
|
2013-03-03 21:07:27 +00:00
|
|
|
dirs <- readAutoStartFile
|
2013-04-23 16:36:37 +00:00
|
|
|
let dirs' = nubBy equalFilePath $ func dirs
|
|
|
|
when (dirs' /= dirs) $ do
|
2013-03-03 21:07:27 +00:00
|
|
|
f <- autoStartFile
|
|
|
|
createDirectoryIfMissing True (parentDir f)
|
2013-04-23 16:36:37 +00:00
|
|
|
viaTmp writeFile f $ unlines $ dirs'
|
|
|
|
|
|
|
|
{- Adds a directory to the autostart file. -}
|
|
|
|
addAutoStartFile :: FilePath -> IO ()
|
|
|
|
addAutoStartFile path = modifyAutoStartFile $ (:) path
|
2013-03-03 21:07:27 +00:00
|
|
|
|
|
|
|
{- Removes a directory from the autostart file. -}
|
|
|
|
removeAutoStartFile :: FilePath -> IO ()
|
2013-04-23 16:36:37 +00:00
|
|
|
removeAutoStartFile path = modifyAutoStartFile $
|
|
|
|
filter (not . equalFilePath path)
|
2013-03-03 21:07:27 +00:00
|
|
|
|
2012-08-02 04:42:33 +00:00
|
|
|
{- The path to git-annex is written here; which is useful when cabal
|
|
|
|
- has installed it to some aweful non-PATH location. -}
|
|
|
|
programFile :: IO FilePath
|
|
|
|
programFile = userConfigFile "program"
|
2012-08-27 17:43:03 +00:00
|
|
|
|
|
|
|
{- Returns a command to run for git-annex. -}
|
|
|
|
readProgramFile :: IO FilePath
|
|
|
|
readProgramFile = do
|
|
|
|
programfile <- programFile
|
2013-04-23 15:41:52 +00:00
|
|
|
catchDefaultIO cmd $
|
|
|
|
fromMaybe cmd . headMaybe . lines <$> readFile programfile
|
|
|
|
where
|
|
|
|
cmd = "git-annex"
|