| 1 | ---------------------------------------------------------------- |
| 2 | -- |
| 3 | -- smew.hs |
| 4 | -- |
| 5 | |
| 6 | module Main where |
| 7 | |
| 8 | import Data.List |
| 9 | import Msg |
| 10 | import Param |
| 11 | import Search |
| 12 | import System.Environment |
| 13 | import Util |
| 14 | |
| 15 | -- ./smew '<[email protected]>' ~/Mail/id.db #imap/kazu@localhost#imap/work.mew-dist |
| 16 | |
| 17 | ---------------------------------------------------------------- |
| 18 | |
| 19 | helpMessage :: String |
| 20 | helpMessage = "[-p|-c] id [db [dir]]" |
| 21 | |
| 22 | options :: [String] |
| 23 | options = ["-p","-c"] |
| 24 | |
| 25 | ---------------------------------------------------------------- |
| 26 | |
| 27 | parseOpts :: [String] -> Maybe (Search [Msg]) |
| 28 | parseOpts opts |
| 29 | | opts == [] = Just searchFamily |
| 30 | | unknown = Nothing |
| 31 | | "-p" `elem` opts = Just searchMe -- to find a parent, specify pid |
| 32 | | "-c" `elem` opts = Just searchChild |
| 33 | | otherwise = Nothing |
| 34 | where |
| 35 | unknown = (opts `union` options) \\ options /= [] |
| 36 | |
| 37 | parseArgs :: [String] -> Maybe (ID,FilePath,FilePath) |
| 38 | parseArgs [] = Nothing |
| 39 | parseArgs [mid] = Just (mid, defaultDB, "") |
| 40 | parseArgs [mid,db] = Just (mid, db, "") |
| 41 | parseArgs [mid,db,dir] = Just (mid, db, dir) |
| 42 | parseArgs _ = Nothing |
| 43 | |
| 44 | ---------------------------------------------------------------- |
| 45 | |
| 46 | main :: IO () |
| 47 | main = do |
| 48 | (args,opts) <- splitArgOpt <$> getArgs |
| 49 | let mtri = parseArgs args |
| 50 | mcmd = parseOpts opts |
| 51 | exec mcmd mtri |
| 52 | where |
| 53 | exec (Just cmd) (Just (mid,db,dir)) = do |
| 54 | db' <- normalizePath db |
| 55 | dir' <- normalizePath dir |
| 56 | withDB db' (cmd mid dir') >>= printResults |
| 57 | exec _ _ = help helpMessage |
| 58 | printResults = mapM_ (putStrLn . path) |