module Main ( main ) where import MainNoPaths ( getYavieDir ) import qualified MainNoPaths as NP ( main ) import System.Environment ( getArgs ) import System.Directory ( doesDirectoryExist, doesFileExist, createDirectory, createDirectoryIfMissing, copyFile ) import Control.Monad ( when ) import Data.Version ( showVersion ) import Paths_yavie ( getDataFileName, version ) uuidFileName :: String uuidFileName = "uuid_yavie" defaultMains :: [ ( String, [ String ] ) ] defaultMains = [ vtyFileNames, x11FileNames, gtkFileNames ] vtyFileNames :: ( String, [ String ] ) vtyFileNames = ( "vty", [ "yavie-vty.hs", "yavie-vty.cabal" ] ) x11FileNames :: ( String, [ String ] ) x11FileNames = ( "x11", [ "yavie-x11.hs", "yavie-x11.cabal" ] ) gtkFileNames :: ( String, [ String ] ) gtkFileNames = ( "gtk", [ "yavie-gtk.hs", "yavie-gtk.cabal" ] ) main :: IO () main = do args <- getArgs yavieDir <- getYavieDir uuidYavie <- getDataFileName uuidFileName >>= readFile existYDir <- doesDirectoryExist yavieDir if existYDir then checkYDir yavieDir uuidYavie else do createUUIDFile yavieDir uuidYavie mapM_ ( copyDefault yavieDir ) defaultMains case args of [ "--version" ] -> putStrLn $ "yavie " ++ showVersion version [ "--reset" ] -> mapM_ ( copyDefault yavieDir ) defaultMains _ -> NP.main checkYDir :: FilePath -> String -> IO () checkYDir yavieDir uuidYavie = do let uuidFile = yavieDir ++ "/" ++ uuidFileName errMsg = "checkYDir: bad yavie directory" errMsg2 yu du = "checkYDir : bad yavie directory " ++ show yu ++ " " ++ show du existUFile <- doesFileExist uuidFile if not existUFile then error errMsg else do uuid <- readFile uuidFile when ( uuid /= uuidYavie ) $ error ( errMsg2 uuidYavie uuid ) createUUIDFile :: FilePath -> String -> IO () createUUIDFile yavieDir uuidYavie = do let uuidFile = yavieDir ++ "/" ++ uuidFileName defaultFile = yavieDir ++ "/default" createDirectory yavieDir writeFile uuidFile uuidYavie writeFile defaultFile "vty\n" copyDefault :: FilePath -> ( String, [ String ] ) -> IO () copyDefault yavieDir ( dirName, files ) = do let dir = yavieDir ++ "/" ++ dirName createDirectoryIfMissing False dir mapM_ ( copyToHome ( "src/" ++ dirName ) dir ) files copyToHome :: FilePath -> FilePath -> FilePath -> IO () copyToHome fd td fn = do fromFile <- getDataFileName $ fd ++ "/" ++ fn let toFile = td ++ "/" ++ fn copyFile fromFile toFile