module Marihana.Build
    ( marihanaBuild
    , marihanaMake
    , marihanaMakeMain
    ) where

import System.IO
import System.Directory
import System.FilePath.Posix

directory = "./posts/"

marihanaBuild :: IO ()
marihanaBuild = do
    createDirectory "./public/"
    createDirectory "./public/posts/"
    copyFile "template/archive.html" "public/index.html"
    list <- listDirectory directory
    marihanaMake (length list)

marihanaMake :: Int -> IO ()
marihanaMake x = do
    list <- listDirectory directory
    if x <= 0
        then return ()
        else do
            marihanaMakeMain ((reverse list) !! (x - 1))
            marihanaMake (x - 1)

marihanaMakeMain :: String -> IO ()
marihanaMakeMain x = do
    print ((takeBaseName x) ++ ".html")
    copyFile "template/post.html" ("public/posts/" ++ (takeBaseName x) ++ ".html")
    (tempName, tempHandle) <- openTempFile "./" ""
    removeFile tempName