module Config where

import System.FilePath
import System.Process
import System.Directory
import Data.ConfigFile

rightOrDie :: Show a => Either a p -> IO p
rightOrDie :: forall a p. Show a => Either a p -> IO p
rightOrDie (Left a
err) = [Char] -> IO p
forall a. [Char] -> IO a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail (a -> [Char]
forall a. Show a => a -> [Char]
show a
err)
rightOrDie (Right p
x) = p -> IO p
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return p
x


configFileName :: [Char]
configFileName :: [Char]
configFileName = [Char]
".imbib"

getConfiguration :: IO ConfigParser
getConfiguration :: IO ConfigParser
getConfiguration = do
  [Char]
homeDirectory <- IO [Char]
getHomeDirectory
  let startConfig :: IO ConfigParser
startConfig = Either CPError ConfigParser -> IO ConfigParser
forall a p. Show a => Either a p -> IO p
rightOrDie (Either CPError ConfigParser -> IO ConfigParser)
-> Either CPError ConfigParser -> IO ConfigParser
forall a b. (a -> b) -> a -> b
$ ConfigParser
-> [Char] -> [Char] -> [Char] -> Either CPError ConfigParser
forall (m :: * -> *).
MonadError CPError m =>
ConfigParser -> [Char] -> [Char] -> [Char] -> m ConfigParser
set (ConfigParser
emptyCP {accessfunc = interpolatingAccess 10}) [Char]
"DEFAULT" [Char]
"home" [Char]
homeDirectory
  ConfigParser
c0 <- Either CPError ConfigParser -> IO ConfigParser
forall a p. Show a => Either a p -> IO p
rightOrDie (Either CPError ConfigParser -> IO ConfigParser)
-> IO (Either CPError ConfigParser) -> IO ConfigParser
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ((ConfigParser -> [Char] -> Either CPError ConfigParser)
-> [Char] -> ConfigParser -> Either CPError ConfigParser
forall a b c. (a -> b -> c) -> b -> a -> c
flip ConfigParser -> [Char] -> Either CPError ConfigParser
forall (m :: * -> *).
MonadError CPError m =>
ConfigParser -> [Char] -> m ConfigParser
readstring [Char]
dflt (ConfigParser -> Either CPError ConfigParser)
-> IO ConfigParser -> IO (Either CPError ConfigParser)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO ConfigParser
startConfig)
  let user :: [Char]
user = [Char]
homeDirectory [Char] -> [Char] -> [Char]
</> [Char]
configFileName
  Bool
ex <- [Char] -> IO Bool
doesFileExist [Char]
user
  if Bool
ex then Either CPError ConfigParser -> IO ConfigParser
forall a p. Show a => Either a p -> IO p
rightOrDie (Either CPError ConfigParser -> IO ConfigParser)
-> IO (Either CPError ConfigParser) -> IO ConfigParser
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ConfigParser -> [Char] -> IO (Either CPError ConfigParser)
forall (m :: * -> *).
MonadError CPError m =>
ConfigParser -> [Char] -> IO (m ConfigParser)
readfile ConfigParser
c0 [Char]
user else ConfigParser -> IO ConfigParser
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ConfigParser
c0



data InitFile = InitFile
     {
       InitFile -> [Char]
attachmentsRoot, InitFile -> [Char]
downloadsDirectory, InitFile -> [Char]
bibfile :: FilePath,
       InitFile -> [Char] -> IO ProcessHandle
runViewer :: String -> IO ProcessHandle,
       InitFile -> Int -> [Char] -> IO ProcessHandle
runEditor :: Int -> String -> IO ProcessHandle 
     }

dflt :: String
dflt :: [Char]
dflt = [Char]
"\
  \watched = %(home)s/Downloads\n\
  \archive = %(home)s/Papers\n\
  \library = %(home)s/library.bib\n\
  \viewer = /usr/bin/evince\n\
  \editor = /usr/bin/emacs\n\
  \"

loadConfiguration :: IO InitFile
loadConfiguration :: IO InitFile
loadConfiguration = do
  ConfigParser
cfg <- IO ConfigParser
getConfiguration
  let getOption :: [Char] -> Either CPError [Char]
getOption = ConfigParser -> [Char] -> [Char] -> Either CPError [Char]
forall a (m :: * -> *).
(Get_C a, MonadError CPError m) =>
ConfigParser -> [Char] -> [Char] -> m a
forall (m :: * -> *).
MonadError CPError m =>
ConfigParser -> [Char] -> [Char] -> m [Char]
get ConfigParser
cfg [Char]
"DEFAULT"
  Either CPError InitFile -> IO InitFile
forall a p. Show a => Either a p -> IO p
rightOrDie (Either CPError InitFile -> IO InitFile)
-> Either CPError InitFile -> IO InitFile
forall a b. (a -> b) -> a -> b
$ do
    [Char]
downloadsDirectory <- [Char] -> Either CPError [Char]
getOption [Char]
"watched"
    [Char]
attachmentsRoot <- [Char] -> Either CPError [Char]
getOption [Char]
"archive"
    [Char]
bibfile <- [Char] -> Either CPError [Char]
getOption [Char]
"library"
    [Char]
editor <- [Char] -> Either CPError [Char]
getOption [Char]
"editor"
    [Char]
viewer <- [Char] -> Either CPError [Char]
getOption [Char]
"viewer"
    let runEditor :: a -> [Char] -> IO ProcessHandle
runEditor a
lineNumber [Char]
file = [Char] -> [[Char]] -> IO ProcessHandle
runProcess' [Char]
editor [Char
'+'Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
:a -> [Char]
forall a. Show a => a -> [Char]
show a
lineNumber,[Char]
file]
        runViewer :: [Char] -> IO ProcessHandle
runViewer [Char]
file = [Char] -> [[Char]] -> IO ProcessHandle
runProcess' [Char]
viewer [[Char]
file]
    InitFile -> Either CPError InitFile
forall a. a -> Either CPError a
forall (m :: * -> *) a. Monad m => a -> m a
return InitFile{[Char]
Int -> [Char] -> IO ProcessHandle
[Char] -> IO ProcessHandle
forall {a}. Show a => a -> [Char] -> IO ProcessHandle
attachmentsRoot :: [Char]
downloadsDirectory :: [Char]
bibfile :: [Char]
runViewer :: [Char] -> IO ProcessHandle
runEditor :: Int -> [Char] -> IO ProcessHandle
downloadsDirectory :: [Char]
attachmentsRoot :: [Char]
bibfile :: [Char]
runEditor :: forall {a}. Show a => a -> [Char] -> IO ProcessHandle
runViewer :: [Char] -> IO ProcessHandle
..}

-- The following should probably not change
-- iconFile = unsafePerformIO $ getDataFileName "icon.svg"

runProcess' :: FilePath -> [String] -> IO ProcessHandle
runProcess' :: [Char] -> [[Char]] -> IO ProcessHandle
runProcess' = \[Char]
bin [[Char]]
args -> [Char]
-> [[Char]]
-> Maybe [Char]
-> Maybe [([Char], [Char])]
-> Maybe Handle
-> Maybe Handle
-> Maybe Handle
-> IO ProcessHandle
runProcess [Char]
bin [[Char]]
args Maybe [Char]
forall a. Maybe a
Nothing Maybe [([Char], [Char])]
forall a. Maybe a
Nothing Maybe Handle
forall a. Maybe a
Nothing Maybe Handle
forall a. Maybe a
Nothing Maybe Handle
forall a. Maybe a
Nothing