module Azubi.Syntax where
import Azubi.Core.Model
import System.FilePath.Posix
content :: Path -> [String] -> State
content path fileContent = States
  [ HasFileContent path fileContent ]
  [
    folderExists (takeDirectory path)
  , State
    [ Not $ FolderExists path ]
    [ Remove path ]
    Nothing
  , State
    [ Not AlwaysYes ]
    [ FileContent path fileContent ]
    Nothing
  ]
  (Just $ unwords [ "Content for File" , path ])
requires :: State -> State -> State
stateA  `requires` stateB = States [Not AlwaysYes] [stateB, stateA] Nothing
submodule :: [State] -> State
submodule states = States [Not AlwaysYes] states Nothing
folderExists :: Path -> State
folderExists path =
  let folders = reverse $ allFolders $ splitDirectories path
  in
  States [FolderExists path]
  (map
    (\folder ->
        States [FolderExists folder]
        [ State [ Not $ DoesExist folder ]
          [ Remove folder ]
          (Just $ "fuckup " ++ folder)
        , State [Not AlwaysYes ]
          [CreateFolder folder]
          (Just $ "create " ++ folder)
        ]
        Nothing
    ) folders )
  Nothing
  where
    allFolders [] = []
    allFolders (x:xs) = (joinPath $ x:xs) : (allFolders xs)
link :: Path -> Path -> State
link path target =
  States
  [ SymlinkExists path target ]
  [ State [Not $ DoesExist path] [Remove path] Nothing
  , State [Not AlwaysYes] [CreateSymlink path target] Nothing
  ]
  (Just $ "link " ++ path ++ " -> " ++ target)
(&) :: [State] -> State -> [State]
states & state = states ++ [state]