packages feed

kibro-0.0: executable/Start.hs

module Start where

import Directory
import Control.Applicative
import Data.List
import System.Process
import System.FilePath
import System.Exit
import Control.Monad

import Build
import Utils

start :: [String] -> IO ()
start _ = inKibro $ \dir name -> do
  let fcgi = "public" ./. (name++".fcgi")
  d <- doesFileExist fcgi
  if d
     then doStart dir name
     else do doing "Not built. Building"
             e <- doBuild dir name
             case e of
               ExitSuccess -> doStart dir name
               _           -> appError "cannot start"

doStart :: String -> String -> IO ()
doStart dir name = do
  pid <- doesFileExist $ "app" ./. "lighttpd" ./. "lighttpd.pid"
  when (not pid) startLighty
  pid <- doesFileExist $ "app" ./. "fastcgi" ./. "fastcgi.pid"
  when (not pid) (startFastCGI dir name)

startLighty = do
  doing "Starting lighttpd"
  r <- runCommand lighttpd >>= waitForProcess
  case r of
    ExitSuccess -> return ()
    _ -> appError "unable to start lighttpd! \
                  \perhaps the port is already \
                  \in use. Do: kibro configure --port <myport>"
    where lighttpd = "lighttpd -f app/lighttpd/lighttpd.conf"

startFastCGI dir name = do
  doing "Starting fastcgi process"
  doing fastcgi
  r <- runCommand fastcgi >>= waitForProcess
  case r of
    ExitSuccess -> return ()
    _ -> appError "unable to start the fastcgi process! is spawn-fcgi installed?"
    where fastcgi = "spawn-fcgi -f " ++ fcgi ++ " -s " ++ sock ++ " -P " ++ pid
          fcgi = "public" ./. name ++ ".fcgi"
          sock = dir ./. "app" ./. "fastcgi" ./. name ++ ".sock"
          pid = "app" ./. "fastcgi" ./. "fastcgi.pid"