1import Control.Monad
2import Data.Digest.Pure.SHA
3import qualified Data.ByteString.Lazy as BS
4import System.Directory
5import System.Environment
6import System.Exit
7
8main :: IO ()
9main = do
10  bname <- getProgName
11  args <- getArgs
12  case args of
13    [] -> do
14      inconts <- BS.getContents
15      print $ ALGORITHM inconts
16      exitSuccess
17    xs -> foldM (sha_file bname) ExitSuccess xs >>= exitWith
18
19sha_file :: String -> ExitCode -> String -> IO ExitCode
20sha_file bname prevEC fname = do
21  is_file <- doesFileExist fname
22  is_dir  <- doesDirectoryExist fname
23  case (is_file, is_dir) of
24    (False, False) -> do
25      putStrLn $ bname ++ ": " ++ fname ++ ": No such file or directory"
26      return $ ExitFailure 22 -- EINVAL
27    (False, True)  -> do
28      putStrLn $ bname ++ ": " ++ fname ++ ": Is a directory"
29      return $ ExitFailure 22 -- EINVAL
30    (True,  _)     -> do
31      conts <- BS.readFile fname
32      putStrLn $ bname ++ " (" ++ fname ++ ") = " ++ show (ALGORITHM conts)
33      return $ combineExitCodes prevEC ExitSuccess
34
35combineExitCodes :: ExitCode -> ExitCode -> ExitCode
36combineExitCodes ExitSuccess ExitSuccess = ExitSuccess
37combineExitCodes _           _           = ExitFailure 22 -- EINVAL
38