packages feed

blubber-server-0.0.1: Setup.hs

{- |
- Module      :  $Header$
- Description :  The blubber server Cabal setup file.
- Copyright   :  (c) plaimi 2015
- License     :  AGPL-3
-
- Maintainer  :  blubber@plaimi.net
- -}

import Control.Exception
  (
  catch,
  )
import Control.Monad
  (
  unless,
  )
import Data.Text
  (
  pack,
  toTitle,
  unpack,
  )
import Distribution.Simple
  (
  defaultMainWithHooks,
  preBuild,
  simpleUserHooks,
  )
import System.Exit
  (
  ExitCode (ExitSuccess),
  )
import System.IO.Error
  (
  isAlreadyExistsError,
  ioeGetErrorString,
  )
import System.Posix
  (
  createDirectory,
  )
import System.Process
  (
  rawSystem,
  )
import Text.Printf
  (
  printf,
  )

main :: IO ()
-- | Make web/src.tar.gz pre-build, so an URL may be served to clients upon
-- request.
main = defaultMainWithHooks
     $ simpleUserHooks
       {preBuild = \args buildFlags -> do
         createDirectory "web" 0o755 `catch` \e ->
           unless (isAlreadyExistsError e) $
             printf ("couldn't make the web directory: %s\n"
                  ++ "please do it yourself. we need it!")
             $ unpack . toTitle . pack $ ioeGetErrorString e
         exitCode <- rawSystem "tar" $ tarOut ++ tarIn
         putStrLn $ echoOut exitCode
         preBuild simpleUserHooks args buildFlags
       }

tarOut :: [String]
-- | The file generated by tar.
tarOut =  ["cfz", "web/src.tar.gz"]

tarIn :: [String]
-- | The files passed to tar.
tarIn = ["Setup.hs"
        ,"blubber-server.cabal"
        ,"src"
        ,"src-exec"
        ]

echoOut :: ExitCode -> String
-- | Make an output text to echo, depending on the passed in 'ExitCode'.
echoOut ExitSuccess = "'put src in web/src.tar.gz'"
echoOut _           = "'uh-oh! couldn't make a gzipped tarball of the src! "
                   ++ "you need to put the src of your blubber server in a "
                   ++ "file \"web/src.tar.gz\" before building. this is to "
                   ++ "comply with the GNU AGPLv3."